aboutsummaryrefslogtreecommitdiff
path: root/py/sequence.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-02-10 06:37:11 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-02-10 07:13:32 +0200
commit0cd1dc06e673e86058eb14cdd7ae6622cb57fde5 (patch)
tree0a40e8da4e193922c108daaeb9f8dbd4a8beca4b /py/sequence.c
parent76f06de96dbdc4274eb059efe1ce2020f9921835 (diff)
Factor out mp_seq_index_obj() function to implement .index() on sequences.
Diffstat (limited to 'py/sequence.c')
-rw-r--r--py/sequence.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/py/sequence.c b/py/sequence.c
index b344ed00b..07c0531b1 100644
--- a/py/sequence.c
+++ b/py/sequence.c
@@ -140,3 +140,26 @@ bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *
return true;
}
+
+// Special-case of index() which searches for mp_obj_t
+mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args) {
+ mp_obj_type_t *type = mp_obj_get_type(args[0]);
+ mp_obj_t *value = args[1];
+ uint start = 0;
+ uint stop = len;
+
+ if (n_args >= 3) {
+ start = mp_get_index(type, len, args[2]);
+ if (n_args >= 4) {
+ stop = mp_get_index(type, len, args[3]);
+ }
+ }
+
+ for (uint i = start; i < stop; i++) {
+ if (mp_obj_equal(items[i], value)) {
+ return mp_obj_new_int_from_uint(i);
+ }
+ }
+
+ nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in sequence"));
+}