Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypyc/doc/str_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Methods
* ``s1.find(s2: str)``
* ``s1.find(s2: str, start: int)``
* ``s1.find(s2: str, start: int, end: int)``
* ``s.isdigit()``
* ``s.join(x: Iterable)``
* ``s.lstrip()``
* ``s.lstrip(chars: str)``
Expand Down
1 change: 1 addition & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged st
CPyTagged CPyStr_Ord(PyObject *obj);
PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count);
bool CPyStr_IsSpace(PyObject *str);
bool CPyStr_IsDigit(PyObject *str);

// Bytes operations

Expand Down
38 changes: 38 additions & 0 deletions mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,41 @@ bool CPyStr_IsSpace(PyObject *str) {
}
return true;
}


bool CPyStr_IsDigit(PyObject *str) {
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a recurring pattern for these primitives, should we try to abstract their codegen?

Gave macros a shot for to hide the per-kind for loop, though we could go a step further and do the same for entire functions I guess.

if (len == 0) return false;

#define CHECK_ISDIGIT(TYPE, DATA, CHECK) \
{ \
const TYPE *data = (const TYPE *)(DATA); \
for (Py_ssize_t i = 0; i < len; i++) { \
if (!CHECK(data[i])) \
return false; \
} \
}

// ASCII fast path
if (PyUnicode_IS_ASCII(str)) {
CHECK_ISDIGIT(Py_UCS1, PyUnicode_1BYTE_DATA(str), Py_ISDIGIT);
return true;
}

switch (PyUnicode_KIND(str)) {
case PyUnicode_1BYTE_KIND:
CHECK_ISDIGIT(Py_UCS1, PyUnicode_1BYTE_DATA(str), Py_UNICODE_ISDIGIT);
break;
case PyUnicode_2BYTE_KIND:
CHECK_ISDIGIT(Py_UCS2, PyUnicode_2BYTE_DATA(str), Py_UNICODE_ISDIGIT);
break;
case PyUnicode_4BYTE_KIND:
CHECK_ISDIGIT(Py_UCS4, PyUnicode_4BYTE_DATA(str), Py_UNICODE_ISDIGIT);
break;
default:
Py_UNREACHABLE();
}
return true;

#undef CHECK_ISDIGIT
}
8 changes: 8 additions & 0 deletions mypyc/primitives/str_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@
error_kind=ERR_NEVER,
)

method_op(
name="isdigit",
arg_types=[str_rprimitive],
return_type=bool_rprimitive,
c_function_name="CPyStr_IsDigit",
error_kind=ERR_NEVER,
)

# obj.decode()
method_op(
name="decode",
Expand Down
1 change: 1 addition & 0 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def removesuffix(self, suffix: str, /) -> str: ...
def islower(self) -> bool: ...
def count(self, substr: str, start: Optional[int] = None, end: Optional[int] = None) -> int: pass
def isspace(self) -> bool: ...
def isdigit(self) -> bool: ...

class float:
def __init__(self, x: object) -> None: pass
Expand Down
11 changes: 11 additions & 0 deletions mypyc/test-data/irbuild-str.test
Original file line number Diff line number Diff line change
Expand Up @@ -983,3 +983,14 @@ def is_space(x):
L0:
r0 = CPyStr_IsSpace(x)
return r0

[case testStrIsDigit]
def is_digit(x: str) -> bool:
return x.isdigit()
[out]
def is_digit(x):
x :: str
r0 :: bool
L0:
r0 = CPyStr_IsDigit(x)
return r0
34 changes: 34 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -1269,3 +1269,37 @@ def test_isspace() -> None:
c = chr(i)
a: Any = c
assert c.isspace() == a.isspace()

[case testIsDigit]
from typing import Any

def test_isdigit() -> None:
for i in range(0x110000):
c = chr(i)
a: Any = c
assert c.isdigit() == a.isdigit()

def test_isdigit_strings() -> None:
# ASCII digits
assert "0123456789".isdigit()
assert not "".isdigit()
assert not " ".isdigit()
assert not "a".isdigit()
assert not "abc".isdigit()
assert not "!@#".isdigit()

# Mixed ASCII
assert not "123abc".isdigit()
assert not "abc123".isdigit()
assert not "12 34".isdigit()
assert not "123!".isdigit()

# Unicode digits
assert "\u0660\u0661\u0662".isdigit()
assert "\u00b2\u00b3".isdigit()
assert "123\U0001d7ce\U0001d7cf\U0001d7d0".isdigit()

# Mixed digits and Unicode non-digits
assert not "\u00e9\u00e8".isdigit()
assert not "123\u00e9".isdigit()
assert not "\U0001d7ce!".isdigit()
Loading