Skip to content

Commit 0deeb44

Browse files
committed
Implement contains (in)
1 parent 1ed5cd2 commit 0deeb44

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/cstring.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,20 @@ static PyObject *cstring_item(PyObject *self, Py_ssize_t i) {
8888
return _cstring_new(Py_TYPE(self), &CSTRING_VALUE(self)[i], 1);
8989
}
9090

91+
static int cstring_contains(PyObject *self, PyObject *arg) {
92+
if(!_ensure_cstring(arg))
93+
return -1;
94+
if(strstr(CSTRING_VALUE(self), CSTRING_VALUE(arg)))
95+
return 1;
96+
return 0;
97+
}
98+
9199
static PySequenceMethods cstring_as_sequence = {
92100
.sq_length = cstring_len,
93101
.sq_concat = cstring_concat,
94102
.sq_repeat = cstring_repeat,
95103
.sq_item = cstring_item,
104+
.sq_contains = cstring_contains,
96105
};
97106

98107
static PyTypeObject cstring_type = {

test/test_cstring.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ def test_item_ok_pos():
5656
assert isinstance(result, cstring)
5757
assert str(result) == str(cstring('o')) # TODO: implement cstring equality!
5858

59+
60+
def test_contains_True():
61+
assert cstring('ell') in cstring('hello')
62+
63+
64+
def test_contains_False():
65+
assert cstring('hello') not in cstring('world')
66+

0 commit comments

Comments
 (0)