Skip to content

Commit 24c4e5c

Browse files
committed
Implement lower method
1 parent 6db73c9 commit 24c4e5c

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/cstring.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,18 @@ PyObject *cstring_isupper(PyObject *self, PyObject *args) {
456456
Py_RETURN_FALSE;
457457
}
458458

459+
PyDoc_STRVAR(lower__doc__, "");
460+
PyObject *cstring_lower(PyObject *self, PyObject *args) {
461+
struct cstring *new = CSTRING_ALLOC(Py_TYPE(self), Py_SIZE(self));
462+
const char *s = CSTRING_VALUE(self);
463+
char *d = CSTRING_VALUE(new);
464+
465+
while((*d++ = tolower(*s++)) != '\0')
466+
;
467+
468+
return (PyObject *)new;
469+
}
470+
459471
PyDoc_STRVAR(rfind__doc__, "");
460472
PyObject *cstring_rfind(PyObject *self, PyObject *args) {
461473
struct _substr_params params;
@@ -548,7 +560,7 @@ static PyMethodDef cstring_methods[] = {
548560
{"isupper", cstring_isupper, METH_VARARGS, isupper__doc__},
549561
/* TODO: join */
550562
/* TODO: ljust */
551-
/* TODO: lower */
563+
{"lower", cstring_lower, METH_VARARGS, lower__doc__},
552564
/* TODO: lstrip */
553565
/* TODO: maketrans */
554566
/* TODO: partition */

test/test_methods.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ def test_isupper_alnum_lc():
138138
assert target.isupper() == False
139139

140140

141+
def test_lower():
142+
target = cstring('HELLO123')
143+
assert target.lower() == cstring('hello123')
144+
145+
141146
def test_rfind():
142147
target = cstring('hello')
143148
assert target.rfind('o') == 4

0 commit comments

Comments
 (0)