Debian Codegraph @

mypyc/test-data/irbuild-str.test

raw
1 [case testStrSplit]
2 from typing import NewType, Optional, List, Union
3 NewStr = NewType("NewStr", str)
4
5 def do_split(s: Union[str, NewStr], sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]:
6 if sep is not None:
7 if max_split is not None:
8 return s.split(sep, max_split)
9 else:
10 return s.split(sep)
11 return s.split()
12 [typing fixtures/typing-full.pyi]
13 [out]
14 def do_split(s, sep, max_split):
15 s :: str
16 sep :: union[str, None]
17 max_split :: union[int, None]
18 r0, r1, r2 :: object
19 r3 :: bit
20 r4 :: object
21 r5 :: bit
22 r6 :: str
23 r7 :: int
24 r8 :: list
25 r9 :: str
26 r10, r11 :: list
27 L0:
28 if is_error(sep) goto L1 else goto L2
29 L1:
30 r0 = box(None, 1)
31 sep = r0
32 L2:
33 if is_error(max_split) goto L3 else goto L4
34 L3:
35 r1 = box(None, 1)
36 max_split = r1
37 L4:
38 r2 = load_address _Py_NoneStruct
39 r3 = sep != r2
40 if r3 goto L5 else goto L9 :: bool
41 L5:
42 r4 = load_address _Py_NoneStruct
43 r5 = max_split != r4
44 if r5 goto L6 else goto L7 :: bool
45 L6:
46 r6 = cast(str, sep)
47 r7 = unbox(int, max_split)
48 r8 = CPyStr_Split(s, r6, r7)
49 return r8
50 L7:
51 r9 = cast(str, sep)
52 r10 = PyUnicode_Split(s, r9, -1)
53 return r10
54 L8:
55 L9:
56 r11 = PyUnicode_Split(s, 0, -1)
57 return r11
58
59
60 [case testStrEquality]
61 from typing import NewType, Union
62 NewStr = NewType("NewStr", str)
63 def eq(x: str, y: str) -> bool:
64 return x == y
65
66 def neq(x: str, y: Union[str, NewStr]) -> bool:
67 return x != y
68
69 [typing fixtures/typing-full.pyi]
70 [out]
71 def eq(x, y):
72 x, y :: str
73 r0 :: bool
74 L0:
75 r0 = CPyStr_Equal(x, y)
76 return r0
77 def neq(x, y):
78 x, y :: str
79 r0 :: bool
80 r1 :: bit
81 L0:
82 r0 = CPyStr_Equal(x, y)
83 r1 = r0 == 0
84 return r1
85
86 [case testStrReplace]
87 from typing import NewType, Optional, Union
88 NewStr = NewType("NewStr", str)
89 def do_replace(s: Union[str, NewStr], old_substr: str, new_substr: str, max_count: Optional[int] = None) -> str:
90 if max_count is not None:
91 return s.replace(old_substr, new_substr, max_count)
92 else:
93 return s.replace(old_substr, new_substr)
94 [typing fixtures/typing-full.pyi]
95 [out]
96 def do_replace(s, old_substr, new_substr, max_count):
97 s, old_substr, new_substr :: str
98 max_count :: union[int, None]
99 r0, r1 :: object
100 r2 :: bit
101 r3 :: int
102 r4, r5 :: str
103 L0:
104 if is_error(max_count) goto L1 else goto L2
105 L1:
106 r0 = box(None, 1)
107 max_count = r0
108 L2:
109 r1 = load_address _Py_NoneStruct
110 r2 = max_count != r1
111 if r2 goto L3 else goto L4 :: bool
112 L3:
113 r3 = unbox(int, max_count)
114 r4 = CPyStr_Replace(s, old_substr, new_substr, r3)
115 return r4
116 L4:
117 r5 = PyUnicode_Replace(s, old_substr, new_substr, -1)
118 return r5
119 L5:
120 unreachable
121
122 [case testStrStartswithEndswithTuple]
123 from typing import NewType, Tuple, Union
124 NewStr = NewType("NewStr", str)
125
126 def do_startswith(s1: Union[str, NewStr], s2: Tuple[str, ...]) -> bool:
127 return s1.startswith(s2)
128
129 def do_endswith(s1: Union[str, NewStr], s2: Tuple[str, ...]) -> bool:
130 return s1.endswith(s2)
131
132 def do_tuple_literal_args(s1: Union[str, NewStr]) -> None:
133 x = s1.startswith(("a", "b"))
134 y = s1.endswith(("a", "b"))
135 [typing fixtures/typing-full.pyi]
136 [out]
137 def do_startswith(s1, s2):
138 s1 :: str
139 s2 :: tuple
140 r0 :: bool
141 L0:
142 r0 = CPyStr_Startswith(s1, s2)
143 return r0
144 def do_endswith(s1, s2):
145 s1 :: str
146 s2 :: tuple
147 r0 :: bool
148 L0:
149 r0 = CPyStr_Endswith(s1, s2)
150 return r0
151 def do_tuple_literal_args(s1):
152 s1, r0, r1 :: str
153 r2 :: tuple[str, str]
154 r3 :: object
155 r4, x :: bool
156 r5, r6 :: str
157 r7 :: tuple[str, str]
158 r8 :: object
159 r9, y :: bool
160 L0:
161 r0 = 'a'
162 r1 = 'b'
163 r2 = (r0, r1)
164 r3 = box(tuple[str, str], r2)
165 r4 = CPyStr_Startswith(s1, r3)
166 x = r4
167 r5 = 'a'
168 r6 = 'b'
169 r7 = (r5, r6)
170 r8 = box(tuple[str, str], r7)
171 r9 = CPyStr_Endswith(s1, r8)
172 y = r9
173 return 1
174
175 [case testStrToBool]
176 from typing import NewType, Union
177 NewStr = NewType("NewStr", str)
178 def is_true(x: Union[str, NewStr]) -> bool:
179 if x:
180 return True
181 else:
182 return False
183 [typing fixtures/typing-full.pyi]
184 [out]
185 def is_true(x):
186 x :: str
187 r0 :: bit
188 L0:
189 r0 = CPyStr_IsTrue(x)
190 if r0 goto L1 else goto L2 :: bool
191 L1:
192 return 1
193 L2:
194 return 0
195 L3:
196 unreachable
197
198 [case testStringFormatMethod]
199 from typing import NewType, Union
200 NewStr = NewType("NewStr", str)
201 def f(s: Union[str, NewStr], num: int) -> None:
202 s1 = "Hi! I'm {}, and I'm {} years old.".format(s, num)
203 s2 = ''.format()
204 s3 = 'abc'.format()
205 s4 = '}}{}{{{}}}{{{}'.format(num, num, num)
206 [typing fixtures/typing-full.pyi]
207 [out]
208 def f(s, num):
209 s :: str
210 num :: int
211 r0, r1, r2, r3, r4, s1, r5, s2, r6, s3, r7, r8, r9, r10, r11, r12, r13, s4 :: str
212 L0:
213 r0 = CPyTagged_Str(num)
214 r1 = "Hi! I'm "
215 r2 = ", and I'm "
216 r3 = ' years old.'
217 r4 = CPyStr_Build(5, r1, s, r2, r0, r3)
218 s1 = r4
219 r5 = ''
220 s2 = r5
221 r6 = 'abc'
222 s3 = r6
223 r7 = CPyTagged_Str(num)
224 r8 = CPyTagged_Str(num)
225 r9 = CPyTagged_Str(num)
226 r10 = '}'
227 r11 = '{'
228 r12 = '}{'
229 r13 = CPyStr_Build(6, r10, r7, r11, r8, r12, r9)
230 s4 = r13
231 return 1
232
233 [case testFStrings_64bit]
234 from typing import NewType, Union
235 NewStr = NewType("NewStr", str)
236 def f(var: Union[str, NewStr], num: int) -> None:
237 s1 = f"Hi! I'm {var}. I am {num} years old."
238 s2 = f'Hello {var:>{num}}'
239 s3 = f''
240 s4 = f'abc'
241 [typing fixtures/typing-full.pyi]
242 [out]
243 def f(var, num):
244 var :: str
245 num :: int
246 r0, r1, r2, r3, r4, s1, r5, r6, r7, r8, r9, r10, r11 :: str
247 r12 :: object[3]
248 r13 :: object_ptr
249 r14 :: object
250 r15 :: str
251 r16 :: list
252 r17 :: ptr
253 r18, s2, r19, s3, r20, s4 :: str
254 L0:
255 r0 = "Hi! I'm "
256 r1 = '. I am '
257 r2 = CPyTagged_Str(num)
258 r3 = ' years old.'
259 r4 = CPyStr_Build(5, r0, var, r1, r2, r3)
260 s1 = r4
261 r5 = ''
262 r6 = 'Hello '
263 r7 = '{:{}}'
264 r8 = '>'
265 r9 = CPyTagged_Str(num)
266 r10 = CPyStr_Build(2, r8, r9)
267 r11 = 'format'
268 r12 = [r7, var, r10]
269 r13 = load_address r12
270 r14 = PyObject_VectorcallMethod(r11, r13, 9223372036854775811, 0)
271 keep_alive r7, var, r10
272 r15 = cast(str, r14)
273 r16 = PyList_New(2)
274 r17 = list_items r16
275 buf_init_item r17, 0, r6
276 buf_init_item r17, 1, r15
277 keep_alive r16
278 r18 = PyUnicode_Join(r5, r16)
279 s2 = r18
280 r19 = ''
281 s3 = r19
282 r20 = 'abc'
283 s4 = r20
284 return 1
285
286 [case testStringFormattingCStyle]
287 from typing import NewType, Union
288 NewStr = NewType("NewStr", str)
289 def f(var: Union[str, NewStr], num: int) -> None:
290 s1 = "Hi! I'm %s." % var
291 s2 = "I am %d years old." % num
292 s3 = "Hi! I'm %s. I am %d years old." % (var, num)
293 s4 = "Float: %f" % num
294 [typing fixtures/typing-full.pyi]
295 [out]
296 def f(var, num):
297 var :: str
298 num :: int
299 r0, r1, r2, s1, r3, r4, r5, r6, s2, r7, r8, r9, r10, r11, s3, r12 :: str
300 r13, r14 :: object
301 r15, s4 :: str
302 L0:
303 r0 = "Hi! I'm "
304 r1 = '.'
305 r2 = CPyStr_Build(3, r0, var, r1)
306 s1 = r2
307 r3 = CPyTagged_Str(num)
308 r4 = 'I am '
309 r5 = ' years old.'
310 r6 = CPyStr_Build(3, r4, r3, r5)
311 s2 = r6
312 r7 = CPyTagged_Str(num)
313 r8 = "Hi! I'm "
314 r9 = '. I am '
315 r10 = ' years old.'
316 r11 = CPyStr_Build(5, r8, var, r9, r7, r10)
317 s3 = r11
318 r12 = 'Float: %f'
319 r13 = box(int, num)
320 r14 = PyNumber_Remainder(r12, r13)
321 r15 = cast(str, r14)
322 s4 = r15
323 return 1
324
325 [case testDecode]
326 def f(b: bytes) -> None:
327 b.decode()
328 b.decode('Utf_8')
329 b.decode('utf-8')
330 b.decode('UTF8')
331 b.decode('latin1')
332 b.decode('Latin-1')
333 b.decode('ascii')
334 encoding = 'utf-8'
335 b.decode(encoding)
336 b.decode('utf-8', 'backslashreplace')
337 def variants(b: bytes) -> None:
338 b.decode(encoding="UTF_8")
339 b.decode("ascii", errors="strict")
340 [out]
341 def f(b):
342 b :: bytes
343 r0, r1, r2, r3, r4, r5, r6, r7, encoding, r8, r9, r10, r11 :: str
344 L0:
345 r0 = CPy_DecodeUTF8(b)
346 r1 = CPy_DecodeUTF8(b)
347 r2 = CPy_DecodeUTF8(b)
348 r3 = CPy_DecodeUTF8(b)
349 r4 = CPy_DecodeLatin1(b)
350 r5 = CPy_DecodeLatin1(b)
351 r6 = CPy_DecodeASCII(b)
352 r7 = 'utf-8'
353 encoding = r7
354 r8 = CPy_Decode(b, encoding, 0)
355 r9 = 'utf-8'
356 r10 = 'backslashreplace'
357 r11 = CPy_Decode(b, r9, r10)
358 return 1
359 def variants(b):
360 b :: bytes
361 r0, r1 :: str
362 L0:
363 r0 = CPy_DecodeUTF8(b)
364 r1 = CPy_DecodeASCII(b)
365 return 1
366
367 [case testEncode_64bit]
368 from typing import NewType, Union
369 NewStr = NewType("NewStr", str)
370 def f(s: Union[str, NewStr]) -> None:
371 s.encode()
372 s.encode('utf-8')
373 s.encode('utf8', 'strict')
374 s.encode('latin1', errors='strict')
375 s.encode(encoding='ascii')
376 s.encode(errors='strict', encoding='latin-1')
377 s.encode('utf-8', 'backslashreplace')
378 s.encode('ascii', 'backslashreplace')
379 encoding = 'utf8'
380 s.encode(encoding)
381 errors = 'strict'
382 s.encode('utf8', errors)
383 s.encode('utf8', errors=errors)
384 s.encode(errors=errors)
385 s.encode(encoding=encoding, errors=errors)
386 s.encode('latin2')
387
388 [typing fixtures/typing-full.pyi]
389 [out]
390 def f(s):
391 s :: str
392 r0, r1, r2, r3, r4, r5 :: bytes
393 r6, r7 :: str
394 r8 :: bytes
395 r9, r10 :: str
396 r11 :: bytes
397 r12, encoding :: str
398 r13 :: bytes
399 r14, errors, r15 :: str
400 r16 :: bytes
401 r17, r18 :: str
402 r19 :: object[3]
403 r20 :: object_ptr
404 r21, r22 :: object
405 r23 :: str
406 r24 :: object[2]
407 r25 :: object_ptr
408 r26, r27 :: object
409 r28 :: str
410 r29 :: object[3]
411 r30 :: object_ptr
412 r31, r32 :: object
413 r33 :: str
414 r34 :: bytes
415 L0:
416 r0 = PyUnicode_AsUTF8String(s)
417 r1 = PyUnicode_AsUTF8String(s)
418 r2 = PyUnicode_AsUTF8String(s)
419 r3 = PyUnicode_AsLatin1String(s)
420 r4 = PyUnicode_AsASCIIString(s)
421 r5 = PyUnicode_AsLatin1String(s)
422 r6 = 'utf-8'
423 r7 = 'backslashreplace'
424 r8 = CPy_Encode(s, r6, r7)
425 r9 = 'ascii'
426 r10 = 'backslashreplace'
427 r11 = CPy_Encode(s, r9, r10)
428 r12 = 'utf8'
429 encoding = r12
430 r13 = CPy_Encode(s, encoding, 0)
431 r14 = 'strict'
432 errors = r14
433 r15 = 'utf8'
434 r16 = CPy_Encode(s, r15, errors)
435 r17 = 'utf8'
436 r18 = 'encode'
437 r19 = [s, r17, errors]
438 r20 = load_address r19
439 r21 = ('errors',)
440 r22 = PyObject_VectorcallMethod(r18, r20, 9223372036854775810, r21)
441 keep_alive s, r17, errors
442 r23 = 'encode'
443 r24 = [s, errors]
444 r25 = load_address r24
445 r26 = ('errors',)
446 r27 = PyObject_VectorcallMethod(r23, r25, 9223372036854775809, r26)
447 keep_alive s, errors
448 r28 = 'encode'
449 r29 = [s, encoding, errors]
450 r30 = load_address r29
451 r31 = ('encoding', 'errors')
452 r32 = PyObject_VectorcallMethod(r28, r30, 9223372036854775809, r31)
453 keep_alive s, encoding, errors
454 r33 = 'latin2'
455 r34 = CPy_Encode(s, r33, 0)
456 return 1
457
458 [case testOrd]
459 from typing import NewType, Union
460 NewStr = NewType("NewStr", str)
461 def str_ord(x: Union[str, NewStr]) -> int:
462 return ord(x)
463 def str_ord_literal() -> int:
464 return ord("a")
465 def bytes_ord(x: bytes) -> int:
466 return ord(x)
467 def bytes_ord_literal() -> int:
468 return ord(b"a")
469 def any_ord(x) -> int:
470 return ord(x)
471 [typing fixtures/typing-full.pyi]
472 [out]
473 def str_ord(x):
474 x :: str
475 r0 :: int
476 L0:
477 r0 = CPyStr_Ord(x)
478 return r0
479 def str_ord_literal():
480 L0:
481 return 194
482 def bytes_ord(x):
483 x :: bytes
484 r0 :: int
485 L0:
486 r0 = CPyBytes_Ord(x)
487 return r0
488 def bytes_ord_literal():
489 L0:
490 return 194
491 def any_ord(x):
492 x, r0 :: object
493 r1 :: str
494 r2 :: object
495 r3 :: object[1]
496 r4 :: object_ptr
497 r5 :: object
498 r6 :: int
499 L0:
500 r0 = builtins :: module
501 r1 = 'ord'
502 r2 = CPyObject_GetAttr(r0, r1)
503 r3 = [x]
504 r4 = load_address r3
505 r5 = PyObject_Vectorcall(r2, r4, 1, 0)
506 keep_alive x
507 r6 = unbox(int, r5)
508 return r6
509
510 [case testOrdOfStrIndex_64bit]
511 from mypy_extensions import i64, i32, i16, u8
512 def ord_str_index(s: str, i: int) -> int:
513 return ord(s[i])
514 def ord_str_index_i64(s: str, i: i64) -> int:
515 return ord(s[i])
516 def ord_str_index_to_i32(s: str, i: i64) -> i32:
517 return ord(s[i])
518 def ord_str_index_to_i16(s: str, i: i64) -> i16:
519 return ord(s[i])
520 def ord_str_index_to_u8(s: str, i: i64) -> u8:
521 return ord(s[i])
522 def ord_str_index_to_i64(s: str, i: i64) -> i64:
523 return ord(s[i])
524 [typing fixtures/typing-full.pyi]
525 [out]
526 def ord_str_index(s, i):
527 s :: str
528 i :: int
529 r0 :: native_int
530 r1 :: bit
531 r2, r3 :: i64
532 r4 :: ptr
533 r5 :: c_ptr
534 r6, r7 :: i64
535 r8, r9 :: bool
536 r10 :: short_int
537 L0:
538 r0 = i & 1
539 r1 = r0 == 0
540 if r1 goto L1 else goto L2 :: bool
541 L1:
542 r2 = i >> 1
543 r3 = r2
544 goto L3
545 L2:
546 r4 = i ^ 1
547 r5 = r4
548 r6 = CPyLong_AsInt64(r5)
549 r3 = r6
550 keep_alive i
551 L3:
552 r7 = CPyStr_AdjustIndex(s, r3)
553 r8 = CPyStr_RangeCheck(s, r7)
554 if r8 goto L5 else goto L4 :: bool
555 L4:
556 r9 = raise IndexError('index out of range')
557 unreachable
558 L5:
559 r10 = CPyStr_GetItemUnsafeAsInt(s, r7)
560 return r10
561 def ord_str_index_i64(s, i):
562 s :: str
563 i, r0 :: i64
564 r1, r2 :: bool
565 r3 :: short_int
566 L0:
567 r0 = CPyStr_AdjustIndex(s, i)
568 r1 = CPyStr_RangeCheck(s, r0)
569 if r1 goto L2 else goto L1 :: bool
570 L1:
571 r2 = raise IndexError('index out of range')
572 unreachable
573 L2:
574 r3 = CPyStr_GetItemUnsafeAsInt(s, r0)
575 return r3
576 def ord_str_index_to_i32(s, i):
577 s :: str
578 i, r0 :: i64
579 r1, r2 :: bool
580 r3 :: short_int
581 r4, r5 :: bit
582 r6 :: native_int
583 r7, r8 :: i32
584 L0:
585 r0 = CPyStr_AdjustIndex(s, i)
586 r1 = CPyStr_RangeCheck(s, r0)
587 if r1 goto L2 else goto L1 :: bool
588 L1:
589 r2 = raise IndexError('index out of range')
590 unreachable
591 L2:
592 r3 = CPyStr_GetItemUnsafeAsInt(s, r0)
593 r4 = r3 < 4294967296 :: signed
594 if r4 goto L3 else goto L5 :: bool
595 L3:
596 r5 = r3 >= -4294967296 :: signed
597 if r5 goto L4 else goto L5 :: bool
598 L4:
599 r6 = r3 >> 1
600 r7 = truncate r6: native_int to i32
601 r8 = r7
602 goto L6
603 L5:
604 CPyInt32_Overflow()
605 unreachable
606 L6:
607 return r8
608 def ord_str_index_to_i16(s, i):
609 s :: str
610 i, r0 :: i64
611 r1, r2 :: bool
612 r3 :: short_int
613 r4, r5 :: bit
614 r6 :: native_int
615 r7, r8 :: i16
616 L0:
617 r0 = CPyStr_AdjustIndex(s, i)
618 r1 = CPyStr_RangeCheck(s, r0)
619 if r1 goto L2 else goto L1 :: bool
620 L1:
621 r2 = raise IndexError('index out of range')
622 unreachable
623 L2:
624 r3 = CPyStr_GetItemUnsafeAsInt(s, r0)
625 r4 = r3 < 65536 :: signed
626 if r4 goto L3 else goto L5 :: bool
627 L3:
628 r5 = r3 >= -65536 :: signed
629 if r5 goto L4 else goto L5 :: bool
630 L4:
631 r6 = r3 >> 1
632 r7 = truncate r6: native_int to i16
633 r8 = r7
634 goto L6
635 L5:
636 CPyInt16_Overflow()
637 unreachable
638 L6:
639 return r8
640 def ord_str_index_to_u8(s, i):
641 s :: str
642 i, r0 :: i64
643 r1, r2 :: bool
644 r3 :: short_int
645 r4, r5 :: bit
646 r6 :: native_int
647 r7, r8 :: u8
648 L0:
649 r0 = CPyStr_AdjustIndex(s, i)
650 r1 = CPyStr_RangeCheck(s, r0)
651 if r1 goto L2 else goto L1 :: bool
652 L1:
653 r2 = raise IndexError('index out of range')
654 unreachable
655 L2:
656 r3 = CPyStr_GetItemUnsafeAsInt(s, r0)
657 r4 = r3 < 512 :: signed
658 if r4 goto L3 else goto L5 :: bool
659 L3:
660 r5 = r3 >= 0 :: signed
661 if r5 goto L4 else goto L5 :: bool
662 L4:
663 r6 = r3 >> 1
664 r7 = truncate r6: native_int to u8
665 r8 = r7
666 goto L6
667 L5:
668 CPyUInt8_Overflow()
669 unreachable
670 L6:
671 return r8
672 def ord_str_index_to_i64(s, i):
673 s :: str
674 i, r0 :: i64
675 r1, r2 :: bool
676 r3 :: short_int
677 r4 :: i64
678 L0:
679 r0 = CPyStr_AdjustIndex(s, i)
680 r1 = CPyStr_RangeCheck(s, r0)
681 if r1 goto L2 else goto L1 :: bool
682 L1:
683 r2 = raise IndexError('index out of range')
684 unreachable
685 L2:
686 r3 = CPyStr_GetItemUnsafeAsInt(s, r0)
687 r4 = r3 >> 1
688 return r4
689
690 [case testStrip]
691 from typing import NewType, Union
692 NewStr = NewType("NewStr", str)
693 def do_strip(s: Union[str, NewStr]) -> None:
694 s.lstrip("x")
695 s.strip("y")
696 s.rstrip("z")
697 s.lstrip()
698 s.strip()
699 s.rstrip()
700 [typing fixtures/typing-full.pyi]
701 [out]
702 def do_strip(s):
703 s, r0, r1, r2, r3, r4, r5, r6, r7, r8 :: str
704 L0:
705 r0 = 'x'
706 r1 = CPyStr_LStrip(s, r0)
707 r2 = 'y'
708 r3 = CPyStr_Strip(s, r2)
709 r4 = 'z'
710 r5 = CPyStr_RStrip(s, r4)
711 r6 = CPyStr_LStrip(s, 0)
712 r7 = CPyStr_Strip(s, 0)
713 r8 = CPyStr_RStrip(s, 0)
714 return 1
715
716 [case testCountAll_64bit]
717 from typing import NewType, Union
718 NewStr = NewType("NewStr", str)
719 def do_count(s: str) -> int:
720 return s.count("x")
721 [typing fixtures/typing-full.pyi]
722 [out]
723 def do_count(s):
724 s, r0 :: str
725 r1 :: native_int
726 r2, r3, r4 :: bit
727 r5, r6, r7 :: int
728 L0:
729 r0 = 'x'
730 r1 = CPyStr_Count(s, r0, 0)
731 r2 = r1 >= 0 :: signed
732 r3 = r1 <= 4611686018427387903 :: signed
733 if r3 goto L1 else goto L2 :: bool
734 L1:
735 r4 = r1 >= -4611686018427387904 :: signed
736 if r4 goto L3 else goto L2 :: bool
737 L2:
738 r5 = CPyTagged_FromInt64(r1)
739 r6 = r5
740 goto L4
741 L3:
742 r7 = r1 << 1
743 r6 = r7
744 L4:
745 return r6
746
747 [case testCountStart_64bit]
748 from typing import NewType, Union
749 NewStr = NewType("NewStr", str)
750 def do_count(s: str, start: int) -> int:
751 return s.count("x", start)
752 [typing fixtures/typing-full.pyi]
753 [out]
754 def do_count(s, start):
755 s :: str
756 start :: int
757 r0 :: str
758 r1 :: native_int
759 r2, r3, r4 :: bit
760 r5, r6, r7 :: int
761 L0:
762 r0 = 'x'
763 r1 = CPyStr_Count(s, r0, start)
764 r2 = r1 >= 0 :: signed
765 r3 = r1 <= 4611686018427387903 :: signed
766 if r3 goto L1 else goto L2 :: bool
767 L1:
768 r4 = r1 >= -4611686018427387904 :: signed
769 if r4 goto L3 else goto L2 :: bool
770 L2:
771 r5 = CPyTagged_FromInt64(r1)
772 r6 = r5
773 goto L4
774 L3:
775 r7 = r1 << 1
776 r6 = r7
777 L4:
778 return r6
779
780 [case testCountStartEnd_64bit]
781 from typing import NewType, Union
782 NewStr = NewType("NewStr", str)
783 def do_count(s: str, start: int, end: int) -> int:
784 return s.count("x", start, end)
785 [typing fixtures/typing-full.pyi]
786 [out]
787 def do_count(s, start, end):
788 s :: str
789 start, end :: int
790 r0 :: str
791 r1 :: native_int
792 r2, r3, r4 :: bit
793 r5, r6, r7 :: int
794 L0:
795 r0 = 'x'
796 r1 = CPyStr_CountFull(s, r0, start, end)
797 r2 = r1 >= 0 :: signed
798 r3 = r1 <= 4611686018427387903 :: signed
799 if r3 goto L1 else goto L2 :: bool
800 L1:
801 r4 = r1 >= -4611686018427387904 :: signed
802 if r4 goto L3 else goto L2 :: bool
803 L2:
804 r5 = CPyTagged_FromInt64(r1)
805 r6 = r5
806 goto L4
807 L3:
808 r7 = r1 << 1
809 r6 = r7
810 L4:
811 return r6
812
813 [case testFStringFromConstants]
814 from typing import Final
815 string: Final = "abc"
816 integer: Final = 123
817 floating: Final = 3.14
818 boolean: Final = True
819
820 def test(x: str) -> str:
821 return f"{string}{integer}{floating}{boolean}{x}{boolean}{floating}{integer}{string}{x}{string}{integer}{floating}{boolean}"
822 def test2(x: str) -> str:
823 return f"{string}{integer}{floating}{boolean}{x}{boolean}{floating}{integer}{string}{x}{string}{integer}{floating}{boolean}{x}"
824 def test3(x: str) -> str:
825 return f"{x}{string}{integer}{floating}{boolean}{x}{boolean}{floating}{integer}{string}{x}{string}{integer}{floating}{boolean}{x}"
826
827 [out]
828 def test(x):
829 x, r0, r1, r2, r3 :: str
830 L0:
831 r0 = 'abc1233.14True'
832 r1 = 'True3.14123abc'
833 r2 = 'abc1233.14True'
834 r3 = CPyStr_Build(5, r0, x, r1, x, r2)
835 return r3
836 def test2(x):
837 x, r0, r1, r2, r3 :: str
838 L0:
839 r0 = 'abc1233.14True'
840 r1 = 'True3.14123abc'
841 r2 = 'abc1233.14True'
842 r3 = CPyStr_Build(6, r0, x, r1, x, r2, x)
843 return r3
844 def test3(x):
845 x, r0, r1, r2, r3 :: str
846 L0:
847 r0 = 'abc1233.14True'
848 r1 = 'True3.14123abc'
849 r2 = 'abc1233.14True'
850 r3 = CPyStr_Build(7, x, r0, x, r1, x, r2, x)
851 return r3
852
853 [case testOptionalStrEquality1]
854 from typing import Optional
855
856 def opt_opt(x: Optional[str], y: Optional[str]) -> bool:
857 return x == y
858 [out]
859 def opt_opt(x, y):
860 x, y :: union[str, None]
861 r0 :: object
862 r1 :: bit
863 r2 :: object
864 r3 :: bit
865 r4 :: bool
866 r5 :: object
867 r6 :: bit
868 r7, r8 :: str
869 r9 :: bool
870 L0:
871 r0 = load_address _Py_NoneStruct
872 r1 = x == r0
873 if r1 goto L1 else goto L2 :: bool
874 L1:
875 r2 = load_address _Py_NoneStruct
876 r3 = y == r2
877 r4 = r3
878 goto L5
879 L2:
880 r5 = load_address _Py_NoneStruct
881 r6 = y == r5
882 if r6 goto L3 else goto L4 :: bool
883 L3:
884 r4 = 0
885 goto L5
886 L4:
887 r7 = unchecked borrow cast(str, x)
888 r8 = unchecked borrow cast(str, y)
889 r9 = CPyStr_Equal(r7, r8)
890 r4 = r9
891 L5:
892 keep_alive x, y
893 return r4
894
895 [case testOptionalStrEquality2]
896 from typing import Optional
897
898 def opt_non_opt(x: Optional[str], y: str) -> bool:
899 return x == y
900 [out]
901 def opt_non_opt(x, y):
902 x :: union[str, None]
903 y :: str
904 r0 :: object
905 r1 :: bit
906 r2 :: bool
907 r3 :: str
908 r4 :: bool
909 L0:
910 r0 = load_address _Py_NoneStruct
911 r1 = x == r0
912 if r1 goto L1 else goto L2 :: bool
913 L1:
914 r2 = 0
915 goto L3
916 L2:
917 r3 = unchecked borrow cast(str, x)
918 r4 = CPyStr_Equal(r3, y)
919 r2 = r4
920 L3:
921 keep_alive x
922 return r2
923
924 [case testStrEqLiteral]
925 from typing import Final
926 literal: Final = "literal"
927 def literal_rhs(x: str) -> bool:
928 return x == literal
929 def literal_lhs(x: str) -> bool:
930 return literal == x
931 def literal_both() -> bool:
932 return literal == "literal"
933 [out]
934 def literal_rhs(x):
935 x, r0 :: str
936 r1 :: bool
937 L0:
938 r0 = 'literal'
939 r1 = CPyStr_EqualLiteral(x, r0, 7)
940 return r1
941 def literal_lhs(x):
942 x, r0 :: str
943 r1 :: bool
944 L0:
945 r0 = 'literal'
946 r1 = CPyStr_EqualLiteral(x, r0, 7)
947 return r1
948 def literal_both():
949 r0, r1 :: str
950 L0:
951 r0 = 'literal'
952 r1 = 'literal'
953 return 1
954
955 [case testStrMultiply]
956 def s_times_i(s: str, n: int) -> str:
957 return s * n
958 def i_times_s(s: str, n: int) -> str:
959 return n * s
960 [out]
961 def s_times_i(s, n):
962 s :: str
963 n :: int
964 r0 :: str
965 L0:
966 r0 = CPyStr_Multiply(s, n)
967 return r0
968 def i_times_s(s, n):
969 s :: str
970 n :: int
971 r0 :: str
972 L0:
973 r0 = CPyStr_Multiply(s, n)
974 return r0
975
976 [case testStrLower]
977 def do_lower(s: str) -> str:
978 return s.lower()
979 [out]
980 def do_lower(s):
981 s, r0 :: str
982 L0:
983 r0 = CPyStr_Lower(s)
984 return r0
985
986 [case testStrUpper]
987 def do_upper(s: str) -> str:
988 return s.upper()
989 [out]
990 def do_upper(s):
991 s, r0 :: str
992 L0:
993 r0 = CPyStr_Upper(s)
994 return r0
995
996 [case testStrIsSpace]
997 def is_space(x: str) -> bool:
998 return x.isspace()
999 [out]
1000 def is_space(x):
1001 x :: str
1002 r0 :: bool
1003 L0:
1004 r0 = CPyStr_IsSpace(x)
1005 return r0
1006
1007 [case testStrIsAlnum]
1008 def is_alnum(x: str) -> bool:
1009 return x.isalnum()
1010 [out]
1011 def is_alnum(x):
1012 x :: str
1013 r0 :: bool
1014 L0:
1015 r0 = CPyStr_IsAlnum(x)
1016 return r0
1017
1018 [case testStrIsDigit]
1019 def is_digit(x: str) -> bool:
1020 return x.isdigit()
1021 [out]
1022 def is_digit(x):
1023 x :: str
1024 r0 :: bool
1025 L0:
1026 r0 = CPyStr_IsDigit(x)
1027 return r0
1028
1029 [case testStrIndexEqLiteral_64bit]
1030 def is_comma(s: str, i: int) -> bool:
1031 return s[i] == ","
1032 def is_comma_swapped(s: str, i: int) -> bool:
1033 return "," == s[i]
1034 def is_comma_ne(s: str, i: int) -> bool:
1035 return s[i] != ","
1036 [out]
1037 def is_comma(s, i):
1038 s :: str
1039 i :: int
1040 r0 :: native_int
1041 r1 :: bit
1042 r2, r3 :: i64
1043 r4 :: ptr
1044 r5 :: c_ptr
1045 r6, r7 :: i64
1046 r8, r9 :: bool
1047 r10 :: short_int
1048 r11 :: bit
1049 L0:
1050 r0 = i & 1
1051 r1 = r0 == 0
1052 if r1 goto L1 else goto L2 :: bool
1053 L1:
1054 r2 = i >> 1
1055 r3 = r2
1056 goto L3
1057 L2:
1058 r4 = i ^ 1
1059 r5 = r4
1060 r6 = CPyLong_AsInt64(r5)
1061 r3 = r6
1062 keep_alive i
1063 L3:
1064 r7 = CPyStr_AdjustIndex(s, r3)
1065 r8 = CPyStr_RangeCheck(s, r7)
1066 if r8 goto L5 else goto L4 :: bool
1067 L4:
1068 r9 = raise IndexError('index out of range')
1069 unreachable
1070 L5:
1071 r10 = CPyStr_GetItemUnsafeAsInt(s, r7)
1072 r11 = int_eq r10, 88
1073 return r11
1074 def is_comma_swapped(s, i):
1075 s :: str
1076 i :: int
1077 r0 :: native_int
1078 r1 :: bit
1079 r2, r3 :: i64
1080 r4 :: ptr
1081 r5 :: c_ptr
1082 r6, r7 :: i64
1083 r8, r9 :: bool
1084 r10 :: short_int
1085 r11 :: bit
1086 L0:
1087 r0 = i & 1
1088 r1 = r0 == 0
1089 if r1 goto L1 else goto L2 :: bool
1090 L1:
1091 r2 = i >> 1
1092 r3 = r2
1093 goto L3
1094 L2:
1095 r4 = i ^ 1
1096 r5 = r4
1097 r6 = CPyLong_AsInt64(r5)
1098 r3 = r6
1099 keep_alive i
1100 L3:
1101 r7 = CPyStr_AdjustIndex(s, r3)
1102 r8 = CPyStr_RangeCheck(s, r7)
1103 if r8 goto L5 else goto L4 :: bool
1104 L4:
1105 r9 = raise IndexError('index out of range')
1106 unreachable
1107 L5:
1108 r10 = CPyStr_GetItemUnsafeAsInt(s, r7)
1109 r11 = int_eq r10, 88
1110 return r11
1111 def is_comma_ne(s, i):
1112 s :: str
1113 i :: int
1114 r0 :: native_int
1115 r1 :: bit
1116 r2, r3 :: i64
1117 r4 :: ptr
1118 r5 :: c_ptr
1119 r6, r7 :: i64
1120 r8, r9 :: bool
1121 r10 :: short_int
1122 r11 :: bit
1123 L0:
1124 r0 = i & 1
1125 r1 = r0 == 0
1126 if r1 goto L1 else goto L2 :: bool
1127 L1:
1128 r2 = i >> 1
1129 r3 = r2
1130 goto L3
1131 L2:
1132 r4 = i ^ 1
1133 r5 = r4
1134 r6 = CPyLong_AsInt64(r5)
1135 r3 = r6
1136 keep_alive i
1137 L3:
1138 r7 = CPyStr_AdjustIndex(s, r3)
1139 r8 = CPyStr_RangeCheck(s, r7)
1140 if r8 goto L5 else goto L4 :: bool
1141 L4:
1142 r9 = raise IndexError('index out of range')
1143 unreachable
1144 L5:
1145 r10 = CPyStr_GetItemUnsafeAsInt(s, r7)
1146 r11 = int_ne r10, 88
1147 return r11
1148
1149 [case testStrIndexEqLiteralNoSpecialize]
1150 def two_char_literal(s: str, i: int) -> bool:
1151 # Multi-char literals don't match the specialization; falls through to
1152 # the generic str equality path.
1153 return s[i] == "ab"
1154 def empty_literal(s: str, i: int) -> bool:
1155 # Empty string literals also fall through; the generic path returns False.
1156 return s[i] == ""
1157 [out]
1158 def two_char_literal(s, i):
1159 s :: str
1160 i :: int
1161 r0, r1 :: str
1162 r2 :: bool
1163 L0:
1164 r0 = CPyStr_GetItem(s, i)
1165 r1 = 'ab'
1166 r2 = CPyStr_EqualLiteral(r0, r1, 2)
1167 return r2
1168 def empty_literal(s, i):
1169 s :: str
1170 i :: int
1171 r0, r1 :: str
1172 r2 :: bool
1173 L0:
1174 r0 = CPyStr_GetItem(s, i)
1175 r1 = ''
1176 r2 = CPyStr_EqualLiteral(r0, r1, 0)
1177 return r2