Comma quibbling: Difference between revisions

Added compatibility for Delphi
(→‎{{header|Forth}}: - the previous implementation was nice but mostly unrelated to the task)
(Added compatibility for Delphi)
Line 2,576:
<Lang Pascal>
program CommaQuibbling;
uses Classes, StrUtils;
 
uses
const OuterBracket=['[',']'];
SysUtils,
Classes,
StrUtils;
 
const
OuterBracket =['[', ']'];
 
type
 
{$IFNDEF FPC}
SizeInt = LongInt;
{$ENDIF}
 
 
 
{ TCommaQuibble }
Line 2,591 ⟶ 2,602:
property CommaQuibble: string read GetCommaquibble write SetCommaQuibble;
end;
 
{$IFNDEF FPC} // Delphi support
 
function WordPosition(const N: Integer; const S: string; const WordDelims:
TSysCharSet): SizeInt;
var
PS, P, PE: PChar;
Count: Integer;
begin
Result := 0;
Count := 0;
PS := PChar(pointer(S));
PE := PS + Length(S);
P := PS;
while (P < PE) and (Count <> N) do
begin
while (P < PE) and (P^ in WordDelims) do
inc(P);
if (P < PE) then
inc(Count);
if (Count <> N) then
while (P < PE) and not (P^ in WordDelims) do
inc(P)
else
Result := (P - PS) + 1;
end;
end;
 
function ExtractWordPos(N: Integer; const S: string; const WordDelims:
TSysCharSet; out Pos: Integer): string;
var
i, j, l: SizeInt;
begin
j := 0;
i := WordPosition(N, S, WordDelims);
if (i > High(Integer)) then
begin
Result := '';
Pos := -1;
Exit;
end;
Pos := i;
if (i <> 0) then
begin
j := i;
l := Length(S);
while (j <= l) and not (S[j] in WordDelims) do
inc(j);
end;
SetLength(Result, j - i);
if ((j - i) > 0) then
Result := copy(S, i, j - i);
end;
 
function ExtractWord(N: Integer; const S: string; const WordDelims: TSysCharSet):
string; inline;
var
i: SizeInt;
begin
Result := ExtractWordPos(N, S, WordDelims, i);
end;
{$ENDIF}
 
{ TCommaQuibble }
Line 2,596 ⟶ 2,669:
procedure TCommaQuibble.SetCommaQuibble(AValue: string);
begin
AValue := ExtractWord(1, AValue, OuterBracket);
commatext :=Avalue AValue;
end;
 
function TCommaQuibble.GetCommaquibble: string;
var
var x: Integer;
Delx: StringInteger;
Del: string;
begin
result := '';
Del := ', ';
for x := 0 to Count - 1 do
begin
result+ := result + Strings[x];
if x = Count - 2 then Del:=' and '
else if x=count-1 then Del := ' and ';
else if x = Count - 1 then
result+=del;
Del := '';
result := result + Del;
end;
result := '{' + result + '}';
end;
 
const
const TestData: array [0..7] of string=( '[]',
TestData: array[0..7] of string = ('[]', '["ABC"]', '["ABC", "DEF"]',
'["ABC", "DEF", "G", "H"]', '', '"ABC"', '"ABC", "DEF"', '["ABC", "DEF"]', "G", "H"');
 
'["ABC", "DEF", "G", "H"]',
var
'',
Quibble: TCommaQuibble;
'"ABC"',
TestString: string;
'"ABC", "DEF"',
 
'"ABC", "DEF", "G", "H"');
var Quibble: TCommaQuibble;
TestString: String;
begin
Quibble := TCommaQuibble.Create;
 
for TestString in TestData do
begin
Quibble.CommaQuibble := TestString;
writeln(Quibble.CommaQuibble);
end;
end.</Lang>
 
end.
 
</Lang>
Output:
<Pre>
478

edits