| 152 | } |
| 153 | |
| 154 | pub(crate) fn inner_indices( |
| 155 | &self, |
| 156 | length: &BigInt, |
| 157 | vm: &VirtualMachine, |
| 158 | ) -> PyResult<(BigInt, BigInt, BigInt)> { |
| 159 | // Calculate step |
| 160 | let step: BigInt; |
| 161 | if vm.is_none(self.step_ref(vm)) { |
| 162 | step = One::one(); |
| 163 | } else { |
| 164 | // Clone the value, not the reference. |
| 165 | let this_step = self.step(vm).try_index(vm)?; |
| 166 | step = this_step.as_bigint().clone(); |
| 167 | |
| 168 | if step.is_zero() { |
| 169 | return Err(vm.new_value_error("slice step cannot be zero.")); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // For convenience |
| 174 | let backwards = step.is_negative(); |
| 175 | |
| 176 | // Each end of the array |
| 177 | let lower = if backwards { |
| 178 | (-1_i8).to_bigint().unwrap() |
| 179 | } else { |
| 180 | Zero::zero() |
| 181 | }; |
| 182 | |
| 183 | let upper = if backwards { |
| 184 | lower.clone() + length |
| 185 | } else { |
| 186 | length.clone() |
| 187 | }; |
| 188 | |
| 189 | // Calculate start |
| 190 | let mut start: BigInt; |
| 191 | if vm.is_none(self.start_ref(vm)) { |
| 192 | // Default |
| 193 | start = if backwards { |
| 194 | upper.clone() |
| 195 | } else { |
| 196 | lower.clone() |
| 197 | }; |
| 198 | } else { |
| 199 | let this_start = self.start(vm).try_index(vm)?; |
| 200 | start = this_start.as_bigint().clone(); |
| 201 | |
| 202 | if start < Zero::zero() { |
| 203 | // From end of array |
| 204 | start += length; |
| 205 | |
| 206 | if start < lower { |
| 207 | start = lower.clone(); |
| 208 | } |
| 209 | } else if start > upper { |
| 210 | start = upper.clone(); |
| 211 | } |