Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d305f51

Browse files
committed
New asynchat.py from Sam Rushing: this foregoes using the regex module
to find the prefix of strings, thus removing a warning, and simply uses straightforward string slicing.
1 parent 9368a12 commit d305f51

1 file changed

Lines changed: 12 additions & 36 deletions

File tree

Lib/asynchat.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- Mode: Python; tab-width: 4 -*-
2-
# Id: asynchat.py,v 2.25 1999/11/18 11:01:08 rushing Exp
2+
# Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp
33
# Author: Sam Rushing <[email protected]>
44

55
# ======================================================================
@@ -279,40 +279,16 @@ def pop (self):
279279
# f_p_a_e ("qwertydkjf", "\r\n") => 0
280280

281281
# this could maybe be made faster with a computed regex?
282-
283-
##def find_prefix_at_end (haystack, needle):
284-
## nl = len(needle)
285-
## result = 0
286-
## for i in range (1,nl):
287-
## if haystack[-(nl-i):] == needle[:(nl-i)]:
288-
## result = nl-i
289-
## break
290-
## return result
291-
292-
# yes, this is about twice as fast, but still seems
293-
# to be negligible CPU. The previous version could do about 290
294-
# searches/sec. the new one about 555/sec.
295-
296-
import regex
297-
298-
prefix_cache = {}
299-
300-
def prefix_regex (needle):
301-
if prefix_cache.has_key (needle):
302-
return prefix_cache[needle]
303-
else:
304-
reg = needle[-1]
305-
for i in range(1,len(needle)):
306-
reg = '%c\(%s\)?' % (needle[-(i+1)], reg)
307-
reg = regex.compile (reg+'$')
308-
prefix_cache[needle] = reg, len(needle)
309-
return reg, len(needle)
282+
# [answer: no; circa Python-2.0, Jan 2001]
283+
# python: 18307/s
284+
# re: 12820/s
285+
# regex: 14035/s
310286

311287
def find_prefix_at_end (haystack, needle):
312-
reg, length = prefix_regex (needle)
313-
lh = len(haystack)
314-
result = reg.search (haystack, max(0,lh-length))
315-
if result >= 0:
316-
return (lh - result)
317-
else:
318-
return 0
288+
nl = len(needle)
289+
result = 0
290+
for i in range (1,nl):
291+
if haystack[-(nl-i):] == needle[:(nl-i)]:
292+
result = nl-i
293+
break
294+
return result

0 commit comments

Comments
 (0)