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('/'):
components.pop() if special(component):
except IndexError: if component == '..':
raise ValueError("too many backrefs in path '%s'" % path) components.pop()
else: else:
append(comp) append(component)
if path[0] in '\\/': except IndexError:
if not components: raise ValueError("too many backrefs in path '%s'" % path)
append("") if path[0] == '/':
components.insert(0, "") return '/%s' % '/'.join(components)
return "/".join(components) 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