summaryrefslogtreecommitdiff
path: root/py/sequence.c
diff options
context:
space:
mode:
authorNicko van Someren <nicko@nicko.org>2019-11-16 17:07:11 -0700
committerDamien George <damien.p.george@gmail.com>2019-12-28 23:55:15 +1100
commit4c93955b7b4d3d860aed1551ca6231ac4e388e69 (patch)
tree33d4e13a6076d847f98da761febeb5f745e94a3c /py/sequence.c
parent007a704d82f07d1482dae6f265fecf5710266767 (diff)
py/objslice: Add support for indices() method on slice objects.
Instances of the slice class are passed to __getitem__() on objects when the user indexes them with a slice. In practice the majority of the time (other than passing it on untouched) is to work out what the slice means in the context of an array dimension of a particular length. Since Python 2.3 there has been a method on the slice class, indices(), that takes a dimension length and returns the real start, stop and step, accounting for missing or negative values in the slice spec. This commit implements such a indices() method on the slice class. It is configurable at compile-time via MICROPY_PY_BUILTINS_SLICE_INDICES, disabled by default, enabled on unix, stm32 and esp32 ports. This commit also adds new tests for slice indices and for slicing unicode strings.
Diffstat (limited to 'py/sequence.c')
-rw-r--r--py/sequence.c74
1 files changed, 8 insertions, 66 deletions
diff --git a/py/sequence.c b/py/sequence.c
index 4c19fc69e..15e925000 100644
--- a/py/sequence.c
+++ b/py/sequence.c
@@ -46,78 +46,20 @@ void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times
#if MICROPY_PY_BUILTINS_SLICE
bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
- mp_obj_t ostart, ostop, ostep;
- mp_int_t start, stop;
- mp_obj_slice_get(slice, &ostart, &ostop, &ostep);
+ mp_obj_slice_indices(slice, len, indexes);
- if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
- indexes->step = mp_obj_get_int(ostep);
- if (indexes->step == 0) {
- mp_raise_ValueError("slice step cannot be zero");
- }
- } else {
- indexes->step = 1;
- }
-
- if (ostart == mp_const_none) {
- if (indexes->step > 0) {
- start = 0;
- } else {
- start = len - 1;
- }
- } else {
- start = mp_obj_get_int(ostart);
- }
- if (ostop == mp_const_none) {
- if (indexes->step > 0) {
- stop = len;
- } else {
- stop = 0;
- }
- } else {
- stop = mp_obj_get_int(ostop);
- if (stop >= 0 && indexes->step < 0) {
- stop += 1;
- }
- }
-
- // Unlike subscription, out-of-bounds slice indexes are never error
- if (start < 0) {
- start = len + start;
- if (start < 0) {
- if (indexes->step < 0) {
- start = -1;
- } else {
- start = 0;
- }
- }
- } else if (indexes->step > 0 && (mp_uint_t)start > len) {
- start = len;
- } else if (indexes->step < 0 && (mp_uint_t)start >= len) {
- start = len - 1;
- }
- if (stop < 0) {
- stop = len + stop;
- if (stop < 0) {
- stop = -1;
- }
- if (indexes->step < 0) {
- stop += 1;
- }
- } else if ((mp_uint_t)stop > len) {
- stop = len;
+ // If the index is negative then stop points to the last item, not after it
+ if (indexes->step < 0) {
+ indexes->stop++;
}
// CPython returns empty sequence in such case, or point for assignment is at start
- if (indexes->step > 0 && start > stop) {
- stop = start;
- } else if (indexes->step < 0 && start < stop) {
- stop = start + 1;
+ if (indexes->step > 0 && indexes->start > indexes->stop) {
+ indexes->stop = indexes->start;
+ } else if (indexes->step < 0 && indexes->start < indexes->stop) {
+ indexes->stop = indexes->start + 1;
}
- indexes->start = start;
- indexes->stop = stop;
-
return indexes->step == 1;
}