LCOV - code coverage report
Current view: top level - py - objslice.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-342-g49af8cad4.info Lines: 73 73 100.0 %
Date: 2024-04-25 10:54:52 Functions: 6 6 100.0 %
Branches: 35 36 97.2 %

           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                 :         16 : static mp_obj_t slice_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
      50                 :            :     // Needed to explicitly opt out of default __hash__.
      51                 :            :     // REVISIT: CPython implements comparison operators for slice.
      52                 :         16 :     return MP_OBJ_NULL;
      53                 :            : }
      54                 :            : 
      55                 :            : #if MICROPY_PY_BUILTINS_SLICE_INDICES
      56                 :         56 : static mp_obj_t slice_indices(mp_obj_t self_in, mp_obj_t length_obj) {
      57                 :         56 :     mp_int_t length = mp_obj_get_int(length_obj);
      58                 :         52 :     mp_bound_slice_t bound_indices;
      59                 :         52 :     mp_obj_slice_indices(self_in, length, &bound_indices);
      60                 :            : 
      61                 :         52 :     mp_obj_t results[3] = {
      62                 :         52 :         MP_OBJ_NEW_SMALL_INT(bound_indices.start),
      63                 :         52 :         MP_OBJ_NEW_SMALL_INT(bound_indices.stop),
      64                 :         52 :         MP_OBJ_NEW_SMALL_INT(bound_indices.step),
      65                 :            :     };
      66                 :         52 :     return mp_obj_new_tuple(3, results);
      67                 :            : }
      68                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(slice_indices_obj, slice_indices);
      69                 :            : #endif
      70                 :            : 
      71                 :            : #if MICROPY_PY_BUILTINS_SLICE_ATTRS
      72                 :         84 : static void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
      73         [ +  + ]:         84 :     if (dest[0] != MP_OBJ_NULL) {
      74                 :            :         // not load attribute
      75                 :            :         return;
      76                 :            :     }
      77                 :         80 :     mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
      78                 :            : 
      79         [ +  + ]:         80 :     if (attr == MP_QSTR_start) {
      80                 :          8 :         dest[0] = self->start;
      81         [ +  + ]:         72 :     } else if (attr == MP_QSTR_stop) {
      82                 :          8 :         dest[0] = self->stop;
      83         [ +  + ]:         64 :     } else if (attr == MP_QSTR_step) {
      84                 :          8 :         dest[0] = self->step;
      85                 :            :     #if MICROPY_PY_BUILTINS_SLICE_INDICES
      86         [ +  - ]:         56 :     } else if (attr == MP_QSTR_indices) {
      87                 :         56 :         dest[0] = MP_OBJ_FROM_PTR(&slice_indices_obj);
      88                 :         56 :         dest[1] = self_in;
      89                 :            :     #endif
      90                 :            :     }
      91                 :            : }
      92                 :            : #endif
      93                 :            : 
      94                 :            : #if MICROPY_PY_BUILTINS_SLICE_INDICES && !MICROPY_PY_BUILTINS_SLICE_ATTRS
      95                 :            : static const mp_rom_map_elem_t slice_locals_dict_table[] = {
      96                 :            :     { MP_ROM_QSTR(MP_QSTR_indices), MP_ROM_PTR(&slice_indices_obj) },
      97                 :            : };
      98                 :            : static MP_DEFINE_CONST_DICT(slice_locals_dict, slice_locals_dict_table);
      99                 :            : #endif
     100                 :            : 
     101                 :            : #if MICROPY_PY_BUILTINS_SLICE_ATTRS
     102                 :            : #define SLICE_TYPE_ATTR_OR_LOCALS_DICT attr, slice_attr,
     103                 :            : #elif MICROPY_PY_BUILTINS_SLICE_INDICES
     104                 :            : #define SLICE_TYPE_ATTR_OR_LOCALS_DICT locals_dict, &slice_locals_dict,
     105                 :            : #else
     106                 :            : #define SLICE_TYPE_ATTR_OR_LOCALS_DICT
     107                 :            : #endif
     108                 :            : 
     109                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     110                 :            :     mp_type_slice,
     111                 :            :     MP_QSTR_slice,
     112                 :            :     MP_TYPE_FLAG_NONE,
     113                 :            :     unary_op, slice_unary_op,
     114                 :            :     SLICE_TYPE_ATTR_OR_LOCALS_DICT
     115                 :            :     print, slice_print
     116                 :            :     );
     117                 :            : 
     118                 :       9121 : mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
     119                 :       9121 :     mp_obj_slice_t *o = mp_obj_malloc(mp_obj_slice_t, &mp_type_slice);
     120                 :       9117 :     o->start = ostart;
     121                 :       9117 :     o->stop = ostop;
     122                 :       9117 :     o->step = ostep;
     123                 :       9117 :     return MP_OBJ_FROM_PTR(o);
     124                 :            : }
     125                 :            : 
     126                 :            : // Return the real index and step values for a slice when applied to a sequence of
     127                 :            : // the given length, resolving missing components, negative values and values off
     128                 :            : // the end of the sequence.
     129                 :       8179 : void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result) {
     130                 :       8179 :     mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
     131                 :       8179 :     mp_int_t start, stop, step;
     132                 :            : 
     133         [ +  + ]:       8179 :     if (self->step == mp_const_none) {
     134                 :            :         step = 1;
     135                 :            :     } else {
     136                 :        202 :         step = mp_obj_get_int(self->step);
     137         [ +  + ]:        202 :         if (step == 0) {
     138                 :          4 :             mp_raise_ValueError(MP_ERROR_TEXT("slice step can't be zero"));
     139                 :            :         }
     140                 :            :     }
     141                 :            : 
     142         [ +  + ]:        198 :     if (step > 0) {
     143                 :            :         // Positive step
     144         [ +  + ]:       8047 :         if (self->start == mp_const_none) {
     145                 :            :             start = 0;
     146                 :            :         } else {
     147                 :       4521 :             start = mp_obj_get_int(self->start);
     148         [ +  + ]:       4521 :             if (start < 0) {
     149                 :       1616 :                 start += length;
     150                 :            :             }
     151                 :       4521 :             start = MIN(length, MAX(start, 0));
     152                 :            :         }
     153                 :            : 
     154         [ +  + ]:       8047 :         if (self->stop == mp_const_none) {
     155                 :            :             stop = length;
     156                 :            :         } else {
     157                 :       3075 :             stop = mp_obj_get_int(self->stop);
     158         [ +  + ]:       3075 :             if (stop < 0) {
     159                 :        100 :                 stop += length;
     160                 :            :             }
     161                 :       3075 :             stop = MIN(length, MAX(stop, 0));
     162                 :            :         }
     163                 :            :     } else {
     164                 :            :         // Negative step
     165         [ +  + ]:        128 :         if (self->start == mp_const_none) {
     166                 :         48 :             start = length - 1;
     167                 :            :         } else {
     168                 :         80 :             start = mp_obj_get_int(self->start);
     169         [ +  + ]:         80 :             if (start < 0) {
     170                 :         24 :                 start += length;
     171                 :            :             }
     172         [ +  + ]:         80 :             start = MIN(length - 1, MAX(start, -1));
     173                 :            :         }
     174                 :            : 
     175         [ +  + ]:        128 :         if (self->stop == mp_const_none) {
     176                 :            :             stop = -1;
     177                 :            :         } else {
     178                 :         84 :             stop = mp_obj_get_int(self->stop);
     179         [ +  + ]:         84 :             if (stop < 0) {
     180                 :         36 :                 stop += length;
     181                 :            :             }
     182         [ +  + ]:         84 :             stop = MIN(length - 1, MAX(stop, -1));
     183                 :            :         }
     184                 :            :     }
     185                 :            : 
     186                 :       8175 :     result->start = start;
     187                 :       8175 :     result->stop = stop;
     188                 :       8175 :     result->step = step;
     189                 :       8175 : }
     190                 :            : 
     191                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d