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 
slice_print(const mp_print_t * print,mp_obj_t o_in,mp_print_kind_t kind)37 STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
38     (void)kind;
39     mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in);
40     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, ")");
47 }
48 
49 #if MICROPY_PY_BUILTINS_SLICE_INDICES
slice_indices(mp_obj_t self_in,mp_obj_t length_obj)50 STATIC 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 }
62 STATIC MP_DEFINE_CONST_FUN_OBJ_2(slice_indices_obj, slice_indices);
63 #endif
64 
65 #if MICROPY_PY_BUILTINS_SLICE_ATTRS
slice_attr(mp_obj_t self_in,qstr attr,mp_obj_t * dest)66 STATIC 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     }
71     mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
72 
73     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;
79     #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
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 const mp_obj_type_t mp_type_slice = {
96     { &mp_type_type },
97     .name = MP_QSTR_slice,
98     .print = slice_print,
99     #if MICROPY_PY_BUILTINS_SLICE_ATTRS
100     .attr = slice_attr,
101     #elif MICROPY_PY_BUILTINS_SLICE_INDICES
102     .locals_dict = (mp_obj_dict_t *)&slice_locals_dict,
103     #endif
104 };
105 
mp_obj_new_slice(mp_obj_t ostart,mp_obj_t ostop,mp_obj_t ostep)106 mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
107     mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t);
108     o->base.type = &mp_type_slice;
109     o->start = ostart;
110     o->stop = ostop;
111     o->step = ostep;
112     return MP_OBJ_FROM_PTR(o);
113 }
114 
115 // Return the real index and step values for a slice when applied to a sequence of
116 // the given length, resolving missing components, negative values and values off
117 // the end of the sequence.
mp_obj_slice_indices(mp_obj_t self_in,mp_int_t length,mp_bound_slice_t * result)118 void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result) {
119     mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
120     mp_int_t start, stop, step;
121 
122     if (self->step == mp_const_none) {
123         step = 1;
124     } else {
125         step = mp_obj_get_int(self->step);
126         if (step == 0) {
127             mp_raise_ValueError(MP_ERROR_TEXT("slice step can't be zero"));
128         }
129     }
130 
131     if (step > 0) {
132         // Positive step
133         if (self->start == mp_const_none) {
134             start = 0;
135         } else {
136             start = mp_obj_get_int(self->start);
137             if (start < 0) {
138                 start += length;
139             }
140             start = MIN(length, MAX(start, 0));
141         }
142 
143         if (self->stop == mp_const_none) {
144             stop = length;
145         } else {
146             stop = mp_obj_get_int(self->stop);
147             if (stop < 0) {
148                 stop += length;
149             }
150             stop = MIN(length, MAX(stop, 0));
151         }
152     } else {
153         // Negative step
154         if (self->start == mp_const_none) {
155             start = length - 1;
156         } else {
157             start = mp_obj_get_int(self->start);
158             if (start < 0) {
159                 start += length;
160             }
161             start = MIN(length - 1, MAX(start, -1));
162         }
163 
164         if (self->stop == mp_const_none) {
165             stop = -1;
166         } else {
167             stop = mp_obj_get_int(self->stop);
168             if (stop < 0) {
169                 stop += length;
170             }
171             stop = MIN(length - 1, MAX(stop, -1));
172         }
173     }
174 
175     result->start = start;
176     result->stop = stop;
177     result->step = step;
178 }
179 
180 #endif
181