Branch data Line data Source code
1 : : /*
2 : : * This file is part of the MicroPython project, http://micropython.org/
3 : : *
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 : :
27 : : #include <stdlib.h>
28 : : #include <assert.h>
29 : :
30 : : #include "py/runtime.h"
31 : :
32 : : /******************************************************************************/
33 : : /* slice object */
34 : :
35 : : #if MICROPY_PY_BUILTINS_SLICE
36 : :
37 : 4 : STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
38 : 4 : (void)kind;
39 : 4 : mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in);
40 : 4 : mp_print_str(print, "slice(");
41 : 4 : mp_obj_print_helper(print, o->start, PRINT_REPR);
42 : 4 : mp_print_str(print, ", ");
43 : 4 : mp_obj_print_helper(print, o->stop, PRINT_REPR);
44 : 4 : mp_print_str(print, ", ");
45 : 4 : mp_obj_print_helper(print, o->step, PRINT_REPR);
46 : 4 : mp_print_str(print, ")");
47 : 4 : }
48 : :
49 : : #if MICROPY_PY_BUILTINS_SLICE_INDICES
50 : 52 : STATIC mp_obj_t slice_indices(mp_obj_t self_in, mp_obj_t length_obj) {
51 : 52 : mp_int_t length = mp_obj_int_get_checked(length_obj);
52 : 52 : mp_bound_slice_t bound_indices;
53 : 52 : mp_obj_slice_indices(self_in, length, &bound_indices);
54 : :
55 : 52 : mp_obj_t results[3] = {
56 : 52 : MP_OBJ_NEW_SMALL_INT(bound_indices.start),
57 : 52 : MP_OBJ_NEW_SMALL_INT(bound_indices.stop),
58 : 52 : MP_OBJ_NEW_SMALL_INT(bound_indices.step),
59 : : };
60 : 52 : return mp_obj_new_tuple(3, results);
61 : : }
62 : : STATIC MP_DEFINE_CONST_FUN_OBJ_2(slice_indices_obj, slice_indices);
63 : : #endif
64 : :
65 : : #if MICROPY_PY_BUILTINS_SLICE_ATTRS
66 : 80 : STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
67 [ + + ]: 80 : if (dest[0] != MP_OBJ_NULL) {
68 : : // not load attribute
69 : : return;
70 : : }
71 : 76 : mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
72 : :
73 [ + + ]: 76 : if (attr == MP_QSTR_start) {
74 : 8 : dest[0] = self->start;
75 [ + + ]: 68 : } else if (attr == MP_QSTR_stop) {
76 : 8 : dest[0] = self->stop;
77 [ + + ]: 60 : } else if (attr == MP_QSTR_step) {
78 : 8 : dest[0] = self->step;
79 : : #if MICROPY_PY_BUILTINS_SLICE_INDICES
80 [ + - ]: 52 : } else if (attr == MP_QSTR_indices) {
81 : 52 : dest[0] = MP_OBJ_FROM_PTR(&slice_indices_obj);
82 : 52 : dest[1] = self_in;
83 : : #endif
84 : : }
85 : : }
86 : : #endif
87 : :
88 : : #if MICROPY_PY_BUILTINS_SLICE_INDICES && !MICROPY_PY_BUILTINS_SLICE_ATTRS
89 : : STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = {
90 : : { MP_ROM_QSTR(MP_QSTR_indices), MP_ROM_PTR(&slice_indices_obj) },
91 : : };
92 : : STATIC MP_DEFINE_CONST_DICT(slice_locals_dict, slice_locals_dict_table);
93 : : #endif
94 : :
95 : : #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, &slice_locals_dict,
99 : : #else
100 : : #define SLICE_TYPE_ATTR_OR_LOCALS_DICT
101 : : #endif
102 : :
103 : : MP_DEFINE_CONST_OBJ_TYPE(
104 : : mp_type_slice,
105 : : MP_QSTR_slice,
106 : : MP_TYPE_FLAG_NONE,
107 : : SLICE_TYPE_ATTR_OR_LOCALS_DICT
108 : : print, slice_print
109 : : );
110 : :
111 : 9137 : mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
112 : 9137 : mp_obj_slice_t *o = mp_obj_malloc(mp_obj_slice_t, &mp_type_slice);
113 : 9133 : o->start = ostart;
114 : 9133 : o->stop = ostop;
115 : 9133 : o->step = ostep;
116 : 9133 : return MP_OBJ_FROM_PTR(o);
117 : : }
118 : :
119 : : // Return the real index and step values for a slice when applied to a sequence of
120 : : // the given length, resolving missing components, negative values and values off
121 : : // the end of the sequence.
122 : 8213 : void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result) {
123 : 8213 : mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
124 : 8213 : mp_int_t start, stop, step;
125 : :
126 [ + + ]: 8213 : if (self->step == mp_const_none) {
127 : : step = 1;
128 : : } else {
129 : 202 : step = mp_obj_get_int(self->step);
130 [ + + ]: 202 : if (step == 0) {
131 : 4 : mp_raise_ValueError(MP_ERROR_TEXT("slice step can't be zero"));
132 : : }
133 : : }
134 : :
135 [ + + ]: 198 : if (step > 0) {
136 : : // Positive step
137 [ + + ]: 8081 : if (self->start == mp_const_none) {
138 : : start = 0;
139 : : } else {
140 : 4541 : start = mp_obj_get_int(self->start);
141 [ + + ]: 4541 : if (start < 0) {
142 : 1604 : start += length;
143 : : }
144 : 4541 : start = MIN(length, MAX(start, 0));
145 : : }
146 : :
147 [ + + ]: 8081 : if (self->stop == mp_const_none) {
148 : : stop = length;
149 : : } else {
150 : 3125 : stop = mp_obj_get_int(self->stop);
151 [ + + ]: 3125 : if (stop < 0) {
152 : 90 : stop += length;
153 : : }
154 : 3125 : stop = MIN(length, MAX(stop, 0));
155 : : }
156 : : } else {
157 : : // Negative step
158 [ + + ]: 128 : if (self->start == mp_const_none) {
159 : 48 : start = length - 1;
160 : : } else {
161 : 80 : start = mp_obj_get_int(self->start);
162 [ + + ]: 80 : if (start < 0) {
163 : 24 : start += length;
164 : : }
165 [ + + ]: 80 : start = MIN(length - 1, MAX(start, -1));
166 : : }
167 : :
168 [ + + ]: 128 : if (self->stop == mp_const_none) {
169 : : stop = -1;
170 : : } else {
171 : 84 : stop = mp_obj_get_int(self->stop);
172 [ + + ]: 84 : if (stop < 0) {
173 : 36 : stop += length;
174 : : }
175 [ + + ]: 84 : stop = MIN(length - 1, MAX(stop, -1));
176 : : }
177 : : }
178 : :
179 : 8209 : result->start = start;
180 : 8209 : result->stop = stop;
181 : 8209 : result->step = step;
182 : 8209 : }
183 : :
184 : : #endif
|