objslice.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/obj.h"
  29. /******************************************************************************/
  30. /* slice object */
  31. #if MICROPY_PY_BUILTINS_SLICE
  32. typedef struct _mp_obj_slice_t {
  33. mp_obj_base_t base;
  34. mp_obj_t start;
  35. mp_obj_t stop;
  36. mp_obj_t step;
  37. } mp_obj_slice_t;
  38. STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  39. (void)kind;
  40. mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in);
  41. mp_print_str(print, "slice(");
  42. mp_obj_print_helper(print, o->start, PRINT_REPR);
  43. mp_print_str(print, ", ");
  44. mp_obj_print_helper(print, o->stop, PRINT_REPR);
  45. mp_print_str(print, ", ");
  46. mp_obj_print_helper(print, o->step, PRINT_REPR);
  47. mp_print_str(print, ")");
  48. }
  49. #if MICROPY_PY_BUILTINS_SLICE_ATTRS
  50. STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  51. if (dest[0] != MP_OBJ_NULL) {
  52. // not load attribute
  53. return;
  54. }
  55. mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
  56. if (attr == MP_QSTR_start) {
  57. dest[0] = self->start;
  58. } else if (attr == MP_QSTR_stop) {
  59. dest[0] = self->stop;
  60. } else if (attr == MP_QSTR_step) {
  61. dest[0] = self->step;
  62. }
  63. }
  64. #endif
  65. const mp_obj_type_t mp_type_slice = {
  66. { &mp_type_type },
  67. .name = MP_QSTR_slice,
  68. .print = slice_print,
  69. #if MICROPY_PY_BUILTINS_SLICE_ATTRS
  70. .attr = slice_attr,
  71. #endif
  72. };
  73. mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
  74. mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t);
  75. o->base.type = &mp_type_slice;
  76. o->start = ostart;
  77. o->stop = ostop;
  78. o->step = ostep;
  79. return MP_OBJ_FROM_PTR(o);
  80. }
  81. void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step) {
  82. assert(mp_obj_is_type(self_in, &mp_type_slice));
  83. mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
  84. *start = self->start;
  85. *stop = self->stop;
  86. *step = self->step;
  87. }
  88. #endif