1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
discard """
output:
'''
Not found!
Found!
1
compiles for 1
i am always two
default for 3
set is 4 not 5
array is 6 not 7
default for 8
an identifier
OK
OK
OK
ayyydd
'''
"""
block arrayconstr:
const md_extension = [".md", ".markdown"]
proc test(ext: string) =
case ext
of ".txt", md_extension:
echo "Found!"
else:
echo "Not found!"
test(".something")
# ensure it's not evaluated at compile-time:
var foo = ".markdown"
test(foo)
converter toInt(x: char): int =
x.int
block t8333:
case 0
of 'a': echo 0
else: echo 1
block emptyset_when:
proc whenCase(a: int) =
case a
of (when compiles(whenCase(1)): 1 else: {}): echo "compiles for 1"
of {}: echo "me not fail"
of 2: echo "i am always two"
of []: echo "me neither"
of {4,5}: echo "set is 4 not 5"
of [6,7]: echo "array is 6 not 7"
of (when compiles(neverCompilesIBet()): 3 else: {}): echo "compiles for 3"
#of {},[]: echo "me neither"
else: echo "default for ", a
whenCase(1)
whenCase(2)
whenCase(3)
whenCase(4)
whenCase(6)
whenCase(8)
block setconstr:
const
SymChars: set[char] = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
proc classify(s: string) =
case s[0]
of SymChars, '_': echo "an identifier"
of {'0'..'9'}: echo "a number"
else: echo "other"
classify("Hurra")
block tduplicates:
type Kind = enum A, B
var k = A
template reject(b) =
static: doAssert(not compiles(b))
reject:
var i = 2
case i
of [1, 1]: discard
else: discard
reject:
var i = 2
case i
of 1, { 1..2 }: discard
else: discard
reject:
var i = 2
case i
of { 1, 1 }: discard
of { 1, 1 }: discard
else: discard
reject:
case k
of [A, A]: discard
var i = 2
case i
of { 1, 1 }: discard
of { 2, 2 }: echo "OK"
else: discard
case i
of { 10..30, 15..25, 5..15, 25..35 }: discard
else: echo "OK"
case k
of {A, A..A}: echo "OK"
of B: discard
block tcasestm:
type
Tenum = enum eA, eB, eC
var
x: string = "yyy"
y: Tenum = eA
i: int
case y
of eA: write(stdout, "a")
of eB, eC: write(stdout, "b or c")
case x
of "Andreas", "Rumpf": write(stdout, "Hallo Meister!")
of "aa", "bb": write(stdout, "Du bist nicht mein Meister")
of "cc", "hash", "when": discard
of "will", "it", "finally", "be", "generated": discard
var z = case i
of 1..5, 8, 9: "aa"
of 6, 7: "bb"
elif x == "Ha":
"cc"
elif x == "yyy":
write(stdout, x)
"dd"
else:
"zz"
echo z
#OUT ayyy
let str1 = "Y"
let str2 = "NN"
let a = case str1:
of "Y": true
of "N": false
else:
echo "no good"
quit("quitting")
proc toBool(s: string): bool =
case s:
of "": raise newException(ValueError, "Invalid boolean")
elif s[0] == 'Y': true
elif s[0] == 'N': false
else: "error".quit(2)
let b = "NN".toBool()
doAssert(a == true)
doAssert(b == false)
static:
#bug #7407
let bstatic = "N".toBool()
doAssert(bstatic == false)
var bb: bool
doAssert(not compiles(
bb = case str2:
of "": raise newException(ValueError, "Invalid boolean")
elif str.startsWith("Y"): true
elif str.startsWith("N"): false
))
doAssert(not compiles(
bb = case str2:
of "Y": true
of "N": false
))
doAssert(not compiles(
bb = case str2:
of "Y": true
of "N": raise newException(ValueError, "N not allowed")
))
doAssert(not compiles(
bb = case str2:
of "Y": raise newException(ValueError, "Invalid Y")
else: raise newException(ValueError, "Invalid N")
))
doAssert(not compiles(
bb = case str2:
of "Y":
raise newException(ValueError, "Invalid Y")
true
else: raise newException(ValueError, "Invalid")
))
doAssert(not compiles(
bb = case str2:
of "Y":
"invalid Y".quit(3)
true
else: raise newException(ValueError, "Invalid")
))
#issue #11552
proc positiveOrNegative(num: int): string =
result = case num
of (low(int)+2) .. -1:
"negative"
of 0:
"zero"
else:
"impossible"
#issue #11551
proc negativeOrNot(num: int): string =
result = case num
of low(int) .. -1:
"negative"
else:
"zero or positive"
doAssert negativeOrNot(-1) == "negative"
doAssert negativeOrNot(10000000) == "zero or positive"
doAssert negativeOrNot(0) == "zero or positive"
########################################################
# issue #13490
import strutils
func foo(input: string): int =
try:
parseInt(input)
except:
return
func foo2(b, input: string): int =
case b:
of "Y":
for c in input:
result = if c in '0'..'9': parseInt($c)
else: break
of "N":
for c in input:
result = if c in '0'..'9': parseInt($c)
else: continue
else: return
static:
doAssert(foo("3") == 3)
doAssert(foo("a") == 0)
doAssert(foo2("Y", "a2") == 0)
doAssert(foo2("Y", "2a") == 2)
doAssert(foo2("N", "a3") == 3)
doAssert(foo2("z", "2") == 0)
doAssert(foo("3") == 3)
doAssert(foo("a") == 0)
doAssert(foo2("Y", "a2") == 0)
doAssert(foo2("Y", "2a") == 2)
doAssert(foo2("N", "a3") == 3)
doAssert(foo2("z", "2") == 0)
# bug #20031
proc main(a: uint64) =
case a
else:
discard
static:
main(10)
main(10)
|