Skip to content

Commit 5c60bfc

Browse files
committed
Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Contributed by John Lenton.
1 parent 4be0bc6 commit 5c60bfc

5 files changed

Lines changed: 88 additions & 9 deletions

File tree

Doc/library/mmap.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ Memory-mapped file objects support the following methods:
139139
an exception being raised.
140140

141141

142-
.. method:: mmap.find(string[, start])
142+
.. method:: mmap.find(string[, start[, end]])
143143

144-
Returns the lowest index in the object where the substring *string* is found.
145-
Returns ``-1`` on failure. *start* is the index at which the search begins, and
146-
defaults to zero.
144+
Returns the lowest index in the object where the substring *string* is found,
145+
such that *string* is contained in the range [*start*, *end*]. Optional
146+
arguments *start* and *end* are interpreted as in slice notation.
147+
Returns ``-1`` on failure.
147148

148149

149150
.. method:: mmap.flush([offset, size])
@@ -188,6 +189,14 @@ Memory-mapped file objects support the following methods:
188189
:exc:`TypeError` exception.
189190

190191

192+
.. method:: mmap.rfind(string[, start[, end]])
193+
194+
Returns the highest index in the object where the substring *string* is
195+
found, such that *string* is contained in the range [*start*,
196+
*end*]. Optional arguments *start* and *end* are interpreted as in slice
197+
notation. Returns ``-1`` on failure.
198+
199+
191200
.. method:: mmap.seek(pos[, whence])
192201

193202
Set the file's current position. *whence* argument is optional and defaults to

Lib/test/test_mmap.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,42 @@ def test_tougher_find(self):
255255
self.assertEqual(m.find(slice + 'x'), -1)
256256
m.close()
257257

258+
def test_find_end(self):
259+
# test the new 'end' parameter works as expected
260+
f = open(TESTFN, 'w+')
261+
data = 'one two ones'
262+
n = len(data)
263+
f.write(data)
264+
f.flush()
265+
m = mmap.mmap(f.fileno(), n)
266+
f.close()
267+
268+
self.assertEqual(m.find('one'), 0)
269+
self.assertEqual(m.find('ones'), 8)
270+
self.assertEqual(m.find('one', 0, -1), 0)
271+
self.assertEqual(m.find('one', 1), 8)
272+
self.assertEqual(m.find('one', 1, -1), 8)
273+
self.assertEqual(m.find('one', 1, -2), -1)
274+
275+
276+
def test_rfind(self):
277+
# test the new 'end' parameter works as expected
278+
f = open(TESTFN, 'w+')
279+
data = 'one two ones'
280+
n = len(data)
281+
f.write(data)
282+
f.flush()
283+
m = mmap.mmap(f.fileno(), n)
284+
f.close()
285+
286+
self.assertEqual(m.rfind('one'), 8)
287+
self.assertEqual(m.rfind('one '), 0)
288+
self.assertEqual(m.rfind('one', 0, -1), 8)
289+
self.assertEqual(m.rfind('one', 0, -2), 0)
290+
self.assertEqual(m.rfind('one', 1, -1), 8)
291+
self.assertEqual(m.rfind('one', 1, -2), -1)
292+
293+
258294
def test_double_close(self):
259295
# make sure a double close doesn't crash on Solaris (Bug# 665913)
260296
f = open(TESTFN, 'w+')

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ Kip Lehman
395395
Joerg Lehmann
396396
Luke Kenneth Casson Leighton
397397
Marc-Andre Lemburg
398+
John Lenton
398399
Mark Levinson
399400
William Lewis
400401
Robert van Liere

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,10 @@ Library
10111011
Extension Modules
10121012
-----------------
10131013

1014+
- Patch 976880: ``mmap`` objects now have an ``rfind`` method that
1015+
works as expected. ``mmap.find`` also takes an optional ``end``
1016+
parameter.
1017+
10141018
- _winreg's HKEY object has gained __enter__ and __exit__ methods to support
10151019
the context manager protocol. The _winreg module also gained a new function
10161020
``ExpandEnvironmentStrings`` to expand REG_EXPAND_SZ keys.

Modules/mmapmodule.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,22 @@ mmap_read_method(mmap_object *self,
248248
}
249249

250250
static PyObject *
251-
mmap_find_method(mmap_object *self,
252-
PyObject *args)
251+
mmap_gfind(mmap_object *self,
252+
PyObject *args,
253+
int reverse)
253254
{
254255
Py_ssize_t start = self->pos;
256+
Py_ssize_t end = self->size;
255257
char *needle;
256258
Py_ssize_t len;
257259

258260
CHECK_VALID(NULL);
259-
if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) {
261+
if (!PyArg_ParseTuple(args, reverse ? "s#|nn:rfind" : "s#|nn:find",
262+
&needle, &len, &start, &end)) {
260263
return NULL;
261264
} else {
262265
char *p;
263-
char *e = self->data + self->size;
266+
char sign = reverse ? -1 : 1;
264267

265268
if (start < 0)
266269
start += self->size;
@@ -269,7 +272,18 @@ mmap_find_method(mmap_object *self,
269272
else if ((size_t)start > self->size)
270273
start = self->size;
271274

272-
for (p = self->data + start; p + len <= e; ++p) {
275+
if (end < 0)
276+
end += self->size;
277+
if (end < 0)
278+
end = 0;
279+
else if ((size_t)end > self->size)
280+
end = self->size;
281+
282+
start += (Py_ssize_t)self->data;
283+
end += (Py_ssize_t)self->data;
284+
285+
for (p = (char *)(reverse ? end - len : start);
286+
p >= (char *)start && p + len <= (char *)end; p+=sign) {
273287
Py_ssize_t i;
274288
for (i = 0; i < len && needle[i] == p[i]; ++i)
275289
/* nothing */;
@@ -281,6 +295,20 @@ mmap_find_method(mmap_object *self,
281295
}
282296
}
283297

298+
static PyObject *
299+
mmap_find_method(mmap_object *self,
300+
PyObject *args)
301+
{
302+
return mmap_gfind(self, args, 0);
303+
}
304+
305+
static PyObject *
306+
mmap_rfind_method(mmap_object *self,
307+
PyObject *args)
308+
{
309+
return mmap_gfind(self, args, 1);
310+
}
311+
284312
static int
285313
is_writeable(mmap_object *self)
286314
{
@@ -593,6 +621,7 @@ mmap_move_method(mmap_object *self, PyObject *args)
593621
static struct PyMethodDef mmap_object_methods[] = {
594622
{"close", (PyCFunction) mmap_close_method, METH_NOARGS},
595623
{"find", (PyCFunction) mmap_find_method, METH_VARARGS},
624+
{"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS},
596625
{"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
597626
{"move", (PyCFunction) mmap_move_method, METH_VARARGS},
598627
{"read", (PyCFunction) mmap_read_method, METH_VARARGS},

0 commit comments

Comments
 (0)