summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-03-19 21:05:12 (GMT)
committerGuido van Rossum <guido@python.org>1999-03-19 21:05:12 (GMT)
commit8f0fa9e47f6bdad5bdc6155a0c83976d1f891fd7 (patch)
treedb61e612289401e574a6141e4a66d3b60db1972b /Lib
parentd7b5fb858cce7ae7a1edd379a6ddf5d3ed3a8baf (diff)
downloadcpython-8f0fa9e47f6bdad5bdc6155a0c83976d1f891fd7.zip
cpython-8f0fa9e47f6bdad5bdc6155a0c83976d1f891fd7.tar.gz
cpython-8f0fa9e47f6bdad5bdc6155a0c83976d1f891fd7.tar.bz2
New code for split() by Tim Peters, behaves more like posixpath.split().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/dospath.py34
-rw-r--r--Lib/ntpath.py31
2 files changed, 25 insertions, 40 deletions
diff --git a/Lib/dospath.py b/Lib/dospath.py
index d7aa752..bd5e560 100644
--- a/Lib/dospath.py
+++ b/Lib/dospath.py
@@ -54,31 +54,23 @@ def splitdrive(p):
# Split a path in head (everything up to the last '/') and tail (the
-# rest). If the original path ends in '/' but is not the root, this
-# '/' is stripped. After the trailing '/' is stripped, the invariant
+# rest). After the trailing '/' is stripped, the invariant
# join(head, tail) == p holds.
# The resulting head won't end in '/' unless it is the root.
def split(p):
- d, p = splitdrive(p)
- slashes = ''
- while p and p[-1:] in '/\\':
- slashes = slashes + p[-1]
- p = p[:-1]
- if p == '':
- p = p + slashes
- head, tail = '', ''
- for c in p:
- tail = tail + c
- if c in '/\\':
- head, tail = head + tail, ''
- slashes = ''
- while head and head[-1:] in '/\\':
- slashes = slashes + head[-1]
- head = head[:-1]
- if head == '':
- head = head + slashes
- return d + head, tail
+ d, p = splitdrive(p)
+ # set i to index beyond p's last slash
+ i = len(p)
+ while i and p[i-1] not in '/\\':
+ i = i - 1
+ head, tail = p[:i], p[i:] # now tail has no slashes
+ # remove trailing slashes from head, unless it's all slashes
+ head2 = head
+ while head2 and head2[-1] in '/\\':
+ head2 = head2[:-1]
+ head = head2 or head
+ return d + head, tail
# Split a path in root and extension.
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 9597c63..a74cce3 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -77,8 +77,7 @@ def splitdrive(p):
# Split a path in head (everything up to the last '/') and tail (the
-# rest). If the original path ends in '/' but is not the root, this
-# '/' is stripped. After the trailing '/' is stripped, the invariant
+# rest). After the trailing '/' is stripped, the invariant
# join(head, tail) == p holds.
# The resulting head won't end in '/' unless it is the root.
@@ -87,24 +86,18 @@ def split(p):
Return tuple (head, tail) where tail is everything after the final slash.
Either part may be empty."""
+
d, p = splitdrive(p)
- slashes = ''
- while p and p[-1:] in '/\\':
- slashes = slashes + p[-1]
- p = p[:-1]
- if p == '':
- p = p + slashes
- head, tail = '', ''
- for c in p:
- tail = tail + c
- if c in '/\\':
- head, tail = head + tail, ''
- slashes = ''
- while head and head[-1:] in '/\\':
- slashes = slashes + head[-1]
- head = head[:-1]
- if head == '':
- head = head + slashes
+ # set i to index beyond p's last slash
+ i = len(p)
+ while i and p[i-1] not in '/\\':
+ i = i - 1
+ head, tail = p[:i], p[i:] # now tail has no slashes
+ # remove trailing slashes from head, unless it's all slashes
+ head2 = head
+ while head2 and head2[-1] in '/\\':
+ head2 = head2[:-1]
+ head = head2 or head
return d + head, tail