]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
added strop.translate(s, table)
authorGuido van Rossum <guido@python.org>
Wed, 13 Sep 1995 17:39:06 +0000 (17:39 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 13 Sep 1995 17:39:06 +0000 (17:39 +0000)
Modules/stropmodule.c

index 1cee66b3f2b5f779a950cffba6dc53bcb71013c2..a82c3138ed918b9e58589f41bef026f4817be19f 100644 (file)
@@ -503,6 +503,35 @@ strop_atof(self, args)
 }
 
 
+static object *
+strop_translate(self, args)
+       object *self;
+       object *args;
+{
+       char *input, *table, *output;
+       int inlen, tablen;
+       object *result;
+       int i;
+
+       if (!newgetargs(args, "s#s#", &input, &inlen, &table, &tablen))
+               return NULL;
+       if (tablen != 256) {
+               err_setstr(ValueError,
+                          "translation table must be 256 characters long");
+               return NULL;
+       }
+       result = newsizedstringobject((char *)NULL, inlen);
+       if (result == NULL)
+               return NULL;
+       output = getstringvalue(result);
+       for (i = 0; i < inlen; i++) {
+               int c = Py_CHARMASK(*input++);
+               *output++ = table[c];
+       }
+       return result;
+}
+
+
 /* List of functions defined in the module */
 
 static struct methodlist strop_methods[] = {
@@ -518,6 +547,7 @@ static struct methodlist strop_methods[] = {
        {"splitfields", strop_splitfields, 1},
        {"strip",       strop_strip},
        {"swapcase",    strop_swapcase},
+       {"translate",   strop_translate, 1},
        {"upper",       strop_upper},
        {NULL,          NULL}   /* sentinel */
 };