when i was modifying eAthena functionOriginally Posted by Delfi
i noticed that 'a,,b' becomes ['a', 'b'] when i think the right is ['a', '', 'b'] (same as python and php results) so i modified it
here is the functions,
MyExplode is the reviewed version of the original function
MyExplode2 my version that works like you want
[pascal]function MyExplode(const src: string): TStringArray;
var
idx: Integer;
count: Integer;
CharPtr: PChar;
aChar: Char;
toklen: Integer;
f: Integer;
begin
CharPtr := Pointer(src);
if CharPtr = nil then Exit;
idx := 1;
f := 1;
toklen := 10;
SetLength(Result, toklen);
count := 0;
while (CharPtr^ <> #0) do
begin
aChar := CharPtr^;
Inc(CharPtr);
if (aChar = (' ')) or (aChar = (';')) or
(aChar = (',')) or (aChar = (#09)) then
begin
if (count + 1 > toklen) then
begin
toklen := toklen + (toklen div 2);
SetLength(Result, toklen);
end;
Result[count] := Copy(src, f, idx - f);
f := idx + 1;
Inc(count);
end;
Inc(idx);
end;
if (idx >= f) then
begin
if (count + 1 > toklen) then
begin
Inc(toklen);
SetLength(Result, toklen);
end;
Result[count] := Copy(src, f, MaxInt);
Inc(count);
end;
if (toklen > count) then
SetLength(Result, count);
end;
function MyExplode2(const src: string): TStringArray;
var
idx: Integer;
count: Integer;
CharPtr: PChar;
aChar: Char;
toklen: Integer;
f: Integer;
valid: Integer;
begin
CharPtr := Pointer(src);
if CharPtr = nil then Exit;
idx := 1;
f := 1;
valid := 0;
toklen := 10;
SetLength(Result, toklen);
count := 0;
while (CharPtr^ <> #0) do
begin
aChar := CharPtr^;
Inc(CharPtr);
if (aChar = (' ')) or (aChar = (';')) or
(aChar = (',')) or (aChar = (#09)) then
begin
if (valid <> 0) then
begin
if (count + 1 > toklen) then
begin
toklen := toklen + (toklen div 2);
SetLength(Result, toklen);
end;
Result[count] := Copy(src, f, idx - f);
f := idx + 1;
valid := 0;
Inc(count);
end
else
Inc(f);
end
else
Inc(valid);
Inc(idx);
end;
if (valid <> 0) then
begin
if (count + 1 > toklen) then
begin
Inc(toklen);
SetLength(Result, toklen);
end;
Result[count] := Copy(src, f, MaxInt);
Inc(count);
end;
if (toklen > count) then
SetLength(Result, count);
end;[/pascal]
Bookmarks