Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type
- TReplaceFlags = (rfReplaceAll, rfIgnoreCase); // TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
- function StrReplace(str, oldPattern, newPattern: string; flags: set of TReplaceFlags): string; // flags: TReplaceFlags
- var
- i, p, c, l, z, q: Integer;
- s, o: string;
- f: Boolean;
- t: Char;
- begin
- l := Length(str);
- z := Length(oldPattern);
- if ((l > 0) and (z > 0) and (oldPattern <> newPattern)) then
- begin
- Result := '';
- s := str;
- o := oldPattern;
- if (rfIgnoreCase in flags) then
- begin
- s := LowerCase(s);
- o := LowerCase(o);
- end;
- i := 1;
- c := 0;
- if (z = 1) then
- t := o[1];
- case (rfReplaceAll in flags) of
- True:
- begin
- q := i;
- repeat
- for p := 1 to z do
- begin
- f := (s[((i + p) - 1)] = o[p]);
- if not f then
- Break;
- end;
- if f then
- begin
- if (c > 0) then
- Result := (Result + Copy(str, q, c));
- Result := (Result + newPattern);
- i := ((i + z) - 1);
- q := (i + 1);
- c := 0;
- end else
- c := (c + 1);
- i := (i + 1);
- until (i > l);
- if (c > 0) then
- Result := (Result + Copy(str, q, c));
- end;
- False:
- begin
- repeat
- for p := 1 to z do
- begin
- f := (s[((i + p) - 1)] = o[p]);
- if not f then
- Break;
- end;
- i := (i + 1);
- until (f or (i > l));
- case f of
- True:
- begin
- i := (i - 1);
- if (i > 1) then
- Result := (Result + Copy(str, 1, (i - 1)));
- Result := (Result + newPattern);
- i := (i + z);
- if not (i > l) then
- Result := (Result + Copy(str, i, ((l - i) + 1)));
- end;
- False: Result := str;
- end;
- end;
- end;
- end else
- Result := str;
- end;
- var
- f: Integer;
- rstr, str: string;
- begin
- f := OpenFile((StrReplace(AppPath, 'bin\', '', [rfReplaceAll]) + 'changelog.txt'), False);
- ReadFileString(f, str, FileSize(f));
- CloseFile(f);
- f := GetSystemTime;
- rstr := StrReplace(str, 'SCAR', 'HM', [rfReplaceAll]);
- WriteLn('StrReplace: ' + IntToStr(GetSystemTime - f) + ' ms. [' + MD5(rstr) + ']');
- rstr := '';
- f := GetSystemTime;
- rstr := Replace(str, 'SCAR', 'HM');
- WriteLn('Replace: ' + IntToStr(GetSystemTime - f) + ' ms. [' + MD5(rstr) + ']');
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement