Commit cc7af721 authored by Victor Stinner's avatar Victor Stinner

Write super-fast version of str.strip(), str.lstrip() and str.rstrip() for pure ASCII

parent f50a4e9b
...@@ -11722,17 +11722,42 @@ PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) ...@@ -11722,17 +11722,42 @@ PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
static PyObject * static PyObject *
do_strip(PyObject *self, int striptype) do_strip(PyObject *self, int striptype)
{ {
int kind;
void *data;
Py_ssize_t len, i, j; Py_ssize_t len, i, j;
if (PyUnicode_READY(self) == -1) if (PyUnicode_READY(self) == -1)
return NULL; return NULL;
kind = PyUnicode_KIND(self);
data = PyUnicode_DATA(self);
len = PyUnicode_GET_LENGTH(self); len = PyUnicode_GET_LENGTH(self);
if (PyUnicode_IS_ASCII(self)) {
Py_UCS1 *data = PyUnicode_1BYTE_DATA(self);
i = 0;
if (striptype != RIGHTSTRIP) {
while (i < len) {
Py_UCS4 ch = data[i];
if (!_Py_ascii_whitespace[ch])
break;
i++;
}
}
j = len;
if (striptype != LEFTSTRIP) {
j--;
while (j >= i) {
Py_UCS4 ch = data[j];
if (!_Py_ascii_whitespace[ch])
break;
j--;
}
j++;
}
}
else {
int kind = PyUnicode_KIND(self);
void *data = PyUnicode_DATA(self);
i = 0; i = 0;
if (striptype != RIGHTSTRIP) { if (striptype != RIGHTSTRIP) {
while (i < len) { while (i < len) {
...@@ -11754,6 +11779,7 @@ do_strip(PyObject *self, int striptype) ...@@ -11754,6 +11779,7 @@ do_strip(PyObject *self, int striptype)
} }
j++; j++;
} }
}
return PyUnicode_Substring(self, i, j); return PyUnicode_Substring(self, i, j);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment