Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,17 @@ 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;

int kind = PyUnicode_KIND(str);
const void *data = PyUnicode_DATA(str);
for (Py_ssize_t i = 0; i < len; i++) {
if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i)))
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
22 changes: 22 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -1257,3 +1257,25 @@ FMT: Final = "{} {}"

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

[case testIsSpace]
def test_isspace_basic() -> None:
assert " ".isspace()
assert "\t".isspace()
assert "\n".isspace()
assert "\r".isspace()
assert "\f".isspace()
assert "\v".isspace()
assert " \t\n".isspace()
assert not "".isspace()
assert not "a".isspace()
assert not " a".isspace()
assert not "a ".isspace()
assert not "hello".isspace()

def test_isspace_unicode() -> None:
assert "\u00A0".isspace()
assert "\u2000".isspace()
assert "\u200A".isspace()
assert "\u3000".isspace()
assert not "\u0041".isspace()
Comment thread
VaggelisD marked this conversation as resolved.
Outdated