* Patch from Serge Anvarov to improve AnsiStartsWith/AnsiEndsWith/Dupestring (Patch attached to bug #33559)

git-svn-id: trunk@38768 -
This commit is contained in:
michael 2018-04-14 11:32:02 +00:00
parent c674f2c31c
commit 54f3cc664e

View File

@ -33,8 +33,8 @@ Function AnsiEndsText(const ASubText, AText: string): Boolean;
Function AnsiReplaceText(const AText, AFromText, AToText: string): string;inline; Function AnsiReplaceText(const AText, AFromText, AToText: string): string;inline;
Function AnsiMatchText(const AText: string; const AValues: array of string): Boolean;inline; Function AnsiMatchText(const AText: string; const AValues: array of string): Boolean;inline;
Function AnsiIndexText(const AText: string; const AValues: array of string): Integer; Function AnsiIndexText(const AText: string; const AValues: array of string): Integer;
Function StartsText(const ASubText, AText: string): Boolean; Function StartsText(const ASubText, AText: string): Boolean; inline;
Function EndsText(const ASubText, AText: string): Boolean; Function EndsText(const ASubText, AText: string): Boolean; inline;
{ --------------------------------------------------------------------- { ---------------------------------------------------------------------
Case sensitive search/replace Case sensitive search/replace
@ -906,39 +906,25 @@ end;
function AnsiStartsText(const ASubText, AText: string): Boolean; function AnsiStartsText(const ASubText, AText: string): Boolean;
begin begin
if (Length(AText) >= Length(ASubText)) and (ASubText <> '') then Result := (ASubText = '') or AnsiSameText(LeftStr(AText, Length(ASubText)), ASubText);
Result := AnsiStrLIComp(PChar(ASubText), PChar(AText), Length(ASubText)) = 0
else
Result := False;
end; end;
function AnsiEndsText(const ASubText, AText: string): Boolean; function AnsiEndsText(const ASubText, AText: string): Boolean;
begin begin
if Length(AText) >= Length(ASubText) then Result := (ASubText = '') or AnsiSameText(RightStr(AText, Length(ASubText)), ASubText);
Result := AnsiStrLIComp(PChar(ASubText),
PChar(AText) + Length(AText) - Length(ASubText), Length(ASubText)) = 0
else
Result := False;
end; end;
function StartsText(const ASubText, AText: string): Boolean; function StartsText(const ASubText, AText: string): Boolean; inline;
begin begin
if (Length(AText) >= Length(ASubText)) and (ASubText <> '') then Result := AnsiStartsText(ASubText, AText);
Result := StrLIComp(PChar(ASubText), PChar(AText), Length(ASubText)) = 0
else
Result := False;
end; end;
function EndsText(const ASubText, AText: string): Boolean; function EndsText(const ASubText, AText: string): Boolean;
begin begin
if Length(AText) >= Length(ASubText) then Result := AnsiEndsText(ASubText, AText);
Result := StrLIComp(PChar(ASubText),
PChar(AText) + Length(AText) - Length(ASubText), Length(ASubText)) = 0
else
Result := False;
end; end;
@ -955,17 +941,11 @@ end;
function AnsiIndexText(const AText: string; const AValues: array of string): Integer; function AnsiIndexText(const AText: string; const AValues: array of string): Integer;
var
i : Integer;
begin begin
Result:=-1; for Result := Low(AValues) to High(AValues) do
if (high(AValues)=-1) or (High(AValues)>MaxInt) Then if AnsiSameText(AValues[Result], AText) then
Exit; Exit;
for i:=low(AValues) to High(Avalues) do Result := -1;
if CompareText(avalues[i],atext)=0 Then
exit(i); // make sure it is the first val.
end; end;
@ -981,20 +961,13 @@ end;
function AnsiStartsStr(const ASubText, AText: string): Boolean; function AnsiStartsStr(const ASubText, AText: string): Boolean;
begin begin
if (Length(AText) >= Length(ASubText)) and (ASubText <> '') then Result := (ASubText = '') or (LeftStr(AText, Length(ASubText)) = ASubText);
Result := AnsiStrLComp(PChar(ASubText), PChar(AText), Length(ASubText)) = 0
else
Result := False;
end; end;
function AnsiEndsStr(const ASubText, AText: string): Boolean; function AnsiEndsStr(const ASubText, AText: string): Boolean;
begin begin
if Length(AText) >= Length(ASubText) then Result := (ASubText = '') or (RightStr(AText, Length(ASubText)) = AText);
Result := AnsiStrLComp(PChar(ASubText),
PChar(AText) + Length(AText) - Length(ASubText), Length(ASubText)) = 0
else
Result := False;
end; end;
@ -1094,18 +1067,25 @@ end;
function DupeString(const AText: string; ACount: Integer): string; function DupeString(const AText: string; ACount: Integer): string;
var i,l : SizeInt; var
Len: SizeInt;
Source, Target: PByte;
begin begin
result:=''; Len := Length(AText);
if aCount>=0 then SetLength(Result, ACount * Len);
begin // Use PByte to skip implicit UniqueString, because SetLength always unique
l:=length(atext); Target := PByte(Result);
SetLength(result,aCount*l); if Target = nil then // ACount = 0 or AText = ''
for i:=0 to ACount-1 do Exit;
move(atext[1],Result[l*i+1],l); // Now ACount > 0 and Len > 0
end; Source := PByte(AText);
end; repeat
Move(Source[0], Target[0], Len * SizeOf(Char));
Inc(Target, Len * SizeOf(Char));
Dec(ACount);
until ACount = 0;
end;
function ReverseString(const AText: string): string; function ReverseString(const AText: string): string;