@@ -84,34 +84,26 @@ def find(text, query, algorithm, **kwargs):
84
84
% (algorithm ))
85
85
return getattr (algorithms , func )(text , query )
86
86
87
- def bitap_search (text : str , pattern : str ) -> int :
87
+ def bitap_search (text , pattern ) :
88
88
"""
89
89
Bitap Algorithm (Shift-Or Algorithm) for exact string matching.
90
90
Returns the starting index of the pattern in the text, or -1 if not found.
91
91
"""
92
92
m = len (pattern )
93
- if m == 0 :
94
- return 0
95
- if m > 64 :
96
- raise ValueError ("Bitap algorithm supports patterns up to 64 characters." )
97
-
93
+ R = ~ 1 # Bit array for tracking matches
98
94
pattern_mask = {}
99
- for i , char in enumerate (pattern ):
100
- pattern_mask [char ] = pattern_mask .get (char , ~ 0 ) & ~ (1 << i )
101
-
102
- R = ~ 1
103
95
104
- for i , char in enumerate (text ):
105
- R = (R << 1 ) | 1
106
- if char in pattern_mask :
107
- R &= pattern_mask [char ]
108
- else :
109
- R = ~ 1
96
+ # Preprocess the pattern into a bitmask
97
+ for i in range (m ):
98
+ pattern_mask [pattern [i ]] = pattern_mask .get (pattern [i ], ~ 0 ) & ~ (1 << i )
110
99
111
- if (R & (1 << (m - 1 ))) == 0 :
112
- return i - m + 1
100
+ for i in range (len (text )):
101
+ R |= pattern_mask .get (text [i ], ~ 0 )
102
+ R <<= 1
103
+ if (R & (1 << m )) == 0 :
104
+ return i - m + 1 # Match found
113
105
114
- return - 1
106
+ return - 1 # No match found
115
107
116
108
def _knuth_morris_pratt (text , query ):
117
109
if len (text ) == 0 or len (query ) == 0 :
0 commit comments