Advertisement
Janilabo

new

Dec 4th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.84 KB | None | 0 0
  1. function StrMaskMatch(expression, str: string; mask: Char; Sensitive: Boolean; var match: string): Boolean;
  2. var
  3.   a: TStrArray;
  4.   s, h, i, l, w, z, f, p, o, m: Integer;  
  5.   e: string;  
  6.   x, y: Boolean;
  7. begin
  8.   match := '';
  9.   Result := False;
  10.   l := Length(expression);
  11.   z := Length(str);
  12.   if ((l > 0) and (z > 0)) then
  13.   begin
  14.     l := (l - 1);
  15.     for i := 1 to l do
  16.       if not ((expression[i] = mask) and (expression[(i + 1)] = mask)) then
  17.         e := (e + expression[i]);
  18.     e := (e + expression[(l + 1)]);  
  19.     a := Explode(mask, e);
  20.     h := High(a);    
  21.     s := 0;      
  22.     w := 0;
  23.     for i := 0 to h do
  24.       if (a[i] <> '') then
  25.       begin
  26.         a[s] := a[i];  
  27.         w := (w + Length(a[s]));
  28.         s := (s + 1);
  29.       end;  
  30.     Result := (s = 0);
  31.     case Result of
  32.       False:
  33.       begin      
  34.         x := (e[1] = mask);
  35.         y := (e[Length(e)] = mask);
  36.         case (s > 1) of
  37.           True:
  38.           begin  
  39.             SetLength(a, s);
  40.             h := (s - 1);  
  41.             s := Length(e);
  42.             if ((x or y) and (w > z)) then
  43.               Exit;
  44.             case Sensitive of
  45.               True:
  46.               begin
  47.                 case x of
  48.                   True:
  49.                   begin
  50.                     f := 2;
  51.                     m := 1;
  52.                   end;
  53.                   False:
  54.                   begin
  55.                     f := 1;
  56.                     m := z;
  57.                   end;
  58.                 end;  
  59.                 p := f;  
  60.                 o := 0;
  61.                 for i := 0 to h do
  62.                 begin
  63.                   p := PosEx(a[i], str, p);
  64.                   if (p < 1) then
  65.                     Exit;
  66.                   if (p < m) then
  67.                     m := p;  
  68.                   p := ((p + Length(a[i])) + 1);
  69.                 end;
  70.                 Result := not y;  
  71.                 case Result of
  72.                   False:
  73.                   begin                                  
  74.                     Result := ((p - 1) <= z);
  75.                     if Result then
  76.                       match := Copy(str, m, ((z - m) + 1));
  77.                   end;
  78.                   True: match := Copy(str, m, (p - m) - 1);
  79.                 end;
  80.               end;
  81.               False:
  82.               begin
  83.                 if x then
  84.                   f := PosEx(a[0], str, 2)
  85.                 else
  86.                   f := Pos(a[0], str);
  87.                 if (f > 0) then
  88.                 begin
  89.                   m := (f + Length(a[0]));
  90.                   if y then
  91.                     p := (z - 1)
  92.                   else
  93.                     p := z;  
  94.                   l := LastPosEx(a[h], str, p);
  95.                   if (l > 0) then
  96.                   begin      
  97.                     p := l;
  98.                     if (h > 1) then
  99.                     for i := (h - 1) downto 1 do
  100.                     begin
  101.                       p := LastPosEx(a[i], str, (p - 1));
  102.                       if (p < m) then
  103.                         Exit;
  104.                       p := ((p - Length(a[i])));
  105.                     end;
  106.                     Result := True;
  107.                     case (x or y) of
  108.                       True:
  109.                       case (x and y) of
  110.                         False:
  111.                         if y then
  112.                           match := Copy(str, f, ((z - f) + 1))
  113.                         else
  114.                           match := Copy(str, 1, ((l + Length(a[h])) - 1));
  115.                         True: match := str;
  116.                       end;
  117.                       False: match := Copy(str, f, (l - f) + Length(a[h]));
  118.                     end;        
  119.                   end;
  120.                 end;
  121.               end;
  122.             end;
  123.           end;
  124.           False:
  125.           begin          
  126.             if x then
  127.               p := PosEx(a[0], str, 2)
  128.             else
  129.               p := Pos(a[0], str);
  130.             if (p > 0) then
  131.               Result := ((((p - 1) + Length(a[0])) < z) or not y);
  132.             if Result then
  133.             case not (x or y) of
  134.               False:  
  135.               case (x and y) of    
  136.                 False:
  137.                 if not x then                            
  138.                   match := (Copy(str, p, (z - p) + 1))
  139.                 else
  140.                   match := (Copy(str, 1, (p - 1)) + a[0]);
  141.                 True: match := str;
  142.               end;
  143.               True: match := a[0];
  144.             end;
  145.           end;        
  146.         end;
  147.       end;
  148.       True: match := str;
  149.     end;    
  150.   end;
  151. end;
  152.  
  153. var
  154.   Str, expression, Match: string;
  155.  
  156. begin
  157.   ClearDebug;
  158.   expression := 'This* * here*!! ';
  159.   Str := 'This... This is text text right here!!! here is something!?';
  160.   if StrMaskMatch(expression, Str, '*', False, Match) then
  161.     WriteLn('Matched: "' + Match + '"');
  162. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement