mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-18 18:19:08 +02:00
lazutils: TranslateUTF8Chars: ascii with delete
git-svn-id: trunk@39795 -
This commit is contained in:
parent
222d3f6e64
commit
dbdae2d1c7
@ -418,9 +418,43 @@ var
|
|||||||
p:=PChar(s)+OldPos;
|
p:=PChar(s)+OldPos;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure ReplaceASCII;
|
procedure ReplaceASCIIWithDelete(Src: PChar);
|
||||||
|
var
|
||||||
|
c: Char;
|
||||||
|
Dst: PChar;
|
||||||
|
NewLen: SizeInt;
|
||||||
|
i: SizeInt;
|
||||||
|
begin
|
||||||
|
UniqString(Src);
|
||||||
|
Dst:=Src;
|
||||||
|
while Src^<>#0 do begin
|
||||||
|
c:=Src^;
|
||||||
|
i:=Pos(c,SrcChars);
|
||||||
|
if i<1 then begin
|
||||||
|
// keep character
|
||||||
|
Dst^:=c;
|
||||||
|
inc(Src);
|
||||||
|
inc(Dst);
|
||||||
|
end else begin
|
||||||
|
if i<=length(DstChars) then begin
|
||||||
|
// replace a character
|
||||||
|
Dst^:=DstChars[i];
|
||||||
|
inc(Src);
|
||||||
|
inc(Dst);
|
||||||
|
end else begin
|
||||||
|
// delete a character = skip
|
||||||
|
inc(Src);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
NewLen:=Dst-PChar(s);
|
||||||
|
SetLength(s,NewLen);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure ReplaceASCII;
|
||||||
|
// use a simple byte replace
|
||||||
|
// if a delete is needed then switch to another algorithm
|
||||||
var
|
var
|
||||||
OldPos: SizeInt;
|
|
||||||
i: SizeInt;
|
i: SizeInt;
|
||||||
p: PChar;
|
p: PChar;
|
||||||
begin
|
begin
|
||||||
@ -438,9 +472,10 @@ var
|
|||||||
inc(p);
|
inc(p);
|
||||||
end else begin
|
end else begin
|
||||||
// delete a character
|
// delete a character
|
||||||
OldPos:=p-PChar(s);
|
// all following characters are moved
|
||||||
Delete(s,OldPos+1,1);
|
// => use an optimized algorithm for this
|
||||||
p:=PChar(s)+OldPos;
|
ReplaceASCIIWithDelete(p);
|
||||||
|
exit;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
@ -69,7 +69,7 @@ procedure TTestLazXML.TestTranslateUTF8Chars;
|
|||||||
h:=s;
|
h:=s;
|
||||||
TranslateUTF8Chars(h,SrcChars,DstChars);
|
TranslateUTF8Chars(h,SrcChars,DstChars);
|
||||||
if h=Expected then exit;
|
if h=Expected then exit;
|
||||||
AssertEquals(Title+' s="'+s+'" SrcChars="'+SrcChars+'" DstChars="'+DstChars+'"',Expected,h);
|
AssertEquals(Title+': s="'+s+'" SrcChars="'+SrcChars+'" DstChars="'+DstChars+'"',Expected,h);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@ -77,6 +77,8 @@ begin
|
|||||||
T('nop','a','b','b','a');
|
T('nop','a','b','b','a');
|
||||||
T('a to b','a','a','b','b');
|
T('a to b','a','a','b','b');
|
||||||
T('switch a,b','abaa','ab','ba','babb');
|
T('switch a,b','abaa','ab','ba','babb');
|
||||||
|
T('delete a','a','a','','');
|
||||||
|
T('delete a','aba','a','','b');
|
||||||
end;
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
|
Loading…
Reference in New Issue
Block a user