View Issue Details

IDProjectCategoryView StatusLast Update
0023987FPCOtherpublic2019-02-18 11:43
ReporterAvishaiAssigned ToMichael Van Canneyt 
PrioritynormalSeverityfeatureReproducibilityhave not tried
Status resolvedResolutionfixed 
Product VersionProduct Build 
Target Version3.2.0Fixed in Version3.3.1 
Summary0023987: Suggest adding StringReplaceAll with Replacement Count
DescriptionI would like to suggest adding a new funtion like StringReplace but adding a Replacement Count when rfReplaceAll is used. I copied StringReplace and assume 'rfReplaceAll' and added Count.

function StringReplaceAll(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Var Count: Integer): string;
var
  Srch,OldP,RemS: string;
  P : Integer;
begin
  Count:= 0;
  Srch:=S;
  OldP:=OldPattern;
  if rfIgnoreCase in Flags then begin
    Srch:=AnsiUpperCase(Srch);
    OldP:=AnsiUpperCase(OldP);
  end;
  RemS:=S;
  Result:='';
  while (Length(Srch)<>0) do begin
    P:=AnsiPos(OldP, Srch);
    if P=0 then begin
      Result:=Result+RemS;
      Srch:='';
    end else begin
      inc(Count);
      Result:=Result+Copy(RemS,1,P-1)+NewPattern;
      P:=P+Length(OldP);
      RemS:=Copy(RemS,P,Length(RemS)-P+1);
      //if not (rfReplaceAll in Flags) then begin
      // Result:=Result+RemS;
      // Srch:='';
      //end else
      Srch:=Copy(Srch,P,Length(Srch)-P+1);
    end;
  end;
end;
TagsNo tags attached.
Fixed in Revision41330
FPCOldBugId
FPCTarget
Attached Files

Activities

Bart Broersma

2013-03-05 18:22

reporter   ~0066032

1.) Shouldn't this be in FPC
2.) Count should probably be an out parameter
3.) Maybe better re-use exiting StringReplace, now we get 2 functions with almost identical code to maintain

Bart Broersma

2019-02-15 14:21

reporter   ~0114144

The idea of a Count parameter in StringReplace isn't that bad.
ATM we seem to have multiple implementations/variants of StringReplace with different strategies and different string types and different units (StrUtils, SysUtils).
StringReplaceBoyerMoore already has an internal MatchesCount and so does StringReplaceFast.

Thaddy de Koning

2019-02-15 14:30

reporter   ~0114145

var count should be out count. Good idea.

Bart Broersma

2019-02-15 17:35

reporter   ~0114156

@Michael: I implemented this on the Lazarus side (LazUtils package) in trunk and put the Count parameter after the Flags parameter.
If this is to be implemented in fpc, could you indicate at what position you would put such a parameter, so that the LazUtils signature will be more or less the same (ignore the Language parameter in the Utf8 version)?

Michael Van Canneyt

2019-02-15 18:56

administrator   ~0114158

I intend to put it on the same place: after flags. It will be an overloaded version of StringReplace. I see no need for a second function.

Bart Broersma

2019-02-15 22:27

reporter   ~0114162

> I see no need for a second function.
Of course not, that's what overload is for.

Michael Van Canneyt

2019-02-16 08:55

administrator   ~0114168

Added overloaded version to SysUtils

Issue History

Date Modified Username Field Change
2013-03-03 14:53 Avishai New Issue
2013-03-05 18:22 Bart Broersma Note Added: 0066032
2013-03-05 21:36 Vincent Snijders Project Lazarus => FPC
2019-02-15 14:21 Bart Broersma Note Added: 0114144
2019-02-15 14:30 Thaddy de Koning Note Added: 0114145
2019-02-15 16:48 Michael Van Canneyt Assigned To => Michael Van Canneyt
2019-02-15 16:48 Michael Van Canneyt Status new => assigned
2019-02-15 17:35 Bart Broersma Note Added: 0114156
2019-02-15 18:56 Michael Van Canneyt Note Added: 0114158
2019-02-15 22:27 Bart Broersma Note Added: 0114162
2019-02-16 08:55 Michael Van Canneyt Fixed in Revision => 41330
2019-02-16 08:55 Michael Van Canneyt Note Added: 0114168
2019-02-16 08:55 Michael Van Canneyt Status assigned => resolved
2019-02-16 08:55 Michael Van Canneyt Fixed in Version => 3.3.1
2019-02-16 08:55 Michael Van Canneyt Resolution open => fixed
2019-02-16 08:55 Michael Van Canneyt Target Version => 3.2.0