Skip to content

Commit 0a6894c

Browse files
committed
str.(r)partition: factor out duplicate code.
Switch str.rpartition to search from left to right. Factor the duplicate code into one helper function.
1 parent 4504ea8 commit 0a6894c

1 file changed

Lines changed: 19 additions & 38 deletions

File tree

py/objstr.c

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -520,63 +520,44 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
520520
return MP_OBJ_NEW_SMALL_INT(num_occurrences);
521521
}
522522

523-
STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
523+
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, bool rpartition) {
524524
assert(MP_OBJ_IS_STR(self_in));
525525
if (!MP_OBJ_IS_STR(arg)) {
526526
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
527527
"Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
528528
}
529-
530529
GET_STR_DATA_LEN(self_in, str, str_len);
531530
GET_STR_DATA_LEN(arg, sep, sep_len);
531+
mp_obj_t result[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
532532

533533
if (sep_len == 0) {
534534
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
535535
}
536+
if (rpartition) {
537+
result[2] = mp_obj_new_str(str, str_len, false);
538+
} else {
539+
result[0] = mp_obj_new_str(str, str_len, false);
540+
}
536541

537542
for (machine_uint_t str_index = 0; str_index + sep_len <= str_len; str_index++) {
538543
if (memcmp(&str[str_index], sep, sep_len) == 0) {
539-
mp_obj_t items[] = {mp_obj_new_str(str, str_index, false), arg,
540-
mp_obj_new_str(str + str_index + sep_len, str_len - str_index - sep_len, false)};
541-
return mp_obj_new_tuple(3, items);
544+
result[0] = mp_obj_new_str(str, str_index, false);
545+
result[1] = arg;
546+
result[2] = mp_obj_new_str(str + str_index + sep_len, str_len - str_index - sep_len, false);
547+
if (!rpartition) {
548+
break;
549+
}
542550
}
543551
}
544-
mp_obj_t items[] = {mp_obj_new_str(str, str_len, false), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
545-
return mp_obj_new_tuple(3, items);
552+
return mp_obj_new_tuple(3, result);
546553
}
547554

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-
}
555+
STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg, bool partition) {
556+
return str_partitioner(self_in, arg, false);
557+
}
576558

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);
559+
STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg, bool partition) {
560+
return str_partitioner(self_in, arg, true);
580561
}
581562

582563
STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {

0 commit comments

Comments
 (0)