GeezerVison is showing github.com — [mypyc] Add `librt.strings.isdigit` codepoint primitive (#21504) · python/mypy@d9f17fb
File treeExpand file treeCollapse file tree Open diff view settings mypy/typeshed/stubs/librt/librt
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: mypy/typeshed/stubs/librt/librt/strings.pyi +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,3 +44,4 @@ def read_f64_be(b: bytes, index: i64, /) -> float: ... | 44 | 44 | # Codepoint classification helpers operating on i32 codepoints (typically | 45 | 45 | # obtained via ord(s[i])). Negative inputs return False. | 46 | 46 | def isspace(c: i32, /) -> bool: ... | | 47 | +def isdigit(c: i32, /) -> bool: ... |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,4 +13,8 @@ static inline bool LibRTStrings_IsSpace(int32_t c) { | 13 | 13 | return c >= 0 && Py_UNICODE_ISSPACE((Py_UCS4)c); | 14 | 14 | } | 15 | 15 |
| | 16 | +static inline bool LibRTStrings_IsDigit(int32_t c) { | | 17 | + return c >= 0 && Py_UNICODE_ISDIGIT((Py_UCS4)c); | | 18 | +} | | 19 | + | 16 | 20 | #endif // MYPYC_CODEPOINT_EXTRA_OPS_H |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/strings/librt_strings.c +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1191,6 +1191,7 @@ cp_parse_i32(PyObject *arg, int32_t *out) { | 1191 | 1191 | } | 1192 | 1192 |
| 1193 | 1193 | DEFINE_CP_BOOL_WRAPPER(isspace, LibRTStrings_IsSpace) | | 1194 | +DEFINE_CP_BOOL_WRAPPER(isdigit, LibRTStrings_IsDigit) | 1194 | 1195 |
| 1195 | 1196 | static PyMethodDef librt_strings_module_methods[] = { | 1196 | 1197 | {"write_i16_le", (PyCFunction) write_i16_le, METH_FASTCALL, | @@ -1256,6 +1257,9 @@ static PyMethodDef librt_strings_module_methods[] = { | 1256 | 1257 | {"isspace", cp_isspace, METH_O, | 1257 | 1258 | PyDoc_STR("Test whether a codepoint (i32) is Unicode whitespace.") | 1258 | 1259 | }, | | 1260 | + {"isdigit", cp_isdigit, METH_O, | | 1261 | + PyDoc_STR("Test whether a codepoint (i32) is a Unicode digit.") | | 1262 | + }, | 1259 | 1263 | {NULL, NULL, 0, NULL} | 1260 | 1264 | }; | 1261 | 1265 |
| |
Collapse file Copy file name to clipboardExpand all lines: mypyc/primitives/librt_strings_ops.py +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 404 | 404 | error_kind=ERR_NEVER, | 405 | 405 | dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | 406 | 406 | ) | | 407 | + | | 408 | +function_op( | | 409 | + name="librt.strings.isdigit", | | 410 | + arg_types=[int32_rprimitive], | | 411 | + return_type=bool_rprimitive, | | 412 | + c_function_name="LibRTStrings_IsDigit", | | 413 | + error_kind=ERR_NEVER, | | 414 | + dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | | 415 | +) |
Collapse file Copy file name to clipboardExpand all lines: mypyc/test-data/irbuild-librt-strings.test +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 345 | 345 | L9: | 346 | 346 | r16 = LibRTStrings_IsSpace(r15) | 347 | 347 | return r16 | | 348 | + | | 349 | +[case testLibrtStringsIsDigitIR] | | 350 | +from librt.strings import isdigit | | 351 | +from mypy_extensions import i32 | | 352 | + | | 353 | +def is_d(c: i32) -> bool: | | 354 | + return isdigit(c) | | 355 | +[out] | | 356 | +def is_d(c): | | 357 | + c :: i32 | | 358 | + r0 :: bool | | 359 | +L0: | | 360 | + r0 = LibRTStrings_IsDigit(c) | | 361 | + return r0 |
Collapse file Copy file name to clipboardExpand all lines: mypyc/test-data/run-librt-strings.test +13-9Lines changed: 13 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1440,19 +1440,23 @@ def test_new_without_init_is_usable() -> None: | 1440 | 1440 | sw.write("hello") | 1441 | 1441 | assert sw.getvalue() == "hello" | 1442 | 1442 |
| 1443 | | -[case testLibrtStringsIsSpace_librt] | | 1443 | +[case testLibrtStringsCodepointClassifiers_librt] | 1444 | 1444 | from typing import Any | 1445 | 1445 | from mypy_extensions import i32 | 1446 | | -from librt.strings import isspace | | 1446 | +from librt.strings import isspace, isdigit | 1447 | 1447 |
| 1448 | 1448 |
| 1449 | | -def test_isspace() -> None: | 1450 | | - assert not isspace(i32(-1)) | 1451 | | - assert not isspace(i32(-113)) | 1452 | | - # Verify our codepoint primitive agrees with str.isspace() across all | 1453 | | - # Unicode codepoints, including the ord(chr(i)) round-trip. Any | 1454 | | - # forces generic dispatch on the str side. | | 1449 | +def test_codepoint_classifiers() -> None: | | 1450 | + # Negative values are not codepoints. | | 1451 | + for bad in (i32(-1), i32(-113)): | | 1452 | + assert not isspace(bad) | | 1453 | + assert not isdigit(bad) | | 1454 | + # Verify each codepoint primitive agrees with the matching str method | | 1455 | + # across all Unicode codepoints, including the ord(chr(i)) round-trip. | | 1456 | + # Any forces generic dispatch on the str side. | 1455 | 1457 | for i in range(0x110000): | 1456 | 1458 | c = chr(i) | 1457 | 1459 | a: Any = c | 1458 | | - assert isspace(ord(c)) == isspace(i) == a.isspace() | | 1460 | + o = ord(c) | | 1461 | + assert isspace(o) == isspace(i) == a.isspace() | | 1462 | + assert isdigit(o) == isdigit(i) == a.isdigit() |
|