diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2018-12-19 04:55:51 -0800 |
---|---|---|
committer | Timothee Cour <timothee.cour2@gmail.com> | 2018-12-19 16:11:23 -0800 |
commit | 656770402c85227c0597f260f593834b1bd2d3f5 (patch) | |
tree | bb9dd7119e8c32fbd1c79f23686745e3df7d6c3b | |
parent | c129107b32528a39d82a0459f8c22ab68fc70a43 (diff) | |
download | Nim-656770402c85227c0597f260f593834b1bd2d3f5.tar.gz |
fix #8255 numerous issues with splitFile
-rw-r--r-- | lib/pure/os.nim | 17 | ||||
-rw-r--r-- | tests/stdlib/tos.nim | 15 |
2 files changed, 28 insertions, 4 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 68c1e28a2..46e4e7b69 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -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) diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index ed3737844..e49ab2506 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -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", "..", "") |