diff options
author | Artem V L <luardev@gmail.com> | 2019-10-28 12:58:02 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-10-28 12:58:02 +0100 |
commit | 580462c886209a0a5b1c1059fcc5b905b6bf1814 (patch) | |
tree | 2009b1b942a93eeacce4e6cc07f71de2747327c9 /lib | |
parent | a9d7796e1c5719132461b1e2918ada085ed4242f (diff) | |
download | Nim-580462c886209a0a5b1c1059fcc5b905b6bf1814.tar.gz |
splitPath() behavior synchronized with splitFile() (#12481)
* splitPath() behavior synchronized with splitFile() having the expected behavior in all languages splitPath() docstrings update, tests added for both splitPath() and splitFile() * Path splitting refined and described
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/os.nim | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 9894831a6..b27eee782 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -216,8 +216,12 @@ proc splitPath*(path: string): tuple[head, tail: string] {. runnableExamples: assert splitPath("usr/local/bin") == ("usr/local", "bin") assert splitPath("usr/local/bin/") == ("usr/local/bin", "") + assert splitPath("/bin/") == ("/bin", "") + when (NimMajor, NimMinor) <= (1, 0): + assert splitPath("/bin") == ("", "bin") + else: + assert splitPath("/bin") == ("/", "bin") assert splitPath("bin") == ("", "bin") - assert splitPath("/bin") == ("", "bin") assert splitPath("") == ("", "") var sepPos = -1 @@ -226,7 +230,12 @@ proc splitPath*(path: string): tuple[head, tail: string] {. sepPos = i break if sepPos >= 0: - result.head = substr(path, 0, sepPos-1) + result.head = substr(path, 0, + when (NimMajor, NimMinor) <= (1, 0): + sepPos-1 + else: + if likely(sepPos >= 1): sepPos-1 else: 0 + ) result.tail = substr(path, sepPos+1) else: result.head = "" @@ -597,13 +606,17 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {. assert dir == "/usr/local" assert name == "" assert ext == "" + (dir, name, ext) = splitFile("/tmp.txt") + assert dir == "/" + assert name == "tmp" + assert ext == ".txt" var namePos = 0 var dotPos = 0 for i in countdown(len(path) - 1, 0): if path[i] in {DirSep, AltSep} or i == 0: if path[i] in {DirSep, AltSep}: - result.dir = substr(path, 0, max(0, i - 1)) + result.dir = substr(path, 0, if likely(i >= 1): i - 1 else: 0) namePos = i + 1 if dotPos > i: result.name = substr(path, namePos, dotPos - 1) |