diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-02-14 18:55:16 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-02-14 18:57:11 +0200 |
commit | c38809e26b71c539584462c2d02b6cd2647ade5b (patch) | |
tree | 4af5fc1e0093d3559dbc442d9fa3cc9bc40108e8 /py/objarray.c | |
parent | 609a9c6b715b80b3b84ef875408e7e3b642c8c9b (diff) | |
download | micropython-c38809e26b71c539584462c2d02b6cd2647ade5b.tar.gz micropython-c38809e26b71c539584462c2d02b6cd2647ade5b.zip |
py/objarray: Implement "in" operator for bytearray.
Diffstat (limited to 'py/objarray.c')
-rw-r--r-- | py/objarray.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/py/objarray.c b/py/objarray.c index a17ae276e2..58285c8660 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -33,6 +33,7 @@ #include "py/runtime0.h" #include "py/runtime.h" #include "py/binary.h" +#include "py/objstr.h" #if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW @@ -283,6 +284,29 @@ STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) return lhs_in; } + case MP_BINARY_OP_IN: { + /* NOTE `a in b` is `b.__contains__(a)` */ + mp_buffer_info_t lhs_bufinfo; + mp_buffer_info_t rhs_bufinfo; + + // Can search string only in bytearray + if (mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) { + if (!MP_OBJ_IS_TYPE(lhs_in, &mp_type_bytearray)) { + return mp_const_false; + } + array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ); + return mp_obj_new_bool( + find_subbytes(lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len, 1) != NULL); + } + + // Otherwise, can only look for a scalar numeric value in an array + if (MP_OBJ_IS_INT(rhs_in) || mp_obj_is_float(rhs_in)) { + mp_not_implemented(""); + } + + return mp_const_false; + } + case MP_BINARY_OP_EQUAL: { mp_buffer_info_t lhs_bufinfo; mp_buffer_info_t rhs_bufinfo; |