LCL-GTK2: Simplify function Ampersands2Underscore.

git-svn-id: trunk@62466 -
This commit is contained in:
juha 2019-12-29 23:18:55 +00:00
parent 7cac33cb8e
commit f721c3bcc5
2 changed files with 32 additions and 166 deletions

View File

@ -9254,83 +9254,22 @@ begin
end; end;
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
function Ampersands2Underscore(Src: PChar) : PChar;
Creates a new PChar. Deletes escaping ampersands, replaces the first single Creates a new PChar. Deletes escaping ampersands, replaces the first single
ampersand with an underscore and deleting all other single ampersands. ampersand with an underscore and deletes all other single ampersands.
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
function Ampersands2Underscore(Src: PChar) : PChar; function Ampersands2Underscore(Src: PChar) : PChar;
var var
i, j: Longint; s: String;
ShortenChars, FirstAmpersand, NewLength, SrcLength: integer;
begin begin
// count ampersands and find first ampersand s := StrPas(Src);
ShortenChars:= 0; // chars to delete s := Ampersands2Underscore(s);
FirstAmpersand:= -1; Result := StrAlloc(Length(s)+1); // +1 for #0 char at end
SrcLength:= StrLen(Src); strcopy(Result, PChar(s));
{ Look for amperands. If found, check if it is an escaped ampersand.
If it is, don't count it in. }
i:=0;
while i<SrcLength do begin
if Src[i] = '&' then begin
if (i < SrcLength - 1) and (Src[i+1] = '&') then begin
// escaping ampersand found
inc(ShortenChars);
inc(i,2);
Continue;
end else begin
// single ampersand found
if (FirstAmpersand < 0) then
// the first will be replaced ...
FirstAmpersand:= i
else
// ... and all others will be deleted
inc(ShortenChars);
end;
end;
inc(i);
end;
// create new PChar
NewLength:= SrcLength - ShortenChars;
Result:=StrAlloc(NewLength+1); // +1 for #0 char at end
// copy string without ampersands
i:=0;
j:=0;
while (j < NewLength) do begin
if Src[i] <> '&' then begin
// copy normal char
Result[j]:= Src[i];
end else begin
// ampersand
if (i < (SrcLength - 1)) and (Src[i+1] = '&') then begin
// escaping ampersand found
inc(i);
Result[j]:='&';
end else begin
// single ampersand found
if i = FirstAmpersand then begin
// replace first single ampersand with underscore
Result[j]:='_';
end else begin
// delete single ampersand
dec(j);
end;
end;
end;
Inc(i);
Inc(j);
end;
Result[NewLength]:=#0;
end; end;
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
function Ampersands2Underscore(const ASource: String): String;
Deletes escaping ampersands, replaces the first single Deletes escaping ampersands, replaces the first single
ampersand with an underscore and deleting all other single ampersands. ampersand with an underscore and deletes all other single ampersands.
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
function Ampersands2Underscore(const ASource: String): String; function Ampersands2Underscore(const ASource: String): String;
var var
@ -9343,26 +9282,20 @@ begin
n := 1; n := 1;
while n <= Length(Result) do while n <= Length(Result) do
begin begin
if Result[n] = '&' if Result[n] = '&' then
then begin begin
if (n < Length(Result))
and (Result[n + 1] = '&')
then begin
// we got a &&, remove the first
Delete(Result, n, 1);
Inc(n);
Continue;
end;
if FirstFound if FirstFound
or ( (n < Length(Result)) and (Result[n+1] = '&') ) // got &&
then begin then begin
// simply remove it
Delete(Result, n, 1); Delete(Result, n, 1);
Continue; if not FirstFound then
end; Inc(n); // Skip the second & of &&
// if we are here it's our first end
else begin
FirstFound := True; FirstFound := True;
Result[n] := '_'; Result[n] := '_';
end; end;
end;
Inc(n); Inc(n);
end; end;
end; end;

View File

@ -8957,83 +8957,22 @@ begin
end; end;
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
function Ampersands2Underscore(Src: PChar) : PChar;
Creates a new PChar. Deletes escaping ampersands, replaces the first single Creates a new PChar. Deletes escaping ampersands, replaces the first single
ampersand with an underscore and deleting all other single ampersands. ampersand with an underscore and deletes all other single ampersands.
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
function Ampersands2Underscore(Src: PChar) : PChar; function Ampersands2Underscore(Src: PChar) : PChar;
var var
i, j: Longint; s: String;
ShortenChars, FirstAmpersand, NewLength, SrcLength: integer;
begin begin
// count ampersands and find first ampersand s := StrPas(Src);
ShortenChars:= 0; // chars to delete s := Ampersands2Underscore(s);
FirstAmpersand:= -1; Result := StrAlloc(Length(s)+1); // +1 for #0 char at end
SrcLength:= StrLen(Src); strcopy(Result, PChar(s));
{ Look for amperands. If found, check if it is an escaped ampersand.
If it is, don't count it in. }
i:=0;
while i<SrcLength do begin
if Src[i] = '&' then begin
if (i < SrcLength - 1) and (Src[i+1] = '&') then begin
// escaping ampersand found
inc(ShortenChars);
inc(i,2);
Continue;
end else begin
// single ampersand found
if (FirstAmpersand < 0) then
// the first will be replaced ...
FirstAmpersand:= i
else
// ... and all others will be deleted
inc(ShortenChars);
end;
end;
inc(i);
end;
// create new PChar
NewLength:= SrcLength - ShortenChars;
Result:=StrAlloc(NewLength+1); // +1 for #0 char at end
// copy string without ampersands
i:=0;
j:=0;
while (j < NewLength) do begin
if Src[i] <> '&' then begin
// copy normal char
Result[j]:= Src[i];
end else begin
// ampersand
if (i < (SrcLength - 1)) and (Src[i+1] = '&') then begin
// escaping ampersand found
inc(i);
Result[j]:='&';
end else begin
// single ampersand found
if i = FirstAmpersand then begin
// replace first single ampersand with underscore
Result[j]:='_';
end else begin
// delete single ampersand
dec(j);
end;
end;
end;
Inc(i);
Inc(j);
end;
Result[NewLength]:=#0;
end; end;
{------------------------------------------------------------------------------- {-------------------------------------------------------------------------------
function Ampersands2Underscore(const ASource: String): String;
Deletes escaping ampersands, replaces the first single Deletes escaping ampersands, replaces the first single
ampersand with an underscore and deleting all other single ampersands. ampersand with an underscore and deletes all other single ampersands.
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
function Ampersands2Underscore(const ASource: String): String; function Ampersands2Underscore(const ASource: String): String;
var var
@ -9046,26 +8985,20 @@ begin
n := 1; n := 1;
while n <= Length(Result) do while n <= Length(Result) do
begin begin
if Result[n] = '&' if Result[n] = '&' then
then begin begin
if (n < Length(Result))
and (Result[n + 1] = '&')
then begin
// we got a &&, remove the first
Delete(Result, n, 1);
Inc(n);
Continue;
end;
if FirstFound if FirstFound
or ( (n < Length(Result)) and (Result[n+1] = '&') ) // got &&
then begin then begin
// simply remove it
Delete(Result, n, 1); Delete(Result, n, 1);
Continue; if not FirstFound then
end; Inc(n); // Skip the second & of &&
// if we are here it's our first end
else begin
FirstFound := True; FirstFound := True;
Result[n] := '_'; Result[n] := '_';
end; end;
end;
Inc(n); Inc(n);
end; end;
end; end;