Skip to content
Merged
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
2 changes: 1 addition & 1 deletion mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start)
Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end);
CPyTagged CPyStr_Ord(PyObject *obj);
PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count);

bool CPyStr_IsSpace(PyObject *str);

// Bytes operations

Expand Down
24 changes: 24 additions & 0 deletions mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,27 @@ PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count) {
}
return PySequence_Repeat(str, temp_count);
}


bool CPyStr_IsSpace(PyObject *str) {
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
if (len == 0) return false;

if (PyUnicode_IS_ASCII(str)) {
const Py_UCS1 *data = PyUnicode_1BYTE_DATA(str);
for (Py_ssize_t i = 0; i < len; i++) {
if (!_Py_ascii_whitespace[data[i]])
return false;
}
return true;
}

int kind = PyUnicode_KIND(str);
const void *data = PyUnicode_DATA(str);
for (Py_ssize_t i = 0; i < len; i++) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (!Py_UNICODE_ISSPACE(ch))
return false;
}
return true;
}
8 changes: 8 additions & 0 deletions mypyc/primitives/str_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@
error_kind=ERR_NEG_INT,
)

method_op(
name="isspace",
arg_types=[str_rprimitive],
return_type=bool_rprimitive,
c_function_name="CPyStr_IsSpace",
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 @@ -131,6 +131,7 @@ def removeprefix(self, prefix: str, /) -> str: ...
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: ...

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 @@ -972,3 +972,14 @@ def i_times_s(s, n):
L0:
r0 = CPyStr_Multiply(s, n)
return r0

[case testStrIsSpace]
def is_space(x: str) -> bool:
return x.isspace()
[out]
def is_space(x):
x :: str
r0 :: bool
L0:
r0 = CPyStr_IsSpace(x)
return r0
12 changes: 12 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -1257,3 +1257,15 @@ FMT: Final = "{} {}"

def test_format() -> None:
assert FMT.format(400 + 20, "roll" + "up") == "420 rollup"

[case testIsSpace]
from typing import Any

def test_isspace() -> None:
# Verify correctness across all Unicode codepoints.
# Exercises UCS-1 (0x00-0xFF), UCS-2 (0x100-0xFFFF), and UCS-4 (0x10000-x10FFFF inclusive) string kinds.
# Any forces generic dispatch so we compare our primitive against stdlib's
for i in range(0x110000):
c = chr(i)
a: Any = c
assert c.isspace() == a.isspace()