Browse Source

* Optimization of TStringHelper.Split by Rika. Fixes issue #39948

Michaël Van Canneyt 2 years ago
parent
commit
d5777174d8
1 changed files with 8 additions and 23 deletions
  1. 8 23
      rtl/objpas/sysutils/syshelp.inc

+ 8 - 23
rtl/objpas/sysutils/syshelp.inc

@@ -1248,9 +1248,6 @@ end;
 function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
 function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
   AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
   AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
 
 
-Const
-  BlockSize = 10;
-
   Function NextSep(StartIndex : SizeInt) : SizeInt;
   Function NextSep(StartIndex : SizeInt) : SizeInt;
 
 
   begin
   begin
@@ -1264,41 +1261,29 @@ Const
 
 
   begin
   begin
     if System.Length(Result)<=CurLen then
     if System.Length(Result)<=CurLen then
-      SetLength(Result,System.Length(Result)+BlockSize);
+      SetLength(Result,System.Length(Result)+4+SizeInt(SizeUint(System.Length(Result)) div 4));
   end;
   end;
 
 
 Var
 Var
   Sep,LastSep,Len : SizeInt;
   Sep,LastSep,Len : SizeInt;
-  T : String;
 
 
 begin
 begin
-  SetLength(Result,BlockSize);
+  Result:=nil;
   Len:=0;
   Len:=0;
   LastSep:=0;
   LastSep:=0;
-  Sep:=NextSep(0);
-  While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
+  While ((ACount=0) or (Len<ACount)) and (LastSep<=System.Length(Self)) do
     begin
     begin
-    T:=SubString(LastSep,Sep-LastSep);
+    Sep:=NextSep(LastSep);
+    if Sep<0 then
+      Sep:=System.Length(Self);
 //    Writeln('Examining >',T,'< at pos ',LastSep,', till pos ',Sep);
 //    Writeln('Examining >',T,'< at pos ',LastSep,', till pos ',Sep);
-    If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
+    If (Sep>LastSep) or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
       begin
       begin
       MaybeGrow(Len);
       MaybeGrow(Len);
-      Result[Len]:=T;
+      Result[Len]:=SubString(LastSep,Sep-LastSep);
       Inc(Len);
       Inc(Len);
       end;
       end;
     LastSep:=Sep+1;
     LastSep:=Sep+1;
-    Sep:=NextSep(LastSep);
-    end;
-  if (LastSep<=Length) and ((ACount=0) or (Len<ACount)) then
-    begin
-    T:=SubString(LastSep);
-//    Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
-    If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
-      begin
-      MaybeGrow(Len);
-      Result[Len]:=T;
-      Inc(Len);
-      end;
     end;
     end;
 
 
   if (TStringSplitOptions.ExcludeLastEmpty=Options) then
   if (TStringSplitOptions.ExcludeLastEmpty=Options) then