diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-12-18 02:26:31 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-12-18 02:26:31 (GMT) |
commit | 73f53b57d18ae3bed924e497244791b953f55c6f (patch) | |
tree | b5091023a70af9b9c31adec2bb000205ef15a60c /Objects/unicodeobject.c | |
parent | b99bb20ae43dd05ebc162b8af4caefd6035d0f39 (diff) | |
download | cpython-73f53b57d18ae3bed924e497244791b953f55c6f.zip cpython-73f53b57d18ae3bed924e497244791b953f55c6f.tar.gz cpython-73f53b57d18ae3bed924e497244791b953f55c6f.tar.bz2 |
Optimize str * n for len(str)==1 and UCS-2 or UCS-4
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index ab0d9fa..c5d361b 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -12028,12 +12028,19 @@ unicode_repeat(PyObject *str, Py_ssize_t len) if (PyUnicode_GET_LENGTH(str) == 1) { const int kind = PyUnicode_KIND(str); const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); - void *to = PyUnicode_DATA(u); - if (kind == PyUnicode_1BYTE_KIND) + if (kind == PyUnicode_1BYTE_KIND) { + void *to = PyUnicode_DATA(u); memset(to, (unsigned char)fill_char, len); - else { + } + else if (kind == PyUnicode_2BYTE_KIND) { + Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); + for (n = 0; n < len; ++n) + ucs2[n] = fill_char; + } else { + Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); + assert(kind == PyUnicode_4BYTE_KIND); for (n = 0; n < len; ++n) - PyUnicode_WRITE(kind, to, n, fill_char); + ucs4[n] = fill_char; } } else { |