Skip to content

Commit 4504ea8

Browse files
committed
Implement str.rpartition and add tests for it.
1 parent 613a8e3 commit 4504ea8

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

py/objstr.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,40 @@ STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
545545
return mp_obj_new_tuple(3, items);
546546
}
547547

548+
STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
549+
assert(MP_OBJ_IS_STR(self_in));
550+
if (!MP_OBJ_IS_STR(arg)) {
551+
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
552+
"Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
553+
}
554+
555+
GET_STR_DATA_LEN(self_in, str, str_len);
556+
GET_STR_DATA_LEN(arg, sep, sep_len);
557+
558+
if (sep_len == 0) {
559+
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
560+
}
561+
562+
if (sep_len > str_len) {
563+
goto not_found;
564+
}
565+
566+
for (machine_uint_t str_index = str_len; ; str_index--) {
567+
if (memcmp(&str[str_index - sep_len], sep, sep_len) == 0) {
568+
mp_obj_t items[] = {mp_obj_new_str(str, str_index - sep_len, false), arg,
569+
mp_obj_new_str(str + str_index, str_len - str_index, false)};
570+
return mp_obj_new_tuple(3, items);
571+
}
572+
if (str_index - sep_len == 0) {
573+
break;
574+
}
575+
}
576+
577+
not_found: ;
578+
mp_obj_t items[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), mp_obj_new_str(str, str_len, false)};
579+
return mp_obj_new_tuple(3, items);
580+
}
581+
548582
STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {
549583
if (flags == BUFFER_READ) {
550584
GET_STR_DATA_LEN(self_in, str_data, str_len);
@@ -568,6 +602,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
568602
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
569603
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
570604
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
605+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
571606

572607
STATIC const mp_method_t str_type_methods[] = {
573608
{ "find", &str_find_obj },
@@ -579,6 +614,7 @@ STATIC const mp_method_t str_type_methods[] = {
579614
{ "replace", &str_replace_obj },
580615
{ "count", &str_count_obj },
581616
{ "partition", &str_partition_obj },
617+
{ "rpartition", &str_rpartition_obj },
582618
{ NULL, NULL }, // end-of-list sentinel
583619
};
584620

tests/basics/string_rpartition.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
print("asdf".rpartition('g'))
2+
print("asdf".rpartition('a'))
3+
print("asdf".rpartition('s'))
4+
print("asdf".rpartition('f'))
5+
print("asdf".rpartition('d'))
6+
print("asdf".rpartition('asd'))
7+
print("asdf".rpartition('sdf'))
8+
print("asdf".rpartition('as'))
9+
print("asdf".rpartition('df'))
10+
print("asdf".rpartition('asdf'))
11+
print("asdf".rpartition('asdfa'))
12+
print("asdf".rpartition('fasdf'))
13+
print("asdf".rpartition('fasdfa'))
14+
print("abba".rpartition('a'))
15+
print("abba".rpartition('b'))
16+
17+
try:
18+
print("asdf".rpartition(1))
19+
except TypeError:
20+
print("Raised TypeError")
21+
else:
22+
print("Did not raise TypeError")
23+
24+
try:
25+
print("asdf".rpartition(''))
26+
except ValueError:
27+
print("Raised ValueError")
28+
else:
29+
print("Did not raise ValueError")

0 commit comments

Comments
 (0)