fix #8255 numerous issues with splitFile

This commit is contained in:
Timothee Cour
2018-12-19 04:55:51 -08:00
parent c129107b32
commit 656770402c
2 changed files with 28 additions and 4 deletions

View File

@@ -349,19 +349,28 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {.
## If `path` has no extension, `ext` is the empty string.
## If `path` has no directory component, `dir` is the empty string.
## If `path` has no filename component, `name` and `ext` are empty strings.
if path.len == 0 or path[path.len-1] in {DirSep, AltSep}:
result = (path, "", "")
if path.len == 0 or path[^1] in {DirSep, AltSep}:
if path.len == 1:
# issue #8255
result = (path[0 .. ^1], "", "")
else:
result = (path[0 ..< ^1], "", "")
else:
var sepPos = -1
var dotPos = path.len
for i in countdown(len(path)-1, 0):
if path[i] == ExtSep:
if dotPos == path.len and i > 0 and
path[i-1] notin {DirSep, AltSep}: dotPos = i
path[i-1] notin {DirSep, AltSep, ExtSep}: dotPos = i
elif path[i] in {DirSep, AltSep}:
sepPos = i
break
result.dir = substr(path, 0, sepPos-1)
if sepPos-1 >= 0:
result.dir = substr(path, 0, sepPos-1)
elif path[0] in {DirSep, AltSep}:
# issue #8255
result.dir = $path[0]
result.name = substr(path, sepPos+1, dotPos-1)
result.ext = substr(path, dotPos)

View File

@@ -239,3 +239,18 @@ block absolutePath:
doAssert absolutePath("a", "/b/c") == "/b/c" / "a"
doAssert absolutePath("/a", "b/") == "/a"
block splitFile:
doAssert splitFile("abc/") == ("abc", "", "")
doAssert splitFile("/") == ("/", "", "")
doAssert splitFile("./abc") == (".", "abc", "")
doAssert splitFile(".txt") == ("", ".txt", "")
doAssert splitFile("abc/.txt") == ("abc", ".txt", "")
doAssert splitFile("abc") == ("", "abc", "")
doAssert splitFile("abc.txt") == ("", "abc", ".txt")
doAssert splitFile("/abc.txt") == ("/", "abc", ".txt")
doAssert splitFile("/foo/abc.txt") == ("/foo", "abc", ".txt")
doAssert splitFile("/foo/abc.txt.gz") == ("/foo", "abc.txt", ".gz")
doAssert splitFile(".") == ("", ".", "")
doAssert splitFile("abc/.") == ("abc", ".", "")
doAssert splitFile("..") == ("", "..", "")
doAssert splitFile("a/..") == ("a", "..", "")