Advertisement
WarPie90

Fast Pascal String

Dec 10th, 2013
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.07 KB | None | 0 0
  1. function Wee(str: string): string;
  2. var
  3.   h, i: Integer;
  4.   s, r: string;
  5. begin
  6.   if (Length(str) > 1) then
  7.   begin
  8.     s := '0'#0 'a'#7 'b'#8 't'#9 'n'#10 'f'#12 'r'#13 'e'#27;
  9.     h := (Length(s) div 2);
  10.     r := #0'[R%B%S]'#0;
  11.     Result := Replace(str, '\\', r, [rfReplaceAll]);
  12.     for i := 1 to h do
  13.       Result := Replace(Result, ('\' + s[((i * 2) - 1)]), s[(i * 2)], [rfReplaceAll]);
  14.     Result := Replace(Result, r, '\', [rfReplaceAll]);
  15.   end;
  16. end;
  17.  
  18. function Wee2(str: string): string;
  19. var
  20.   b, h, i, l, p, c, q: Integer;
  21.   s, r, t, z: string;
  22. begin
  23.   Result := '';
  24.   if (Length(str) > 1) then
  25.   begin
  26.     s := '0abtnfre'#0#7#8#9#10#12#13#27;
  27.     h := (Length(s) div 2);
  28.     r := #0'[R%B%S]'#0;
  29.     c := 0;
  30.     p := 0;
  31.     b := 1;
  32.     z := Replace(str, '\\', r, [rfReplaceAll]);
  33.     l := (Length(z) - 1);
  34.     repeat
  35.       p := (p + 1);
  36.       if (z[p] = '\') then
  37.       begin
  38.         q := (p + 1);
  39.         for i := 1 to h do
  40.           if (z[q] = s[i]) then
  41.           begin
  42.             if (c > 0) then
  43.               Result := (Result + Copy(z, b, c));
  44.             Result := (Result + s[(i + h)]);
  45.             p := q;
  46.             b := (p + 1);
  47.             c := -1;
  48.             Break;
  49.           end;
  50.       end;
  51.       c := (c + 1);
  52.     until (p >= l);
  53.     Result := (Result + Copy(z, b, (c + 1)));
  54.     Result := Replace(Result, r, '\', [rfReplaceAll]);
  55.   end;
  56. end;
  57.  
  58.  
  59. function Wee3(Str: string): String;
  60. var
  61.   hi,i,j,k,hipat: Integer;
  62.   ptrn, repl: TCharArray;
  63.   prev,next: Char;
  64.   DidRep: Boolean;
  65. begin
  66.   Result := '';
  67.   if (Length(str) > 1) then
  68.   begin
  69.     ptrn := ['0','a','b','t','n','f','r','e'];
  70.     repl := [#0, #7, #8, #9, #10,#12,#13,#27];
  71.     hipat := High(ptrn);
  72.     Hi := Length(Str);
  73.     SetLength(Result, (Hi+1));
  74.     prev := ' ';
  75.     k := 1;
  76.     i := 1;
  77.     repeat
  78.       DidRep := False;
  79.       if (Str[i] = '\') then
  80.         if (Prev = '\') then
  81.           Inc(i)
  82.         else begin
  83.           next := Str[i+1];
  84.           for j := 0 to HiPat do
  85.             if (next = ptrn[j]) then
  86.             begin
  87.               Result[k] := repl[j];
  88.               Inc(k);
  89.               DidRep := True;
  90.               i := i+2;
  91.               break;
  92.             end;
  93.           if DidRep then Continue;
  94.         end;
  95.       Result[k] := Str[i];
  96.       prev := Str[i];
  97.       Inc(k);
  98.       Inc(i);
  99.     until (i > Hi);
  100.     SetLength(Result, k-1);
  101.   end else
  102.     Result := Str[1];
  103. end;
  104.  
  105. var
  106.   t: Integer;
  107.   str, data: string;
  108.  
  109. begin
  110.   data := 'Test \r\n Wtf \\n Wee\r\n';
  111.   for t := 0 to 13 do
  112.     data := (data + data);
  113.   WriteLn(Length(data));
  114.   t := GetSystemTime;
  115.   str := Wee2(data);
  116.   WriteLn('Wee2 - ' + IntToStr(Length(str)) + ': ' + IntToStr(GetSystemTime - t) + ' ms. [' + MD5(str) + ']');
  117.   str := '';
  118.   t := GetSystemTime;
  119.   str := Wee(data);
  120.   WriteLn('Wee - ' + IntToStr(Length(str)) + ': ' + IntToStr(GetSystemTime - t) + ' ms. [' + MD5(str) + ']');
  121.   t := GetSystemTime;
  122.   str := Wee3(data);
  123.   WriteLn('Wee3 - ' + IntToStr(Length(str)) + ': ' + IntToStr(GetSystemTime - t) + ' ms. [' + MD5(str) + ']');
  124. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement