* 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); procedure SetLineBreak(const S : String);
Function GetSkipLastLineBreak : Boolean; Function GetSkipLastLineBreak : Boolean;
procedure SetSkipLastLineBreak(const AValue : Boolean); procedure SetSkipLastLineBreak(const AValue : Boolean);
Procedure DoSetDelimitedText(const AValue: string; DoClear,aStrictDelimiter : Boolean; aQuoteChar,aDelimiter : Char);
protected protected
procedure DefineProperties(Filer: TFiler); override; procedure DefineProperties(Filer: TFiler); override;
procedure Error(const Msg: string; Data: Integer); procedure Error(const Msg: string; Data: Integer);

View File

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