This commit was manufactured by cvs2svn to create tag 'r23a1-fork'.
[python/dscho.git] / Objects / sliceobject.c
blob796df2bb0cb77a8f17a2db3218ca4d84493418bf
1 /*
2 Written by Jim Hugunin and Chris Chase.
4 This includes both the singular ellipsis object and slice objects.
6 Guido, feel free to do whatever you want in the way of copyrights
7 for this file.
8 */
10 /*
11 Py_Ellipsis encodes the '...' rubber index token. It is similar to
12 the Py_NoneStruct in that there is no way to create other objects of
13 this type and there is exactly one in existence.
16 #include "Python.h"
17 #include "structmember.h"
19 static PyObject *
20 ellipsis_repr(PyObject *op)
22 return PyString_FromString("Ellipsis");
25 static PyTypeObject PyEllipsis_Type = {
26 PyObject_HEAD_INIT(&PyType_Type)
27 0, /* ob_size */
28 "ellipsis", /* tp_name */
29 0, /* tp_basicsize */
30 0, /* tp_itemsize */
31 0, /*never called*/ /* tp_dealloc */
32 0, /* tp_print */
33 0, /* tp_getattr */
34 0, /* tp_setattr */
35 0, /* tp_compare */
36 (reprfunc)ellipsis_repr, /* tp_repr */
37 0, /* tp_as_number */
38 0, /* tp_as_sequence */
39 0, /* tp_as_mapping */
40 0, /* tp_hash */
41 0, /* tp_call */
42 0, /* tp_str */
43 PyObject_GenericGetAttr, /* tp_getattro */
44 0, /* tp_setattro */
45 0, /* tp_as_buffer */
46 Py_TPFLAGS_DEFAULT, /* tp_flags */
49 PyObject _Py_EllipsisObject = {
50 PyObject_HEAD_INIT(&PyEllipsis_Type)
54 /* Slice object implementation
56 start, stop, and step are python objects with None indicating no
57 index is present.
60 PyObject *
61 PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
63 PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
65 if (obj == NULL)
66 return NULL;
68 if (step == NULL) step = Py_None;
69 Py_INCREF(step);
70 if (start == NULL) start = Py_None;
71 Py_INCREF(start);
72 if (stop == NULL) stop = Py_None;
73 Py_INCREF(stop);
75 obj->step = step;
76 obj->start = start;
77 obj->stop = stop;
79 return (PyObject *) obj;
82 int
83 PySlice_GetIndices(PySliceObject *r, int length,
84 int *start, int *stop, int *step)
86 if (r->step == Py_None) {
87 *step = 1;
88 } else {
89 if (!PyInt_Check(r->step)) return -1;
90 *step = PyInt_AsLong(r->step);
92 if (r->start == Py_None) {
93 *start = *step < 0 ? length-1 : 0;
94 } else {
95 if (!PyInt_Check(r->start)) return -1;
96 *start = PyInt_AsLong(r->start);
97 if (*start < 0) *start += length;
99 if (r->stop == Py_None) {
100 *stop = *step < 0 ? -1 : length;
101 } else {
102 if (!PyInt_Check(r->stop)) return -1;
103 *stop = PyInt_AsLong(r->stop);
104 if (*stop < 0) *stop += length;
106 if (*stop > length) return -1;
107 if (*start >= length) return -1;
108 if (*step == 0) return -1;
109 return 0;
113 PySlice_GetIndicesEx(PySliceObject *r, int length,
114 int *start, int *stop, int *step, int *slicelength)
116 /* this is harder to get right than you might think */
118 int defstart, defstop;
120 if (r->step == Py_None) {
121 *step = 1;
123 else {
124 if (!_PyEval_SliceIndex(r->step, step)) return -1;
125 if (*step == 0) {
126 PyErr_SetString(PyExc_ValueError,
127 "slice step cannot be zero");
128 return -1;
132 defstart = *step < 0 ? length-1 : 0;
133 defstop = *step < 0 ? -1 : length;
135 if (r->start == Py_None) {
136 *start = defstart;
138 else {
139 if (!_PyEval_SliceIndex(r->start, start)) return -1;
140 if (*start < 0) *start += length;
141 if (*start < 0) *start = (*step < 0) ? -1 : 0;
142 if (*start >= length)
143 *start = (*step < 0) ? length - 1 : length;
146 if (r->stop == Py_None) {
147 *stop = defstop;
149 else {
150 if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
151 if (*stop < 0) *stop += length;
152 if (*stop < 0) *stop = -1;
153 if (*stop > length) *stop = length;
156 if ((*step < 0 && *stop >= *start)
157 || (*step > 0 && *start >= *stop)) {
158 *slicelength = 0;
160 else if (*step < 0) {
161 *slicelength = (*stop-*start+1)/(*step)+1;
163 else {
164 *slicelength = (*stop-*start-1)/(*step)+1;
167 return 0;
170 static PyObject *
171 slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
173 PyObject *start, *stop, *step;
175 start = stop = step = NULL;
177 if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
178 return NULL;
180 /* This swapping of stop and start is to maintain similarity with
181 range(). */
182 if (stop == NULL) {
183 stop = start;
184 start = NULL;
186 return PySlice_New(start, stop, step);
189 PyDoc_STRVAR(slice_doc,
190 "slice([start,] stop[, step])\n\
192 Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
194 static void
195 slice_dealloc(PySliceObject *r)
197 Py_DECREF(r->step);
198 Py_DECREF(r->start);
199 Py_DECREF(r->stop);
200 PyObject_Del(r);
203 static PyObject *
204 slice_repr(PySliceObject *r)
206 PyObject *s, *comma;
208 s = PyString_FromString("slice(");
209 comma = PyString_FromString(", ");
210 PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
211 PyString_Concat(&s, comma);
212 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
213 PyString_Concat(&s, comma);
214 PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
215 PyString_ConcatAndDel(&s, PyString_FromString(")"));
216 Py_DECREF(comma);
217 return s;
220 static PyMemberDef slice_members[] = {
221 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
222 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
223 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
227 static PyObject*
228 slice_indices(PySliceObject* self, PyObject* len)
230 int ilen, start, stop, step, slicelength;
232 ilen = PyInt_AsLong(len);
234 if (ilen == -1 && PyErr_Occurred()) {
235 return NULL;
238 if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
239 &step, &slicelength) < 0) {
240 return NULL;
243 return Py_BuildValue("(iii)", start, stop, step);
246 PyDoc_STRVAR(slice_indices_doc,
247 "S.indices(len) -> (start, stop, stride)\n\
249 Assuming a sequence of length len, calculate the start and stop\n\
250 indices, and the stride length of the extended slice described by\n\
251 S. Out of bounds indices are clipped in a manner consistent with the\n\
252 handling of normal slices.");
254 static PyMethodDef slice_methods[] = {
255 {"indices", (PyCFunction)slice_indices,
256 METH_O, slice_indices_doc},
257 {NULL, NULL}
260 static int
261 slice_compare(PySliceObject *v, PySliceObject *w)
263 int result = 0;
265 if (v == w)
266 return 0;
268 if (PyObject_Cmp(v->start, w->start, &result) < 0)
269 return -2;
270 if (result != 0)
271 return result;
272 if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
273 return -2;
274 if (result != 0)
275 return result;
276 if (PyObject_Cmp(v->step, w->step, &result) < 0)
277 return -2;
278 return result;
281 PyTypeObject PySlice_Type = {
282 PyObject_HEAD_INIT(&PyType_Type)
283 0, /* Number of items for varobject */
284 "slice", /* Name of this type */
285 sizeof(PySliceObject), /* Basic object size */
286 0, /* Item size for varobject */
287 (destructor)slice_dealloc, /* tp_dealloc */
288 0, /* tp_print */
289 0, /* tp_getattr */
290 0, /* tp_setattr */
291 (cmpfunc)slice_compare, /* tp_compare */
292 (reprfunc)slice_repr, /* tp_repr */
293 0, /* tp_as_number */
294 0, /* tp_as_sequence */
295 0, /* tp_as_mapping */
296 0, /* tp_hash */
297 0, /* tp_call */
298 0, /* tp_str */
299 PyObject_GenericGetAttr, /* tp_getattro */
300 0, /* tp_setattro */
301 0, /* tp_as_buffer */
302 Py_TPFLAGS_DEFAULT, /* tp_flags */
303 slice_doc, /* tp_doc */
304 0, /* tp_traverse */
305 0, /* tp_clear */
306 0, /* tp_richcompare */
307 0, /* tp_weaklistoffset */
308 0, /* tp_iter */
309 0, /* tp_iternext */
310 slice_methods, /* tp_methods */
311 slice_members, /* tp_members */
312 0, /* tp_getset */
313 0, /* tp_base */
314 0, /* tp_dict */
315 0, /* tp_descr_get */
316 0, /* tp_descr_set */
317 0, /* tp_dictoffset */
318 0, /* tp_init */
319 0, /* tp_alloc */
320 slice_new, /* tp_new */