mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
SF Patch 681780: Faster commonprefix (OS independent)
Improved based on discussions at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252177 http://groups.google.com/groups?th=fc7b54f11af6b24e&seekm=bss2so$om$00$1@news.t-online.com
This commit is contained in:
parent
9b4dab4da1
commit
74bb7f03b1
1 changed files with 7 additions and 10 deletions
|
@ -123,16 +123,13 @@ def dirname(p):
|
|||
def commonprefix(m):
|
||||
"Given a list of pathnames, returns the longest common leading component"
|
||||
if not m: return ''
|
||||
prefix = m[0]
|
||||
for item in m:
|
||||
for i in range(len(prefix)):
|
||||
if prefix[:i+1] != item[:i+1]:
|
||||
prefix = prefix[:i]
|
||||
if i == 0:
|
||||
return ''
|
||||
break
|
||||
return prefix
|
||||
|
||||
s1 = min(m)
|
||||
s2 = max(m)
|
||||
n = min(len(s1), len(s2))
|
||||
for i in xrange(n):
|
||||
if s1[i] != s2[i]:
|
||||
return s1[:i]
|
||||
return s1[:n]
|
||||
|
||||
# Get size, mtime, atime of files.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue