From 613a8e3edf078c284bd981426cc5a256eabb2323 Mon Sep 17 00:00:00 2001 From: xbe Date: Tue, 18 Mar 2014 00:06:29 -0700 Subject: [PATCH 1/3] Implement str.partition and add tests for it. --- py/objstr.c | 27 +++++++++++++++++++++++++++ tests/basics/string_partition.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/basics/string_partition.py diff --git a/py/objstr.c b/py/objstr.c index d660bf952b..03711debb2 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -520,6 +520,31 @@ 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) { + 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); + + if (sep_len == 0) { + nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator")); + } + + 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); + } + } + 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); +} + STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) { if (flags == BUFFER_READ) { GET_STR_DATA_LEN(self_in, str_data, str_len); @@ -542,6 +567,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition); STATIC const mp_method_t str_type_methods[] = { { "find", &str_find_obj }, @@ -552,6 +578,7 @@ STATIC const mp_method_t str_type_methods[] = { { "format", &str_format_obj }, { "replace", &str_replace_obj }, { "count", &str_count_obj }, + { "partition", &str_partition_obj }, { NULL, NULL }, // end-of-list sentinel }; diff --git a/tests/basics/string_partition.py b/tests/basics/string_partition.py new file mode 100644 index 0000000000..ad70d02509 --- /dev/null +++ b/tests/basics/string_partition.py @@ -0,0 +1,29 @@ +print("asdf".partition('g')) +print("asdf".partition('a')) +print("asdf".partition('s')) +print("asdf".partition('f')) +print("asdf".partition('d')) +print("asdf".partition('asd')) +print("asdf".partition('sdf')) +print("asdf".partition('as')) +print("asdf".partition('df')) +print("asdf".partition('asdf')) +print("asdf".partition('asdfa')) +print("asdf".partition('fasdf')) +print("asdf".partition('fasdfa')) +print("abba".partition('a')) +print("abba".partition('b')) + +try: + print("asdf".partition(1)) +except TypeError: + print("Raised TypeError") +else: + print("Did not raise TypeError") + +try: + print("asdf".partition('')) +except ValueError: + print("Raised ValueError") +else: + print("Did not raise ValueError") From 4504ea8007bbc97aef51ced20a9ff3f460cd7caf Mon Sep 17 00:00:00 2001 From: xbe Date: Wed, 19 Mar 2014 00:46:14 -0700 Subject: [PATCH 2/3] Implement str.rpartition and add tests for it. --- py/objstr.c | 36 +++++++++++++++++++++++++++++++ tests/basics/string_rpartition.py | 29 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 tests/basics/string_rpartition.py diff --git a/py/objstr.c b/py/objstr.c index 03711debb2..c719935789 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -545,6 +545,40 @@ STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) { return mp_obj_new_tuple(3, items); } +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))); + } + + 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 machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) { if (flags == BUFFER_READ) { 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); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count); STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition); STATIC const mp_method_t str_type_methods[] = { { "find", &str_find_obj }, @@ -579,6 +614,7 @@ STATIC const mp_method_t str_type_methods[] = { { "replace", &str_replace_obj }, { "count", &str_count_obj }, { "partition", &str_partition_obj }, + { "rpartition", &str_rpartition_obj }, { NULL, NULL }, // end-of-list sentinel }; diff --git a/tests/basics/string_rpartition.py b/tests/basics/string_rpartition.py new file mode 100644 index 0000000000..656121c94d --- /dev/null +++ b/tests/basics/string_rpartition.py @@ -0,0 +1,29 @@ +print("asdf".rpartition('g')) +print("asdf".rpartition('a')) +print("asdf".rpartition('s')) +print("asdf".rpartition('f')) +print("asdf".rpartition('d')) +print("asdf".rpartition('asd')) +print("asdf".rpartition('sdf')) +print("asdf".rpartition('as')) +print("asdf".rpartition('df')) +print("asdf".rpartition('asdf')) +print("asdf".rpartition('asdfa')) +print("asdf".rpartition('fasdf')) +print("asdf".rpartition('fasdfa')) +print("abba".rpartition('a')) +print("abba".rpartition('b')) + +try: + print("asdf".rpartition(1)) +except TypeError: + print("Raised TypeError") +else: + print("Did not raise TypeError") + +try: + print("asdf".rpartition('')) +except ValueError: + print("Raised ValueError") +else: + print("Did not raise ValueError") From 0a6894c24b0d760755253c10a59824c68a40701e Mon Sep 17 00:00:00 2001 From: xbe Date: Fri, 21 Mar 2014 01:12:26 -0700 Subject: [PATCH 3/3] str.(r)partition: factor out duplicate code. Switch str.rpartition to search from left to right. Factor the duplicate code into one helper function. --- py/objstr.c | 57 ++++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 38 deletions(-) diff --git a/py/objstr.c b/py/objstr.c index c719935789..c2b3f8d4cc 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -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) {