Skip to content

Commit 3d9a39e

Browse files
committed
py: Implement str.[r]index() and add tests for them.
1 parent 729be9b commit 3d9a39e

4 files changed

Lines changed: 177 additions & 4 deletions

File tree

py/objstr.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
441441
return res;
442442
}
443443

444-
STATIC mp_obj_t str_finder(uint n_args, const mp_obj_t *args, machine_int_t direction) {
444+
STATIC mp_obj_t str_finder(uint n_args, const mp_obj_t *args, machine_int_t direction, bool is_index) {
445445
assert(2 <= n_args && n_args <= 4);
446446
assert(MP_OBJ_IS_STR(args[0]));
447447
assert(MP_OBJ_IS_STR(args[1]));
@@ -461,19 +461,31 @@ STATIC mp_obj_t str_finder(uint n_args, const mp_obj_t *args, machine_int_t dire
461461
const byte *p = find_subbytes(haystack + start, end - start, needle, needle_len, direction);
462462
if (p == NULL) {
463463
// not found
464-
return MP_OBJ_NEW_SMALL_INT(-1);
464+
if (is_index) {
465+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "substring not found"));
466+
} else {
467+
return MP_OBJ_NEW_SMALL_INT(-1);
468+
}
465469
} else {
466470
// found
467471
return MP_OBJ_NEW_SMALL_INT(p - haystack);
468472
}
469473
}
470474

471475
STATIC mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
472-
return str_finder(n_args, args, 1);
476+
return str_finder(n_args, args, 1, false);
473477
}
474478

475479
STATIC mp_obj_t str_rfind(uint n_args, const mp_obj_t *args) {
476-
return str_finder(n_args, args, -1);
480+
return str_finder(n_args, args, -1, false);
481+
}
482+
483+
STATIC mp_obj_t str_index(uint n_args, const mp_obj_t *args) {
484+
return str_finder(n_args, args, 1, true);
485+
}
486+
487+
STATIC mp_obj_t str_rindex(uint n_args, const mp_obj_t *args) {
488+
return str_finder(n_args, args, -1, true);
477489
}
478490

479491
// TODO: (Much) more variety in args
@@ -1307,6 +1319,8 @@ STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, in
13071319

13081320
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
13091321
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj, 2, 4, str_rfind);
1322+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_index_obj, 2, 4, str_index);
1323+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rindex_obj, 2, 4, str_rindex);
13101324
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
13111325
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
13121326
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
@@ -1320,6 +1334,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
13201334
STATIC const mp_map_elem_t str_locals_dict_table[] = {
13211335
{ MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj },
13221336
{ MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj },
1337+
{ MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&str_index_obj },
1338+
{ MP_OBJ_NEW_QSTR(MP_QSTR_rindex), (mp_obj_t)&str_rindex_obj },
13231339
{ MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj },
13241340
{ MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
13251341
{ MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Q(union)
181181
Q(update)
182182
Q(find)
183183
Q(rfind)
184+
Q(rindex)
184185
Q(split)
185186
Q(startswith)
186187
Q(replace)

tests/basics/string_index.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
print("hello world".index("ll"))
2+
print("hello world".index("ll", None))
3+
print("hello world".index("ll", 1))
4+
print("hello world".index("ll", 1, None))
5+
print("hello world".index("ll", None, None))
6+
print("hello world".index("ll", 1, -1))
7+
8+
try:
9+
print("hello world".index("ll", 1, 1))
10+
except ValueError:
11+
print("Raised ValueError")
12+
else:
13+
print("Did not raise ValueError")
14+
15+
try:
16+
print("hello world".index("ll", 1, 2))
17+
except ValueError:
18+
print("Raised ValueError")
19+
else:
20+
print("Did not raise ValueError")
21+
22+
try:
23+
print("hello world".index("ll", 1, 3))
24+
except ValueError:
25+
print("Raised ValueError")
26+
else:
27+
print("Did not raise ValueError")
28+
29+
print("hello world".index("ll", 1, 4))
30+
print("hello world".index("ll", 1, 5))
31+
print("hello world".index("ll", -100))
32+
print("0000".index('0'))
33+
print("0000".index('0', 0))
34+
print("0000".index('0', 1))
35+
print("0000".index('0', 2))
36+
print("0000".index('0', 3))
37+
38+
try:
39+
print("0000".index('0', 4))
40+
except ValueError:
41+
print("Raised ValueError")
42+
else:
43+
print("Did not raise ValueError")
44+
45+
try:
46+
print("0000".index('0', 5))
47+
except ValueError:
48+
print("Raised ValueError")
49+
else:
50+
print("Did not raise ValueError")
51+
52+
try:
53+
print("0000".index('-1', 3))
54+
except ValueError:
55+
print("Raised ValueError")
56+
else:
57+
print("Did not raise ValueError")
58+
59+
try:
60+
print("0000".index('1', 3))
61+
except ValueError:
62+
print("Raised ValueError")
63+
else:
64+
print("Did not raise ValueError")
65+
66+
try:
67+
print("0000".index('1', 4))
68+
except ValueError:
69+
print("Raised ValueError")
70+
else:
71+
print("Did not raise ValueError")
72+
73+
try:
74+
print("0000".index('1', 5))
75+
except ValueError:
76+
print("Raised ValueError")
77+
else:
78+
print("Did not raise ValueError")

tests/basics/string_rindex.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
print("hello world".rindex("ll"))
2+
print("hello world".rindex("ll", None))
3+
print("hello world".rindex("ll", 1))
4+
print("hello world".rindex("ll", 1, None))
5+
print("hello world".rindex("ll", None, None))
6+
print("hello world".rindex("ll", 1, -1))
7+
8+
try:
9+
print("hello world".rindex("ll", 1, 1))
10+
except ValueError:
11+
print("Raised ValueError")
12+
else:
13+
print("Did not raise ValueError")
14+
15+
try:
16+
print("hello world".rindex("ll", 1, 2))
17+
except ValueError:
18+
print("Raised ValueError")
19+
else:
20+
print("Did not raise ValueError")
21+
22+
try:
23+
print("hello world".rindex("ll", 1, 3))
24+
except ValueError:
25+
print("Raised ValueError")
26+
else:
27+
print("Did not raise ValueError")
28+
29+
print("hello world".rindex("ll", 1, 4))
30+
print("hello world".rindex("ll", 1, 5))
31+
print("hello world".rindex("ll", -100))
32+
print("0000".rindex('0'))
33+
print("0000".rindex('0', 0))
34+
print("0000".rindex('0', 1))
35+
print("0000".rindex('0', 2))
36+
print("0000".rindex('0', 3))
37+
38+
try:
39+
print("0000".rindex('0', 4))
40+
except ValueError:
41+
print("Raised ValueError")
42+
else:
43+
print("Did not raise ValueError")
44+
45+
try:
46+
print("0000".rindex('0', 5))
47+
except ValueError:
48+
print("Raised ValueError")
49+
else:
50+
print("Did not raise ValueError")
51+
52+
try:
53+
print("0000".rindex('-1', 3))
54+
except ValueError:
55+
print("Raised ValueError")
56+
else:
57+
print("Did not raise ValueError")
58+
59+
try:
60+
print("0000".rindex('1', 3))
61+
except ValueError:
62+
print("Raised ValueError")
63+
else:
64+
print("Did not raise ValueError")
65+
66+
try:
67+
print("0000".rindex('1', 4))
68+
except ValueError:
69+
print("Raised ValueError")
70+
else:
71+
print("Did not raise ValueError")
72+
73+
try:
74+
print("0000".rindex('1', 5))
75+
except ValueError:
76+
print("Raised ValueError")
77+
else:
78+
print("Did not raise ValueError")

0 commit comments

Comments
 (0)