From fde66e1bcc4431a59f36bad0ca0dd7a848ce794b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lemburg?= Date: Mon, 29 Jan 2001 11:14:16 +0000 Subject: [PATCH] Fixed .capitalize() method of Unicode objects to work like the 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 | 2 ++ Lib/test/test_unicode.py | 2 ++ Objects/unicodeobject.c | 22 ++++++++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index c3010d6517b..2e912c8ab3b 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -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') diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 855b0a2edb1..2a24255b049 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -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') diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 5c193dda4ce..7b12594f72f 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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