From c5d70ba48b77508cdfc2a5e08128db375cf618d2 Mon Sep 17 00:00:00 2001 From: xbe Date: Thu, 13 Mar 2014 00:29:15 -0700 Subject: Fix issues in str.count implementation. See pull request #343. --- py/objstr.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'py/objstr.c') diff --git a/py/objstr.c b/py/objstr.c index c5c7f87f6..6a2625b62 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -509,19 +509,14 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) { // needle won't exist in haystack if it's longer, so nothing to count if (needle_len > haystack_len) { - MP_OBJ_NEW_SMALL_INT(0); + MP_OBJ_NEW_SMALL_INT(0); } - for (machine_uint_t haystack_index = start; haystack_index <= end; haystack_index++) { - for (machine_uint_t needle_index = 0; needle_index < needle_len; needle_index++) { - if ((haystack_index + needle_len) > end) { - return MP_OBJ_NEW_SMALL_INT(num_occurrences); - } - if (haystack[haystack_index + needle_index] == needle[needle_index] && needle_index == (needle_len - 1)) { - num_occurrences++; - } - - } + for (machine_uint_t haystack_index = start; haystack_index + needle_len <= end; haystack_index++) { + if (memcmp(&haystack[haystack_index], needle, needle_len) == 0) { + num_occurrences++; + haystack_index += needle_len - 1; + } } return MP_OBJ_NEW_SMALL_INT(num_occurrences); -- cgit v1.2.3