summaryrefslogtreecommitdiffstatshomepage
path: root/py/sequence.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/sequence.c')
-rw-r--r--py/sequence.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/py/sequence.c b/py/sequence.c
index 8418f1ef67..c940d9f69f 100644
--- a/py/sequence.c
+++ b/py/sequence.c
@@ -51,15 +51,10 @@ void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void
}
}
-bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end) {
+bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
mp_obj_t ostart, ostop, ostep;
machine_int_t start, stop;
mp_obj_slice_get(slice, &ostart, &ostop, &ostep);
- 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"));
- return false;
- }
if (ostart == mp_const_none) {
start = 0;
@@ -96,11 +91,38 @@ bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_u
stop = start;
}
- *begin = start;
- *end = stop;
+ indexes->start = start;
+ indexes->stop = stop;
+
+ if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
+ indexes->step = MP_OBJ_SMALL_INT_VALUE(ostep);
+ return false;
+ }
+ indexes->step = 1;
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) {