LCL-GTK2: Simplify function RemoveAmpersands and move it to LCLProc.

git-svn-id: trunk@62465 -
This commit is contained in:
juha 2019-12-29 23:18:53 +00:00
parent ec99bd95e2
commit 7cac33cb8e
3 changed files with 35 additions and 93 deletions

View File

@ -9075,95 +9075,6 @@ begin
Result := StringReplace(Str, '_', '__', [rfReplaceAll]);
end;
{-------------------------------------------------------------------------------
function RemoveAmpersands(Src: PChar; LineLength : Longint) : PChar;
Creates a new PChar removing all escaping ampersands.
-------------------------------------------------------------------------------}
function RemoveAmpersands(Src: PChar; LineLength : Longint) : PChar;
var
i, j: Longint;
ShortenChars, NewLength, SrcLength: integer;
begin
// count ampersands and find first ampersand
ShortenChars:= 0; // chars to delete
SrcLength:= LineLength;
{ 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
inc(ShortenChars);
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
// delete single ampersand
dec(j);
end;
Inc(i);
Inc(j);
end;
Result[NewLength]:=#0;
end;
{-------------------------------------------------------------------------------
function RemoveAmpersands(const ASource: String): String;
Removing all escaping ampersands.
-------------------------------------------------------------------------------}
function RemoveAmpersands(const ASource: String): String;
var
n: Integer;
begin
Result := ASource;
n := 1;
while n <= Length(Result) do
begin
if Result[n] = '&'
then 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;
// simply remove it
Delete(Result, n, 1);
Continue;
end;
Inc(n);
end;
end;
{-------------------------------------------------------------------------------
procedure LabelFromAmpersands(var AText, APattern: String; var AAccelChar: Char)

View File

@ -588,8 +588,6 @@ procedure ConnectInternalWidgetsSignals(AWidget: PGtkWidget;
function Ampersands2Underscore(Src: PChar): PChar;
function Ampersands2Underscore(const ASource: String): String;
function EscapeUnderscores(const Str: String): String; inline;
function RemoveAmpersands(Src: PChar; LineLength: Longint): PChar;
function RemoveAmpersands(const ASource: String): String;
procedure LabelFromAmpersands(var AText, APattern: String; var AAccelChar: Char);
function GetAccelGroup(const Widget: PGtkWidget;

View File

@ -132,6 +132,8 @@ procedure CalculateLeftTopWidthHeight(X1,Y1,X2,Y2: integer;
// Ampersands
function DeleteAmpersands(var Str : String) : Integer;
function RemoveAmpersands(const ASource: String): String;
function RemoveAmpersands(Src: PChar; LineLength: Longint): PChar;
function ComparePointers(p1, p2: Pointer): integer; inline;
function CompareHandles(h1, h2: THandle): integer;
@ -357,8 +359,8 @@ begin
SrcPos:=1;
DestPos:=1;
while SrcPos<=SrcLen do begin
if (Str[SrcPos]='&') and (SrcPos<SrcLen) then begin
// & found
if (Str[SrcPos]='&') and (SrcPos<SrcLen) then
begin
inc(SrcPos); // skip &
if (Str[SrcPos]<>'&') and (Result<1) then // Ignore && as accelerator
Result:=DestPos;
@ -372,6 +374,37 @@ begin
SetLength(Str,DestPos-1);
end;
function RemoveAmpersands(const ASource: String): String;
var
n: Integer;
DoubleAmp: Boolean;
begin
Result := ASource;
n := 1;
while n <= Length(Result) do
begin
if Result[n] = '&' then
begin
DoubleAmp := (n < Length(Result)) and (Result[n+1] = '&');
Delete(Result, n, 1);
if DoubleAmp then
Inc(n); // skip the second & of &&
end;
Inc(n);
end;
end;
function RemoveAmpersands(Src: PChar; LineLength: Longint): PChar;
var
s: String;
begin
SetLength(s, LineLength);
strlcopy(PChar(s), Src, LineLength);
s := RemoveAmpersands(s);
Result := StrAlloc(Length(s)+1); // +1 for #0 char at end
strcopy(Result, PChar(s));
end;
//-----------------------------------------------------------------------------
// Keys and shortcuts