diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-13 21:53:36 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-13 21:53:36 +0000 |
commit | de4d7aecc8d2a47b4a07eb29047a3bf3ac021115 (patch) | |
tree | 02c79e39de9d3433d7c796887fb9d97040bb49dc /py/obj.c | |
parent | aae40ee6444db616a994c5827e5007fe19895f8d (diff) | |
parent | c5d70ba48b77508cdfc2a5e08128db375cf618d2 (diff) |
Merge pull request #343 from xbe/master
Implement str.count and add tests for it.
Diffstat (limited to 'py/obj.c')
-rw-r--r-- | py/obj.c | 26 |
1 files changed, 19 insertions, 7 deletions
@@ -218,20 +218,32 @@ mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) { } } -uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) { - // TODO False and True are considered 0 and 1 for indexing purposes +// is_slice determines whether the index is a slice index +uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice) { + int i; if (MP_OBJ_IS_SMALL_INT(index)) { - int i = MP_OBJ_SMALL_INT_VALUE(index); + i = MP_OBJ_SMALL_INT_VALUE(index); + } else if (MP_OBJ_IS_TYPE(index, &bool_type)) { + i = (index == mp_const_true ? 1 : 0); + } else { + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s indices must be integers, not %s", qstr_str(type->name), mp_obj_get_type_str(index))); + } + + if (i < 0) { + i += len; + } + if (is_slice) { if (i < 0) { - i += len; + i = 0; + } else if (i > len) { + i = len; } + } else { if (i < 0 || i >= len) { nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "%s index out of range", qstr_str(type->name))); } - return i; - } else { - nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s indices must be integers, not %s", qstr_str(type->name), mp_obj_get_type_str(index))); } + return i; } // may return MP_OBJ_NULL |