blob: 7baca1fbe6685b419126b40ce54dac44aed48f2f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020027#include <stdlib.h>
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020028#include <assert.h>
29
Nicko van Someren4c939552019-11-16 17:07:11 -070030#include "py/runtime.h"
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020031
Damien Georgee9906ac2014-01-04 18:44:46 +000032/******************************************************************************/
Damien Georgee9906ac2014-01-04 18:44:46 +000033/* slice object */
34
Damien Georgefb510b32014-06-01 13:32:54 +010035#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020036
Damien George7f9d1d62015-04-09 23:56:15 +010037STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000038 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000039 mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in);
Damien George7f9d1d62015-04-09 23:56:15 +010040 mp_print_str(print, "slice(");
41 mp_obj_print_helper(print, o->start, PRINT_REPR);
42 mp_print_str(print, ", ");
43 mp_obj_print_helper(print, o->stop, PRINT_REPR);
44 mp_print_str(print, ", ");
45 mp_obj_print_helper(print, o->step, PRINT_REPR);
46 mp_print_str(print, ")");
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020047}
48
Nicko van Someren4c939552019-11-16 17:07:11 -070049#if MICROPY_PY_BUILTINS_SLICE_INDICES
50STATIC mp_obj_t slice_indices(mp_obj_t self_in, mp_obj_t length_obj) {
51 mp_int_t length = mp_obj_int_get_checked(length_obj);
52 mp_bound_slice_t bound_indices;
53 mp_obj_slice_indices(self_in, length, &bound_indices);
54
55 mp_obj_t results[3] = {
56 MP_OBJ_NEW_SMALL_INT(bound_indices.start),
57 MP_OBJ_NEW_SMALL_INT(bound_indices.stop),
58 MP_OBJ_NEW_SMALL_INT(bound_indices.step),
59 };
60 return mp_obj_new_tuple(3, results);
61}
62STATIC MP_DEFINE_CONST_FUN_OBJ_2(slice_indices_obj, slice_indices);
63#endif
64
Tom Soulanilleaeb62f92015-09-11 14:31:32 -070065#if MICROPY_PY_BUILTINS_SLICE_ATTRS
66STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
67 if (dest[0] != MP_OBJ_NULL) {
68 // not load attribute
69 return;
70 }
Damien George999cedb2015-11-27 17:01:44 +000071 mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
Nicko van Someren4c939552019-11-16 17:07:11 -070072
Tom Soulanilleaeb62f92015-09-11 14:31:32 -070073 if (attr == MP_QSTR_start) {
74 dest[0] = self->start;
75 } else if (attr == MP_QSTR_stop) {
76 dest[0] = self->stop;
77 } else if (attr == MP_QSTR_step) {
78 dest[0] = self->step;
Nicko van Someren4c939552019-11-16 17:07:11 -070079 #if MICROPY_PY_BUILTINS_SLICE_INDICES
80 } else if (attr == MP_QSTR_indices) {
81 dest[0] = MP_OBJ_FROM_PTR(&slice_indices_obj);
82 dest[1] = self_in;
83 #endif
Tom Soulanilleaeb62f92015-09-11 14:31:32 -070084 }
85}
86#endif
87
Nicko van Someren4c939552019-11-16 17:07:11 -070088#if MICROPY_PY_BUILTINS_SLICE_INDICES && !MICROPY_PY_BUILTINS_SLICE_ATTRS
89STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = {
90 { MP_ROM_QSTR(MP_QSTR_indices), MP_ROM_PTR(&slice_indices_obj) },
91};
92STATIC MP_DEFINE_CONST_DICT(slice_locals_dict, slice_locals_dict_table);
93#endif
94
Jim Mussaredb7d6ee92022-06-24 16:22:38 +100095#if MICROPY_PY_BUILTINS_SLICE_ATTRS
96#define SLICE_TYPE_ATTR_OR_LOCALS_DICT attr, slice_attr,
97#elif MICROPY_PY_BUILTINS_SLICE_INDICES
98#define SLICE_TYPE_ATTR_OR_LOCALS_DICT locals_dict, (mp_obj_dict_t *)&slice_locals_dict,
99#else
100#define SLICE_TYPE_ATTR_OR_LOCALS_DICT
101#endif
102
Jim Mussared662b9762021-07-14 14:38:38 +1000103MP_DEFINE_CONST_OBJ_TYPE(
104 mp_type_slice,
105 MP_QSTR_slice,
106 MP_TYPE_FLAG_NONE,
107 MP_TYPE_NULL_MAKE_NEW,
Jim Mussaredb7d6ee92022-06-24 16:22:38 +1000108 SLICE_TYPE_ATTR_OR_LOCALS_DICT
Jim Mussared662b9762021-07-14 14:38:38 +1000109 print, slice_print
Jim Mussared662b9762021-07-14 14:38:38 +1000110 );
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200111
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200112mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
Jim Mussared0e7bfc82022-04-22 17:09:15 +1000113 mp_obj_slice_t *o = mp_obj_malloc(mp_obj_slice_t, &mp_type_slice);
Paul Sokolovskyafaaf532014-05-25 01:39:27 +0300114 o->start = ostart;
115 o->stop = ostop;
116 o->step = ostep;
Damien George999cedb2015-11-27 17:01:44 +0000117 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200118}
119
Nicko van Someren4c939552019-11-16 17:07:11 -0700120// Return the real index and step values for a slice when applied to a sequence of
121// the given length, resolving missing components, negative values and values off
122// the end of the sequence.
123void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result) {
124 mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
125 mp_int_t start, stop, step;
126
127 if (self->step == mp_const_none) {
128 step = 1;
129 } else {
130 step = mp_obj_get_int(self->step);
131 if (step == 0) {
Damien George8e048d22020-04-09 17:22:25 +1000132 mp_raise_ValueError(MP_ERROR_TEXT("slice step can't be zero"));
Nicko van Someren4c939552019-11-16 17:07:11 -0700133 }
134 }
135
136 if (step > 0) {
137 // Positive step
138 if (self->start == mp_const_none) {
139 start = 0;
140 } else {
141 start = mp_obj_get_int(self->start);
142 if (start < 0) {
143 start += length;
144 }
145 start = MIN(length, MAX(start, 0));
146 }
147
148 if (self->stop == mp_const_none) {
149 stop = length;
150 } else {
151 stop = mp_obj_get_int(self->stop);
152 if (stop < 0) {
153 stop += length;
154 }
155 stop = MIN(length, MAX(stop, 0));
156 }
157 } else {
158 // Negative step
159 if (self->start == mp_const_none) {
160 start = length - 1;
161 } else {
162 start = mp_obj_get_int(self->start);
163 if (start < 0) {
164 start += length;
165 }
166 start = MIN(length - 1, MAX(start, -1));
167 }
168
169 if (self->stop == mp_const_none) {
170 stop = -1;
171 } else {
172 stop = mp_obj_get_int(self->stop);
173 if (stop < 0) {
174 stop += length;
175 }
176 stop = MIN(length - 1, MAX(stop, -1));
177 }
178 }
179
180 result->start = start;
181 result->stop = stop;
182 result->step = step;
183}
184
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200185#endif