mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
needforspeed: speed up upper and lower for 8-bit string objects.
(the unicode versions of these are still 2x faster on windows, though...) based on work by Andrew Dalke, with tweaks by yours truly.
This commit is contained in:
parent
c620bada3a
commit
39ccef607e
1 changed files with 20 additions and 22 deletions
|
@ -2036,26 +2036,25 @@ Return a copy of the string S converted to lowercase.");
|
|||
static PyObject *
|
||||
string_lower(PyStringObject *self)
|
||||
{
|
||||
char *s = PyString_AS_STRING(self), *s_new;
|
||||
char *s;
|
||||
Py_ssize_t i, n = PyString_GET_SIZE(self);
|
||||
PyObject *newobj;
|
||||
|
||||
newobj = PyString_FromStringAndSize(NULL, n);
|
||||
if (newobj == NULL)
|
||||
newobj = PyString_FromStringAndSize(PyString_AS_STRING(self), n);
|
||||
if (!newobj)
|
||||
return NULL;
|
||||
s_new = PyString_AsString(newobj);
|
||||
|
||||
s = PyString_AS_STRING(newobj);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
int c = Py_CHARMASK(*s++);
|
||||
if (isupper(c)) {
|
||||
*s_new = tolower(c);
|
||||
} else
|
||||
*s_new = c;
|
||||
s_new++;
|
||||
char c = Py_CHARMASK(s[i]);
|
||||
if (isupper(c))
|
||||
s[i] = _tolower(c);
|
||||
}
|
||||
|
||||
return newobj;
|
||||
}
|
||||
|
||||
|
||||
PyDoc_STRVAR(upper__doc__,
|
||||
"S.upper() -> string\n\
|
||||
\n\
|
||||
|
@ -2064,26 +2063,25 @@ Return a copy of the string S converted to uppercase.");
|
|||
static PyObject *
|
||||
string_upper(PyStringObject *self)
|
||||
{
|
||||
char *s = PyString_AS_STRING(self), *s_new;
|
||||
char *s;
|
||||
Py_ssize_t i, n = PyString_GET_SIZE(self);
|
||||
PyObject *newobj;
|
||||
|
||||
newobj = PyString_FromStringAndSize(NULL, n);
|
||||
if (newobj == NULL)
|
||||
newobj = PyString_FromStringAndSize(PyString_AS_STRING(self), n);
|
||||
if (!newobj)
|
||||
return NULL;
|
||||
s_new = PyString_AsString(newobj);
|
||||
|
||||
s = PyString_AS_STRING(newobj);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
int c = Py_CHARMASK(*s++);
|
||||
if (islower(c)) {
|
||||
*s_new = toupper(c);
|
||||
} else
|
||||
*s_new = c;
|
||||
s_new++;
|
||||
char c = Py_CHARMASK(s[i]);
|
||||
if (islower(c))
|
||||
s[i] = _toupper(c);
|
||||
}
|
||||
|
||||
return newobj;
|
||||
}
|
||||
|
||||
|
||||
PyDoc_STRVAR(title__doc__,
|
||||
"S.title() -> string\n\
|
||||
\n\
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue