summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-12-31 22:44:29 +0000
committerRaymond Hettinger <python@rcn.com>2003-12-31 22:44:29 +0000
commit81ddabfa5bb2096c7698e0ef61a9ad92a86e4366 (patch)
tree099bdfc2317b881ef476422ef267f0ec42e7969e /Lib
parent83b3b907984af2c9d110ff7b8f4e4a6fc0927eee (diff)
downloadcpython-81ddabfa5bb2096c7698e0ef61a9ad92a86e4366.tar.gz
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
Diffstat (limited to 'Lib')
-rw-r--r--Lib/posixpath.py17
1 files changed, 7 insertions, 10 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 7f907ef519..7ee4911d31 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -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.