diff options
author | Miran <narimiran@disroot.org> | 2019-05-21 15:57:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-21 15:57:20 +0200 |
commit | 658651651124fb47f7ce721882528e7e4eed2452 (patch) | |
tree | f69f58f293522e494cc875efe3ac62c277a98e91 | |
parent | 5e552ad3a5bbece5f9b09816e4669e87d613cde1 (diff) | |
download | Nim-658651651124fb47f7ce721882528e7e4eed2452.tar.gz |
fix #8395, fix #8734: normalize os.tailDir and os.parentDir (#11288)
-rw-r--r-- | lib/pure/os.nim | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim index d549585a5..8a185e81c 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -327,12 +327,16 @@ proc parentDir*(path: string): string {. when defined(posix): assert parentDir("/usr/local/bin") == "/usr/local" assert parentDir("foo/bar/") == "foo" + assert parentDir("foo/bar//") == "foo" + assert parentDir("//foo//bar//") == "//foo" assert parentDir("./foo") == "." assert parentDir("/foo") == "" - let sepPos = parentDirPos(path) + result = normalizePathEnd(path) + var sepPos = parentDirPos(result) if sepPos >= 0: - result = substr(path, 0, sepPos-1) + while sepPos >= 0 and result[sepPos] in {DirSep, AltSep}: dec sepPos + result = substr(result, 0, sepPos) else: result = "" @@ -347,14 +351,18 @@ proc tailDir*(path: string): string {. runnableExamples: assert tailDir("/bin") == "bin" assert tailDir("bin") == "" + assert tailDir("bin/") == "" assert tailDir("/usr/local/bin") == "usr/local/bin" + assert tailDir("//usr//local//bin//") == "usr//local//bin//" + assert tailDir("./usr/local/bin") == "usr/local/bin" assert tailDir("usr/local/bin") == "local/bin" - var q = 1 - if len(path) >= 1 and path[len(path)-1] in {DirSep, AltSep}: q = 2 - for i in 0..len(path)-q: + var i = 0 + while i < len(path): if path[i] in {DirSep, AltSep}: - return substr(path, i+1) + while i < len(path) and path[i] in {DirSep, AltSep}: inc i + return substr(path, i) + inc i result = "" proc isRootDir*(path: string): bool {. |