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

Skip to content

Commit 649f8e7

Browse files
committed
patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
1 parent b370059 commit 649f8e7

5 files changed

Lines changed: 35 additions & 41 deletions

File tree

Lib/macpath.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,14 @@ def lexists(path):
175175
def commonprefix(m):
176176
"Given a list of pathnames, returns the longest common leading component"
177177
if not m: return ''
178-
prefix = m[0]
179-
for item in m:
180-
for i in range(len(prefix)):
181-
if prefix[:i+1] != item[:i+1]:
182-
prefix = prefix[:i]
183-
if i == 0: return ''
184-
break
185-
return prefix
178+
s1 = min(m)
179+
s2 = max(m)
180+
n = min(len(s1), len(s2))
181+
for i in xrange(n):
182+
if s1[i] != s2[i]:
183+
return s1[:i]
184+
return s1[:n]
185+
186186

187187
def expandvars(path):
188188
"""Dummy to retain interface-compatibility with other operating systems."""

Lib/ntpath.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,13 @@ def dirname(p):
212212
def commonprefix(m):
213213
"Given a list of pathnames, returns the longest common leading component"
214214
if not m: return ''
215-
prefix = m[0]
216-
for item in m:
217-
for i in range(len(prefix)):
218-
if prefix[:i+1] != item[:i+1]:
219-
prefix = prefix[:i]
220-
if i == 0: return ''
221-
break
222-
return prefix
215+
s1 = min(m)
216+
s2 = max(m)
217+
n = min(len(s1), len(s2))
218+
for i in xrange(n):
219+
if s1[i] != s2[i]:
220+
return s1[:i]
221+
return s1[:n]
223222

224223

225224
# Get size, mtime, atime of files.

Lib/os2emxpath.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,13 @@ def dirname(p):
173173
def commonprefix(m):
174174
"Given a list of pathnames, returns the longest common leading component"
175175
if not m: return ''
176-
prefix = m[0]
177-
for item in m:
178-
for i in range(len(prefix)):
179-
if prefix[:i+1] != item[:i+1]:
180-
prefix = prefix[:i]
181-
if i == 0: return ''
182-
break
183-
return prefix
176+
s1 = min(m)
177+
s2 = max(m)
178+
n = min(len(s1), len(s2))
179+
for i in xrange(n):
180+
if s1[i] != s2[i]:
181+
return s1[:i]
182+
return s1[:n]
184183

185184

186185
# Get size, mtime, atime of files.

Lib/plat-riscos/riscospath.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,16 @@ def dirname(p):
168168
return split(p)[0]
169169

170170

171-
def commonprefix(ps):
172-
"""
173-
Return the longest prefix of all list elements. Purely string-based; does not
174-
separate any path parts. Why am I in os.path?
175-
"""
176-
if len(ps)==0:
177-
return ''
178-
prefix= ps[0]
179-
for p in ps[1:]:
180-
prefix= prefix[:len(p)]
181-
for i in range(len(prefix)):
182-
if prefix[i] <> p[i]:
183-
prefix= prefix[:i]
184-
if i==0:
185-
return ''
186-
break
187-
return prefix
171+
def commonprefix(m):
172+
"Given a list of pathnames, returns the longest common leading component"
173+
if not m: return ''
174+
s1 = min(m)
175+
s2 = max(m)
176+
n = min(len(s1), len(s2))
177+
for i in xrange(n):
178+
if s1[i] != s2[i]:
179+
return s1[:i]
180+
return s1[:n]
188181

189182

190183
## File access functions. Why are we in os.path?

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ Extension Modules
178178
Library
179179
-------
180180

181+
- Patch #1105730: Apply the new implementation of commonprefix in posixpath
182+
to ntpath, macpath, os2emxpath and riscospath.
183+
181184
- Fix a problem in Tkinter introduced by SF patch #869468: delete bogus
182185
__hasattr__ and __delattr__ methods on class Tk that were breaking
183186
Tkdnd.

0 commit comments

Comments
 (0)