]> granicus.if.org Git - python/commitdiff
needforspeed: make new upper/lower work properly for single-character
authorFredrik Lundh <fredrik@pythonware.com>
Thu, 25 May 2006 15:49:45 +0000 (15:49 +0000)
committerFredrik Lundh <fredrik@pythonware.com>
Thu, 25 May 2006 15:49:45 +0000 (15:49 +0000)
strings too... (thanks to georg brandl for spotting the exact problem
faster than anyone else)

Objects/stringobject.c

index d3ab54e214ab66ff2131ae554130477168db905e..3ec45243d23c8019b18f7112536a5a02bf7550f2 100644 (file)
@@ -2040,14 +2040,16 @@ string_lower(PyStringObject *self)
        Py_ssize_t i, n = PyString_GET_SIZE(self);
        PyObject *newobj;
 
-       newobj = PyString_FromStringAndSize(PyString_AS_STRING(self), n);
+       newobj = PyString_FromStringAndSize(NULL, n);
        if (!newobj)
                return NULL;
 
        s = PyString_AS_STRING(newobj);
 
+       memcpy(s, PyString_AS_STRING(self), n);
+
        for (i = 0; i < n; i++) {
-               char c = Py_CHARMASK(s[i]);
+               int c = Py_CHARMASK(s[i]);
                if (isupper(c))
                        s[i] = _tolower(c);
        }
@@ -2067,14 +2069,16 @@ string_upper(PyStringObject *self)
        Py_ssize_t i, n = PyString_GET_SIZE(self);
        PyObject *newobj;
 
-       newobj = PyString_FromStringAndSize(PyString_AS_STRING(self), n);
+       newobj = PyString_FromStringAndSize(NULL, n);
        if (!newobj)
                return NULL;
 
        s = PyString_AS_STRING(newobj);
 
+       memcpy(s, PyString_AS_STRING(self), n);
+
        for (i = 0; i < n; i++) {
-               char c = Py_CHARMASK(s[i]);
+               int c = Py_CHARMASK(s[i]);
                if (islower(c))
                        s[i] = _toupper(c);
        }