[Python-checkins] cpython: Write super-fast version of str.strip(), str.lstrip() and str.rstrip() for pure
victor.stinner
python-checkins at python.org
Tue Apr 9 22:39:43 CEST 2013
http://hg.python.org/cpython/rev/115dc74d3ff5
changeset: 83223:115dc74d3ff5
user: Victor Stinner <victor.stinner at gmail.com>
date: Tue Apr 09 22:39:24 2013 +0200
summary:
Write super-fast version of str.strip(), str.lstrip() and str.rstrip() for pure ASCII
files:
Objects/unicodeobject.c | 72 +++++++++++++++++++---------
1 files changed, 49 insertions(+), 23 deletions(-)
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11722,37 +11722,63 @@
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++;
- }
- }
-
- j = len;
- if (striptype != LEFTSTRIP) {
- j--;
- while (j >= i) {
- Py_UCS4 ch = PyUnicode_READ(kind, data, j);
- if (!Py_UNICODE_ISSPACE(ch))
- break;
+ 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--;
- }
- 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;
+ 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++;
+ }
}
return PyUnicode_Substring(self, i, j);
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list