| 1 |
#include "Python.h" |
| 2 |
|
| 3 |
Py_ssize_t |
| 4 |
PySlice_AdjustIndices(Py_ssize_t length, |
| 5 |
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step) |
| 6 |
{ |
| 7 |
|
| 8 |
|
| 9 |
assert(step != 0); |
| 10 |
assert(step >= -PY_SSIZE_T_MAX); |
| 11 |
|
| 12 |
if (*start < 0) { |
| 13 |
*start += length; |
| 14 |
if (*start < 0) { |
| 15 |
*start = (step < 0) ? -1 : 0; |
| 16 |
} |
| 17 |
} |
| 18 |
else if (*start >= length) { |
| 19 |
*start = (step < 0) ? length - 1 : length; |
| 20 |
} |
| 21 |
|
| 22 |
if (*stop < 0) { |
| 23 |
*stop += length; |
| 24 |
if (*stop < 0) { |
| 25 |
*stop = (step < 0) ? -1 : 0; |
| 26 |
} |
| 27 |
} |
| 28 |
else if (*stop >= length) { |
| 29 |
*stop = (step < 0) ? length - 1 : length; |
| 30 |
} |
| 31 |
|
| 32 |
if (step < 0) { |
| 33 |
if (*stop < *start) { |
| 34 |
return (*start - *stop - 1) / (-step) + 1; |
| 35 |
} |
| 36 |
} |
| 37 |
else { |
| 38 |
if (*start < *stop) { |
| 39 |
return (*stop - *start - 1) / step + 1; |
| 40 |
} |
| 41 |
} |
| 42 |
return 0; |
| 43 |
} |