Advertisement
Janilabo

pfs

Dec 10th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.35 KB | None | 0 0
  1. // Credits to Dgby714
  2. function Wee(str: string): string;
  3. var
  4.   h, i: Integer;
  5.   s, r: string;
  6. begin
  7.   if (Length(str) > 1) then
  8.   begin
  9.     s := '0'#0 'a'#7 'b'#8 't'#9 'n'#10 'f'#12 'r'#13 'e'#27;
  10.     h := (Length(s) div 2);
  11.     r := #0'[R%B%S]'#0;
  12.     Result := Replace(str, '\\', r, [rfReplaceAll]);
  13.     for i := 1 to h do
  14.       Result := Replace(Result, ('\' + s[((i * 2) - 1)]), s[(i * 2)], [rfReplaceAll]);
  15.     Result := Replace(Result, r, '\', [rfReplaceAll]);
  16.   end else
  17.     Result := str;
  18. end;
  19.  
  20. function Wee2(str: string): string;
  21. var
  22.   b, h, i, l, p, c, q: Integer;
  23.   s, r, t, z: string;
  24. begin
  25.   Result := '';
  26.   if (Length(str) > 1) then
  27.   begin
  28.     s := '0abtnfre'#0#7#8#9#10#12#13#27;
  29.     h := (Length(s) div 2);
  30.     r := #0'[R%B%S]'#0;
  31.     c := 0;
  32.     p := 0;
  33.     b := 1;
  34.     z := Replace(str, '\\', r, [rfReplaceAll]);
  35.     l := (Length(z) - 1);
  36.     repeat
  37.       p := (p + 1);
  38.       if (z[p] = '\') then
  39.       begin
  40.         q := (p + 1);
  41.         for i := 1 to h do
  42.           if (z[q] = s[i]) then
  43.           begin
  44.             if (c > 0) then
  45.               Result := (Result + Copy(z, b, c));
  46.             Result := (Result + s[(i + h)]);
  47.             p := q;
  48.             b := (p + 1);
  49.             c := -1;
  50.             Break;
  51.           end;
  52.       end;
  53.       c := (c + 1);
  54.     until (p >= l);
  55.     Result := (Result + Copy(z, b, (c + 1)));
  56.     Result := Replace(Result, r, '\', [rfReplaceAll]);
  57.   end else
  58.     Result := str;
  59. end;
  60.  
  61. // Credits to slacky
  62. function Wee3(str: string): String;
  63. var
  64.   hi,i,j,k,hipat: Integer;
  65.   ptrn, repl: TCharArray;
  66.   prev,next: Char;
  67.   DidRep: Boolean;
  68. begin
  69.   if (Length(str) > 1) then
  70.   begin
  71.     Result := '';
  72.     ptrn := ['0','a','b','t','n','f','r','e'];
  73.     repl := [#0, #7, #8, #9, #10,#12,#13,#27];
  74.     hipat := High(ptrn);
  75.     Hi := Length(Str);
  76.     SetLength(Result, (Hi + 1));
  77.     prev := ' ';
  78.     k := 1;
  79.     i := 1;
  80.     repeat
  81.       DidRep := False;
  82.       if (Str[i] = '\') then
  83.         if not (Prev = '\') then
  84.         begin
  85.           next := Str[(i + 1)];
  86.           for j := 0 to HiPat do
  87.             if (next = ptrn[j]) then
  88.             begin
  89.               Result[k] := repl[j];
  90.               Inc(k);
  91.               DidRep := True;
  92.               i := (i + 2);
  93.               Break;
  94.             end;
  95.           if DidRep then
  96.             Continue;
  97.         end else
  98.           Inc(i);
  99.       Result[k] := Str[i];
  100.       prev := Str[i];
  101.       Inc(k);
  102.       Inc(i);
  103.     until (i > Hi);
  104.     SetLength(Result, (k - 1));
  105.   end else
  106.     Result := Str;
  107. end;
  108.  
  109. // Credits to slacky
  110. function Wee4(str: string): string;
  111. var
  112.   l, i, j, k, g: Integer;
  113.   s, c: string;
  114.   p, n: Char;
  115.   d: Boolean;
  116. begin
  117.   l := Length(str);
  118.   if (l > 1) then
  119.   begin
  120.     s := '0abtnfre';
  121.     c := #0#7#8#9#10#12#13#27;
  122.     g := Length(s);
  123.     SetLength(Result, (l + 1));
  124.     p := #32;
  125.     k := 1;
  126.     i := 1;
  127.     repeat
  128.       if (str[i] = '\') then
  129.         if not (p = '\') then
  130.         begin
  131.           n := str[(i + 1)];
  132.           for j := 1 to g do
  133.           begin
  134.             d := (n = s[j]);
  135.             if d then
  136.             begin
  137.               Result[k] := c[j];
  138.               k := (k + 1);
  139.               i := (i + 2);
  140.               Break;
  141.             end;
  142.           end;
  143.           if d then
  144.             Continue;
  145.         end else
  146.           i := (i + 1);
  147.       Result[k] := str[i];
  148.       p := str[i];
  149.       k := (k + 1);
  150.       i := (i + 1);
  151.     until (i > l);
  152.     SetLength(Result, (k - 1));
  153.   end else
  154.     Result := str;
  155. end;
  156.  
  157. var
  158.   t: Integer;
  159.   str, data: string;
  160.  
  161. begin
  162.   ClearDebug;
  163.   data := 'Test \r\n Wtf \\n Wee\r\n';
  164.   for t := 0 to 13 do
  165.     data := (data + data);
  166.   WriteLn(Length(data));
  167.   t := GetSystemTime;
  168.   str := Wee3(data);
  169.   WriteLn('Wee3 - ' + IntToStr(Length(str)) + ': ' + IntToStr(GetSystemTime - t) + ' ms. [' + MD5(str) + ']');
  170.   str := '';
  171.   t := GetSystemTime;
  172.   str := Wee4(data);
  173.   WriteLn('Wee4 - ' + IntToStr(Length(str)) + ': ' + IntToStr(GetSystemTime - t) + ' ms. [' + MD5(str) + ']');
  174.   str := '';
  175.   t := GetSystemTime;
  176.   str := Wee2(data);
  177.   WriteLn('Wee2 - ' + IntToStr(Length(str)) + ': ' + IntToStr(GetSystemTime - t) + ' ms. [' + MD5(str) + ']');
  178.   str := '';
  179.   t := GetSystemTime;
  180.   str := Wee(data);
  181.   WriteLn('Wee - ' + IntToStr(Length(str)) + ': ' + IntToStr(GetSystemTime - t) + ' ms. [' + MD5(str) + ']');
  182. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement