ماهو الخطاء في دالة تبحث بإستعمال ال WildCards
#1
السلام عليكم .
انا الان بصدد تكملة دالة تبحث بإستعمال ال WildCards .
لكن لاادري إن كان هناك خطاء ام لا سوف اطرح الدالة ومثال صغير ومن راى خطاء فليوجهني الى الصواب من فظلكم :
اولا الدالة :




PHP كود :
uses
   FastStrings
;

     (*
         
StrToPattern() and PosPattern()
       
      
     

         
Uses
           FastStrings 
//from DroopyEyes

         
Tested with Delphi 7.
         Handles ASCII text
.


         
Find a string pattern in a target string.

         
Wildcard characters: '*' and '?'

         '*' 
: Any number of characters including none.
         
'?' : Any character.

         
Pattern string: *??abc*?abc*def??ghi*


                    -
2    -1   0   2    0
                    n     n    n   n    n
                    
*??abc*?abc*def??ghi*

                    
n  <0  min pad length
                       0   any
/none pad length
                       0
>  required pad length
     
*)

     
type
       TPattern 
= record
         Strs 
: array[0..49]of string[50];
         
Lens : array[0..49]of integer;
         
FirstStr, LastStr, MinLen, Count : integer;
         
PrefixPad, SuffixPad : integer;
       
end;

     
procedure StrToPattern(const s: string; var P: TPattern);
     var
       
a, b, k, len, n : integer;
     
begin
       FillChar
(P, SizeOf(P), 0);
       
P.FirstStr := -1;
       
n := 0;

       if 
s <> '' then begin
         len 
:= Length(s);
         
a := 1;
         while (
a <= len) and (n < 50) do begin
           
if s[a] in ['*', '?'] then begin
             k 
:= 1;
             while (
a <= len) and (s[a] in ['*', '?']) do begin
               
if s[a] = '*' then
                 
// *
                 
k := -1
               
else
                 
// ?
                 
Inc(P.Lens[n]);

               
Inc(a);
             
end;

             
P.Lens[n] := P.Lens[n] *k; // -/+
             
Inc(P.MinLen, Abs(P.Lens[n]));

             
Inc(n);
             
P.Count := n;

           
end else begin
             b 
:= a;
             while (
b <= len) and not(s[b] in ['*', '?']) do
               
Inc(b);

             
P.Lens[n] := b -a;
             
P.Strs[n] := Copy(s, a, b -a);
             
Inc(P.MinLen, Length(P.Strs[n]));
             if 
P.FirstStr < 0 then
               P
.FirstStr := n;
             
P.LastStr := n;

             
Inc(n);
             
P.Count := n;

             
a := b;
           
end;
         
end;

         if 
P.FirstStr > 0 then begin
           
for a := 0 to P.FirstStr -1 do
             
Inc(P.PrefixPad, Abs(P.Lens[a]));

           
a := P.Count -1;
           while 
a > P.LastStr do begin
             Inc
(P.SuffixPad, Abs(P.Lens[a]));
             
Dec(a);
           
end;
         
end;

       
end;
     
end;

  function 
PosPattern(const s: string; P: TPattern): integer;
    var
      
i, j, k, n, dx, len, absPos : integer;
    
begin
      result 
:= 0;

      
len := Length(s);
      if (
P.Count > 0) and (len >= P.MinLen) then begin
        
if (P.Count = 1) and (P.Lens[0] = 0) then
          
// '*'
          
result := 1
        
else begin
          
if P.FirstStr < 0 then begin
            
// '????'
            
if len >= P.MinLen then
              result 
:= 1;
          
end else begin

            i 
:= 1 +P.PrefixPad;

            while (
i <= len -P.SuffixPad) and (result < 1) do begin

              j 
:= FastPos(s, P.Strs[P.FirstStr], len, P.Lens[P.FirstStr], i);
              if 
j > 0 then begin
                k 
:= j +P.Lens[P.FirstStr];
                
dx := P.FirstStr +1;
                
absPos := 0;
                while (
dx < P.Count) and (result < 1) do begin
                  
if P.Strs[dx] = '' then begin
                    
// '*' , '?'
                    
Inc(k, Abs(P.Lens[dx]));
                    if 
P.Lens[dx] > 0 then
                      absPos 
:= k
                    
else
                      
absPos := 0;
                  
end else begin
                    n 
:= FastPos(s, P.Strs[dx], len, P.Lens[dx], k);
                    if 
n < 1 then
                      
exit
                    else  
begin
                      
if (absPos > 0) and (n <> absPos) then
                        
break
                      else
                        
k := n +P.Lens[dx];
                    
end;
                    
absPos := 0;
                  
end;

                  
Inc(dx);
                
end;

                if (
dx >= P.Count) and (k -1 <= len) then
                  result 
:= j;
              
end else
                exit;

              
i := j +1;
            
end;

          
end;
        
end;
      
end;
    
end;


end. 





ثم المثال

PHP كود :
//Example....
procedure TForm1.Button1Click(Sender: TObject);
var
   
p : TPattern;
   
i : integer;
begin
   
(*
       
Edit1 contains the source string
       Edit2 contains the pattern string
   
*)
   
StrToPattern(Edit2.Text, p);      // '????****?*??2233445????*??****?'
   
i := PosPattern(Edit1.Text, p);   // 
'khgs;oigjsoijvm;sn22334455inhjrvljqijrm'
   
if i > 0 then
     Label1
.Caption := 'Found at ' +IntToStr(i)
   else
     
Label1.Caption := 'Not found';

end; 
الرد


الردود في هذا الموضوع
ماهو الخطاء في دالة تبحث بإستعمال ال WildCards - بواسطة mohfa - 05-02-2009, 02:09 PM

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم