Last active
August 29, 2015 14:00
-
-
Save Varriount/11268428 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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