diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2007-03-07 11:04:33 +0000 |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2007-03-07 11:04:33 +0000 |
commit | 05c075d629fc81035b438e3b146872dc7799f260 (patch) | |
tree | 4240955ee3ea5894d0c068c9669244a7200e768c /Lib/genericpath.py | |
parent | f08c073ded5a7cf5786f9a561e3d9529def81b6e (diff) | |
download | cpython-git-05c075d629fc81035b438e3b146872dc7799f260.tar.gz |
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Diffstat (limited to 'Lib/genericpath.py')
-rw-r--r-- | Lib/genericpath.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/genericpath.py b/Lib/genericpath.py index 1574cef0c8..6d11ec03b0 100644 --- a/Lib/genericpath.py +++ b/Lib/genericpath.py @@ -75,3 +75,32 @@ def commonprefix(m): if s1[i] != s2[i]: return s1[:i] return s1[:n] + +# Split a path in root and extension. +# The extension is everything starting at the last dot in the last +# pathname component; the root is everything before that. +# It is always true that root + ext == p. + +# Generic implementation of splitext, to be parametrized with +# the separators +def _splitext(p, sep, altsep, extsep): + """Split the extension from a pathname. + + Extension is everything from the last dot to the end, ignoring + leading dots. Returns "(root, ext)"; ext may be empty.""" + + sepIndex = p.rfind(sep) + if altsep: + altsepIndex = p.rfind(altsep) + sepIndex = max(sepIndex, altsepIndex) + + dotIndex = p.rfind(extsep) + if dotIndex > sepIndex: + # skip all leading dots + filenameIndex = sepIndex + 1 + while filenameIndex < dotIndex: + if p[filenameIndex] != extsep: + return p[:dotIndex], p[dotIndex:] + filenameIndex += 1 + + return p, '' |