objslice.c 6.0 KB

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