Advertisement
Janilabo

fix't

Dec 4th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.99 KB | None | 0 0
  1. function StrMaskMatch(expression, str: string; mask: Char; 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.     if not Result then
  32.     begin  
  33.       SetLength(a, s);
  34.       h := (s - 1);
  35.       s := Length(e);
  36.       x := (e[1] = mask);
  37.       y := (e[s] = mask);
  38.       if ((x or y) and (w > z)) then
  39.         Exit;  
  40.       case x of
  41.         True:
  42.         begin
  43.           f := 2;
  44.           m := 1;
  45.         end;
  46.         False:
  47.         begin
  48.           f := 1;
  49.           m := z;
  50.         end;
  51.       end;  
  52.       p := f;  
  53.       o := 0;
  54.       for i := 0 to h do
  55.       begin
  56.         p := PosEx(a[i], str, p);
  57.         if (p < 1) then
  58.           Exit;
  59.         if (p < m) then
  60.           m := p;  
  61.         p := ((p + Length(a[i])) + 1);
  62.       end;
  63.       Result := not y;  
  64.       case Result of
  65.         False:
  66.         begin                                  
  67.           Result := ((p - 1) <= z);
  68.           if Result then
  69.             match := Copy(str, m, ((z - m) + 1));
  70.         end;
  71.         True: match := Copy(str, m, (p - m) - 1);
  72.       end;
  73.     end else
  74.       match := str;
  75.   end;
  76. end;
  77.  
  78. var
  79.   Str, expression, Match: string;
  80.  
  81. begin
  82.   ClearDebug;
  83.   expression := '*';
  84.   Str := 'This is text text right here!';
  85.   if StrMaskMatch(expression, Str, '*', Match) then
  86.     WriteLn('Matched: "' + Match + '"');
  87. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement