diff options
author | xbe <xbe@machine> | 2014-03-13 00:29:15 -0700 |
---|---|---|
committer | xbe <xbe@machine> | 2014-03-13 00:33:07 -0700 |
commit | c5d70ba48b77508cdfc2a5e08128db375cf618d2 (patch) | |
tree | 04aa750c5aeaa4ec3a1041c0c003341a3aeb7661 /py/objstr.c | |
parent | 9e1e8cd6428e875eb29be98124ee3b1ba2bace30 (diff) | |
download | micropython-c5d70ba48b77508cdfc2a5e08128db375cf618d2.tar.gz micropython-c5d70ba48b77508cdfc2a5e08128db375cf618d2.zip |
Fix issues in str.count implementation.
See pull request #343.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/py/objstr.c b/py/objstr.c index c5c7f87f6d..6a2625b621 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); |