From de3337913f08266b16842f21a67427d10725ed8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Thu, 12 Dec 2002 20:30:20 +0000 Subject: [PATCH] Patch #536661: Improve performance of splitext. Add test_macpath. --- Lib/macpath.py | 19 +++-------- Lib/ntpath.py | 20 ++++-------- Lib/posixpath.py | 19 +++-------- Lib/test/test_macpath.py | 64 ++++++++++++++++++++++++++++++++++++++ Lib/test/test_ntpath.py | 10 ++++++ Lib/test/test_posixpath.py | 5 +++ 6 files changed, 95 insertions(+), 42 deletions(-) create mode 100644 Lib/test/test_macpath.py diff --git a/Lib/macpath.py b/Lib/macpath.py index f19b4f7652f..734bae292ed 100644 --- a/Lib/macpath.py +++ b/Lib/macpath.py @@ -62,20 +62,11 @@ def splitext(p): pathname component; the root is everything before that. It is always true that root + ext == p.""" - root, ext = '', '' - for c in p: - if c == ':': - root, ext = root + ext + c, '' - elif c == '.': - if ext: - root, ext = root + ext, c - else: - ext = c - elif ext: - ext = ext + c - else: - root = root + c - return root, ext + i = p.rfind('.') + if i<=p.rfind(':'): + return p, '' + else: + return p[:i], p[i:] def splitdrive(p): diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 0f1dd4dc608..4e7bb885a9f 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -169,20 +169,12 @@ def splitext(p): Extension is everything from the last dot to the end. Return (root, ext), either part may be empty.""" - root, ext = '', '' - for c in p: - if c in ['/','\\']: - root, ext = root + ext + c, '' - elif c == '.': - if ext: - root, ext = root + ext, c - else: - ext = c - elif ext: - ext = ext + c - else: - root = root + c - return root, ext + + i = p.rfind('.') + if i<=max(p.rfind('/'), p.rfind('\\')): + return p, '' + else: + return p[:i], p[i:] # Return the tail (basename) part of a path. diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 6a0640d2fc5..4a92f18cb91 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -78,20 +78,11 @@ def split(p): def splitext(p): """Split the extension from a pathname. Extension is everything from the last dot to the end. Returns "(root, ext)", either part may be empty.""" - root, ext = '', '' - for c in p: - if c == '/': - root, ext = root + ext + c, '' - elif c == '.': - if ext: - root, ext = root + ext, c - else: - ext = c - elif ext: - ext = ext + c - else: - root = root + c - return root, ext + i = p.rfind('.') + if i<=p.rfind('/'): + return p, '' + else: + return p[:i], p[i:] # Split a pathname into a drive specification and the rest of the diff --git a/Lib/test/test_macpath.py b/Lib/test/test_macpath.py new file mode 100644 index 00000000000..67b1bedbbc1 --- /dev/null +++ b/Lib/test/test_macpath.py @@ -0,0 +1,64 @@ +import macpath +from test import test_support +import unittest + + +class MacPathTestCase(unittest.TestCase): + + def test_abspath(self): + self.assert_(macpath.abspath("xx:yy") == "xx:yy") + + def test_isabs(self): + isabs = macpath.isabs + self.assert_(isabs("xx:yy")) + self.assert_(isabs("xx:yy:")) + self.assert_(isabs("xx:")) + self.failIf(isabs("foo")) + self.failIf(isabs(":foo")) + self.failIf(isabs(":foo:bar")) + self.failIf(isabs(":foo:bar:")) + + + def test_commonprefix(self): + commonprefix = macpath.commonprefix + self.assert_(commonprefix(["home:swenson:spam", "home:swen:spam"]) + == "home:swen") + self.assert_(commonprefix([":home:swen:spam", ":home:swen:eggs"]) + == ":home:swen:") + self.assert_(commonprefix([":home:swen:spam", ":home:swen:spam"]) + == ":home:swen:spam") + + def test_split(self): + split = macpath.split + self.assertEquals(split("foo:bar"), + ('foo:', 'bar')) + self.assertEquals(split("conky:mountpoint:foo:bar"), + ('conky:mountpoint:foo', 'bar')) + + self.assertEquals(split(":"), ('', '')) + self.assertEquals(split(":conky:mountpoint:"), + (':conky:mountpoint', '')) + + def test_splitdrive(self): + splitdrive = macpath.splitdrive + self.assertEquals(splitdrive("foo:bar"), ('', 'foo:bar')) + self.assertEquals(splitdrive(":foo:bar"), ('', ':foo:bar')) + + def test_splitext(self): + splitext = macpath.splitext + self.assertEquals(splitext(":foo.ext"), (':foo', '.ext')) + self.assertEquals(splitext("foo:foo.ext"), ('foo:foo', '.ext')) + self.assertEquals(splitext(".ext"), ('', '.ext')) + self.assertEquals(splitext("foo.ext:foo"), ('foo.ext:foo', '')) + self.assertEquals(splitext(":foo.ext:"), (':foo.ext:', '')) + self.assertEquals(splitext(""), ('', '')) + self.assertEquals(splitext("foo.bar.ext"), ('foo.bar', '.ext')) + + +def test_main(): + test_support.run_unittest(MacPathTestCase) + + +if __name__ == "__main__": + test_main() + diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 40d785f04ec..2419844e418 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -15,6 +15,16 @@ def tester(fn, wantResult): print " returned: " + str(gotResult) print "" errors = errors + 1 + +tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) +tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) +tester('ntpath.splitext(".ext")', ('', '.ext')) +tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) +tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) +tester('ntpath.splitext("")', ('', '')) +tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) +tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) +tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) tester('ntpath.splitdrive("c:\\foo\\bar")', ('c:', '\\foo\\bar')) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 362483f6db5..0aaddaf0e66 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -21,6 +21,11 @@ tester('posixpath.split("foo")', ('', 'foo')) tester('posixpath.splitext("foo.ext")', ('foo', '.ext')) tester('posixpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) +tester('posixpath.splitext(".ext")', ('', '.ext')) +tester('posixpath.splitext("/foo.ext/foo")', ('/foo.ext/foo', '')) +tester('posixpath.splitext("foo.ext/")', ('foo.ext/', '')) +tester('posixpath.splitext("")', ('', '')) +tester('posixpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) tester('posixpath.isabs("/")', 1) tester('posixpath.isabs("/foo")', 1)