A simple task: parse a line of text, separated by ",", "tab", "space", ";" into a array of strings, also multiple separator characters can separate values.

There's a utility function "StripAfter" which strips one-linecomments from a single line string (for stripping // comments from a string)

This is my implementation, feel free to use it, but can you do it better or faster?

The data is stored in a global variable, this is a class-free code, if you think using a class would be better give me your reasons and thoughts.

[pascal]

{ ************************************************** ********************* }
{ }
{ Crazy text parsing Unit }
{ }
{ ************************************************** ********************* }

unit textparser;

interface

uses sysutils;

procedure Explode(str: string; cleanup: boolean);
function Parsed(n: integer): string;
function ParsedInt(n: integer): integer;
function ParsedLW(n: integer): longword;
function ParsedBool(n: integer): boolean;
function ParsedFloat(n: integer): single;

function StripAfter(const Text, After: string): string;

var
Tokens: array of string;
Items: integer = 0;

implementation

procedure Explode(str: string; cleanup: boolean);
var
i: integer;
begin

if cleanup = True then
begin
// cleanup old data
for i := 0 to Items - 1 do
begin
Tokens[i] := '';
end;

Items := 0;
setlength(Tokens, Items + 1);

end;

// parse new string
for i := 1 to length(str) do
begin
if ((str[i] = ' ') or (str[i] = ';') or (str[i] = ',')) and (Tokens[Items] <> '') then
begin
Items := Items + 1;
setlength(Tokens, Items + 1);
end
else
Tokens[Items] := Tokens[Items] + trim(string(str[i]));
end;

end;

function Parsed(n: integer): string;
begin
if Items = -1 then
Result := ''
else
Result := Tokens[n];
end;

function ParsedInt(n: integer): integer;
begin
Result := StrToInt(Parsed(n));
end;

function ParsedLW(n: integer): longword;
begin
{$R-}
Result := StrToInt(Parsed(n));
{$R+}
end;

function ParsedBool(n: integer): boolean;
begin
Result := (Parsed(n) = '1') or (lowercase(Parsed(n)) = 'true') or (lowercase(Parsed(n)) = 'yes');
end;

function ParsedFloat(n: integer): single;
begin
Result := strtofloat(Parsed(n));
end;

function StripAfter(const Text, After: string): string;
var
p: integer;
begin
p := pos(After, Text);
if p <> 0 then
Result := copy(Text, 0, p - 1)
else
Result := Text;
end;

end.
[/pascal]