Janilabo

Janilabo | Find() DEV [Simba]

May 25th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 10.40 KB | None | 0 0
  1. const
  2.   TEXT = 'TEST! Test, Test1 test? test TeSt testest testtest test.';
  3.   STR = 'test';
  4.  
  5. {==============================================================================]
  6.   Explanation: Important types for Find() function! Contains the string matching methods.
  7. [==============================================================================}
  8. type
  9.   TMatchMethod = (mmAll, mmIgnoreCase, mmOverlap, mmWholeWords, mmStrictWW);
  10.   TMatchMethods = set of TMatchMethod;
  11.  
  12. {==============================================================================]
  13.   Explanation: Returns a TIA that contains all the value from start value (aStart) to finishing value (aFinish)..
  14. [==============================================================================}
  15. function TIAByRange(aStart, aFinish: Integer): TIntegerArray;
  16. var
  17.   i, s, f: Integer;
  18. begin
  19.   if (aStart <> aFinish) then
  20.   begin
  21.     s := Integer(aStart);
  22.     f := Integer(aFinish);
  23.     SetLength(Result, (IAbs(aStart - aFinish) + 1));
  24.     case (aStart > aFinish) of
  25.       True:
  26.       for i := s downto f do
  27.         Result[(s - i)] := i;
  28.       False:
  29.       for i := s to f do
  30.         Result[(i - s)] := i;
  31.     end;
  32.   end else
  33.     Result := [Integer(aStart)];
  34. end;
  35.  
  36. {==============================================================================]
  37.   Explanation: Merges T2DIntegerArray (ATIA) to TIntegerArray.
  38.                Example: [[1, 2], [3, 4]] => [1, 2, 3, 4]
  39. [==============================================================================}
  40. function ATIAMerge(ATIA: T2DIntegerArray): TIntegerArray;
  41. var
  42.   i, i2, h, h2, r: Integer;
  43. begin
  44.   h := High(ATIA);
  45.   if (h > -1) then
  46.   begin
  47.     for i := 0 to h do
  48.       IncEx(r, (High(ATIA[i]) + 1));
  49.     SetLength(Result, r);
  50.     r := 0;
  51.     for i := 0 to h do
  52.     begin
  53.       h2 := High(ATIA[i]);
  54.       for i2 := 0 to h2 do
  55.       begin
  56.         Result[r] := Integer(ATIA[i][i2]);
  57.         Inc(r);
  58.       end;
  59.     end;
  60.   end else
  61.     SetLength(Result, 0);
  62. end;
  63.  
  64. {==============================================================================]
  65.   Explanation: Returns the string that is behind the position in data, size being the length for result that is copied from position.
  66. [==============================================================================}
  67. function Behind(data: string; position, size: Integer): string;
  68. var
  69.   l, r: Integer;
  70. begin
  71.   l := Length(data);
  72.   case ((l > 0) and (position > 1) and (size > 0)) of
  73.     True:
  74.     begin
  75.       if ((position - size) < 1) then
  76.         size := ((position - size) + (size - 1));
  77.       if (position > (l + 1)) then
  78.       begin
  79.         r := ((position - l) - 1);
  80.         size := (size - r);
  81.       end;
  82.       Result := Copy(data, ((position - size) - r), size);
  83.     end;
  84.     False: Result := '';
  85.   end;
  86. end;
  87.  
  88. {==============================================================================]
  89.   Explanation: Returns the string that is ahead from position in data, size being the length for result that is copied from position.
  90. [==============================================================================}
  91. function Ahead(data: string; position, size: Integer): string;
  92. var
  93.   l: Integer;
  94. begin
  95.   l := Length(data);
  96.   case ((l > 0) and (position <= l) and (size > 0)) of
  97.     True:
  98.     begin
  99.       if (position < 1) then
  100.       begin
  101.         size := (size - iAbs(position - 1));
  102.         position := 1;
  103.       end;
  104.       if ((size > 0) and ((position + size) > l)) then
  105.         size := (size - (((position + size) - l) - 1));
  106.       Result := Copy(data, position, size);
  107.     end;
  108.     False: Result := '';
  109.   end;
  110. end;
  111.  
  112. {==============================================================================]
  113.   Explanation: Returns all the positions of found/matching strings (findStr) in text.
  114.                Uses a set of TMatchMethod (methods) for string matching.
  115.                Contains field for offset.
  116. [==============================================================================}
  117. function FindEx(text, findStr: string; methods: TMatchMethods; offset: Integer): TIntegerArray;
  118. var
  119.   sb, sa: string;
  120.   r, l, f, o, p, d, x, y: Integer;
  121.   re: TRegExp;
  122.   ma, mb, a, s, ol: Boolean;
  123.   c: TIntegerArray;
  124. begin
  125.   l := Length(text);
  126.   f := Length(findStr);
  127.   if ((l > 0) and (f > 0) and (offset <= (l - f))) then
  128.   begin
  129.     if (offset < 1) then
  130.       offset := 1;
  131.     SetLength(Result, l);
  132.     re := TRegExp.Create;
  133.     re.InputString := text;
  134.     re.Expression := findStr;
  135.     if (mmIgnoreCase in methods) then
  136.       re.ModifierI := True;
  137.     a := (mmAll in methods);
  138.     case a of
  139.       False: re.ModifierG := True;
  140.       True: re.ModifierG := False;
  141.     end;
  142.     ol := (mmOverlap in methods);
  143.     if not ol then
  144.       o := (Length(findStr) - 1);
  145.     Inc(o);
  146.     p := Offset;
  147.     if re.ExecPos(p) then
  148.     repeat
  149.       if (re.Match[0] <> '') then
  150.       begin
  151.         Result[r] := re.MatchPos[0];
  152.         p := (Result[r] + o);
  153.         Inc(r);
  154.       end;
  155.     until not re.ExecPos(p);
  156.     re.Free;
  157.     SetLength(Result, r);
  158.     if ((r > 0) and (mmWholeWords in methods)) then
  159.     begin
  160.       s := (mmStrictWW in methods);
  161.       if not s then
  162.         c := ATIAMerge([TIAByRange(Ord('A'), Ord('Z')), TIAByRange(Ord('a'), Ord('z')), TIAByRange(Ord('0'), Ord('9'))]);
  163.       for x := (r - 1) downto 0 do
  164.       begin
  165.         sb := Behind(text, Result[x], 1);
  166.         sa := Ahead(text, (Result[x] + f), 1);
  167.         case s of
  168.           True:
  169.           begin
  170.             case ol of
  171.               True:
  172.               case (x > 0) of
  173.                 True:
  174.                 case ((r - 1) > x) of
  175.                   True: mb := ((((Result[(x + 1)] - Result[x]) <= f) or ((Result[x] - Result[(x - 1)]) <= f)) or ((sb = ' ') or (sb = '')));
  176.                   False: mb := (((Result[x] - Result[(x - 1)]) <= f) or ((sb = ' ') or (sb = '')));
  177.                 end;
  178.                 False:
  179.                 case ((r - 1) > x) of
  180.                   True: mb := (((Result[(x + 1)] - Result[x]) <= f) or ((sb = ' ') or (sb = '')));
  181.                   False: mb := ((sb = ' ') or (sb = ''));
  182.                 end;
  183.               end;
  184.               False: mb := ((sb = ' ') or (sb = ''));
  185.             end;
  186.             case ol of
  187.               True:
  188.               case (x > 0) of
  189.                 True:
  190.                 case ((r - 1) > x) of
  191.                   True: ma := ((((Result[(x + 1)] - Result[x]) <= f) or ((Result[x] - Result[(x - 1)]) <= f)) or ((sa = ' ') or (sa = '')));
  192.                   False: ma := (((Result[x] - Result[(x - 1)]) <= f) or ((sa = ' ') or (sa = '')));
  193.                 end;
  194.                 False:
  195.                 case ((r - 1) > x) of
  196.                   True: ma := (((Result[(x + 1)] - Result[x]) <= f) or ((sa = ' ') or (sa = '')));
  197.                   False: ma := ((sa = ' ') or (sa = ''));
  198.                 end;
  199.               end;
  200.               False: ma := ((sa = ' ') or (sa = ''));
  201.             end;
  202.           end;
  203.           False:
  204.           begin
  205.             case (sb <> '') of
  206.               True:
  207.               case ol of
  208.                 True:
  209.                 case (x > 0) of
  210.                   True:
  211.                   case ((r - 1) > x) of
  212.                     True: mb := ((((Result[(x + 1)] - Result[x]) <= f) or ((Result[x] - Result[(x - 1)]) <= f)) or not InIntArray(c, Ord(sb[1])));
  213.                     False: mb := (((Result[x] - Result[(x - 1)]) <= f) or not InIntArray(c, Ord(sb[1])));
  214.                   end;
  215.                   False:
  216.                   case ((r - 1) > x) of
  217.                     True: mb := (((Result[(x + 1)] - Result[x]) <= f) or not InIntArray(c, Ord(sb[1])));
  218.                     False: mb := not InIntArray(c, Ord(sb[1]));
  219.                   end;
  220.                 end;
  221.                 False: mb := not InIntArray(c, Ord(sb[1]));
  222.               end;
  223.               False: mb := True;
  224.             end;
  225.             case (sa <> '') of
  226.               True:
  227.               case ol of
  228.                 True:
  229.                 case (x > 0) of
  230.                   True:
  231.                   case ((r - 1) > x) of
  232.                     True: ma := ((((Result[(x + 1)] - Result[x]) <= f) or ((Result[x] - Result[(x - 1)]) <= f)) or not InIntArray(c, Ord(sa[1])));
  233.                     False: ma := (((Result[x] - Result[(x - 1)]) <= f) or not InIntArray(c, Ord(sa[1])));
  234.                   end;
  235.                   False:
  236.                   case ((r - 1) > x) of
  237.                     True: ma := (((Result[(x + 1)] - Result[x]) <= f) or not InIntArray(c, Ord(sa[1])));
  238.                     False: ma := not InIntArray(c, Ord(sa[1]));
  239.                   end;
  240.                 end;
  241.                 False: ma := not InIntArray(c, Ord(sa[1]));
  242.               end;
  243.               False: ma := True;
  244.             end;
  245.           end;
  246.         end;
  247.         if not (mb and ma) then
  248.         begin
  249.           y := (r - 1);
  250.           for d := x to (y - 1) do
  251.             Result[d] := Result[(d + 1)];
  252.           SetLength(Result, y);
  253.           Dec(r);
  254.         end;
  255.       end;
  256.     end;
  257.     if (not a and (r > 0)) then
  258.       SetLength(Result, 1);
  259.   end else
  260.     SetLength(Result, 0);
  261. end;
  262.  
  263. {==============================================================================]
  264.   Explanation: Returns all the positions of found/matching strings (findStr) in text.
  265.                Uses a set of TMatchMethod (methods) for string matching.
  266.                Without offset field.
  267. [==============================================================================}
  268. function Find(text, findStr: string; methods: TMatchMethods): TIntegerArray;
  269. begin
  270.   Result := FindEx(text, findStr, methods, 1);
  271. end;
  272.  
  273. begin
  274.   ClearDebug;
  275.   WriteLn(ToStr(Find(TEXT, STR, [mmIgnoreCase, mmWholeWords, mmStrictWW])) + ' Find(TEXT, STR, [mmIgnoreCase, mmWholeWords, mmStrictWW])');
  276.   WriteLn(ToStr(Find(TEXT, STR, [mmIgnoreCase, mmWholeWords])) + ' Find(TEXT, STR, [mmIgnoreCase, mmWholeWords])');
  277.   WriteLn(ToStr(Find(TEXT, STR, [mmIgnoreCase])) + ' Find(TEXT, STR, [mmIgnoreCase])');
  278.   WriteLn(ToStr(Find(TEXT, STR, [])) + ' Find(TEXT, STR, [])');
  279.   WriteLn(ToStr(Find(TEXT, STR, [mmAll])) + ' Find(TEXT, STR, [mmAll])');
  280.   WriteLn(ToStr(Find(TEXT, STR, [mmAll, mmIgnoreCase])) + ' Find(TEXT, STR, [mmAll, mmIgnoreCase])');
  281.   WriteLn(ToStr(Find(TEXT, STR, [mmAll, mmIgnoreCase, mmWholeWords])) + ' Find(TEXT, STR, [mmAll, mmIgnoreCase, mmWholeWords])');
  282.   WriteLn(ToStr(Find(TEXT, STR, [mmAll, mmIgnoreCase, mmWholeWords, mmStrictWW])) + ' Find(TEXT, STR, [mmAll, mmIgnoreCase, mmWholeWords, mmStrictWW])');
  283. end.
Advertisement
Add Comment
Please, Sign In to add comment