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
This commit is contained in:
Tomohiro
2023-08-05 03:00:43 +09:00
committed by GitHub
parent 73a29d72e3
commit db435a4a79
2 changed files with 49 additions and 4 deletions

View File

@@ -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".} =