Commit 30f051ef by willmcgugan

Optimized normpath -- that that it was exactly snow before!

parent 61fe3cfe
...@@ -40,25 +40,27 @@ def normpath(path): ...@@ -40,25 +40,27 @@ def normpath(path):
if path in ('', '/'): if path in ('', '/'):
return path return path
path = path.replace('\\', '/')
# An early out if there is no need to normalize this path # An early out if there is no need to normalize this path
if not _requires_normalization(path): if not _requires_normalization(path):
return path.rstrip('/') return path.rstrip('/')
components = [] components = []
append = components.append append = components.append
for comp in [c for c in path.replace('\\','/').split("/") if c not in ('', '.')]: special = ('', '.', '..').__contains__
if comp == "..":
try: try:
for component in path.split('/'):
if special(component):
if component == '..':
components.pop() components.pop()
else:
append(component)
except IndexError: except IndexError:
raise ValueError("too many backrefs in path '%s'" % path) raise ValueError("too many backrefs in path '%s'" % path)
else: if path[0] == '/':
append(comp) return '/%s' % '/'.join(components)
if path[0] in '\\/': return '/'.join(components)
if not components:
append("")
components.insert(0, "")
return "/".join(components)
def iteratepath(path, numsplits=None): def iteratepath(path, numsplits=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment