diff options
author | Damien George <damien.p.george@gmail.com> | 2014-01-05 15:55:55 -0800 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-01-05 15:55:55 -0800 |
commit | 17f4497d6d16eafb00aa4ca62e9ab24ec6cf6775 (patch) | |
tree | 875c1f70bad22d610ad0ed5a10f55ce08d8d51f3 | |
parent | 73595feb75d36501a5b1fdea979357b2b6863fc2 (diff) | |
parent | c553162ebcc01765fd1556731ec98067f3981cd5 (diff) | |
download | micropython-17f4497d6d16eafb00aa4ca62e9ab24ec6cf6775.tar.gz micropython-17f4497d6d16eafb00aa4ca62e9ab24ec6cf6775.zip |
Merge pull request #91 from chipaca/list_index
Fix off-by-one in non-default values of index's 2nd and 3rd arguments.
-rw-r--r-- | py/objlist.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/py/objlist.c b/py/objlist.c index a656a4aeb6..e416f7fed2 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -182,13 +182,17 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) { assert(MP_OBJ_IS_TYPE(args[0], &list_type)); mp_obj_list_t *self = args[0]; mp_obj_t *value = args[1]; + uint start = 0; + uint stop = self->len; - uint start = mp_get_index(self->base.type, self->len, - n_args >= 3 ? args[2] : mp_obj_new_int(0)); - uint stop = mp_get_index(self->base.type, self->len, - n_args >= 4 ? args[3] : mp_obj_new_int(-1)); + if (n_args >= 3) { + start = mp_get_index(self->base.type, self->len, args[2]); + if (n_args >= 4) { + stop = mp_get_index(self->base.type, self->len, args[3]); + } + } - for (uint i = start; i <= stop; i++) { + for (uint i = start; i < stop; i++) { if (mp_obj_equal(self->items[i], value)) { return mp_obj_new_int(i); } |