diff options
author | Michael W. Hudson <mwh@python.net> | 2002-06-11 10:55:12 (GMT) |
---|---|---|
committer | Michael W. Hudson <mwh@python.net> | 2002-06-11 10:55:12 (GMT) |
commit | 5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58 (patch) | |
tree | 7358a0479ab98893ad5a5457ae0df328d0e60251 /Objects/sliceobject.c | |
parent | f90ae20354ceb501f0ba0b6459df17f1a8005a47 (diff) | |
download | cpython-5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58.zip cpython-5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58.tar.gz cpython-5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58.tar.bz2 |
This is my nearly two year old patch
[ 400998 ] experimental support for extended slicing on lists
somewhat spruced up and better tested than it was when I wrote it.
Includes docs & tests. The whatsnew section needs expanding, and arrays
should support extended slices -- later.
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r-- | Objects/sliceobject.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 7562d34..7499d31 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -109,6 +109,59 @@ PySlice_GetIndices(PySliceObject *r, int length, return 0; } +int +PySlice_GetIndicesEx(PySliceObject *r, int length, + int *start, int *stop, int *step, int *slicelength) +{ + /* this is harder to get right than you might think */ + int defstart, defstop; + + if (r->step == Py_None) { + *step = 1; + } else { + *step = PyInt_AsLong(r->step); + if (*step == -1 && PyErr_Occurred()) { + return -1; + } + else if (*step == 0) { + PyErr_SetString(PyExc_ValueError, + "slice step cannot be zero"); + return -1; + } + } + + defstart = *step < 0 ? length-1 : 0; + defstop = *step < 0 ? -1 : length; + + if (r->start == Py_None) { + *start = defstart; + } else { + if (!_PyEval_SliceIndex(r->start, start)) return -1; + if (*start < 0) *start += length; + if (*start < 0) *start = (*step < 0) ? -1 : 0; + if (*start >= length) + *start = (*step < 0) ? length - 1 : length; + } + + if (r->stop == Py_None) { + *stop = defstop; + } else { + if (!_PyEval_SliceIndex(r->stop, stop)) return -1; + if (*stop < 0) *stop += length; + if (*stop < 0) *stop = -1; + if (*stop > length) *stop = length; + } + + if (*step < 0) { + *slicelength = (*stop-*start+1)/(*step)+1; + } else { + *slicelength = (*stop-*start-1)/(*step)+1; + } + if (*slicelength < 0) *slicelength = 0; + + return 0; +} + static void slice_dealloc(PySliceObject *r) { |