Optimize str * n for len(str)==1 and UCS-2 or UCS-4

This commit is contained in:
Victor Stinner 2011-12-18 03:26:31 +01:00
parent b99bb20ae4
commit 73f53b57d1
1 changed files with 11 additions and 4 deletions

View File

@ -12028,12 +12028,19 @@ unicode_repeat(PyObject *str, Py_ssize_t len)
if (PyUnicode_GET_LENGTH(str) == 1) { if (PyUnicode_GET_LENGTH(str) == 1) {
const int kind = PyUnicode_KIND(str); const int kind = PyUnicode_KIND(str);
const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); 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); 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) for (n = 0; n < len; ++n)
PyUnicode_WRITE(kind, to, n, fill_char); ucs2[n] = fill_char;
} else {
Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u);
assert(kind == PyUnicode_4BYTE_KIND);
for (n = 0; n < len; ++n)
ucs4[n] = fill_char;
} }
} }
else { else {