Advertisement
Janilabo

matches

Dec 2nd, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.98 KB | None | 0 0
  1. {==============================================================================]
  2. @action: Returns true if s exists as position in str.
  3. @note: None.
  4. @contributors: Janilabo, slacky
  5. [==============================================================================}
  6. function StrPosMatch(s, str: string; position: Integer): Boolean; // callconv inline;
  7. var
  8.   a, b, x, y: Integer;
  9. begin
  10.   a := Length(str);
  11.   b := Length(s);
  12.   Result := False;
  13.   if ((position > 0) and (b > 0) and not ((b > a) or (position > a))) then
  14.   case (b > 1) of
  15.     True:
  16.     if (position <= ((a - b) + 1)) then
  17.     begin
  18.       y := ((position + b) - 1);
  19.       for x := position to y do
  20.         if not (str[x] = s[((x - position) + 1)]) then
  21.           Exit;
  22.       Result := True;
  23.     end;
  24.     False: Result := (str[position] = s[1])
  25.   end;
  26. end;
  27.  
  28. {==============================================================================]
  29. @action: Returns s position from str. Starts scanning from offset.
  30. If s doesn't exist in str, result will be set as 0.
  31. @note: PosEx()
  32. @contributors: Janilabo, slacky
  33. [==============================================================================}
  34. function StrPosEx(s, str: string; offset: Integer): Integer; // callconv inline;
  35. var
  36.   a, b, o, p: Integer;
  37.   c: Char;
  38. begin
  39.   Result := 0;
  40.   a := Length(str);
  41.   b := Length(s);
  42.   if ((b > 0) and not ((b > a) or (offset > a))) then
  43.   begin
  44.     if not (offset < 1) then
  45.       o := offset
  46.     else
  47.       o := 1;
  48.     case (b > 1) of
  49.       True:
  50.       if (o < ((a - b) + 2)) then
  51.       begin
  52.         p := ((a - b) + 1);
  53.         for Result := o to p do
  54.           if StrPosMatch(s, str, Result) then
  55.             Exit;
  56.         Result := 0;
  57.       end;
  58.       False:
  59.       begin
  60.         c := s[1];
  61.         for Result := o to a do
  62.           if (str[Result] = c) then
  63.             Exit;
  64.         Result := 0;
  65.       end;
  66.     end;
  67.   end;
  68. end;
  69.  
  70. {==============================================================================]
  71. @action: Returns the last s position in str.
  72. @note: None
  73. @contributors: Janilabo, slacky
  74. [==============================================================================}
  75. function StrLastPos(s, str: string): Integer; // callconv
  76. var
  77.   l, t, p: Integer;
  78.   m: Boolean;
  79.   c: Char;
  80. begin
  81.   l := Length(str);
  82.   t := Length(s);
  83.   if ((t > 0) and not (t > l)) then
  84.   case (t > 1) of
  85.     True:
  86.     begin
  87.       p := ((l - t) + 1);
  88.       repeat
  89.         m := StrPosMatch(s, str, p);
  90.         if not m then
  91.           p := (p - 1);
  92.       until (m or (p < 1));
  93.       Result := p; // Exit(p);
  94.       Exit;
  95.     end;
  96.     False:
  97.     begin
  98.       c := s[1];
  99.       for Result := l downto 1 do
  100.         if (str[Result] = c) then
  101.           Exit;
  102.     end;
  103.   end;
  104.   Result := 0;
  105. end;
  106.  
  107. {*
  108.   Matches a pattern string and returns the match.
  109.   Takes a ignore-char, so we skip what is between then given parts of the ignore char.
  110.   > Developed by Janilabo!
  111. *}
  112. function StrMatches(Expr, Str: string; Ignore: Char; Sensitive: Boolean; var Match: String): Boolean;
  113. var
  114.   TSA: TStrArray;
  115.   Hi, i, _Right, P, Left: Integer;
  116. begin
  117.   Result := False;
  118.   _Right := Length(Expr);
  119.   if ((_Right > 0) and not (_Right > Length(Str))) then
  120.   begin
  121.     TSA := Explode(ignore, expr);
  122.     Hi := High(TSA);
  123.     Left := Pos(TSA[0], Str);
  124.     _Right := (Left + Length(TSA[0]));
  125.     if ((Hi > 0) and (Left > 0)) then
  126.     begin
  127.       case Sensitive of
  128.         True:
  129.           for i := 1 to Hi do
  130.           begin
  131.             p := StrPosEx(TSA[i], Str, _Right);
  132.             if (p > 0) then
  133.               _Right := (P + Length(TSA[i]))
  134.             else
  135.               Exit;
  136.           end;
  137.         False:
  138.           for i := 1 to Hi do
  139.           begin
  140.             p := StrLastPos(TSA[i], str);
  141.             if (p > _Right) then
  142.               _Right := (P + Length(TSA[i]))
  143.             else
  144.               Exit;
  145.           end;  
  146.       end;  
  147.       Match := Copy(Str, Left, (_Right - Left));
  148.       Result := True;      
  149.     end;
  150.   end;
  151. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement