aboutsummaryrefslogtreecommitdiff
path: root/py/sequence.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-25 22:12:56 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-25 22:12:56 +0300
commit5fd5af98d0292e06f42e0e0dc6ea8278219cdd6e (patch)
tree8a49fbbaf377757b1db76f6a33e70c68e5470389 /py/sequence.c
parentde4b9329f99794dc2025a7f9aa203811a156b3c4 (diff)
objlist: Implement support for arbitrary (3-arg) slices.
Diffstat (limited to 'py/sequence.c')
-rw-r--r--py/sequence.c23
1 files changed, 21 insertions, 2 deletions
diff --git a/py/sequence.c b/py/sequence.c
index db5eed4c8..c940d9f69 100644
--- a/py/sequence.c
+++ b/py/sequence.c
@@ -95,8 +95,6 @@ bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, mp_bound_
indexes->stop = stop;
if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
- nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
- "Only slices with step=1 (aka None) are supported"));
indexes->step = MP_OBJ_SMALL_INT_VALUE(ostep);
return false;
}
@@ -104,6 +102,27 @@ bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, mp_bound_
return true;
}
+mp_obj_t mp_seq_extract_slice(uint len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
+ machine_int_t start = indexes->start, stop = indexes->stop;
+ machine_int_t step = indexes->step;
+
+ mp_obj_t res = mp_obj_new_list(0, NULL);
+
+ if (step < 0) {
+ stop--;
+ while (start <= stop) {
+ mp_obj_list_append(res, seq[stop]);
+ stop += step;
+ }
+ } else {
+ while (start < stop) {
+ mp_obj_list_append(res, seq[start]);
+ start += step;
+ }
+ }
+ return res;
+}
+
// Special-case comparison function for sequences of bytes
// Don't pass MP_BINARY_OP_NOT_EQUAL here
bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2) {