Skip to content

Commit 6a5916b

Browse files
committed
Implement isspace method
1 parent 5cf4681 commit 6a5916b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/cstring.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ PyObject *cstring_isprintable(PyObject *self, PyObject *args) {
424424
Py_RETURN_TRUE;
425425
}
426426

427+
PyDoc_STRVAR(isspace__doc__, "");
428+
PyObject *cstring_isspace(PyObject *self, PyObject *args) {
429+
const char *p = CSTRING_VALUE(self);
430+
while(*p) {
431+
if(!isspace(*p))
432+
Py_RETURN_FALSE;
433+
++p;
434+
}
435+
return PyBool_FromLong(p != CSTRING_VALUE(self));
436+
}
437+
427438
PyDoc_STRVAR(rfind__doc__, "");
428439
PyObject *cstring_rfind(PyObject *self, PyObject *args) {
429440
struct _substr_params params;
@@ -511,7 +522,7 @@ static PyMethodDef cstring_methods[] = {
511522
{"islower", cstring_islower, METH_VARARGS, islower__doc__},
512523
/* TODO: isnumeric */
513524
{"isprintable", cstring_isprintable, METH_VARARGS, isprintable__doc__},
514-
/* TODO: isspace */
525+
{"isspace", cstring_isspace, METH_VARARGS, isspace__doc__},
515526
/* TODO: istitle */
516527
/* TODO: isupper */
517528
/* TODO: join */

test/test_methods.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ def test_isprintable_False():
108108
assert target.isprintable() == False
109109

110110

111+
def test_isspace_empty():
112+
target = cstring('')
113+
assert target.isspace() == False
114+
115+
116+
def test_isspace_True():
117+
target = cstring('\t\n ')
118+
assert target.isspace() == True
119+
120+
121+
def test_isspace_False():
122+
target = cstring('hello, world\n')
123+
assert target.isspace() == False
124+
125+
111126
def test_rfind():
112127
target = cstring('hello')
113128
assert target.rfind('o') == 4

0 commit comments

Comments
 (0)