summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-04-09 20:39:24 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2013-04-09 20:39:24 (GMT)
commitcc7af7219217f247775b9079f75713399f2f0f28 (patch)
tree575bc8eacb29a1ea94886e43c2e1b1a1621a6e4f /Objects
parentf50a4e9bc940e701feb142c35a267c90fc1fff8e (diff)
downloadcpython-cc7af7219217f247775b9079f75713399f2f0f28.zip
cpython-cc7af7219217f247775b9079f75713399f2f0f28.tar.gz
cpython-cc7af7219217f247775b9079f75713399f2f0f28.tar.bz2
Write super-fast version of str.strip(), str.lstrip() and str.rstrip() for pure ASCII
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c64
1 files changed, 45 insertions, 19 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 838d9de..e348a46 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11722,37 +11722,63 @@ PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
static PyObject *
do_strip(PyObject *self, int striptype)
{
- int kind;
- void *data;
Py_ssize_t len, i, j;
if (PyUnicode_READY(self) == -1)
return NULL;
- kind = PyUnicode_KIND(self);
- data = PyUnicode_DATA(self);
len = PyUnicode_GET_LENGTH(self);
- i = 0;
- if (striptype != RIGHTSTRIP) {
- while (i < len) {
- Py_UCS4 ch = PyUnicode_READ(kind, data, i);
- if (!Py_UNICODE_ISSPACE(ch))
- break;
- i++;
+ 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);
- j = len;
- if (striptype != LEFTSTRIP) {
- j--;
- while (j >= i) {
- Py_UCS4 ch = PyUnicode_READ(kind, data, j);
- if (!Py_UNICODE_ISSPACE(ch))
- break;
+ i = 0;
+ if (striptype != RIGHTSTRIP) {
+ while (i < len) {
+ Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+ if (!Py_UNICODE_ISSPACE(ch))
+ break;
+ i++;
+ }
+ }
+
+ j = len;
+ if (striptype != LEFTSTRIP) {
j--;
+ while (j >= i) {
+ Py_UCS4 ch = PyUnicode_READ(kind, data, j);
+ if (!Py_UNICODE_ISSPACE(ch))
+ break;
+ j--;
+ }
+ j++;
}
- j++;
}
return PyUnicode_Substring(self, i, j);