Advertisement
Janilabo

Untitled

Dec 15th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.83 KB | None | 0 0
  1. function StrReplace(str, oldPattern, newPattern: string; flags: TReplaceFlags): string;
  2. var
  3.   i, p, c, l, z, q: Integer;
  4.   s, o: string;
  5.   f: Boolean;
  6. begin
  7.   l := Length(str);
  8.   z := Length(oldPattern);
  9.   if ((l > 0) and (z > 0) and (oldPattern <> newPattern)) then
  10.   begin
  11.     Result := '';
  12.     s := str;
  13.     o := oldPattern;
  14.     if (rfIgnoreCase in flags) then
  15.     begin
  16.       s := LowerCase(s);
  17.       o := LowerCase(o);
  18.     end;
  19.     i := 1;
  20.     c := 0;
  21.     case (rfReplaceAll in flags) of
  22.       True:
  23.       begin
  24.         q := i;
  25.         repeat
  26.           for p := 1 to z do
  27.           begin
  28.             f := (s[((i + p) - 1)] = o[p]);
  29.             if not f then
  30.               Break;
  31.           end;
  32.           if f then
  33.           begin
  34.             if (c > 0) then
  35.               Result := (Result + Copy(str, q, c));
  36.             Result := (Result + newPattern);
  37.             i := ((i + z) - 1);
  38.             q := (i + 1);
  39.             c := 0;
  40.           end else
  41.             c := (c + 1);
  42.           i := (i + 1);
  43.         until (i > l);
  44.         if (c > 0) then
  45.           Result := (Result + Copy(str, q, c));
  46.       end;
  47.       False:
  48.       begin
  49.         repeat
  50.           for p := 1 to z do
  51.           begin
  52.             f := (s[((i + p) - 1)] = o[p]);
  53.             if not f then
  54.               Break;
  55.           end;
  56.           i := (i + 1);
  57.         until (f or (i > l));
  58.         case f of
  59.           True:
  60.           begin
  61.             i := (i - 1);
  62.             if (i > 1) then
  63.               Result := (Result + Copy(str, 1, (i - 1)));
  64.             Result := (Result + newPattern);
  65.             i := (i + z);
  66.             if not (i > l) then
  67.               Result := (Result + Copy(str, i, ((l - i) + 1)));
  68.           end;
  69.           False: Result := str;
  70.         end;
  71.       end;
  72.     end;
  73.   end else
  74.     Result := str;
  75. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement