Skip to content

Commit 6db73c9

Browse files
committed
Implement isupper method
1 parent 6a5916b commit 6db73c9

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/cstring.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,27 @@ PyObject *cstring_isspace(PyObject *self, PyObject *args) {
435435
return PyBool_FromLong(p != CSTRING_VALUE(self));
436436
}
437437

438+
PyDoc_STRVAR(isupper__doc__, "");
439+
PyObject *cstring_isupper(PyObject *self, PyObject *args) {
440+
const char *p = CSTRING_VALUE(self);
441+
while(*p) {
442+
if(isalpha(*p)) {
443+
if(!isupper(*p))
444+
Py_RETURN_FALSE;
445+
++p;
446+
while(*p) {
447+
if(isalpha(*p) && !isupper(*p))
448+
Py_RETURN_FALSE;
449+
++p;
450+
}
451+
/* at least one uc alpha and no lc alphas */
452+
Py_RETURN_TRUE;
453+
}
454+
++p;
455+
}
456+
Py_RETURN_FALSE;
457+
}
458+
438459
PyDoc_STRVAR(rfind__doc__, "");
439460
PyObject *cstring_rfind(PyObject *self, PyObject *args) {
440461
struct _substr_params params;
@@ -524,7 +545,7 @@ static PyMethodDef cstring_methods[] = {
524545
{"isprintable", cstring_isprintable, METH_VARARGS, isprintable__doc__},
525546
{"isspace", cstring_isspace, METH_VARARGS, isspace__doc__},
526547
/* TODO: istitle */
527-
/* TODO: isupper */
548+
{"isupper", cstring_isupper, METH_VARARGS, isupper__doc__},
528549
/* TODO: join */
529550
/* TODO: ljust */
530551
/* TODO: lower */

test/test_methods.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ def test_isspace_False():
123123
assert target.isspace() == False
124124

125125

126+
def test_isupper_numeric():
127+
target = cstring('123')
128+
assert target.isupper() == False
129+
130+
131+
def test_isupper_alnum_uc():
132+
target = cstring('HELLO123')
133+
assert target.isupper() == True
134+
135+
136+
def test_isupper_alnum_lc():
137+
target = cstring('HELLo123')
138+
assert target.isupper() == False
139+
140+
126141
def test_rfind():
127142
target = cstring('hello')
128143
assert target.rfind('o') == 4

0 commit comments

Comments
 (0)