Skip to content

Commit 0b944f2

Browse files
committed
Implement isalnum method
1 parent 96adc39 commit 0b944f2

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/cstring.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,17 @@ PyObject *cstring_index(PyObject *self, PyObject *args) {
359359
return PyLong_FromSsize_t(p - CSTRING_VALUE(self));
360360
}
361361

362+
PyDoc_STRVAR(isalnum__doc__, "");
363+
PyObject *cstring_isalnum(PyObject *self, PyObject *args) {
364+
const char *p = CSTRING_VALUE(self);
365+
while(*p) {
366+
if(!isalnum(*p))
367+
Py_RETURN_FALSE;
368+
++p;
369+
}
370+
Py_RETURN_TRUE;
371+
}
372+
362373
PyDoc_STRVAR(isalpha__doc__, "");
363374
PyObject *cstring_isalpha(PyObject *self, PyObject *args) {
364375
const char *p = CSTRING_VALUE(self);
@@ -459,7 +470,7 @@ static PyMethodDef cstring_methods[] = {
459470
/* TODO: format */
460471
/* TODO: format_map */
461472
{"index", cstring_index, METH_VARARGS, index__doc__},
462-
/* TODO: isalnum */
473+
{"isalnum", cstring_isalnum, METH_VARARGS, isalnum__doc__},
463474
{"isalpha", cstring_isalpha, METH_VARARGS, isalpha__doc__},
464475
/* TODO: isascii */
465476
/* TODO: isdecimal */

test/test_methods.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def test_index_missing():
5353
return target.index('lo', 0, 4)
5454

5555

56+
def test_isalnum_True():
57+
target = cstring('hello123')
58+
assert target.isalnum() == True
59+
60+
61+
def test_isalnum_False():
62+
target = cstring('hello_123')
63+
assert target.isalnum() == False
64+
65+
5666
def test_isalpha_True():
5767
target = cstring('hello')
5868
assert target.isalpha() == True

0 commit comments

Comments
 (0)