mirror of
https://github.com/python/cpython.git
synced 2025-09-06 00:41:39 +00:00
A faster version of the find_prefix_at_end() function (that I found in the
last Medusa release) Should be safe as a bugfix candidate, though it's not fixing a bug.
This commit is contained in:
parent
913b9078cf
commit
c63a396c5f
1 changed files with 7 additions and 9 deletions
|
@ -280,20 +280,18 @@ class fifo:
|
||||||
# characters matched.
|
# characters matched.
|
||||||
# for example:
|
# for example:
|
||||||
# f_p_a_e ("qwerty\r", "\r\n") => 1
|
# f_p_a_e ("qwerty\r", "\r\n") => 1
|
||||||
# f_p_a_e ("qwerty\r\n", "\r\n") => 2
|
|
||||||
# f_p_a_e ("qwertydkjf", "\r\n") => 0
|
# f_p_a_e ("qwertydkjf", "\r\n") => 0
|
||||||
|
# f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
|
||||||
|
|
||||||
# this could maybe be made faster with a computed regex?
|
# this could maybe be made faster with a computed regex?
|
||||||
# [answer: no; circa Python-2.0, Jan 2001]
|
# [answer: no; circa Python-2.0, Jan 2001]
|
||||||
# python: 18307/s
|
# new python: 28961/s
|
||||||
|
# old python: 18307/s
|
||||||
# re: 12820/s
|
# re: 12820/s
|
||||||
# regex: 14035/s
|
# regex: 14035/s
|
||||||
|
|
||||||
def find_prefix_at_end (haystack, needle):
|
def find_prefix_at_end (haystack, needle):
|
||||||
nl = len(needle)
|
l = len(needle) - 1
|
||||||
result = 0
|
while l and not haystack.endswith(needle[:l]):
|
||||||
for i in range (1,nl):
|
l -= 1
|
||||||
if haystack[-(nl-i):] == needle[:(nl-i)]:
|
return l
|
||||||
result = nl-i
|
|
||||||
break
|
|
||||||
return result
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue