str.(r)partition: factor out duplicate code.

Switch str.rpartition to search from left to right. Factor the
duplicate code into one helper function.
This commit is contained in:
xbe 2014-03-21 01:12:26 -07:00
parent 4504ea8007
commit 0a6894c24b
1 changed files with 19 additions and 38 deletions

View File

@ -520,63 +520,44 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
return MP_OBJ_NEW_SMALL_INT(num_occurrences);
}
STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, bool rpartition) {
assert(MP_OBJ_IS_STR(self_in));
if (!MP_OBJ_IS_STR(arg)) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
}
GET_STR_DATA_LEN(self_in, str, str_len);
GET_STR_DATA_LEN(arg, sep, sep_len);
mp_obj_t result[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
if (sep_len == 0) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
}
if (rpartition) {
result[2] = mp_obj_new_str(str, str_len, false);
} else {
result[0] = mp_obj_new_str(str, str_len, false);
}
for (machine_uint_t str_index = 0; str_index + sep_len <= str_len; str_index++) {
if (memcmp(&str[str_index], sep, sep_len) == 0) {
mp_obj_t items[] = {mp_obj_new_str(str, str_index, false), arg,
mp_obj_new_str(str + str_index + sep_len, str_len - str_index - sep_len, false)};
return mp_obj_new_tuple(3, items);
result[0] = mp_obj_new_str(str, str_index, false);
result[1] = arg;
result[2] = mp_obj_new_str(str + str_index + sep_len, str_len - str_index - sep_len, false);
if (!rpartition) {
break;
}
}
}
mp_obj_t items[] = {mp_obj_new_str(str, str_len, false), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
return mp_obj_new_tuple(3, items);
return mp_obj_new_tuple(3, result);
}
STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
assert(MP_OBJ_IS_STR(self_in));
if (!MP_OBJ_IS_STR(arg)) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
}
STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg, bool partition) {
return str_partitioner(self_in, arg, false);
}
GET_STR_DATA_LEN(self_in, str, str_len);
GET_STR_DATA_LEN(arg, sep, sep_len);
if (sep_len == 0) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
}
if (sep_len > str_len) {
goto not_found;
}
for (machine_uint_t str_index = str_len; ; str_index--) {
if (memcmp(&str[str_index - sep_len], sep, sep_len) == 0) {
mp_obj_t items[] = {mp_obj_new_str(str, str_index - sep_len, false), arg,
mp_obj_new_str(str + str_index, str_len - str_index, false)};
return mp_obj_new_tuple(3, items);
}
if (str_index - sep_len == 0) {
break;
}
}
not_found: ;
mp_obj_t items[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), mp_obj_new_str(str, str_len, false)};
return mp_obj_new_tuple(3, items);
STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg, bool partition) {
return str_partitioner(self_in, arg, true);
}
STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {