* Refactor AddDelimitedText/SetDelimitedText

git-svn-id: trunk@43415 -
This commit is contained in:
michael 2019-11-08 09:21:08 +00:00
parent d3ceede910
commit e578477334
2 changed files with 40 additions and 37 deletions

View File

@ -655,6 +655,7 @@ type
procedure SetLineBreak(const S : String);
Function GetSkipLastLineBreak : Boolean;
procedure SetSkipLastLineBreak(const AValue : Boolean);
Procedure DoSetDelimitedText(const AValue: string; DoClear,aStrictDelimiter : Boolean; aQuoteChar,aDelimiter : Char);
protected
procedure DefineProperties(Filer: TFiler); override;
procedure Error(const Msg: string; Data: Integer);

View File

@ -504,12 +504,24 @@ end;
Procedure TStrings.SetDelimitedText(const AValue: string);
begin
CheckSpecialChars;
DoSetDelimitedText(aValue,True,FStrictDelimiter,FQuoteChar,FDelimiter);
end;
Procedure TStrings.DoSetDelimitedText(const AValue: string; DoClear,aStrictDelimiter : Boolean; aQuoteChar,aDelimiter : Char);
var
len,i,j: SizeInt;
aNotFirst:boolean;
Procedure AddQuoted;
begin
Add(StringReplace(Copy(AValue,i+1,j-i-1),aQuoteChar+aQuoteChar,aQuoteChar, [rfReplaceAll]));
end;
begin
CheckSpecialChars;
BeginUpdate;
i:=1;
@ -524,40 +536,40 @@ begin
try
Clear;
len:=length(AValue);
If StrictDelimiter then
If aStrictDelimiter then
begin
while i<=Len do begin
// skip delimiter
if aNotFirst and (i<=len) and (AValue[i]=FDelimiter) then
if aNotFirst and (i<=len) and (AValue[i]=aDelimiter) then
inc(i);
// read next string
if i<=len then begin
if AValue[i]=FQuoteChar then begin
if AValue[i]=aQuoteChar then begin
// next string is quoted
j:=i+1;
while (j<=len) and
( (AValue[j]<>FQuoteChar) or
( (j+1<=len) and (AValue[j+1]=FQuoteChar) ) ) do begin
if (j<=len) and (AValue[j]=FQuoteChar) then inc(j,2)
else inc(j);
end;
// j is position of closing quote
Add( StringReplace (Copy(AValue,i+1,j-i-1),
FQuoteChar+FQuoteChar,FQuoteChar, [rfReplaceAll]));
((AValue[j]<>aQuoteChar) or
((j+1<=len) and (AValue[j+1]=aQuoteChar))) do
begin
if (j<=len) and (AValue[j]=aQuoteChar) then
inc(j,2)
else
inc(j);
end;
AddQuoted;
i:=j+1;
end else begin
// next string is not quoted; read until delimiter
j:=i;
while (j<=len) and
(AValue[j]<>FDelimiter) do inc(j);
(AValue[j]<>aDelimiter) do inc(j);
Add( Copy(AValue,i,j-i));
i:=j;
end;
end else begin
if aNotFirst then Add('');
end;
aNotFirst:=true;
end;
end
@ -565,32 +577,30 @@ begin
begin
while i<=len do begin
// skip delimiter
if aNotFirst and (i<=len) and (AValue[i]=FDelimiter) then inc(i);
if aNotFirst and (i<=len) and (AValue[i]=aDelimiter) then inc(i);
// skip spaces
while (i<=len) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
// read next string
if i<=len then begin
if AValue[i]=FQuoteChar then begin
if AValue[i]=aQuoteChar then begin
// next string is quoted
j:=i+1;
while (j<=len) and
( (AValue[j]<>FQuoteChar) or
( (j+1<=len) and (AValue[j+1]=FQuoteChar) ) ) do begin
if (j<=len) and (AValue[j]=FQuoteChar) then inc(j,2)
( (AValue[j]<>aQuoteChar) or
( (j+1<=len) and (AValue[j+1]=aQuoteChar) ) ) do begin
if (j<=len) and (AValue[j]=aQuoteChar) then inc(j,2)
else inc(j);
end;
// j is position of closing quote
Add( StringReplace (Copy(AValue,i+1,j-i-1),
FQuoteChar+FQuoteChar,FQuoteChar, [rfReplaceAll]));
AddQuoted;
i:=j+1;
end else begin
// next string is not quoted; read until control character/space/delimiter
j:=i;
while (j<=len) and
(Ord(AValue[j])>Ord(' ')) and
(AValue[j]<>FDelimiter) do inc(j);
(AValue[j]<>aDelimiter) do inc(j);
Add( Copy(AValue,i,j-i));
i:=j;
end;
@ -944,25 +954,17 @@ begin
end;
end;
procedure TStrings.AddDelimitedText(const S: String; ADelimiter: Char;
AStrictDelimiter: Boolean);
var
L: TStringList;
procedure TStrings.AddDelimitedText(const S: String; ADelimiter: Char; AStrictDelimiter: Boolean);
begin
L := TStringList.Create;
try
L.Delimiter := ADelimiter;
L.StrictDelimiter := AStrictDelimiter;
L.DelimitedText := S;
AddStrings(L);
finally
L.Free;
end;
CheckSpecialChars;
DoSetDelimitedText(S,False,AStrictDelimiter,FQuoteChar,ADelimiter);
end;
procedure TStrings.AddDelimitedText(const S: String);
begin
AddDelimitedText(S, FDelimiter, FStrictDelimiter);
CheckSpecialChars;
DoSetDelimitedText(S,False,FStrictDelimiter,FQuoteChar,FDelimiter);
end;
Procedure TStrings.SetUpdateState(Updating: Boolean);