File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ import std / syncio
3+
4+ const
5+ DirSep * = '/'
6+ AltSep * = when defined (windows): '\\ ' else : '/'
7+
8+ type
9+ Path * = distinct string
10+
11+ proc len * (p: Path ): int {.inline .} = string (p).len
12+ template `$` * (p: Path ): string = string (p)
13+
14+ proc searchExtPos (path: string ; len: int = 1 ): int =
15+ # Do not search until 0! .DS_Store is no file extension!
16+ result = - 1
17+ for i in countdown (len (path)- 1 , len):
18+ if path[i] == '.' :
19+ if i- 1 >= 0 and path[i- 1 ] in {DirSep , AltSep }:
20+ discard " bad luck"
21+ else :
22+ result = i
23+ break
24+ elif path[i] in {DirSep , AltSep }:
25+ break # do not skip over path
26+
27+ type
28+ Stringlike = concept
29+ proc len (s: Self ): int
30+ proc `[]` (s: Self ; i: int ): char
31+
32+ proc searchExtPosG [T: Stringlike ](path: T; len: int = 1 ): int =
33+ # Do not search until 0! .DS_Store is no file extension!
34+ result = - 1
35+ for i in countdown (len (path)- 1 , len):
36+ if path[i] == '.' :
37+ if i- 1 >= 0 and path[i- 1 ] in {DirSep , AltSep }:
38+ discard " bad luck"
39+ else :
40+ result = i
41+ break
42+ elif path[i] in {DirSep , AltSep }:
43+ break # do not skip over path
44+
45+ echo searchExtPos (" abc.def" ), " " , searchExtPosG (" abc.def" )
Original file line number Diff line number Diff line change 1+ 3 3
You can’t perform that action at this time.
0 commit comments