8000 Implement index() method · atpalmer-python/python-cstring@1e01714 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e01714

Browse files
committed
Implement index() method
1 parent 6e02b0d commit 1e01714

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Alternate string representation to the built-in `str` type.
1010

1111
## Methods
1212

13+
1314
### count(substring [,start [,end]])
1415

1516
See: https://docs.python.org/3/library/stdtypes.html#str.count
@@ -19,6 +20,7 @@ Notes:
1920
* `substring` may be a `cstring` or Python `str` object.
2021
* `start` and `end`, if provided, are _byte_ indexes.
2122

23+
2224
### find(substring [,start [,end]])
2325

2426
See: https://docs.python.org/3/library/stdtypes.html#str.find
@@ -28,6 +30,17 @@ Notes:
2830
* `substring` may be a `cstring` or Python `str` object.
2931
* `start` and `end`, if provided, are _byte_ indexes.
3032

33+
34+
### index(substring [,start [,end]])
35+
36+
See: https://docs.python.org/3/library/stdtypes.html#str.index
37+
38+
Notes:
39+
40+
* `substring` may be a `cstring` or Python `str` object.
41+
* `start` and `end`, if provided, are _byte_ indexes.
42+
43+
3144
## TODO
3245

3346
* Write docs (see `str` type docs)

src/cstring.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,34 @@ PyObject *cstring_find(PyObject *self, PyObject *args) {
262262

263263
char *p = strstr(params.start, params.substr);
264264
if(!p)
265-
return PyLong_FromLong(-1);
265+
goto err;
266266
if(p + params.substr_len > params.end)
267-
return PyLong_FromLong(-1);
267+
goto err;
268268

269269
return PyLong_FromSsize_t(p - CSTRING_VALUE(self));
270+
271< 10000 /td>+
err:
272+
return PyLong_FromLong(-1);
273+
}
274+
275+
PyDoc_STRVAR(index__doc__, "");
276+
PyObject *cstring_index(PyObject *self, PyObject *args) {
277+
struct _substr_params params;
278+
279+
if(!_parse_substr_args(self, args, &params))
280+
return NULL;
281+
282+
char *p = strstr(params.start, params.substr);
283+
if(!p)
284+
goto err;
285+
if(p + params.substr_len > params.end)
286+
goto err;
287+
288+
return PyLong_FromSsize_t(p - CSTRING_VALUE(self));
289+
290+
err:
291+
PyErr_SetString(PyExc_ValueError, "substring not found");
292+
return NULL;
270293
}
271294

272295
static PySequenceMethods cstring_as_sequence = {
@@ -285,6 +308,7 @@ static PyMappingMethods cstring_as_mapping = {
285308
static PyMethodDef cstring_methods[] = {
286309
{"count", cstring_count, METH_VARARGS, count__doc__},
287310
{"find", cstring_find, METH_VARARGS, find__doc__},
311+
{"index", cstring_index, METH_VARARGS, index__doc__},
288312
{0},
289313
};
290314

test/test_methods.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from cstring import cstring
23

34

@@ -35,3 +36,19 @@ def test_find_missing():
3536
target = cstring('hello')
3637
assert target.find('lo', 0, 4) == -1
3738

39+
40+
def test_index():
41+
target = cstring('hello')
42+
assert target.index('lo') == 3
43+
44+
45+
def test_index_with_start():
46+
target = cstring('hello')
47+
assert target.index('lo', 3) == 3
48+
49+
50+
def test_index_missing():
51+
target = cstring('hello')
52+
with pytest.raises(ValueError):
53+
return target.index('lo', 0, 4)
54+

0 commit comments

Comments
 (0)
0