mirror of https://github.com/python/cpython
Optimize string slicing to use the new API
This commit is contained in:
parent
b7591d4780
commit
7aec401966
|
@ -12253,9 +12253,9 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
|
||||||
return unicode_getitem((PyObject*)self, i);
|
return unicode_getitem((PyObject*)self, i);
|
||||||
} else if (PySlice_Check(item)) {
|
} else if (PySlice_Check(item)) {
|
||||||
Py_ssize_t start, stop, step, slicelength, cur, i;
|
Py_ssize_t start, stop, step, slicelength, cur, i;
|
||||||
const Py_UNICODE* source_buf;
|
|
||||||
Py_UNICODE* result_buf;
|
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
|
void *src_data, *dest_data;
|
||||||
|
int kind;
|
||||||
|
|
||||||
if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
|
if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
|
||||||
&start, &stop, &step, &slicelength) < 0) {
|
&start, &stop, &step, &slicelength) < 0) {
|
||||||
|
@ -12272,22 +12272,20 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
|
||||||
} else if (step == 1) {
|
} else if (step == 1) {
|
||||||
return PyUnicode_Substring((PyObject*)self,
|
return PyUnicode_Substring((PyObject*)self,
|
||||||
start, start + slicelength);
|
start, start + slicelength);
|
||||||
} else {
|
}
|
||||||
source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
|
/* General (less optimized) case */
|
||||||
result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength*
|
result = PyUnicode_New(slicelength, PyUnicode_MAX_CHAR_VALUE(self));
|
||||||
sizeof(Py_UNICODE));
|
if (result == NULL)
|
||||||
|
return NULL;
|
||||||
if (result_buf == NULL)
|
kind = PyUnicode_KIND(self);
|
||||||
return PyErr_NoMemory();
|
src_data = PyUnicode_DATA(self);
|
||||||
|
dest_data = PyUnicode_DATA(result);
|
||||||
|
|
||||||
for (cur = start, i = 0; i < slicelength; cur += step, i++) {
|
for (cur = start, i = 0; i < slicelength; cur += step, i++) {
|
||||||
result_buf[i] = source_buf[cur];
|
Py_UCS4 ch = PyUnicode_READ(kind, src_data, cur);
|
||||||
|
PyUnicode_WRITE(kind, dest_data, i, ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = PyUnicode_FromUnicode(result_buf, slicelength);
|
|
||||||
PyObject_FREE(result_buf);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(PyExc_TypeError, "string indices must be integers");
|
PyErr_SetString(PyExc_TypeError, "string indices must be integers");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Reference in New Issue