diff --git a/py/obj.h b/py/obj.h index 85462b3a8b..f372030b7e 100644 --- a/py/obj.h +++ b/py/obj.h @@ -400,3 +400,4 @@ void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end); #define m_seq_copy(dest, src, len, item_sz) memcpy(dest, src, len * sizeof(item_sz)) bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2); +bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2); diff --git a/py/objlist.c b/py/objlist.c index aa857e41c2..29d0c92c52 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -75,51 +75,8 @@ static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) { } mp_obj_list_t *self = self_in; mp_obj_list_t *another = another_in; - if (op == RT_BINARY_OP_EQUAL && self->len != another->len) { - return false; - } - - // Let's deal only with > & >= - if (op == RT_BINARY_OP_LESS || op == RT_BINARY_OP_LESS_EQUAL) { - mp_obj_t t = self; - self = another; - another = t; - if (op == RT_BINARY_OP_LESS) { - op = RT_BINARY_OP_MORE; - } else { - op = RT_BINARY_OP_MORE_EQUAL; - } - } - - int len = self->len < another->len ? self->len : another->len; - bool eq_status = true; // empty lists are equal - bool rel_status; - for (int i = 0; i < len; i++) { - eq_status = mp_obj_equal(self->items[i], another->items[i]); - if (op == RT_BINARY_OP_EQUAL && !eq_status) { - return false; - } - rel_status = (rt_binary_op(op, self->items[i], another->items[i]) == mp_const_true); - if (!eq_status && !rel_status) { - return false; - } - } - - // If we had tie in the last element... - if (eq_status) { - // ... and we have lists of different lengths... - if (self->len != another->len) { - if (self->len < another->len) { - // ... then longer list length wins (we deal only with >) - return false; - } - } else if (op == RT_BINARY_OP_MORE) { - // Otherwise, if we have strict relation, equality means failure - return false; - } - } - return true; + return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len); } static mp_obj_t list_unary_op(int op, mp_obj_t self_in) { diff --git a/py/sequence.c b/py/sequence.c index 74b4fcfdf8..b344ed00b4 100644 --- a/py/sequence.c +++ b/py/sequence.c @@ -91,3 +91,52 @@ bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, u } return true; } + +// Special-case comparison function for sequences of mp_obj_t +// Don't pass RT_BINARY_OP_NOT_EQUAL here +bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2) { + if (op == RT_BINARY_OP_EQUAL && len1 != len2) { + return false; + } + + // Let's deal only with > & >= + if (op == RT_BINARY_OP_LESS || op == RT_BINARY_OP_LESS_EQUAL) { + SWAP(const mp_obj_t *, items1, items2); + SWAP(uint, len1, len2); + if (op == RT_BINARY_OP_LESS) { + op = RT_BINARY_OP_MORE; + } else { + op = RT_BINARY_OP_MORE_EQUAL; + } + } + + int len = len1 < len2 ? len1 : len2; + bool eq_status = true; // empty lists are equal + bool rel_status; + for (int i = 0; i < len; i++) { + eq_status = mp_obj_equal(items1[i], items2[i]); + if (op == RT_BINARY_OP_EQUAL && !eq_status) { + return false; + } + rel_status = (rt_binary_op(op, items1[i], items2[i]) == mp_const_true); + if (!eq_status && !rel_status) { + return false; + } + } + + // If we had tie in the last element... + if (eq_status) { + // ... and we have lists of different lengths... + if (len1 != len2) { + if (len1 < len2) { + // ... then longer list length wins (we deal only with >) + return false; + } + } else if (op == RT_BINARY_OP_MORE) { + // Otherwise, if we have strict relation, equality means failure + return false; + } + } + + return true; +}