* Add overloaded version of stringreplace with replacement count (ID 23987)

git-svn-id: trunk@41330 -
This commit is contained in:
michael 2019-02-16 07:55:26 +00:00
parent 90541cc3c3
commit a750039f7c
7 changed files with 43 additions and 9 deletions

View File

@ -1,9 +1,10 @@
var
OldPat,Srch: SRstring; // Srch and Oldp can contain uppercase versions of S,OldPattern
PatLength,NewPatLength,P,Cnt,PatCount,PrevP: Integer;
PatLength,NewPatLength,P,Cnt,PrevP: Integer;
c,d: SRPChar ;
begin
aCount:=0;
Result:='';
c:= NIL; d:=NIL;
OldPat:='';
@ -31,6 +32,7 @@ begin
repeat
P:=Pos(OldPat,Srch,P);
if P>0 then begin
inc(aCount);
move(NewPattern[1],Result[P],PatLength*SizeOf(SRChar));
if not (rfReplaceAll in Flags) then exit;
inc(P,PatLength);
@ -40,21 +42,21 @@ begin
//Different pattern length -> Result length will change
//To avoid creating a lot of temporary strings, we count how many
//replacements we're going to make.
P:=1; PatCount:=0;
P:=1;
repeat
P:=Pos(OldPat,Srch,P);
if P>0 then begin
inc(P,PatLength);
inc(PatCount);
inc(aCount);
if not (rfReplaceAll in Flags) then break;
end;
until p=0;
if PatCount=0 then begin
if aCount=0 then begin
Result:=S;
exit;
end;
NewPatLength:=Length(NewPattern);
SetLength(Result,Length(S)+PatCount*(NewPatLength-PatLength));
SetLength(Result,Length(S)+aCount*(NewPatLength-PatLength));
P:=1; PrevP:=0;
c:=SRPChar(Result); d:=SRPChar(S);
repeat

View File

@ -2294,6 +2294,16 @@ end;
{$define SRCHAR:=Char}
Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
Var
C : Integer;
begin
Result:=StringReplace(S,OldPattern,NewPattern,Flags,C);
end;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Out aCount : Integer): string;
{$i syssr.inc}
{$undef INSTRINGREPLACE}

View File

@ -234,6 +234,7 @@ function TryStrToBool(const S: string; out Value: Boolean): Boolean;
function TryStrToBool(const S: string; out Value: Boolean; Const FormatSettings: TFormatSettings): Boolean;
function LastDelimiter(const Delimiters, S: string): SizeInt;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Out aCount : Integer): string;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
Function IsDelimiter(const Delimiters, S: string; Index: SizeInt): Boolean;

View File

@ -540,7 +540,16 @@ end;
{$define SRPCHAR:=PUnicodeChar}
{$define SRCHAR:=UnicodeChar}
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags): UnicodeString;
Function UnicodeStringReplace(const S, OldPattern, NewPattern: Unicodestring; Flags: TReplaceFlags): Unicodestring;
Var
C : Integer;
begin
Result:=UnicodeStringReplace(S,OldPattern,NewPattern,Flags,C);
end;
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags; Out aCount : Integer): UnicodeString;
{$i syssr.inc}
{$undef INUNICODESTRINGREPLACE}

View File

@ -68,3 +68,5 @@ function WideBytesOf(const Value: UnicodeString): TBytes;
function WideStringOf(const Value: TBytes): UnicodeString;
function ByteLength(const S: UnicodeString): Integer;
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags): UnicodeString;
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags; Out aCount : Integer): UnicodeString;

View File

@ -193,11 +193,21 @@ end;
{$define SRPChar:=PWideChar}
{$define SRChar:=WideChar}
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags): WideString;
Function WideStringReplace(const S, OldPattern, NewPattern: Widestring; Flags: TReplaceFlags): Widestring;
Var
C : Integer;
begin
Result:=WideStringReplace(S,OldPattern,NewPattern,Flags,C);
end;
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags; Out aCount : Integer): WideString;
{$i syssr.inc}
{$undef INWIDESTRINGREPLACE}
{$undef SRString}
{$undef SRUpperCase}
{$undef SRPChar}
{$undef SRChar}
{$undef SRChar}

View File

@ -35,6 +35,6 @@ function StrCopy(Dest, Source: PWideChar): PWideChar; overload;
function StrLCopy(Dest,Source: PWideChar; MaxLen: SizeInt): PWideChar; overload;
Function CharInSet(Ch:WideChar;Const CSet : TSysCharSet) : Boolean;
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags): WideString;
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags; Out aCount : Integer): WideString;
function IsLeadChar(Ch: WideChar): Boolean; inline; overload;