objslice.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #if MICROPY_PY_BUILTINS_SLICE_INDICES
  44. STATIC mp_obj_t slice_indices(mp_obj_t self_in, mp_obj_t length_obj) {
  45. mp_int_t length = mp_obj_int_get_checked(length_obj);
  46. mp_bound_slice_t bound_indices;
  47. mp_obj_slice_indices(self_in, length, &bound_indices);
  48. mp_obj_t results[3] = {
  49. MP_OBJ_NEW_SMALL_INT(bound_indices.start),
  50. MP_OBJ_NEW_SMALL_INT(bound_indices.stop),
  51. MP_OBJ_NEW_SMALL_INT(bound_indices.step),
  52. };
  53. return mp_obj_new_tuple(3, results);
  54. }
  55. STATIC MP_DEFINE_CONST_FUN_OBJ_2(slice_indices_obj, slice_indices);
  56. #endif
  57. #if MICROPY_PY_BUILTINS_SLICE_ATTRS
  58. STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  59. if (dest[0] != MP_OBJ_NULL) {
  60. // not load attribute
  61. return;
  62. }
  63. mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
  64. if (attr == MP_QSTR_start) {
  65. dest[0] = self->start;
  66. } else if (attr == MP_QSTR_stop) {
  67. dest[0] = self->stop;
  68. } else if (attr == MP_QSTR_step) {
  69. dest[0] = self->step;
  70. #if MICROPY_PY_BUILTINS_SLICE_INDICES
  71. } else if (attr == MP_QSTR_indices) {
  72. dest[0] = MP_OBJ_FROM_PTR(&slice_indices_obj);
  73. dest[1] = self_in;
  74. #endif
  75. }
  76. }
  77. #endif
  78. #if MICROPY_PY_BUILTINS_SLICE_INDICES && !MICROPY_PY_BUILTINS_SLICE_ATTRS
  79. STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = {
  80. { MP_ROM_QSTR(MP_QSTR_indices), MP_ROM_PTR(&slice_indices_obj) },
  81. };
  82. STATIC MP_DEFINE_CONST_DICT(slice_locals_dict, slice_locals_dict_table);
  83. #endif
  84. const mp_obj_type_t mp_type_slice = {
  85. { &mp_type_type },
  86. .name = MP_QSTR_slice,
  87. .print = slice_print,
  88. #if MICROPY_PY_BUILTINS_SLICE_ATTRS
  89. .attr = slice_attr,
  90. #elif MICROPY_PY_BUILTINS_SLICE_INDICES
  91. .locals_dict = (mp_obj_dict_t *)&slice_locals_dict,
  92. #endif
  93. };
  94. mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
  95. mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t);
  96. o->base.type = &mp_type_slice;
  97. o->start = ostart;
  98. o->stop = ostop;
  99. o->step = ostep;
  100. return MP_OBJ_FROM_PTR(o);
  101. }
  102. // Return the real index and step values for a slice when applied to a sequence of
  103. // the given length, resolving missing components, negative values and values off
  104. // the end of the sequence.
  105. void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result) {
  106. mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
  107. mp_int_t start, stop, step;
  108. if (self->step == mp_const_none) {
  109. step = 1;
  110. } else {
  111. step = mp_obj_get_int(self->step);
  112. if (step == 0) {
  113. mp_raise_ValueError(MP_ERROR_TEXT("slice step can't be zero"));
  114. }
  115. }
  116. if (step > 0) {
  117. // Positive step
  118. if (self->start == mp_const_none) {
  119. start = 0;
  120. } else {
  121. start = mp_obj_get_int(self->start);
  122. if (start < 0) {
  123. start += length;
  124. }
  125. start = MIN(length, MAX(start, 0));
  126. }
  127. if (self->stop == mp_const_none) {
  128. stop = length;
  129. } else {
  130. stop = mp_obj_get_int(self->stop);
  131. if (stop < 0) {
  132. stop += length;
  133. }
  134. stop = MIN(length, MAX(stop, 0));
  135. }
  136. } else {
  137. // Negative step
  138. if (self->start == mp_const_none) {
  139. start = length - 1;
  140. } else {
  141. start = mp_obj_get_int(self->start);
  142. if (start < 0) {
  143. start += length;
  144. }
  145. start = MIN(length - 1, MAX(start, -1));
  146. }
  147. if (self->stop == mp_const_none) {
  148. stop = -1;
  149. } else {
  150. stop = mp_obj_get_int(self->stop);
  151. if (stop < 0) {
  152. stop += length;
  153. }
  154. stop = MIN(length - 1, MAX(stop, -1));
  155. }
  156. }
  157. result->start = start;
  158. result->stop = stop;
  159. result->step = step;
  160. }
  161. #endif