]> granicus.if.org Git - python/commitdiff
add translate() -- which was in strop per release 1.3
authorGuido van Rossum <guido@python.org>
Tue, 28 May 1996 23:08:45 +0000 (23:08 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 28 May 1996 23:08:45 +0000 (23:08 +0000)
Lib/string.py
Lib/stringold.py

index 8ad900b99526ecae58d8125c19df2b9e592a6f34..c9593476df8044ae7370d4976c5bc00f41627181 100644 (file)
@@ -253,6 +253,14 @@ def expandtabs(s, tabsize=8):
                        line = ''
        return res + line
 
+# Character translation through look-up table.
+def translate(s, table):
+    if type(table) != type('') or len(table) != 256:
+       raise TypeError, "translation table must be 256-char string"
+    res = ""
+    for c in s:
+       res = res + table[ord(c)]
+    return res
 
 # Try importing optional built-in module "strop" -- if it exists,
 # it redefines some string operations that are 100-1000 times faster.
index 8ad900b99526ecae58d8125c19df2b9e592a6f34..c9593476df8044ae7370d4976c5bc00f41627181 100644 (file)
@@ -253,6 +253,14 @@ def expandtabs(s, tabsize=8):
                        line = ''
        return res + line
 
+# Character translation through look-up table.
+def translate(s, table):
+    if type(table) != type('') or len(table) != 256:
+       raise TypeError, "translation table must be 256-char string"
+    res = ""
+    for c in s:
+       res = res + table[ord(c)]
+    return res
 
 # Try importing optional built-in module "strop" -- if it exists,
 # it redefines some string operations that are 100-1000 times faster.