|
| 1 | +// Slice object |
| 2 | + |
| 3 | +package py |
| 4 | + |
| 5 | +// A python Slice object |
| 6 | +type Slice struct { |
| 7 | + Start Object |
| 8 | + Stop Object |
| 9 | + Step Object |
| 10 | +} |
| 11 | + |
| 12 | +var SliceType = NewType("slice", `slice(stop) -> slice object |
| 13 | +"slice(stop) |
| 14 | +slice(start, stop[, step]) |
| 15 | +
|
| 16 | +Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).`) |
| 17 | + |
| 18 | +// Type of this object |
| 19 | +func (o *Slice) Type() *Type { |
| 20 | + return SliceType |
| 21 | +} |
| 22 | + |
| 23 | +// Make a new slice object |
| 24 | +func NewSlice(start, stop, step Object) *Slice { |
| 25 | + return &Slice{ |
| 26 | + Start: start, |
| 27 | + Stop: stop, |
| 28 | + Step: step, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// SliceNew |
| 33 | +func SliceNew(metatype *Type, args Tuple, kwargs StringDict) Object { |
| 34 | + var start Object = None |
| 35 | + var stop Object = None |
| 36 | + var step Object = None |
| 37 | + UnpackTuple(args, kwargs, "slice", 1, 3, &start, &stop, &step) |
| 38 | + if len(args) == 1 { |
| 39 | + return NewSlice(None, start, None) |
| 40 | + } |
| 41 | + return NewSlice(start, stop, step) |
| 42 | +} |
| 43 | + |
| 44 | +// GetIndices |
| 45 | +// |
| 46 | +// Retrieve the start, stop, and step indices from the slice object |
| 47 | +// slice assuming a sequence of length length, and store the length of |
| 48 | +// the slice in slicelength. Out of bounds indices are clipped in a |
| 49 | +// manner consistent with the handling of normal slices. |
| 50 | +func (r *Slice) GetIndices(length int) (start, stop, step, slicelength int) { |
| 51 | + var defstart, defstop int |
| 52 | + |
| 53 | + if r.Step == None { |
| 54 | + step = 1 |
| 55 | + } else { |
| 56 | + step = IndexInt(r.Step) |
| 57 | + if step == 0 { |
| 58 | + panic(ExceptionNewf(ValueError, "slice step cannot be zero")) |
| 59 | + } |
| 60 | + const PY_SSIZE_T_MAX = int(^uint(0) >> 1) |
| 61 | + /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it |
| 62 | + * with -PY_SSIZE_T_MAX. This doesn't affect the semantics, and it |
| 63 | + * guards against later undefined behaviour resulting from code that |
| 64 | + * does "step = -step" as part of a slice reversal. |
| 65 | + */ |
| 66 | + if step < -PY_SSIZE_T_MAX { |
| 67 | + step = -PY_SSIZE_T_MAX |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if step < 0 { |
| 72 | + defstart = length - 1 |
| 73 | + defstop = -1 |
| 74 | + } else { |
| 75 | + defstart = 0 |
| 76 | + defstop = length |
| 77 | + } |
| 78 | + |
| 79 | + if r.Start == None { |
| 80 | + start = defstart |
| 81 | + } else { |
| 82 | + start = IndexInt(r.Start) |
| 83 | + if start < 0 { |
| 84 | + start += length |
| 85 | + } |
| 86 | + if start < 0 { |
| 87 | + if step < 0 { |
| 88 | + |
| 89 | + start = -1 |
| 90 | + } else { |
| 91 | + start = 0 |
| 92 | + } |
| 93 | + } |
| 94 | + if start >= length { |
| 95 | + if step < 0 { |
| 96 | + start = length - 1 |
| 97 | + } else { |
| 98 | + start = length |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if r.Stop == None { |
| 104 | + stop = defstop |
| 105 | + } else { |
| 106 | + stop = IndexInt(r.Stop) |
| 107 | + if stop < 0 { |
| 108 | + stop += length |
| 109 | + } |
| 110 | + if stop < 0 { |
| 111 | + if step < 0 { |
| 112 | + stop = -1 |
| 113 | + } else { |
| 114 | + stop = 0 |
| 115 | + } |
| 116 | + } |
| 117 | + if stop >= length { |
| 118 | + if step < 0 { |
| 119 | + stop = length - 1 |
| 120 | + } else { |
| 121 | + stop = length |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (step < 0 && stop >= start) || (step > 0 && start >= stop) { |
| 127 | + slicelength = 0 |
| 128 | + } else if step < 0 { |
| 129 | + slicelength = (stop-start+1)/(step) + 1 |
| 130 | + } else { |
| 131 | + slicelength = (stop-start-1)/(step) + 1 |
| 132 | + } |
| 133 | + |
| 134 | + return |
| 135 | +} |
| 136 | + |
| 137 | +// Check interface is satisfied |
0 commit comments