diff options
author | Guido van Rossum <guido@python.org> | 2002-08-23 18:21:28 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-08-23 18:21:28 (GMT) |
commit | 8b1a6d694fa2f38cde77892c5ee0bb177be49db6 (patch) | |
tree | e3f7c901c642cd2500cb87648b6976c8d539a1fb /Objects/unicodeobject.c | |
parent | 280488b9a3b5cc213d01aae794751b89144f8609 (diff) | |
download | cpython-8b1a6d694fa2f38cde77892c5ee0bb177be49db6.zip cpython-8b1a6d694fa2f38cde77892c5ee0bb177be49db6.tar.gz cpython-8b1a6d694fa2f38cde77892c5ee0bb177be49db6.tar.bz2 |
Code by Inyeol Lee, submitted to SF bug 595350, to implement
the string/unicode method .replace() with a zero-lengt first argument.
Inyeol contributed tests for this too.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index b264936..6dea94f 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3494,11 +3494,6 @@ PyObject *replace(PyUnicodeObject *self, { PyUnicodeObject *u; - if (str1->length == 0) { - PyErr_SetString(PyExc_ValueError, "empty pattern string"); - return NULL; - } - if (maxcount < 0) maxcount = INT_MAX; @@ -3549,19 +3544,30 @@ PyObject *replace(PyUnicodeObject *self, if (u) { i = 0; p = u->str; - while (i <= self->length - str1->length) - if (Py_UNICODE_MATCH(self, i, str1)) { - /* replace string segment */ + if (str1->length > 0) { + while (i <= self->length - str1->length) + if (Py_UNICODE_MATCH(self, i, str1)) { + /* replace string segment */ + Py_UNICODE_COPY(p, str2->str, str2->length); + p += str2->length; + i += str1->length; + if (--n <= 0) { + /* copy remaining part */ + Py_UNICODE_COPY(p, self->str+i, self->length-i); + break; + } + } else + *p++ = self->str[i++]; + } else { + while (n > 0) { Py_UNICODE_COPY(p, str2->str, str2->length); p += str2->length; - i += str1->length; - if (--n <= 0) { - /* copy remaining part */ - Py_UNICODE_COPY(p, self->str+i, self->length-i); + if (--n <= 0) break; - } - } else *p++ = self->str[i++]; + } + Py_UNICODE_COPY(p, self->str+i, self->length-i); + } } } } |