fixed bug in flm.py
"find longest match" (flm.py) was parsing the input string as a regex pattern. now the string is properly escaped. i might gonne add the option to search for regex later
This commit is contained in:
@@ -2,7 +2,11 @@
|
|||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
ignore_case=True
|
||||||
|
|
||||||
pattern=str(sys.argv[1])
|
pattern=str(sys.argv[1])
|
||||||
|
if ignore_case:
|
||||||
|
pattern=pattern.lower()
|
||||||
filename=str(sys.argv[2])
|
filename=str(sys.argv[2])
|
||||||
shortpattern=""
|
shortpattern=""
|
||||||
print("Pattern is '%s'" % pattern)
|
print("Pattern is '%s'" % pattern)
|
||||||
@@ -13,7 +17,8 @@ for char in pattern:
|
|||||||
newchar={}
|
newchar={}
|
||||||
newchar['char']=char
|
newchar['char']=char
|
||||||
newchar['count']=pattern.count(char)
|
newchar['count']=pattern.count(char)
|
||||||
newchar['idx']=[m.start() for m in re.finditer(char,pattern)]
|
newchar['idx']=[m.start() for m in re.finditer(re.escape(char),pattern)]
|
||||||
|
print(char)
|
||||||
#print("Char '%s' occurs %d times in pattern %s" % (c,newchar['count'],newchar['idx']))
|
#print("Char '%s' occurs %d times in pattern %s" % (c,newchar['count'],newchar['idx']))
|
||||||
chars[char]=newchar
|
chars[char]=newchar
|
||||||
shortpattern=shortpattern + char
|
shortpattern=shortpattern + char
|
||||||
@@ -25,9 +30,12 @@ except:
|
|||||||
|
|
||||||
print(shortpattern)
|
print(shortpattern)
|
||||||
longest_match_yet=0
|
longest_match_yet=0
|
||||||
|
def get_char():
|
||||||
|
return f.read(1).lower() if ignore_case else f.read(1)
|
||||||
|
|
||||||
while longest_match_yet<len(pattern):
|
while longest_match_yet<len(pattern):
|
||||||
read_a_char=f.read(1)
|
# read_a_char=f.read(1)
|
||||||
|
read_a_char=get_char()
|
||||||
if read_a_char in shortpattern and read_a_char in chars:
|
if read_a_char in shortpattern and read_a_char in chars:
|
||||||
#candidate
|
#candidate
|
||||||
for index in chars[read_a_char]['idx']:
|
for index in chars[read_a_char]['idx']:
|
||||||
@@ -41,7 +49,8 @@ while longest_match_yet<len(pattern):
|
|||||||
# print("trying to find rest of pattern '%s'" % sub_pattern)
|
# print("trying to find rest of pattern '%s'" % sub_pattern)
|
||||||
x=1
|
x=1
|
||||||
for char_to_compare in sub_pattern:
|
for char_to_compare in sub_pattern:
|
||||||
next_char=f.read(1)
|
# next_char=f.read(1)
|
||||||
|
next_char=get_char()
|
||||||
if not read_a_char:
|
if not read_a_char:
|
||||||
print("No more Chars to consume in File")
|
print("No more Chars to consume in File")
|
||||||
break
|
break
|
||||||
|
|||||||
Reference in New Issue
Block a user