summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/pure/os.nim5
-rw-r--r--lib/pure/pathnorm.nim7
-rw-r--r--tests/stdlib/tospaths.nim5
3 files changed, 15 insertions, 2 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 1646d7c7a..6521d827c 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -105,7 +105,10 @@ proc joinPath*(head, tail: string): string {.
   result = newStringOfCap(head.len + tail.len)
   var state = 0
   addNormalizePath(head, result, state, DirSep)
-  addNormalizePath(tail, result, state, DirSep)
+  if tail.len == 0:
+    result.add DirSep
+  else:
+    addNormalizePath(tail, result, state, DirSep)
   when false:
     if len(head) == 0:
       result = tail
diff --git a/lib/pure/pathnorm.nim b/lib/pure/pathnorm.nim
index 696f6b2ef..a33afefbd 100644
--- a/lib/pure/pathnorm.nim
+++ b/lib/pure/pathnorm.nim
@@ -57,7 +57,12 @@ proc addNormalizePath*(x: string; result: var string; state: var int; dirSep = D
 
   # state: 0th bit set if isAbsolute path. Other bits count
   # the number of path components.
-  for b in dirs(x):
+  var it: PathIter
+  it.notFirst = (state shr 1) > 0
+  if it.notFirst:
+    while it.i < x.len and x[it.i] in {DirSep, AltSep}: inc it.i
+  while hasNext(it, x):
+    let b = next(it, x)
     if (state shr 1 == 0) and isSlash(x, b):
       result.add dirSep
       state = state or 1
diff --git a/tests/stdlib/tospaths.nim b/tests/stdlib/tospaths.nim
index 563584c4c..ce00b5a95 100644
--- a/tests/stdlib/tospaths.nim
+++ b/tests/stdlib/tospaths.nim
@@ -92,3 +92,8 @@ doAssert relativePath("/Users///me/bar//z.nim", "//Users/", '/') == "me/bar/z.ni
 doAssert relativePath("/Users/me/bar/z.nim", "/Users/me", '/') == "bar/z.nim"
 doAssert relativePath("", "/users/moo", '/') == ""
 doAssert relativePath("foo", "", '/') == "foo"
+
+doAssert joinPath("usr", "") == unixToNativePath"usr/"
+doAssert joinPath("", "lib") == "lib"
+doAssert joinPath("", "/lib") == unixToNativePath"/lib"
+doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib"