MCPcopy Index your code
hub / github.com/RustPython/RustPython / inner_indices

Method inner_indices

crates/vm/src/builtins/slice.rs:154–234  ·  view source on GitHub ↗
(
        &self,
        length: &BigInt,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

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 }

Callers 2

indicesMethod · 0.80
__getitem__Method · 0.80

Calls 12

step_refMethod · 0.80
try_indexMethod · 0.80
as_bigintMethod · 0.80
start_refMethod · 0.80
ErrClass · 0.50
is_noneMethod · 0.45
stepMethod · 0.45
cloneMethod · 0.45
is_zeroMethod · 0.45
unwrapMethod · 0.45
startMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected