diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index 8f2daba02b72d..b0c6190d02f48 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -45,7 +45,12 @@ Methods * ``s1.find(s2: str, start: int)`` * ``s1.find(s2: str, start: int, end: int)`` * ``s.isalnum()`` +* ``s.isalpha()`` +* ``s.isascii()`` +* ``s.isdecimal()`` * ``s.isdigit()`` +* ``s.isnumeric()`` +* ``s.isprintable()`` * ``s.isspace()`` * ``s.lower()`` * ``s.join(x: Iterable)`` diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 65cc8f038e508..e6510f0b520e3 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1043,6 +1043,13 @@ PyObject *CPyStr_Upper(PyObject *str); bool CPyStr_IsSpace(PyObject *str); bool CPyStr_IsAlnum(PyObject *str); bool CPyStr_IsDigit(PyObject *str); +bool CPyStr_IsAlpha(PyObject *str); +static inline bool CPyStr_IsAscii(PyObject *str) { + return PyUnicode_IS_ASCII(str); +} +bool CPyStr_IsDecimal(PyObject *str); +bool CPyStr_IsNumeric(PyObject *str); +bool CPyStr_IsPrintable(PyObject *str); // Bytes operations diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index 9e3d494868017..f1293f913e7a8 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -806,3 +806,63 @@ bool CPyStr_IsDigit(PyObject *str) { #undef CHECK_ISDIGIT } + +#define CPY_STR_CHECK_CHARS(TYPE, DATA, LEN, CHECK) \ + { \ + const TYPE *_data = (const TYPE *)(DATA); \ + for (Py_ssize_t _i = 0; _i < (LEN); _i++) { \ + if (!CHECK(_data[_i])) \ + return false; \ + } \ + } + +// Return from the enclosing function whether every character of str satisfies a +// predicate. ASCII strings are checked using ASCII_CHECK, which avoids Unicode database +// lookups, and other strings using UNICODE_CHECK. Empty strings return EMPTY_RESULT. +#define CPY_STR_RETURN_ALL_CHARS(str, ASCII_CHECK, UNICODE_CHECK, EMPTY_RESULT) \ + { \ + Py_ssize_t _len = PyUnicode_GET_LENGTH(str); \ + if (_len == 0) \ + return (EMPTY_RESULT); \ + if (PyUnicode_IS_ASCII(str)) { \ + CPY_STR_CHECK_CHARS(Py_UCS1, PyUnicode_1BYTE_DATA(str), _len, ASCII_CHECK); \ + return true; \ + } \ + switch (PyUnicode_KIND(str)) { \ + case PyUnicode_1BYTE_KIND: \ + CPY_STR_CHECK_CHARS(Py_UCS1, PyUnicode_1BYTE_DATA(str), _len, UNICODE_CHECK); \ + break; \ + case PyUnicode_2BYTE_KIND: \ + CPY_STR_CHECK_CHARS(Py_UCS2, PyUnicode_2BYTE_DATA(str), _len, UNICODE_CHECK); \ + break; \ + case PyUnicode_4BYTE_KIND: \ + CPY_STR_CHECK_CHARS(Py_UCS4, PyUnicode_4BYTE_DATA(str), _len, UNICODE_CHECK); \ + break; \ + default: \ + Py_UNREACHABLE(); \ + } \ + return true; \ + } + +// Printable ASCII characters are from ' ' (0x20) to '~' (0x7e) +#define CPY_ASCII_ISPRINTABLE(c) ((c) >= 0x20 && (c) < 0x7f) + +bool CPyStr_IsAlpha(PyObject *str) { + CPY_STR_RETURN_ALL_CHARS(str, Py_ISALPHA, Py_UNICODE_ISALPHA, false); +} + +bool CPyStr_IsDecimal(PyObject *str) { + CPY_STR_RETURN_ALL_CHARS(str, Py_ISDIGIT, Py_UNICODE_ISDECIMAL, false); +} + +bool CPyStr_IsNumeric(PyObject *str) { + CPY_STR_RETURN_ALL_CHARS(str, Py_ISDIGIT, Py_UNICODE_ISNUMERIC, false); +} + +bool CPyStr_IsPrintable(PyObject *str) { + CPY_STR_RETURN_ALL_CHARS(str, CPY_ASCII_ISPRINTABLE, Py_UNICODE_ISPRINTABLE, true); +} + +#undef CPY_ASCII_ISPRINTABLE +#undef CPY_STR_RETURN_ALL_CHARS +#undef CPY_STR_CHECK_CHARS diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index 27cf79e62352d..4b591a89e45e2 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -439,6 +439,51 @@ error_kind=ERR_NEVER, ) +# str.isalpha() +method_op( + name="isalpha", + arg_types=[str_rprimitive], + return_type=bool_rprimitive, + c_function_name="CPyStr_IsAlpha", + error_kind=ERR_NEVER, +) + +# str.isascii() +method_op( + name="isascii", + arg_types=[str_rprimitive], + return_type=bool_rprimitive, + c_function_name="CPyStr_IsAscii", + error_kind=ERR_NEVER, +) + +# str.isdecimal() +method_op( + name="isdecimal", + arg_types=[str_rprimitive], + return_type=bool_rprimitive, + c_function_name="CPyStr_IsDecimal", + error_kind=ERR_NEVER, +) + +# str.isnumeric() +method_op( + name="isnumeric", + arg_types=[str_rprimitive], + return_type=bool_rprimitive, + c_function_name="CPyStr_IsNumeric", + error_kind=ERR_NEVER, +) + +# str.isprintable() +method_op( + name="isprintable", + arg_types=[str_rprimitive], + return_type=bool_rprimitive, + c_function_name="CPyStr_IsPrintable", + 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 8fed798bbc695..14950b15a4be6 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -140,6 +140,11 @@ def count(self, substr: str, start: Optional[int] = None, end: Optional[int] = N def isspace(self) -> bool: ... def isalnum(self) -> bool: ... def isdigit(self) -> bool: ... + def isalpha(self) -> bool: ... + def isascii(self) -> bool: ... + def isdecimal(self) -> bool: ... + def isnumeric(self) -> bool: ... + def isprintable(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 bf2773196cdec..6593184c7373d 100644 --- a/mypyc/test-data/irbuild-str.test +++ b/mypyc/test-data/irbuild-str.test @@ -1026,6 +1026,61 @@ L0: r0 = CPyStr_IsDigit(x) return r0 +[case testStrIsAlpha] +def is_alpha(x: str) -> bool: + return x.isalpha() +[out] +def is_alpha(x): + x :: str + r0 :: bool +L0: + r0 = CPyStr_IsAlpha(x) + return r0 + +[case testStrIsAscii] +def is_ascii(x: str) -> bool: + return x.isascii() +[out] +def is_ascii(x): + x :: str + r0 :: bool +L0: + r0 = CPyStr_IsAscii(x) + return r0 + +[case testStrIsDecimal] +def is_decimal(x: str) -> bool: + return x.isdecimal() +[out] +def is_decimal(x): + x :: str + r0 :: bool +L0: + r0 = CPyStr_IsDecimal(x) + return r0 + +[case testStrIsNumeric] +def is_numeric(x: str) -> bool: + return x.isnumeric() +[out] +def is_numeric(x): + x :: str + r0 :: bool +L0: + r0 = CPyStr_IsNumeric(x) + return r0 + +[case testStrIsPrintable] +def is_printable(x: str) -> bool: + return x.isprintable() +[out] +def is_printable(x): + x :: str + r0 :: bool +L0: + r0 = CPyStr_IsPrintable(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 f26b160d816c1..07f6a9be0ccdf 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -1410,6 +1410,106 @@ def test_isdigit_strings() -> None: assert not "123\u00e9".isdigit() assert not "\U0001d7ce!".isdigit() +[case testStrIsCharClass] +from typing import Any + +def check(s: str) -> None: + # Any forces generic dispatch, so we compare our primitives against CPython's + a: Any = s + assert s.isalpha() == a.isalpha(), f"isalpha mismatch for {s!r}" + assert s.isascii() == a.isascii(), f"isascii mismatch for {s!r}" + assert s.isdecimal() == a.isdecimal(), f"isdecimal mismatch for {s!r}" + assert s.isnumeric() == a.isnumeric(), f"isnumeric mismatch for {s!r}" + assert s.isprintable() == a.isprintable(), f"isprintable mismatch for {s!r}" + +def test_all_code_points() -> None: + # Covers the ASCII, UCS-1, UCS-2 and UCS-4 string kinds + for i in range(0x110000): + check(chr(i)) + +def test_strings() -> None: + samples = [ + "", " ", "abc", "ABCxyz", "abc1", "123", "1.5", "-1", "a b", "abc!", + "Hello, World! 123 ~", "\t", "a\nb", "\x00", "\x7f", + "été", "é²", "²½", "\u00a0", + "٠١٢", "Ⅷ三", "中文", "\u2000", "\u200b", + "abcé", "123٠", "\U00010400\U00010428", "\U0001d7ce\U0001d7cf", + "123\U0001d7ce", "\U0001f600", "abc\U0001f600", "abc\U000e0001", + ] + for s in samples: + check(s) + check(s * 3) + +def test_isalpha() -> None: + assert "abc".isalpha() + assert "ABCxyz".isalpha() + assert not "".isalpha() + assert not "abc1".isalpha() + assert not "a b".isalpha() + assert "été".isalpha() + assert "中文".isalpha() + assert "\U00010400\U00010428".isalpha() + assert not "٠".isalpha() + assert not "\U0001d7ce".isalpha() + +def test_isascii() -> None: + assert "".isascii() + assert "abc".isascii() + assert "\x00\x7f".isascii() + assert not "\x80".isascii() + assert not "café".isascii() + assert not "中".isascii() + assert not "abc\U0001f600".isascii() + +def test_isdecimal() -> None: + assert "0123456789".isdecimal() + assert not "".isdecimal() + assert not "12a".isdecimal() + assert not "1.5".isdecimal() + assert "٠١٢".isdecimal() + assert "123\U0001d7ce".isdecimal() + assert not "²".isdecimal() + assert not "½".isdecimal() + assert not "Ⅷ".isdecimal() + +def test_isnumeric() -> None: + assert "0123456789".isnumeric() + assert not "".isnumeric() + assert not "12a".isnumeric() + assert not "1.5".isnumeric() + assert "٠١٢".isnumeric() + assert "²½".isnumeric() + assert "Ⅷ三".isnumeric() + assert "123\U0001d7ce".isnumeric() + assert not "é".isnumeric() + +def test_isprintable() -> None: + assert "".isprintable() + assert " ".isprintable() + assert "Hello, World! 123 ~".isprintable() + assert not "\t".isprintable() + assert not "a\nb".isprintable() + assert not "\x00".isprintable() + assert not "\x7f".isprintable() + assert "été".isprintable() + assert not "\u00a0".isprintable() + assert not "\u2000".isprintable() + assert "\U0001f600".isprintable() + assert not "abc\U000e0001".isprintable() + +def test_str_subclass() -> None: + from strsubclass import S + s: str = S("abc") + assert s.isalpha() + assert s.isascii() + assert not s.isdecimal() + assert not s.isnumeric() + assert s.isprintable() + +[file strsubclass.py] +class S(str): + pass + [case testStrIndexEqLiteralSpecialize] from testutil import assertRaises