GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [mypyc] Add `librt.strings.isdigit` codepoint primitive (#21504) · python/mypy@d9f17fb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • mypyc
    • lib-rt
      • codepoint_extra_ops.h
      • strings
        • librt_strings.c
    • primitives
      • librt_strings_ops.py
    • test-data
      • irbuild-librt-strings.test
      • run-librt-strings.test
  • mypy/typeshed/stubs/librt/librt
    • strings.pyi
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎mypy/typeshed/stubs/librt/librt/strings.pyi‎

Copy file name to clipboardExpand all lines: mypy/typeshed/stubs/librt/librt/strings.pyi
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ def read_f64_be(b: bytes, index: i64, /) -> float: ...
4444
# Codepoint classification helpers operating on i32 codepoints (typically
4545
# obtained via ord(s[i])). Negative inputs return False.
4646
def isspace(c: i32, /) -> bool: ...
47+
def isdigit(c: i32, /) -> bool: ...
Collapse file

‎mypyc/lib-rt/codepoint_extra_ops.h‎

Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.h
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ static inline bool LibRTStrings_IsSpace(int32_t c) {
1313
return c >= 0 && Py_UNICODE_ISSPACE((Py_UCS4)c);
1414
}
1515

16+
static inline bool LibRTStrings_IsDigit(int32_t c) {
17+
return c >= 0 && Py_UNICODE_ISDIGIT((Py_UCS4)c);
18+
}
19+
1620
#endif // MYPYC_CODEPOINT_EXTRA_OPS_H
Collapse file

‎mypyc/lib-rt/strings/librt_strings.c‎

Copy file name to clipboardExpand all lines: mypyc/lib-rt/strings/librt_strings.c
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ cp_parse_i32(PyObject *arg, int32_t *out) {
11911191
}
11921192

11931193
DEFINE_CP_BOOL_WRAPPER(isspace, LibRTStrings_IsSpace)
1194+
DEFINE_CP_BOOL_WRAPPER(isdigit, LibRTStrings_IsDigit)
11941195

11951196
static PyMethodDef librt_strings_module_methods[] = {
11961197
{"write_i16_le", (PyCFunction) write_i16_le, METH_FASTCALL,
@@ -1256,6 +1257,9 @@ static PyMethodDef librt_strings_module_methods[] = {
12561257
{"isspace", cp_isspace, METH_O,
12571258
PyDoc_STR("Test whether a codepoint (i32) is Unicode whitespace.")
12581259
},
1260+
{"isdigit", cp_isdigit, METH_O,
1261+
PyDoc_STR("Test whether a codepoint (i32) is a Unicode digit.")
1262+
},
12591263
{NULL, NULL, 0, NULL}
12601264
};
12611265

Collapse file

‎mypyc/primitives/librt_strings_ops.py‎

Copy file name to clipboardExpand all lines: mypyc/primitives/librt_strings_ops.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,12 @@
404404
error_kind=ERR_NEVER,
405405
dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS],
406406
)
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

‎mypyc/test-data/irbuild-librt-strings.test‎

Copy file name to clipboardExpand all lines: mypyc/test-data/irbuild-librt-strings.test
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,17 @@ L8:
345345
L9:
346346
r16 = LibRTStrings_IsSpace(r15)
347347
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

‎mypyc/test-data/run-librt-strings.test‎

Copy file name to clipboardExpand all lines: mypyc/test-data/run-librt-strings.test
+13-9Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,19 +1440,23 @@ def test_new_without_init_is_usable() -> None:
14401440
sw.write("hello")
14411441
assert sw.getvalue() == "hello"
14421442

1443-
[case testLibrtStringsIsSpace_librt]
1443+
[case testLibrtStringsCodepointClassifiers_librt]
14441444
from typing import Any
14451445
from mypy_extensions import i32
1446-
from librt.strings import isspace
1446+
from librt.strings import isspace, isdigit
14471447

14481448

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.
14551457
for i in range(0x110000):
14561458
c = chr(i)
14571459
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()

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.