diff options
author | Andrew M. Kuchling <amk@amk.ca> | 2008-01-19 18:18:41 (GMT) |
---|---|---|
committer | Andrew M. Kuchling <amk@amk.ca> | 2008-01-19 18:18:41 (GMT) |
commit | 5c60bfcfbf2dd50cc40210771fdc8d2cfc20cdad (patch) | |
tree | 906959917e2562717aa503edee65d6462a6d5682 /Modules/mmapmodule.c | |
parent | 4be0bc642e89939d9d9bf2559d92eafe9f0a0933 (diff) | |
download | cpython-5c60bfcfbf2dd50cc40210771fdc8d2cfc20cdad.zip cpython-5c60bfcfbf2dd50cc40210771fdc8d2cfc20cdad.tar.gz cpython-5c60bfcfbf2dd50cc40210771fdc8d2cfc20cdad.tar.bz2 |
Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Contributed by John Lenton.
Diffstat (limited to 'Modules/mmapmodule.c')
-rw-r--r-- | Modules/mmapmodule.c | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 9a39b24..dd35c10 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -248,19 +248,22 @@ mmap_read_method(mmap_object *self, } static PyObject * -mmap_find_method(mmap_object *self, - PyObject *args) +mmap_gfind(mmap_object *self, + PyObject *args, + int reverse) { Py_ssize_t start = self->pos; + Py_ssize_t end = self->size; char *needle; Py_ssize_t len; CHECK_VALID(NULL); - if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) { + if (!PyArg_ParseTuple(args, reverse ? "s#|nn:rfind" : "s#|nn:find", + &needle, &len, &start, &end)) { return NULL; } else { char *p; - char *e = self->data + self->size; + char sign = reverse ? -1 : 1; if (start < 0) start += self->size; @@ -269,7 +272,18 @@ mmap_find_method(mmap_object *self, else if ((size_t)start > self->size) start = self->size; - for (p = self->data + start; p + len <= e; ++p) { + if (end < 0) + end += self->size; + if (end < 0) + end = 0; + else if ((size_t)end > self->size) + end = self->size; + + start += (Py_ssize_t)self->data; + end += (Py_ssize_t)self->data; + + for (p = (char *)(reverse ? end - len : start); + p >= (char *)start && p + len <= (char *)end; p+=sign) { Py_ssize_t i; for (i = 0; i < len && needle[i] == p[i]; ++i) /* nothing */; @@ -281,6 +295,20 @@ mmap_find_method(mmap_object *self, } } +static PyObject * +mmap_find_method(mmap_object *self, + PyObject *args) +{ + return mmap_gfind(self, args, 0); +} + +static PyObject * +mmap_rfind_method(mmap_object *self, + PyObject *args) +{ + return mmap_gfind(self, args, 1); +} + static int is_writeable(mmap_object *self) { @@ -593,6 +621,7 @@ mmap_move_method(mmap_object *self, PyObject *args) static struct PyMethodDef mmap_object_methods[] = { {"close", (PyCFunction) mmap_close_method, METH_NOARGS}, {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, + {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS}, {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, |