diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-13 22:07:55 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-13 22:07:55 +0000 |
commit | 536dde254be99e19700a0934af38b913256475e3 (patch) | |
tree | 3bfccae64e56c592a0a74150e0b907fe0d991acf /py/objstr.c | |
parent | de4d7aecc8d2a47b4a07eb29047a3bf3ac021115 (diff) |
py: In string.count, handle case of zero-length needle.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/py/objstr.c b/py/objstr.c index 6a2625b62..64ba6c5fa 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -495,8 +495,8 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) { GET_STR_DATA_LEN(args[0], haystack, haystack_len); GET_STR_DATA_LEN(args[1], needle, needle_len); - size_t start = 0; - size_t end = haystack_len; + machine_uint_t start = 0; + machine_uint_t end = haystack_len; /* TODO use a non-exception-throwing mp_get_index */ if (n_args >= 3 && args[2] != mp_const_none) { start = mp_get_index(&str_type, haystack_len, args[2], true); @@ -505,13 +505,13 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) { end = mp_get_index(&str_type, haystack_len, args[3], true); } - machine_int_t num_occurrences = 0; - - // 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); + // if needle_len is zero then we count each gap between characters as an occurrence + if (needle_len == 0) { + return MP_OBJ_NEW_SMALL_INT(end - start + 1); } + // count the occurrences + machine_int_t num_occurrences = 0; 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++; |