diff Lib/macpath.py @ 35308:d533f08a9dfa legacy-trunk

patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
author Georg Brandl <georg@python.org>
date Wed, 03 Aug 2005 07:30:12 +0000
parents 505c9063850e
children e7ed0e6aed2f
line wrap: on
line diff
--- a/Lib/macpath.py	Wed Aug 03 07:17:33 2005 +0000
+++ b/Lib/macpath.py	Wed Aug 03 07:30:12 2005 +0000
@@ -175,14 +175,14 @@
 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]
+
 
 def expandvars(path):
     """Dummy to retain interface-compatibility with other operating systems."""