summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-07-23 18:12:39 (GMT)
committerGuido van Rossum <guido@python.org>1996-07-23 18:12:39 (GMT)
commited7253ca507ab2755400b3aab612e18efc7249e2 (patch)
tree84062f003f28bfffd4d54c8acb716191ffb8bcc2 /Lib
parent4dd0bf92e613433cdd7806d46002a9bfe5e53fda (diff)
downloadcpython-ed7253ca507ab2755400b3aab612e18efc7249e2.zip
cpython-ed7253ca507ab2755400b3aab612e18efc7249e2.tar.gz
cpython-ed7253ca507ab2755400b3aab612e18efc7249e2.tar.bz2
Added 3rd optional argument to translate(), a string of characters to delete.
Added maketrans(), a utility to create a translation table.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/string.py32
-rw-r--r--Lib/stringold.py32
2 files changed, 46 insertions, 18 deletions
diff --git a/Lib/string.py b/Lib/string.py
index edf24a4..afa0787 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -254,23 +254,37 @@ def expandtabs(s, tabsize=8):
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
+def translate(s, table, deletions=""):
+ if type(table) != type('') or len(table) != 256:
+ raise TypeError, "translation table must be 256 characters long"
+ res = ""
+ for c in s:
+ if c not in deletions:
+ res = res + table[ord(c)]
+ return res
# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
def capitalize(s):
- return upper(s[:1]) + lower(s[1:])
+ return upper(s[:1]) + lower(s[1:])
# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
# See also regsub.capwords().
def capwords(s):
- return join(map(capitalize, split(s)))
+ return join(map(capitalize, split(s)))
+# Construct a translation string
+_idmapL = None
+def maketrans(fromstr, tostr):
+ if len(fromstr) != len(tostr):
+ raise ValueError, "maketrans arguments must have same length"
+ global _idmapL
+ if not _idmapL:
+ _idmapL = map(None, _idmap)
+ L = _idmapL[:]
+ fromstr = map(ord, fromstr)
+ for i in range(len(fromstr)):
+ L[fromstr[i]] = tostr[i]
+ return joinfields(L, "")
# 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 edf24a4..afa0787 100644
--- a/Lib/stringold.py
+++ b/Lib/stringold.py
@@ -254,23 +254,37 @@ def expandtabs(s, tabsize=8):
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
+def translate(s, table, deletions=""):
+ if type(table) != type('') or len(table) != 256:
+ raise TypeError, "translation table must be 256 characters long"
+ res = ""
+ for c in s:
+ if c not in deletions:
+ res = res + table[ord(c)]
+ return res
# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
def capitalize(s):
- return upper(s[:1]) + lower(s[1:])
+ return upper(s[:1]) + lower(s[1:])
# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
# See also regsub.capwords().
def capwords(s):
- return join(map(capitalize, split(s)))
+ return join(map(capitalize, split(s)))
+# Construct a translation string
+_idmapL = None
+def maketrans(fromstr, tostr):
+ if len(fromstr) != len(tostr):
+ raise ValueError, "maketrans arguments must have same length"
+ global _idmapL
+ if not _idmapL:
+ _idmapL = map(None, _idmap)
+ L = _idmapL[:]
+ fromstr = map(ord, fromstr)
+ for i in range(len(fromstr)):
+ L[fromstr[i]] = tostr[i]
+ return joinfields(L, "")
# Try importing optional built-in module "strop" -- if it exists,
# it redefines some string operations that are 100-1000 times faster.