summary refs log tree commit diff stats
path: root/lib/std/private
diff options
context:
space:
mode:
authorTomohiro <gpuppur@gmail.com>2023-08-05 03:00:43 +0900
committerGitHub <noreply@github.com>2023-08-04 20:00:43 +0200
commitdb435a4a797adbbd4dd42edf89267902c0b2e34f (patch)
tree4d23817d8e16ef5f9682d7f14adcc1ede1657af3 /lib/std/private
parent73a29d72e347817a239f097c8185842e5bdca149 (diff)
downloadNim-db435a4a797adbbd4dd42edf89267902c0b2e34f.tar.gz
Fix searchExtPos so that it returns -1 when the path is not a file ext (#22245)
* Fix searchExtPos so that it returns -1 when the path is not a file ext

* fix comparision expression

* Remove splitDrive from searchExtPos
Diffstat (limited to 'lib/std/private')
-rw-r--r--lib/std/private/ospaths2.nim21
1 files changed, 17 insertions, 4 deletions
diff --git a/lib/std/private/ospaths2.nim b/lib/std/private/ospaths2.nim
index 612003023..18a01b104 100644
--- a/lib/std/private/ospaths2.nim
+++ b/lib/std/private/ospaths2.nim
@@ -584,15 +584,28 @@ proc searchExtPos*(path: string): int =
     assert searchExtPos("c.nim") == 1
     assert searchExtPos("a/b/c.nim") == 5
     assert searchExtPos("a.b.c.nim") == 5
+    assert searchExtPos(".nim") == -1
+    assert searchExtPos("..nim") == -1
+    assert searchExtPos("a..nim") == 2
 
-  # BUGFIX: do not search until 0! .DS_Store is no file extension!
+  # Unless there is any char that is not `ExtSep` before last `ExtSep` in the file name,
+  # it is not a file extension.
+  const DirSeps = when doslikeFileSystem: {DirSep, AltSep, ':'} else: {DirSep, AltSep}
   result = -1
-  for i in countdown(len(path)-1, 1):
+  var i = path.high
+  while i >= 1:
     if path[i] == ExtSep:
+      break
+    elif path[i] in DirSeps:
+      return -1 # do not skip over path
+    dec i
+
+  for j in countdown(i - 1, 0):
+    if path[j] in DirSeps:
+      return -1
+    elif path[j] != ExtSep:
       result = i
       break
-    elif path[i] in {DirSep, AltSep}:
-      break # do not skip over path
 
 proc splitFile*(path: string): tuple[dir, name, ext: string] {.
   noSideEffect, rtl, extern: "nos$1".} =