From 649f8e7de2ad8fc42748b56e8e64574478d2d7fe Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 3 Aug 2005 07:30:12 +0000 Subject: patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc. --- Lib/ntpath.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'Lib/ntpath.py') diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 649e424f264..35f266c69be 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -212,14 +212,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. -- cgit v1.2.3