]> granicus.if.org Git - python/commitdiff
added capitalize()
authorGuido van Rossum <guido@python.org>
Wed, 12 Jun 1996 04:24:52 +0000 (04:24 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 12 Jun 1996 04:24:52 +0000 (04:24 +0000)
Modules/stropmodule.c

index a82c3138ed918b9e58589f41bef026f4817be19f..52d4cee3668392c5f6a5a3a76204a3a6c2ecee2e 100644 (file)
@@ -367,6 +367,50 @@ strop_upper(self, args)
 }
 
 
+static object *
+strop_capitalize(self, args)
+       object *self; /* Not used */
+       object *args;
+{
+       char *s, *s_new;
+       int i, n;
+       object *new;
+       int changed;
+
+       if (!getargs(args, "s#", &s, &n))
+               return NULL;
+       new = newsizedstringobject(NULL, n);
+       if (new == NULL)
+               return NULL;
+       s_new = getstringvalue(new);
+       changed = 0;
+       {
+               int c = Py_CHARMASK(*s++);
+               if (islower(c)) {
+                       changed = 1;
+                       *s_new = toupper(c);
+               } else
+                       *s_new = c;
+               s_new++;
+       }
+       for (i = 1; i < n; i++) {
+               int c = Py_CHARMASK(*s++);
+               if (isupper(c)) {
+                       changed = 1;
+                       *s_new = tolower(c);
+               } else
+                       *s_new = c;
+               s_new++;
+       }
+       if (!changed) {
+               DECREF(new);
+               INCREF(args);
+               return args;
+       }
+       return new;
+}
+
+
 static object *
 strop_swapcase(self, args)
        object *self; /* Not used */
@@ -538,6 +582,7 @@ static struct methodlist strop_methods[] = {
        {"atof",        strop_atof},
        {"atoi",        strop_atoi},
        {"atol",        strop_atol},
+       {"capitalize",  strop_capitalize},
        {"find",        strop_find},
        {"join",        strop_joinfields, 1},
        {"joinfields",  strop_joinfields, 1},