Skip to content

Commit c33260c

Browse files
committed
Add str.isspace primitive
1 parent 00b5064 commit c33260c

6 files changed

Lines changed: 57 additions & 1 deletion

File tree

mypyc/lib-rt/CPy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start)
780780
Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end);
781781
CPyTagged CPyStr_Ord(PyObject *obj);
782782
PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count);
783-
783+
bool CPyStr_IsSpace(PyObject *str);
784784

785785
// Bytes operations
786786

mypyc/lib-rt/str_ops.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,17 @@ PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count) {
630630
}
631631
return PySequence_Repeat(str, temp_count);
632632
}
633+
634+
635+
bool CPyStr_IsSpace(PyObject *str) {
636+
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
637+
if (len == 0) return false;
638+
639+
int kind = PyUnicode_KIND(str);
640+
const void *data = PyUnicode_DATA(str);
641+
for (Py_ssize_t i = 0; i < len; i++) {
642+
if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i)))
643+
return false;
644+
}
645+
return true;
646+
}

mypyc/primitives/str_ops.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,14 @@
397397
error_kind=ERR_NEG_INT,
398398
)
399399

400+
method_op(
401+
name="isspace",
402+
arg_types=[str_rprimitive],
403+
return_type=bool_rprimitive,
404+
c_function_name="CPyStr_IsSpace",
405+
error_kind=ERR_NEVER,
406+
)
407+
400408
# obj.decode()
401409
method_op(
402410
name="decode",

mypyc/test-data/fixtures/ir.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def removeprefix(self, prefix: str, /) -> str: ...
131131
def removesuffix(self, suffix: str, /) -> str: ...
132132
def islower(self) -> bool: ...
133133
def count(self, substr: str, start: Optional[int] = None, end: Optional[int] = None) -> int: pass
134+
def isspace(self) -> bool: ...
134135

135136
class float:
136137
def __init__(self, x: object) -> None: pass

mypyc/test-data/irbuild-str.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,14 @@ def i_times_s(s, n):
972972
L0:
973973
r0 = CPyStr_Multiply(s, n)
974974
return r0
975+
976+
[case testStrIsSpace]
977+
def is_space(x: str) -> bool:
978+
return x.isspace()
979+
[out]
980+
def is_space(x):
981+
x :: str
982+
r0 :: bool
983+
L0:
984+
r0 = CPyStr_IsSpace(x)
985+
return r0

mypyc/test-data/run-strings.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,3 +1257,25 @@ FMT: Final = "{} {}"
12571257

12581258
def test_format() -> None:
12591259
assert FMT.format(400 + 20, "roll" + "up") == "420 rollup"
1260+
1261+
[case testIsSpace]
1262+
def test_isspace_basic() -> None:
1263+
assert " ".isspace()
1264+
assert "\t".isspace()
1265+
assert "\n".isspace()
1266+
assert "\r".isspace()
1267+
assert "\f".isspace()
1268+
assert "\v".isspace()
1269+
assert " \t\n".isspace()
1270+
assert not "".isspace()
1271+
assert not "a".isspace()
1272+
assert not " a".isspace()
1273+
assert not "a ".isspace()
1274+
assert not "hello".isspace()
1275+
1276+
def test_isspace_unicode() -> None:
1277+
assert "\u00A0".isspace()
1278+
assert "\u2000".isspace()
1279+
assert "\u200A".isspace()
1280+
assert "\u3000".isspace()
1281+
assert not "\u0041".isspace()

0 commit comments

Comments
 (0)