diff options
author | Guido van Rossum <guido@python.org> | 1992-09-11 23:55:51 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1992-09-11 23:55:51 (GMT) |
commit | 5c85062e1ce4c7e51daaad1a4eb3f66f6b5a0ea8 (patch) | |
tree | 7232c5445b143972568fc02f5ad11e02cd2383f0 /Modules/stropmodule.c | |
parent | d10d8291f1de5bef74628bd1c766403ee9444dca (diff) | |
download | cpython-5c85062e1ce4c7e51daaad1a4eb3f66f6b5a0ea8.zip cpython-5c85062e1ce4c7e51daaad1a4eb3f66f6b5a0ea8.tar.gz cpython-5c85062e1ce4c7e51daaad1a4eb3f66f6b5a0ea8.tar.bz2 |
Makefile uses $> more often; cgen supports filename argument; added
lower, upper and swapcase to strop; cosmetics.
Diffstat (limited to 'Modules/stropmodule.c')
-rw-r--r-- | Modules/stropmodule.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/Modules/stropmodule.c b/Modules/stropmodule.c index cc2e26a..835c7db 100644 --- a/Modules/stropmodule.c +++ b/Modules/stropmodule.c @@ -181,13 +181,121 @@ strop_strip(self, args) } +#include <ctype.h> + +static object * +strop_lower(self, args) + object *self; /* Not used */ + object *args; +{ + char *s; + int i, n; + object *new; + int changed; + + if (!getargs(args, "s#", &s, &n)) + return NULL; + new = newsizedstringobject(s, n); + if (new == NULL) + return NULL; + s = getstringvalue(new); + changed = 0; + for (i = 0; i < n; i++) { + char c = s[i]; + if (isupper(c)) { + changed = 1; + s[i] = tolower(c); + } + } + if (!changed) { + DECREF(new); + INCREF(args); + return args; + } + return new; +} + + +static object * +strop_upper(self, args) + object *self; /* Not used */ + object *args; +{ + char *s; + int i, n; + object *new; + int changed; + + if (!getargs(args, "s#", &s, &n)) + return NULL; + new = newsizedstringobject(s, n); + if (new == NULL) + return NULL; + s = getstringvalue(new); + changed = 0; + for (i = 0; i < n; i++) { + char c = s[i]; + if (islower(c)) { + changed = 1; + s[i] = toupper(c); + } + } + if (!changed) { + DECREF(new); + INCREF(args); + return args; + } + return new; +} + + +static object * +strop_swapcase(self, args) + object *self; /* Not used */ + object *args; +{ + char *s; + int i, n; + object *new; + int changed; + + if (!getargs(args, "s#", &s, &n)) + return NULL; + new = newsizedstringobject(s, n); + if (new == NULL) + return NULL; + s = getstringvalue(new); + changed = 0; + for (i = 0; i < n; i++) { + char c = s[i]; + if (islower(c)) { + changed = 1; + s[i] = toupper(c); + } + else if (isupper(c)) { + changed = 1; + s[i] = tolower(c); + } + } + if (!changed) { + DECREF(new); + INCREF(args); + return args; + } + return new; +} + + /* List of functions defined in the module */ static struct methodlist strop_methods[] = { {"index", strop_index}, + {"lower", strop_lower}, {"split", strop_split}, {"splitfields", strop_splitfields}, {"strip", strop_strip}, + {"swapcase", strop_swapcase}, + {"upper", strop_upper}, {NULL, NULL} /* sentinel */ }; |