diff options
author | Georg Brandl <georg@python.org> | 2009-04-12 12:01:50 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2009-04-12 12:01:50 (GMT) |
commit | 222de0f713ef31bf5c877b9051bcdfc39c510dd4 (patch) | |
tree | 6a73dd5f3e7071f457bfdf105e158fccdf290ca6 /Objects | |
parent | 8a73278cd897e72a5721c3f608d7bd115bada8c1 (diff) | |
download | cpython-222de0f713ef31bf5c877b9051bcdfc39c510dd4.zip cpython-222de0f713ef31bf5c877b9051bcdfc39c510dd4.tar.gz cpython-222de0f713ef31bf5c877b9051bcdfc39c510dd4.tar.bz2 |
#5708: a bit of streamlining in unicode_repeat().
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index d3e5283..f52c435 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -7738,8 +7738,10 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) Py_ssize_t nchars; size_t nbytes; - if (len < 0) - len = 0; + if (len < 1) { + Py_INCREF(unicode_empty); + return (PyObject *)unicode_empty; + } if (len == 1 && PyUnicode_CheckExact(str)) { /* no repeat, return original string */ @@ -7751,7 +7753,7 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) * needed doesn't overflow size_t */ nchars = len * str->length; - if (len && nchars / len != str->length) { + if (nchars / len != str->length) { PyErr_SetString(PyExc_OverflowError, "repeated string is too long"); return NULL; @@ -7768,14 +7770,11 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) p = u->str; - if (str->length == 1 && len > 0) { + if (str->length == 1) { Py_UNICODE_FILL(p, str->str[0], len); } else { - Py_ssize_t done = 0; /* number of characters copied this far */ - if (done < nchars) { - Py_UNICODE_COPY(p, str->str, str->length); - done = str->length; - } + Py_ssize_t done = str->length; /* number of characters copied this far */ + Py_UNICODE_COPY(p, str->str, str->length); while (done < nchars) { Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; Py_UNICODE_COPY(p+done, p, n); |