Advertisement
Janilabo

Untitled

Dec 23rd, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.13 KB | None | 0 0
  1. function SubStrs(const Text: String; const Ptrn:String): TIntegerArray;
  2. var
  3.   hits,maxhits,h,i: Integer;
  4. begin
  5.   MaxHits := Length(Ptrn);
  6.   Hits := 1;
  7.   h := 0;
  8.   for i:=1 to Length(Text) do
  9.   begin
  10.     if Text[i] = Ptrn[Hits] then
  11.     begin
  12.       Inc(Hits);
  13.       if (Hits >= MaxHits) then
  14.       begin
  15.         SetLength(Result, h+1);
  16.         Result[h] := (i - Hits) + 1;
  17.         Inc(h);
  18.         Hits := 1;
  19.       end;
  20.     end else
  21.       Hits := 1;
  22.   end;
  23. end;
  24.  
  25.  
  26. function StrReplace(Text, Ptrn, Rep: String): String;
  27. var
  28.   Hi,HiPtrn,HiRep,i,j,k: Integer;
  29.   Prev,Curr:Integer;
  30.   Subs: TIntegerArray;
  31. begin
  32.   Hi := Length(Text);
  33.   if Hi = 0 then Exit;
  34.  
  35.   Subs := SubStrs(Text, Ptrn);
  36.  
  37.   if Length(Subs) = 0 then
  38.     Exit(Copy(Text, 1,Hi));
  39.  
  40.   HiRep := Length(Rep);
  41.   HiPtrn := Length(Ptrn);
  42.  
  43.   SetLength(Result, Hi + (Length(Subs) * (HiRep-HiPtrn)));
  44.   k := 1;
  45.   Prev := 1;
  46.   for i:=0 to High(Subs) do
  47.   begin
  48.     Curr := Subs[i];
  49.     for j:=Prev to Curr do
  50.     begin
  51.       Result[k] := Text[j];
  52.       Inc(k);
  53.     end;
  54.     for j:=1 to HiRep do
  55.     begin
  56.       Result[k] := Rep[j];
  57.       Inc(k);
  58.     end;
  59.     Prev := Curr + HiPtrn + 1;
  60.   end;
  61.  
  62.   //Last remaining part:
  63.   for i:=Prev to Hi do
  64.   begin
  65.     Result[k] := Text[i];
  66.     Inc(k);
  67.   end;
  68.   SetLength(Result, k);
  69. end;
  70.  
  71. function StrReplace2(text, findStr, replaceStr: string; flags: TReplaceFlags): string;
  72. var
  73.   i, p, c, l, z, q: Integer;
  74.   s, o: string;
  75.   f: Boolean;
  76.   t: Char;
  77. begin
  78.   l := Length(text);
  79.   z := Length(findStr);
  80.   if ((l > 0) and (z > 0) and (findStr <> replaceStr)) then
  81.   begin
  82.     Result := '';
  83.     s := text;
  84.     o := findStr;
  85.     if (rfIgnoreCase in flags) then
  86.     begin
  87.       s := LowerCase(s);
  88.       o := LowerCase(o);
  89.     end;
  90.     i := 1;
  91.     c := 0;
  92.     if (z = 1) then
  93.       t := o[1];
  94.     if (rfReplaceAll in flags) then
  95.     begin
  96.       if not (z = 1) then
  97.       begin
  98.         begin
  99.           q := i;
  100.           repeat
  101.             for p := 1 to z do
  102.             begin
  103.               f := (s[((i + p) - 1)] = o[p]);
  104.               if not f then
  105.                 Break;
  106.             end;
  107.             if f then
  108.             begin
  109.               if (c > 0) then
  110.                 Result := (Result + Copy(text, q, c));
  111.               Result := (Result + replaceStr);
  112.               i := ((i + z) - 1);
  113.               q := (i + 1);
  114.               c := 0;
  115.             end else
  116.               c := (c + 1);
  117.             i := (i + 1);
  118.           until (i > l);
  119.           if (c > 0) then
  120.             Result := (Result + Copy(text, q, c));
  121.         end;
  122.       end else
  123.         begin
  124.           q := i;
  125.           repeat
  126.             if (s[((i + p) - 1)] = t) then
  127.             begin
  128.               if (c > 0) then
  129.                 Result := (Result + Copy(text, q, c));
  130.               Result := (Result + replaceStr);
  131.               i := ((i + z) - 1);
  132.               q := (i + 1);
  133.               c := 0;
  134.             end else
  135.               c := (c + 1);
  136.             i := (i + 1);
  137.           until (i > l);
  138.           if (c > 0) then
  139.             Result := (Result + Copy(text, q, c));
  140.         end;
  141.     end else
  142.     begin
  143.       if not (z = 1) then
  144.       begin
  145.         begin
  146.           repeat
  147.             for p := 1 to z do
  148.             begin
  149.               f := (s[((i + p) - 1)] = o[p]);
  150.               if not f then
  151.                 Break;
  152.             end;
  153.             i := (i + 1);
  154.           until (f or (i > l));
  155.           if f then
  156.           begin
  157.             i := (i - 1);
  158.             if (i > 1) then
  159.               Result := (Result + Copy(text, 1, (i - 1)));
  160.             Result := (Result + replaceStr);
  161.             i := (i + z);
  162.             if not (i > l) then
  163.               Result := (Result + Copy(text, i, ((l - i) + 1)));
  164.           end else
  165.             Result := text;
  166.         end;
  167.       end else
  168.       begin
  169.         begin
  170.           repeat
  171.             f := (s[((i + p) - 1)] = t);
  172.             i := (i + 1);
  173.           until (f or (i > l));
  174.           if f then
  175.           begin
  176.             begin
  177.               i := (i - 1);
  178.               if (i > 1) then
  179.                 Result := (Result + Copy(text, 1, (i - 1)));
  180.               Result := (Result + replaceStr);
  181.               i := (i + z);
  182.               if not (i > l) then
  183.                 Result := (Result + Copy(text, i, ((l - i) + 1)));
  184.             end;
  185.           end else
  186.             Result := text;
  187.         end;
  188.       end;
  189.     end;
  190.   end else
  191.     Result := text;
  192. end;
  193.  
  194. var
  195.   str: string;
  196.   Text:String;
  197.   i, t: Integer;
  198.  
  199. begin
  200.   Text := 'this is a | this is a |this is a|';
  201.   for i := 0 to 15 do
  202.     Text := (Text + Text);
  203.   t := GetTimeRunning;
  204.   str := StrReplace(Text, 'i', '|');
  205.   WriteLn('StrReplace took ' + IntToStr(GetTimeRunning - t) + ' ms.');
  206.   WriteLn(MD5(str));
  207.   str := '';
  208.   t := GetTimeRunning;
  209.   str := StrReplace2(Text, 'i', '|', [rfReplaceAll]);
  210.   WriteLn('StrReplace2 took ' + IntToStr(GetTimeRunning - t) + ' ms.');
  211.   WriteLn(MD5(str));
  212.   str := '';
  213.   t := GetTimeRunning;
  214.   str := Replace(Text, 'i', '|', [rfReplaceAll]);
  215.   WriteLn('Replace took ' + IntToStr(GetTimeRunning - t) + ' ms.');
  216.   WriteLn(MD5(str));
  217. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement