diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index 04209aec4ba6..65809926aaaa 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -49,6 +49,7 @@ Methods * ``s.isascii()`` * ``s.isdecimal()`` * ``s.isdigit()`` +* ``s.isidentifier()`` * ``s.islower()`` * ``s.isnumeric()`` * ``s.isprintable()`` diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 5151bb7cd745..4c2739bac943 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1158,6 +1158,7 @@ bool CPyStr_IsPrintable(PyObject *str); bool CPyStr_IsLower(PyObject *str); bool CPyStr_IsTitle(PyObject *str); bool CPyStr_IsUpper(PyObject *str); +bool CPyStr_IsIdentifier(PyObject *str); // Bytes operations diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index 8d3965644f6a..f38804a99150 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -985,3 +985,21 @@ bool CPyStr_IsTitle(PyObject *str) { } return cased; } + +bool CPyStr_IsIdentifier(PyObject *str) { + if (PyUnicode_IS_ASCII(str)) { + // ASCII fast path: [A-Za-z_][A-Za-z0-9_]* + Py_ssize_t len = PyUnicode_GET_LENGTH(str); + if (len == 0) return false; + const Py_UCS1 *data = PyUnicode_1BYTE_DATA(str); + if (!Py_ISALPHA(data[0]) && data[0] != '_') + return false; + for (Py_ssize_t i = 1; i < len; i++) { + if (!Py_ISALNUM(data[i]) && data[i] != '_') + return false; + } + return true; + } + // Non-ASCII identifiers need XID_Start/XID_Continue checks (PEP 3131) + return PyUnicode_IsIdentifier(str) == 1; +} diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index 3ea676785bf8..586d15bac058 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -511,6 +511,15 @@ error_kind=ERR_NEVER, ) +# str.isidentifier() +method_op( + name="isidentifier", + arg_types=[str_rprimitive], + return_type=bool_rprimitive, + c_function_name="CPyStr_IsIdentifier", + error_kind=ERR_NEVER, +) + # obj.decode() method_op( diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 9f83160038e9..1866df1f40e1 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -148,6 +148,7 @@ def isnumeric(self) -> bool: ... def isprintable(self) -> bool: ... def istitle(self) -> bool: ... def isupper(self) -> bool: ... + def isidentifier(self) -> bool: ... class float: def __init__(self, x: object) -> None: pass diff --git a/mypyc/test-data/irbuild-str.test b/mypyc/test-data/irbuild-str.test index 951aa11e00f0..7dd14c8de293 100644 --- a/mypyc/test-data/irbuild-str.test +++ b/mypyc/test-data/irbuild-str.test @@ -1136,6 +1136,17 @@ L0: r0 = CPyStr_IsUpper(x) return r0 +[case testStrIsIdentifier] +def is_identifier(x: str) -> bool: + return x.isidentifier() +[out] +def is_identifier(x): + x :: str + r0 :: bool +L0: + r0 = CPyStr_IsIdentifier(x) + return r0 + [case testStrIndexEqLiteral_64bit] def is_comma(s: str, i: int) -> bool: return s[i] == "," diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index a31ef0110f24..95fb554702e7 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -1668,6 +1668,61 @@ def test_str_subclass() -> None: class S(str): pass +[case testStrIsIdentifier] +from typing import Any + +def check(s: str) -> None: + # Any forces generic dispatch, so we compare our primitive against CPython's + a: Any = s + assert s.isidentifier() == a.isidentifier(), f"isidentifier mismatch for {s!r}" + +def test_all_code_points() -> None: + # Covers the ASCII, UCS-1, UCS-2 and UCS-4 string kinds, and both the start + # and continuation positions. Check all BMP code points, and a sample of the + # others (checking all of them is slow). + for i in [*range(0x10000), *range(0x10000, 0x110000, 97), 0x10FFFF]: + c = chr(i) + check(c) + check("a" + c) + +def test_strings() -> None: + samples = [ + "", "_", "__", "a", "abc", "ABC", "_abc_123", "abc_", "class", "None", "1", "1abc", + "abc-def", "a b", "abc!", "abc\n", "\u00e9t\u00e9", "\u00e9", "\u4e2d\u6587", "x\u0660", + "\u0660x", "x\u00b2", "\u2167", "\u212e", "\U00010400abc", "abc\U00010400", + "\U0001f600", "abc\u2000", "abc\u00a0", + ] + for s in samples: + check(s) + check(s + s) + +def test_isidentifier() -> None: + assert "abc".isidentifier() + assert "_".isidentifier() + assert "_abc_123".isidentifier() + assert "ABC".isidentifier() + assert "class".isidentifier() # Keywords are identifiers + assert not "".isidentifier() + assert not "1abc".isidentifier() + assert not "abc-def".isidentifier() + assert not "a b".isidentifier() + assert "\u00e9t\u00e9".isidentifier() + assert "\u4e2d\u6587".isidentifier() + assert "x\u0660".isidentifier() + assert not "\u0660x".isidentifier() + assert "\U00010400abc".isidentifier() + assert not "\U0001f600".isidentifier() + assert not "abc\u2000".isidentifier() + +def test_str_subclass() -> None: + from strsubclass import S + assert S("abc").isidentifier() + assert not S("1abc").isidentifier() + +[file strsubclass.py] +class S(str): + pass + [case testStrIndexEqLiteralSpecialize] from testutil import assertRaises