Janilabo

Janilabo | Comment & String Filtering [Simba]

Jun 6th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.93 KB | None | 0 0
  1. const
  2.   COMMENT_FILTER = '@'; // CHAR, NOT STRING! Set as * if you want to disable this filter.
  3.   STRING_FILTER = '#'; // CHAR, NOT STRING! Set as * if you want to disable this filter.
  4.   USE_CLIPBOARD_DATA = False; // Use text from clipboard as the str to filter comments and/or strings from?
  5.                               // Set as true for clipboard, false to use the custom str inside SetupSTR procedure.
  6.  
  7. var
  8.   str: string;
  9.  
  10. procedure SetupSTR;
  11. begin
  12.   case USE_CLIPBOARD_DATA of
  13.     False:
  14.     begin
  15.       str := 'begin // THIS IS A TEST ;)' + #13#10 +
  16.              '{' + #13#10 +
  17.              '  ..MULTILINE COMMENT...' #13#10 +
  18.              '  IT JUST WORKS! RIGHT?' + #13#10 +
  19.              '}' + #13#10 +
  20.              '  WriteLn(''WOOHOOOOO, some text over here?'');' + #13#10 +
  21.              '  WriteLn(''USERNAME: '' + Players[9999999].Name + '', PASSWORD: '' + Players[-1].Pass + ''); // SC4MZ AL3RT!!111 :D' + #13#10 +
  22.              '  Wait(1000 + (* PERHAPS A LITTLE COMMENT HERE? *) + Random(1000));' + #13#10 +
  23.              '  WriteLn(''Lets test comment inside here...'' + {COMMENT TEST!} + ''..seems to work!'')' + #13#10 +
  24.              'end.';
  25.     end;
  26.     True: str := GetClipBoard;
  27.   end;
  28. end;
  29.  
  30. {==============================================================================]
  31.   Explanation: Finds position from s items in str. Stores the ID of the found s item to index variable.
  32.                The importance order for d items is from left to right (=>).
  33.                So place the important ones first and then less important after those.
  34.                Contains field for offset.
  35. [==============================================================================}
  36. function PosMultiIDEx(s: TStringArray; str: string; var index: Integer; offset: Integer): Integer;
  37. var
  38.   h, i, p, t: Integer;
  39. begin
  40.   Result := -1;
  41.   index := -1;
  42.   h := High(s);
  43.   if ((h > -1) and (str <> '')) then
  44.   begin
  45.     t := (Length(str) + 1);
  46.     Result := t;
  47.     for i := 0 to h do
  48.     begin
  49.       p := PosEx(s[i], str, offset);
  50.       if ((p > 0) and (p < Result)) then
  51.       begin
  52.         Result := p;
  53.         index := i;
  54.       end;
  55.     end;
  56.     if (Result = t) then
  57.       Result := 0;
  58.   end;
  59. end;
  60.  
  61. type
  62.   TRange = record
  63.     minimum, maximum: Integer;
  64.   end;
  65.   TRangeArray = array of TRange;
  66.  
  67. function ToRange(minimum, maximum: Integer): TRange;
  68. begin
  69.   Result.minimum := Integer(minimum);
  70.   Result.maximum := Integer(maximum);
  71. end;
  72.  
  73. procedure TRAAppend(var TRA: TRangeArray; x: TRange);
  74. var
  75.   aL: Integer;
  76. begin
  77.   aL := (Length(TRA) + 1);
  78.   SetLength(TRA, aL);
  79.   TRA[(aL - 1)] := TRange(x);
  80. end;
  81.  
  82. function TrackCaS(str: string; var comments, strings: TRangeArray): Boolean;
  83. var
  84.   p, s, i, o, e, x, l, a: Integer;
  85.   t: TStringArray;
  86. begin
  87.   Result := False;
  88.   SetLength(comments, 0);
  89.   SetLength(strings, 0);
  90.   l := Length(str);
  91.   if (l > 0) then
  92.   begin
  93.     o := 1;
  94.     t := ['//', '(*', '{', ''''];
  95.     repeat
  96.       s := PosMultiIDEx(t, str, i, o);
  97.       if (s > 0) then
  98.       begin
  99.         o := (s + 1);
  100.         a := 0;
  101.         case i of
  102.           0, 1, 2:
  103.           begin
  104.             case i of
  105.               0:
  106.               begin
  107.                 e := PosMultiIDEx([#13#10, #13, #10], str, x, o);
  108.                 if (x = 0) then
  109.                   a := 1;
  110.                 TRAAppend(comments, ToRange(s, e));
  111.               end;
  112.               1, 2:
  113.               begin
  114.                 case i of
  115.                   1:
  116.                   begin
  117.                     e := PosEx('*)', str, o);
  118.                     a := 1;
  119.                   end;
  120.                   2: e := PosEx('}', str, o);
  121.                 end;
  122.                 if (e = 0) then
  123.                   e := l;
  124.                 TRAAppend(comments, ToRange(s, (e + a)));
  125.               end;
  126.             end;
  127.           end;
  128.           3:
  129.           begin
  130.             e := PosMultiIDEx([#13#10, #13, #10, ''''], str, x, o);
  131.             if (x = 0) then
  132.               a := 1;
  133.             case (e = 0) of
  134.               True:
  135.               begin
  136.                 e := l;
  137.                 TRAAppend(strings, ToRange(s, e));
  138.               end;
  139.               False:
  140.               case x of
  141.                 0, 1, 2: TRAAppend(strings, ToRange(s, e));
  142.                 3: TRAAppend(strings, ToRange(s, (e + a)));
  143.               end;
  144.             end;
  145.           end;
  146.         end;
  147.         o := ((e + 1) + a);
  148.       end;
  149.     until (s = 0);
  150.   end;
  151. end;
  152.  
  153. {==============================================================================]
  154.   Explanation: Returns all the positions by items from s array in str. Place s items in importance order (=>)
  155.                If overlap is set to true, strings can overlap.
  156.                (['aa'], 'baaaah', False) => [2,3,4]
  157.                (['aa'], 'baaaah', True) => [2,4]
  158. [==============================================================================}
  159. function PosAllMulti(s: TStringArray; str: string; overlap: Boolean): TIntegerArray;
  160. var
  161.   h, l, p, o, x, i, t, r, y, d: Integer;
  162. begin
  163.   h := High(s);
  164.   y := Length(str);
  165.   if ((y > 0) and (h > -1)) then
  166.   begin
  167.     SetLength(Result, y);
  168.     o := 1;
  169.     repeat
  170.       p := 0;
  171.       for x := 0 to h do
  172.       begin
  173.         t := PosEx(s[x], str, (l + o));
  174.         case (t < 1) of
  175.           True:
  176.           begin
  177.             for d := x to (h - 1) do
  178.               s[d] := s[(d + 1)];
  179.             SetLength(s, h);
  180.             Dec(x);
  181.             Dec(h);
  182.           end;
  183.           False:
  184.           if ((p = 0) or (t < p)) then
  185.           begin
  186.             p := t;
  187.             i := x;
  188.           end;
  189.         end;
  190.       end;
  191.       if (p > 0) then
  192.       begin
  193.         Result[r] := p;
  194.         Inc(r);
  195.         l := p;
  196.         if not overlap then
  197.           o := Length(s[i]);
  198.       end;
  199.     until (p <= 0);
  200.   end;
  201.   SetLength(Result, r);
  202. end;
  203.  
  204. procedure FillStrRangeEx(var str: string; fillWith: Char; range: TRange; exceptions: TIntegerArray);
  205. var
  206.   i, l, m, c: Integer;
  207. begin
  208.   l := Length(str);
  209.   if ((l > 0) and not (range.minimum > range.maximum)) then
  210.   begin
  211.     if (range.minimum < 1) then
  212.       range.minimum := 1;
  213.     if (range.maximum > l) then
  214.       range.maximum := l;
  215.     if (range.minimum > l) then
  216.       Exit;
  217.     c := iAbs(range.maximum - range.minimum);
  218.     for i := range.minimum to range.maximum do
  219.       if not InIntArray(exceptions, i) then
  220.         str[i] := fillWith;
  221.   end;
  222. end;
  223.  
  224. var
  225.   ID, position: Integer;
  226.   ignore: TIntegerArray;
  227.   a, b: TRangeArray;
  228.   h, i: Integer;
  229.  
  230. begin
  231.   ClearDebug;
  232.   SetupSTR;
  233.   WriteLn('STR BEFORE: ' + #13#10 + str);
  234.   TrackCaS(str, a, b);
  235.   h := High(a);
  236.   ignore := PosAllMulti([#13, #10], str, False);
  237.   if (COMMENT_FILTER <> '*') then
  238.   for i := 0 to h do
  239.     FillStrRangeEx(str, COMMENT_FILTER, a[i], ignore);
  240.   WriteLn('');
  241.   h := High(b);
  242.   if (STRING_FILTER <> '*') then
  243.   for i := 0 to h do
  244.     FillStrRangeEx(str, STRING_FILTER, b[i], ignore);
  245.   WriteLn('STR AFTER: ' + #13#10 + str);
  246. end.
Advertisement
Add Comment
Please, Sign In to add comment