Commit fde66e1b authored by Marc-André Lemburg's avatar Marc-André Lemburg

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.
parent 30be8708
...@@ -53,6 +53,8 @@ def run_method_tests(test): ...@@ -53,6 +53,8 @@ def run_method_tests(test):
test('capitalize', ' hello ', ' hello ') test('capitalize', ' hello ', ' hello ')
test('capitalize', 'hello ', 'Hello ') test('capitalize', 'hello ', 'Hello ')
test('capitalize', 'aaaa', 'Aaaa')
test('capitalize', 'AaAa', 'Aaaa')
test('count', 'aaa', 3, 'a') test('count', 'aaa', 3, 'a')
test('count', 'aaa', 0, 'b') test('count', 'aaa', 0, 'b')
......
...@@ -31,6 +31,8 @@ def test(method, input, output, *args): ...@@ -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'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', 3, u'a')
test('count', u'aaa', 0, u'b') test('count', u'aaa', 0, u'b')
......
...@@ -2631,11 +2631,25 @@ int fixswapcase(PyUnicodeObject *self) ...@@ -2631,11 +2631,25 @@ int fixswapcase(PyUnicodeObject *self)
static static
int fixcapitalize(PyUnicodeObject *self) int fixcapitalize(PyUnicodeObject *self)
{ {
if (self->length > 0 && Py_UNICODE_ISLOWER(self->str[0])) { int len = self->length;
self->str[0] = Py_UNICODE_TOUPPER(self->str[0]); Py_UNICODE *s = self->str;
return 1; 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 static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment