From 96303ce055e0bdc1b5f53f38bd424e747e289621 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 20 Aug 2002 16:57:58 +0000 Subject: [PATCH] Fix some endcase bugs in unicode rfind()/rindex() and endswith(). These were reported and fixed by Inyeol Lee in SF bug 595350. The endswith() bug is already fixed in 2.3; I'll fix the others in 2.3 next. --- Lib/test/test_unicode.py | 6 ++++++ Misc/ACKS | 1 + Objects/stringobject.c | 6 +++--- Objects/unicodeobject.c | 6 +++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 411801986709..5d1c35a6d36f 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -83,6 +83,10 @@ test('find', u'abcdefghiabc', 9, u'abc', 1) test('find', u'abcdefghiabc', -1, u'def', 4) test('rfind', u'abcdefghiabc', 9, u'abc') +test('rfind', 'abcdefghiabc', 9, u'abc') +test('rfind', 'abcdefghiabc', 12, u'') +test('rfind', u'abcdefghiabc', 12, '') +test('rfind', u'abcdefghiabc', 12, u'') test('lower', u'HeLLo', u'hello') test('lower', u'hello', u'hello') @@ -211,6 +215,8 @@ test('endswith', u'helloworld', 0, u'lowo', 4, 7) test('endswith', u'helloworld', 0, u'lowo', 3, 8) test('endswith', u'ab', 0, u'ab', 0, 1) test('endswith', u'ab', 0, u'ab', 0, 0) +test('endswith', 'helloworld', 1, u'd') +test('endswith', 'helloworld', 0, u'l') test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi') test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi', 8) diff --git a/Misc/ACKS b/Misc/ACKS index 56a7efe0275c..a543a4e30ae5 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -257,6 +257,7 @@ Soren Larsen Piers Lauder Chris Lawrence Christopher Lee +Inyeol Lee Kip Lehman Marc-Andre Lemburg William Lewis diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 7311df04a400..529a424d8a1a 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -1303,7 +1303,7 @@ string_find_internal(PyStringObject *self, PyObject *args, int dir) } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(subobj)) - return PyUnicode_Find((PyObject *)self, subobj, i, last, 1); + return PyUnicode_Find((PyObject *)self, subobj, i, last, dir); #endif else if (PyObject_AsCharBuffer(subobj, &sub, &n)) return -2; @@ -2145,7 +2145,7 @@ string_startswith(PyStringObject *self, PyObject *args) const char* prefix; int plen; int start = 0; - int end = -1; + int end = INT_MAX; PyObject *subobj; if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, @@ -2204,7 +2204,7 @@ string_endswith(PyStringObject *self, PyObject *args) const char* suffix; int slen; int start = 0; - int end = -1; + int end = INT_MAX; int lower, upper; PyObject *subobj; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 5232aa335557..d4e247196dfb 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -2866,9 +2866,6 @@ int findstring(PyUnicodeObject *self, if (start < 0) start = 0; - if (substring->length == 0) - return start; - if (end > self->length) end = self->length; if (end < 0) @@ -2876,6 +2873,9 @@ int findstring(PyUnicodeObject *self, if (end < 0) end = 0; + if (substring->length == 0) + return (direction > 0) ? start : end; + end -= substring->length; if (direction < 0) { -- 2.47.3