csvdocument: improved ChangeLineEndings function

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1626 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
vvzh
2011-05-10 13:28:22 +00:00
parent ef62720766
commit 14f614f81b

View File

@ -209,42 +209,50 @@ var
P, StartPos: Integer; P, StartPos: Integer;
Src: PChar; Src: PChar;
Dest: PChar; Dest: PChar;
EndLen: Integer; EndingStart: PChar;
EndingLen: Integer;
EndPos: PChar; EndPos: PChar;
begin begin
if AString = '' then if AString = '' then
begin Exit(AString);
Result := AString; EndingLen := Length(ALineEnding);
Exit;
end;
EndLen := Length(ALineEnding);
NewLength := Length(AString); NewLength := Length(AString);
P := 1;
while P < Length(AString) do Src := PChar(AString);
EndPos := Src + NewLength;
while Src < EndPos do
begin begin
if AString[P] in [#10,#13] then if (Src^ = CR) then
begin begin
StartPos := P; Inc(Src);
Inc(P); if (Src^ = LF) then
if (AString[P] in [#10,#13]) and (AString[P] <> AString[P-1]) then Inc(P); begin
Inc(NewLength, EndLen - (P - StartPos)); Inc(Src);
Inc(NewLength, EndingLen - 2);
end else
Inc(NewLength, EndingLen - 1);
end else end else
Inc(P); begin
if (Src^ = LF) then
Inc(NewLength, EndingLen - 1);
Inc(Src);
end;
end; end;
SetLength(Result, NewLength); SetLength(Result, NewLength);
Src := PChar(AString); Src := PChar(AString);
Dest := PChar(Result); Dest := PChar(Result);
EndPos := Dest + NewLength; EndPos := Dest + NewLength;
while (Dest < EndPos) do while (Dest < EndPos) do
begin begin
if Src^ in [#10,#13] then if Src^ in LineEndingChars then
begin begin
for P := 1 to EndLen do for P := 1 to EndingLen do
begin begin
Dest^ := ALineEnding[P]; Dest^ := ALineEnding[P];
Inc(Dest); Inc(Dest);
end; end;
if (Src[1] in [#10,#13]) and (Src^ <> Src[1]) then if (Src^ = CR) and (Src[1] = LF) then
Inc(Src, 2) Inc(Src, 2)
else else
Inc(Src); Inc(Src);