irbuild-str.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. [case testStrSplit]
  2. from typing import Optional, List
  3. def do_split(s: str, sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]:
  4. if sep is not None:
  5. if max_split is not None:
  6. return s.split(sep, max_split)
  7. else:
  8. return s.split(sep)
  9. return s.split()
  10. [out]
  11. def do_split(s, sep, max_split):
  12. s :: str
  13. sep :: union[str, None]
  14. max_split :: union[int, None]
  15. r0, r1, r2 :: object
  16. r3 :: bit
  17. r4 :: object
  18. r5 :: bit
  19. r6 :: str
  20. r7 :: int
  21. r8 :: list
  22. r9 :: str
  23. r10, r11 :: list
  24. L0:
  25. if is_error(sep) goto L1 else goto L2
  26. L1:
  27. r0 = box(None, 1)
  28. sep = r0
  29. L2:
  30. if is_error(max_split) goto L3 else goto L4
  31. L3:
  32. r1 = box(None, 1)
  33. max_split = r1
  34. L4:
  35. r2 = load_address _Py_NoneStruct
  36. r3 = sep != r2
  37. if r3 goto L5 else goto L9 :: bool
  38. L5:
  39. r4 = load_address _Py_NoneStruct
  40. r5 = max_split != r4
  41. if r5 goto L6 else goto L7 :: bool
  42. L6:
  43. r6 = cast(str, sep)
  44. r7 = unbox(int, max_split)
  45. r8 = CPyStr_Split(s, r6, r7)
  46. return r8
  47. L7:
  48. r9 = cast(str, sep)
  49. r10 = PyUnicode_Split(s, r9, -1)
  50. return r10
  51. L8:
  52. L9:
  53. r11 = PyUnicode_Split(s, 0, -1)
  54. return r11
  55. [case testStrEquality]
  56. def eq(x: str, y: str) -> bool:
  57. return x == y
  58. def neq(x: str, y: str) -> bool:
  59. return x != y
  60. [out]
  61. def eq(x, y):
  62. x, y :: str
  63. r0 :: i32
  64. r1 :: bit
  65. r2 :: object
  66. r3, r4, r5 :: bit
  67. L0:
  68. r0 = PyUnicode_Compare(x, y)
  69. r1 = r0 == -1
  70. if r1 goto L1 else goto L3 :: bool
  71. L1:
  72. r2 = PyErr_Occurred()
  73. r3 = r2 != 0
  74. if r3 goto L2 else goto L3 :: bool
  75. L2:
  76. r4 = CPy_KeepPropagating()
  77. L3:
  78. r5 = r0 == 0
  79. return r5
  80. def neq(x, y):
  81. x, y :: str
  82. r0 :: i32
  83. r1 :: bit
  84. r2 :: object
  85. r3, r4, r5 :: bit
  86. L0:
  87. r0 = PyUnicode_Compare(x, y)
  88. r1 = r0 == -1
  89. if r1 goto L1 else goto L3 :: bool
  90. L1:
  91. r2 = PyErr_Occurred()
  92. r3 = r2 != 0
  93. if r3 goto L2 else goto L3 :: bool
  94. L2:
  95. r4 = CPy_KeepPropagating()
  96. L3:
  97. r5 = r0 != 0
  98. return r5
  99. [case testStrReplace]
  100. from typing import Optional
  101. def do_replace(s: str, old_substr: str, new_substr: str, max_count: Optional[int] = None) -> str:
  102. if max_count is not None:
  103. return s.replace(old_substr, new_substr, max_count)
  104. else:
  105. return s.replace(old_substr, new_substr)
  106. [out]
  107. def do_replace(s, old_substr, new_substr, max_count):
  108. s, old_substr, new_substr :: str
  109. max_count :: union[int, None]
  110. r0, r1 :: object
  111. r2 :: bit
  112. r3 :: int
  113. r4, r5 :: str
  114. L0:
  115. if is_error(max_count) goto L1 else goto L2
  116. L1:
  117. r0 = box(None, 1)
  118. max_count = r0
  119. L2:
  120. r1 = load_address _Py_NoneStruct
  121. r2 = max_count != r1
  122. if r2 goto L3 else goto L4 :: bool
  123. L3:
  124. r3 = unbox(int, max_count)
  125. r4 = CPyStr_Replace(s, old_substr, new_substr, r3)
  126. return r4
  127. L4:
  128. r5 = PyUnicode_Replace(s, old_substr, new_substr, -1)
  129. return r5
  130. L5:
  131. unreachable
  132. [case testStrToBool]
  133. def is_true(x: str) -> bool:
  134. if x:
  135. return True
  136. else:
  137. return False
  138. [out]
  139. def is_true(x):
  140. x :: str
  141. r0 :: bit
  142. L0:
  143. r0 = CPyStr_IsTrue(x)
  144. if r0 goto L1 else goto L2 :: bool
  145. L1:
  146. return 1
  147. L2:
  148. return 0
  149. L3:
  150. unreachable
  151. [case testStringFormatMethod]
  152. def f(s: str, num: int) -> None:
  153. s1 = "Hi! I'm {}, and I'm {} years old.".format(s, num)
  154. s2 = ''.format()
  155. s3 = 'abc'.format()
  156. s4 = '}}{}{{{}}}{{{}'.format(num, num, num)
  157. [out]
  158. def f(s, num):
  159. s :: str
  160. num :: int
  161. r0, r1, r2, r3, r4, s1, r5, s2, r6, s3, r7, r8, r9, r10, r11, r12, r13, s4 :: str
  162. L0:
  163. r0 = CPyTagged_Str(num)
  164. r1 = "Hi! I'm "
  165. r2 = ", and I'm "
  166. r3 = ' years old.'
  167. r4 = CPyStr_Build(5, r1, s, r2, r0, r3)
  168. s1 = r4
  169. r5 = ''
  170. s2 = r5
  171. r6 = 'abc'
  172. s3 = r6
  173. r7 = CPyTagged_Str(num)
  174. r8 = CPyTagged_Str(num)
  175. r9 = CPyTagged_Str(num)
  176. r10 = '}'
  177. r11 = '{'
  178. r12 = '}{'
  179. r13 = CPyStr_Build(6, r10, r7, r11, r8, r12, r9)
  180. s4 = r13
  181. return 1
  182. [case testFStrings]
  183. def f(var: str, num: int) -> None:
  184. s1 = f"Hi! I'm {var}. I am {num} years old."
  185. s2 = f'Hello {var:>{num}}'
  186. s3 = f''
  187. s4 = f'abc'
  188. [out]
  189. def f(var, num):
  190. var :: str
  191. num :: int
  192. r0, r1, r2, r3, r4, s1, r5, r6, r7, r8, r9, r10, r11 :: str
  193. r12 :: object
  194. r13 :: str
  195. r14 :: list
  196. r15, r16, r17 :: ptr
  197. r18, s2, r19, s3, r20, s4 :: str
  198. L0:
  199. r0 = "Hi! I'm "
  200. r1 = '. I am '
  201. r2 = CPyTagged_Str(num)
  202. r3 = ' years old.'
  203. r4 = CPyStr_Build(5, r0, var, r1, r2, r3)
  204. s1 = r4
  205. r5 = ''
  206. r6 = 'Hello '
  207. r7 = '{:{}}'
  208. r8 = '>'
  209. r9 = CPyTagged_Str(num)
  210. r10 = CPyStr_Build(2, r8, r9)
  211. r11 = 'format'
  212. r12 = CPyObject_CallMethodObjArgs(r7, r11, var, r10, 0)
  213. r13 = cast(str, r12)
  214. r14 = PyList_New(2)
  215. r15 = get_element_ptr r14 ob_item :: PyListObject
  216. r16 = load_mem r15 :: ptr*
  217. set_mem r16, r6 :: builtins.object*
  218. r17 = r16 + WORD_SIZE*1
  219. set_mem r17, r13 :: builtins.object*
  220. keep_alive r14
  221. r18 = PyUnicode_Join(r5, r14)
  222. s2 = r18
  223. r19 = ''
  224. s3 = r19
  225. r20 = 'abc'
  226. s4 = r20
  227. return 1
  228. [case testStringFormattingCStyle]
  229. def f(var: str, num: int) -> None:
  230. s1 = "Hi! I'm %s." % var
  231. s2 = "I am %d years old." % num
  232. s3 = "Hi! I'm %s. I am %d years old." % (var, num)
  233. s4 = "Float: %f" % num
  234. [typing fixtures/typing-full.pyi]
  235. [out]
  236. def f(var, num):
  237. var :: str
  238. num :: int
  239. r0, r1, r2, s1, r3, r4, r5, r6, s2, r7, r8, r9, r10, r11, s3, r12 :: str
  240. r13, r14 :: object
  241. r15, s4 :: str
  242. L0:
  243. r0 = "Hi! I'm "
  244. r1 = '.'
  245. r2 = CPyStr_Build(3, r0, var, r1)
  246. s1 = r2
  247. r3 = CPyTagged_Str(num)
  248. r4 = 'I am '
  249. r5 = ' years old.'
  250. r6 = CPyStr_Build(3, r4, r3, r5)
  251. s2 = r6
  252. r7 = CPyTagged_Str(num)
  253. r8 = "Hi! I'm "
  254. r9 = '. I am '
  255. r10 = ' years old.'
  256. r11 = CPyStr_Build(5, r8, var, r9, r7, r10)
  257. s3 = r11
  258. r12 = 'Float: %f'
  259. r13 = box(int, num)
  260. r14 = PyNumber_Remainder(r12, r13)
  261. r15 = cast(str, r14)
  262. s4 = r15
  263. return 1
  264. [case testDecode]
  265. def f(b: bytes) -> None:
  266. b.decode()
  267. b.decode('utf-8')
  268. b.decode('utf-8', 'backslashreplace')
  269. [out]
  270. def f(b):
  271. b :: bytes
  272. r0, r1, r2, r3, r4, r5 :: str
  273. L0:
  274. r0 = CPy_Decode(b, 0, 0)
  275. r1 = 'utf-8'
  276. r2 = CPy_Decode(b, r1, 0)
  277. r3 = 'utf-8'
  278. r4 = 'backslashreplace'
  279. r5 = CPy_Decode(b, r3, r4)
  280. return 1
  281. [case testEncode]
  282. def f(s: str) -> None:
  283. s.encode()
  284. s.encode('utf-8')
  285. s.encode('ascii', 'backslashreplace')
  286. [out]
  287. def f(s):
  288. s :: str
  289. r0 :: bytes
  290. r1 :: str
  291. r2 :: bytes
  292. r3, r4 :: str
  293. r5 :: bytes
  294. L0:
  295. r0 = CPy_Encode(s, 0, 0)
  296. r1 = 'utf-8'
  297. r2 = CPy_Encode(s, r1, 0)
  298. r3 = 'ascii'
  299. r4 = 'backslashreplace'
  300. r5 = CPy_Encode(s, r3, r4)
  301. return 1