XSplit

From Soldat Community Wiki
Jump to: navigation, search

Snippet by DorkeyDear (Curt) and CurryWurst

function Explode(Source: string; const Delimiter: string): array of string;
var
  Position, DelLength, ResLength: integer;
begin
  DelLength := Length(Delimiter);
  Source := Source + Delimiter;
  repeat
    Position := Pos(Delimiter, Source);
    SetArrayLength(Result, ResLength + 1);
    Result[ResLength] := Copy(Source, 1, Position - 1);
    ResLength := ResLength + 1;
    Delete(Source, 1, Position + DelLength - 1);
  until (Position = 0);
  SetArrayLength(Result, ResLength - 1);
end;

This is Explode v2. There is a first version that was simply slower. Both of these were enspired by KeYDoN's XSplit. Explode, after many tests, definitely is faster. Soldat Forums topic may be found at http://forums.soldat.pl/index.php?topic=35698.0 and http://forums.soldat.pl/index.php?topic=27844.0

Snippet by KeYDoN

function XSplit(const Source: string; const Delimiter: string): tstringarray;
var
  i,x,d: integer;
  s: string;
begin
  d := Length(Delimiter);
  x := 0;
  i := 1;
  SetArrayLength(Result,1);
  while i <= Length(source) do begin
    s := Copy(Source,i,d);
    if s = Delimiter then begin
      Inc(i,d);
      Inc(x,1);
      SetArrayLength(result,x + 1);
    end else begin
      Result[x] := Result[x] + Copy(s,1,1);
      Inc(i,1);
    end;
  end;
end;