Skip to content

Commit

Permalink
fix #8395, fix #8734: normalize os.tailDir and os.parentDir (#11288)
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran authored May 21, 2019
1 parent 5e552ad commit 6586516
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand All @@ -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 {.
Expand Down

0 comments on commit 6586516

Please sign in to comment.