Skip to content

Commit 9ab518b

Browse files
committed
fix nativeToUnixPath
1 parent c769875 commit 9ab518b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/std/private/globs.nim

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ iterator walkDirRecFilter*(dir: string, follow: proc(entry: PathEntry): bool = n
4545

4646
proc nativeToUnixPath*(path: string): string =
4747
# pending https://github.com/nim-lang/Nim/pull/13265
48-
doAssert not path.isAbsolute # not implemented here; absolute files need more care for the drive
48+
result = path
49+
when defined(windows):
50+
if path.len >= 2 and path[0] in {'a'..'z', 'A'..'Z'} and path[1] == ':':
51+
result[0] = '/'
52+
result[1] = path[0]
53+
if path.len > 2 and path[2] != '\\':
54+
doAssert false, "paths like `C:foo` are currently unsupported, path: " & path
4955
when DirSep == '\\':
50-
result = replace(path, '\\', '/')
51-
else: result = path
56+
result = replace(result, '\\', '/')
5257

5358
when isMainModule:
5459
import sugar

tests/stdlib/tglobs.nim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import std/private/globs
2+
3+
template main =
4+
when defined(windows):
5+
doAssert nativeToUnixPath("C:") == "/C"
6+
doAssert nativeToUnixPath(r"D:\") == "/D/"
7+
doAssert nativeToUnixPath(r"E:\a") == "/E/a"
8+
doAssert nativeToUnixPath(r"E:\a1\") == "/E/a1/"
9+
doAssert nativeToUnixPath(r"E:\a1\bc") == "/E/a1/bc"
10+
doAssert nativeToUnixPath(r"\a1\bc") == "/a1/bc"
11+
doAssert nativeToUnixPath(r"a1\bc") == "a1/bc"
12+
doAssert nativeToUnixPath("a1") == "a1"
13+
doAssert nativeToUnixPath("") == ""
14+
doAssert nativeToUnixPath(".") == "."
15+
doAssert nativeToUnixPath("..") == ".."
16+
doAssert nativeToUnixPath(r"..\") == "../"
17+
doAssert nativeToUnixPath(r"..\..\.\") == "../.././"
18+
19+
static: main()
20+
main()

0 commit comments

Comments
 (0)