Advertisement
Janilabo

StrReplace

Dec 15th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.57 KB | None | 0 0
  1. type
  2.   TReplaceFlags = (rfReplaceAll, rfIgnoreCase); // TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
  3.  
  4. function StrReplace(str, oldPattern, newPattern: string; flags: set of TReplaceFlags): string; // flags: TReplaceFlags
  5. var
  6.   i, p, c, l, z, q: Integer;
  7.   s, o: string;
  8.   f: Boolean;
  9.   t: Char;
  10. begin
  11.   l := Length(str);
  12.   z := Length(oldPattern);
  13.   if ((l > 0) and (z > 0) and (oldPattern <> newPattern)) then
  14.   begin
  15.     Result := '';
  16.     s := str;
  17.     o := oldPattern;
  18.     if (rfIgnoreCase in flags) then
  19.     begin
  20.       s := LowerCase(s);
  21.       o := LowerCase(o);
  22.     end;
  23.     i := 1;
  24.     c := 0;
  25.     if (z = 1) then
  26.       t := o[1];
  27.     case (rfReplaceAll in flags) of
  28.       True:
  29.       begin
  30.         q := i;
  31.         repeat
  32.           for p := 1 to z do
  33.           begin
  34.             f := (s[((i + p) - 1)] = o[p]);
  35.             if not f then
  36.               Break;
  37.           end;
  38.           if f then
  39.           begin
  40.             if (c > 0) then
  41.               Result := (Result + Copy(str, q, c));
  42.             Result := (Result + newPattern);
  43.             i := ((i + z) - 1);
  44.             q := (i + 1);
  45.             c := 0;
  46.           end else
  47.             c := (c + 1);
  48.           i := (i + 1);
  49.         until (i > l);
  50.         if (c > 0) then
  51.           Result := (Result + Copy(str, q, c));
  52.       end;
  53.       False:
  54.       begin
  55.         repeat
  56.           for p := 1 to z do
  57.           begin
  58.             f := (s[((i + p) - 1)] = o[p]);
  59.             if not f then
  60.               Break;
  61.           end;
  62.           i := (i + 1);
  63.         until (f or (i > l));
  64.         case f of
  65.           True:
  66.           begin
  67.             i := (i - 1);
  68.             if (i > 1) then
  69.               Result := (Result + Copy(str, 1, (i - 1)));
  70.             Result := (Result + newPattern);
  71.             i := (i + z);
  72.             if not (i > l) then
  73.               Result := (Result + Copy(str, i, ((l - i) + 1)));
  74.           end;
  75.           False: Result := str;
  76.         end;
  77.       end;
  78.     end;
  79.   end else
  80.     Result := str;
  81. end;
  82.  
  83. var
  84.   f: Integer;
  85.   rstr, str: string;
  86.  
  87. begin
  88.   f := OpenFile((StrReplace(AppPath, 'bin\', '', [rfReplaceAll]) + 'changelog.txt'), False);
  89.   ReadFileString(f, str, FileSize(f));
  90.   CloseFile(f);                        
  91.   f := GetSystemTime;
  92.   rstr := StrReplace(str, 'SCAR', 'HM', [rfReplaceAll]);
  93.   WriteLn('StrReplace: ' + IntToStr(GetSystemTime - f) + ' ms. [' + MD5(rstr) + ']');
  94.   rstr := '';
  95.   f := GetSystemTime;
  96.   rstr := Replace(str, 'SCAR', 'HM');
  97.   WriteLn('Replace: ' + IntToStr(GetSystemTime - f) + ' ms. [' + MD5(rstr) + ']');
  98. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement