]> granicus.if.org Git - python/commitdiff
Fixed .capitalize() method of Unicode objects to work like the
authorMarc-André Lemburg <mal@egenix.com>
Mon, 29 Jan 2001 11:14:16 +0000 (11:14 +0000)
committerMarc-André Lemburg <mal@egenix.com>
Mon, 29 Jan 2001 11:14:16 +0000 (11:14 +0000)
corresponding string method. Added tests for this too.

Patch written by Marc-Andre Lemburg. Copyright assigned to Guido van Rossum.

Lib/test/string_tests.py
Lib/test/test_unicode.py
Objects/unicodeobject.c

index c3010d6517bb4addba8b8d5c677865ef958b478f..2e912c8ab3b8e881066089e72d9e4fac7209d970 100644 (file)
@@ -53,6 +53,8 @@ def run_method_tests(test):
 
     test('capitalize', ' hello ', ' hello ')
     test('capitalize', 'hello ', 'Hello ')
+    test('capitalize', 'aaaa', 'Aaaa')
+    test('capitalize', 'AaAa', 'Aaaa')
 
     test('count', 'aaa', 3, 'a')
     test('count', 'aaa', 0, 'b')
index 855b0a2edb12c42eb9941e35e12182aab46f8676..2a24255b0491bccc3a834a2b8909a2d73afb2718 100644 (file)
@@ -31,6 +31,8 @@ def test(method, input, output, *args):
 
 test('capitalize', u' hello ', u' hello ')
 test('capitalize', u'hello ', u'Hello ')
+test('capitalize', u'aaaa', u'Aaaa')
+test('capitalize', u'AaAa', u'Aaaa')
 
 test('count', u'aaa', 3, u'a')
 test('count', u'aaa', 0, u'b')
index 5c193dda4ce837e7fc2cc25eebb9cb726ba80a0d..7b12594f72f4b89df5d7a9b7416b5a0247ec8bf6 100644 (file)
@@ -2631,11 +2631,25 @@ int fixswapcase(PyUnicodeObject *self)
 static 
 int fixcapitalize(PyUnicodeObject *self)
 {
-    if (self->length > 0 && Py_UNICODE_ISLOWER(self->str[0])) {
-       self->str[0] = Py_UNICODE_TOUPPER(self->str[0]);
-       return 1;
+    int len = self->length;
+    Py_UNICODE *s = self->str;
+    int status = 0;
+    
+    if (len == 0)
+       return 0;
+    if (Py_UNICODE_ISLOWER(*s)) {
+       *s = Py_UNICODE_TOUPPER(*s);
+       status = 1;
     }
-    return 0;
+    s++;
+    while (--len > 0) {
+        if (Py_UNICODE_ISUPPER(*s)) {
+            *s = Py_UNICODE_TOLOWER(*s);
+            status = 1;
+        }
+        s++;
+    }
+    return status;
 }
 
 static