Skip to content

Commit 586faa6

Browse files
authored
added tlen test case (#1111)
1 parent 0c8884b commit 586faa6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

tests/nimony/scopes/tlen.nim

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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")

tests/nimony/scopes/tlen.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3 3

0 commit comments

Comments
 (0)