diff options
author | Michael W. Hudson <mwh@python.net> | 2002-06-11 10:55:12 +0000 |
---|---|---|
committer | Michael W. Hudson <mwh@python.net> | 2002-06-11 10:55:12 +0000 |
commit | 02f73743f536a02940f4c79adda2d30814b1a40b (patch) | |
tree | 464f9b809828f4066b793a998f14279a00c8369e /Objects/stringobject.c | |
parent | 405adb9e26516403e1b1ffdfe494ae29e23ecaba (diff) | |
download | cpython-02f73743f536a02940f4c79adda2d30814b1a40b.tar.gz |
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/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 64 |
1 files changed, 62 insertions, 2 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 89e414af47..b88778ea15 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -940,6 +940,60 @@ string_hash(PyStringObject *a) return x; } +static PyObject* +string_subscript(PyStringObject* self, PyObject* item) +{ + if (PyInt_Check(item)) { + long i = PyInt_AS_LONG(item); + if (i < 0) + i += PyString_GET_SIZE(self); + return string_item(self,i); + } + else if (PyLong_Check(item)) { + long i = PyLong_AsLong(item); + if (i == -1 && PyErr_Occurred()) + return NULL; + if (i < 0) + i += PyString_GET_SIZE(self); + return string_item(self,i); + } + else if (PySlice_Check(item)) { + int start, stop, step, slicelength, cur, i; + char* source_buf; + char* result_buf; + PyObject* result; + + if (PySlice_GetIndicesEx((PySliceObject*)item, + PyString_GET_SIZE(self), + &start, &stop, &step, &slicelength) < 0) { + return NULL; + } + + if (slicelength <= 0) { + return PyString_FromStringAndSize("", 0); + } + else { + source_buf = PyString_AsString((PyObject*)self); + result_buf = PyMem_Malloc(slicelength); + + for (cur = start, i = 0; i < slicelength; + cur += step, i++) { + result_buf[i] = source_buf[cur]; + } + + result = PyString_FromStringAndSize(result_buf, + slicelength); + PyMem_Free(result_buf); + return result; + } + } + else { + PyErr_SetString(PyExc_TypeError, + "string indices must be integers"); + return NULL; + } +} + static int string_buffer_getreadbuf(PyStringObject *self, int index, const void **ptr) { @@ -991,6 +1045,12 @@ static PySequenceMethods string_as_sequence = { (objobjproc)string_contains /*sq_contains*/ }; +static PyMappingMethods string_as_mapping = { + (inquiry)string_length, + (binaryfunc)string_subscript, + 0, +}; + static PyBufferProcs string_as_buffer = { (getreadbufferproc)string_buffer_getreadbuf, (getwritebufferproc)string_buffer_getwritebuf, @@ -2929,7 +2989,7 @@ PyTypeObject PyString_Type = { (reprfunc)string_repr, /* tp_repr */ 0, /* tp_as_number */ &string_as_sequence, /* tp_as_sequence */ - 0, /* tp_as_mapping */ + &string_as_mapping, /* tp_as_mapping */ (hashfunc)string_hash, /* tp_hash */ 0, /* tp_call */ (reprfunc)string_str, /* tp_str */ @@ -3349,7 +3409,7 @@ PyString_Format(PyObject *format, PyObject *args) arglen = -1; argidx = -2; } - if (args->ob_type->tp_as_mapping) + if (args->ob_type->tp_as_mapping && !PyTuple_Check(args)) dict = args; while (--fmtcnt >= 0) { if (*fmt != '%') { |