summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-05-28 23:08:45 (GMT)
committerGuido van Rossum <guido@python.org>1996-05-28 23:08:45 (GMT)
commit2539528810e844c9ee293bb46b4deb58a9e38447 (patch)
treec87a7ef89f81c57d5e32dd28cc0b526a3c4f8e45 /Lib
parenta13edb489bf5bd7fe65436c2370ae6734481825b (diff)
downloadcpython-2539528810e844c9ee293bb46b4deb58a9e38447.zip
cpython-2539528810e844c9ee293bb46b4deb58a9e38447.tar.gz
cpython-2539528810e844c9ee293bb46b4deb58a9e38447.tar.bz2
add translate() -- which was in strop per release 1.3
Diffstat (limited to 'Lib')
-rw-r--r--Lib/string.py8
-rw-r--r--Lib/stringold.py8
2 files changed, 16 insertions, 0 deletions
diff --git a/Lib/string.py b/Lib/string.py
index 8ad900b..c959347 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -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.
diff --git a/Lib/stringold.py b/Lib/stringold.py
index 8ad900b..c959347 100644
--- a/Lib/stringold.py
+++ b/Lib/stringold.py
@@ -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.