Skip to content

Instantly share code, notes, and snippets.

@Varriount
Last active August 29, 2015 14:00
Show Gist options
  • Save Varriount/11268428 to your computer and use it in GitHub Desktop.
Save Varriount/11268428 to your computer and use it in GitHub Desktop.
proc simplifyPath(path: string): string =
## Simplify a path, removing '..' and '.', double
result = path
var
bounds = newSeq[int]()
writePos, readPos = 0
while readPos < path.len():
if path[readPos] == '/':
if path[readPos+1] == '/': # Handle redundant path separator tokens.
while path[readPos+1] == '/':
readPos.inc()
readPos.dec()
elif path[readPos+1 .. readPos+3] == "../": # Handle parent dir tokens
writePos = bounds.pop()
readPos.inc(3)
elif path[readPos+1 .. readPos+2] == "./": # Handle current dir tokens
readPos.inc(2)
else:
bounds.add(readPos)
echo(repr(bounds))
result[writePos] = path[readPos]
writePos.inc
readPos.inc
result.setLen(writePos)
echo simplifyPath("/User/.//Clay/../Ben/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment