blob: 2ee0ad950afe403e1eae1db0779709868170cbff [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# module 'string' -- A collection of string operations
2
Barry Warsaw226ae6c1999-10-12 19:54:53 +00003# Warning: most of the code you see here isn't normally used nowadays. With
4# Python 1.6, many of these functions are implemented as methods on the
5# standard string object. They used to be implemented by a built-in module
6# called strop, but strop is now obsolete itself.
Guido van Rossumc6360141990-10-13 19:23:40 +00007
Guido van Rossum20032041997-12-29 19:26:28 +00008"""Common string manipulations.
9
10Public module variables:
11
12whitespace -- a string containing all characters considered whitespace
13lowercase -- a string containing all characters considered lowercase letters
14uppercase -- a string containing all characters considered uppercase letters
15letters -- a string containing all characters considered letters
16digits -- a string containing all characters considered decimal digits
17hexdigits -- a string containing all characters considered hexadecimal digits
18octdigits -- a string containing all characters considered octal digits
19
20"""
21
Guido van Rossumc6360141990-10-13 19:23:40 +000022# Some strings for ctype-style character classification
Guido van Rossum8e2ec561993-07-29 09:37:38 +000023whitespace = ' \t\n\r\v\f'
Guido van Rossumc6360141990-10-13 19:23:40 +000024lowercase = 'abcdefghijklmnopqrstuvwxyz'
25uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
26letters = lowercase + uppercase
27digits = '0123456789'
28hexdigits = digits + 'abcdef' + 'ABCDEF'
29octdigits = '01234567'
30
31# Case conversion helpers
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000032_idmap = ''
33for i in range(256): _idmap = _idmap + chr(i)
Guido van Rossumc6360141990-10-13 19:23:40 +000034del i
35
Guido van Rossum710c3521994-08-17 13:16:11 +000036# Backward compatible names for exceptions
37index_error = ValueError
38atoi_error = ValueError
39atof_error = ValueError
40atol_error = ValueError
41
Guido van Rossumc6360141990-10-13 19:23:40 +000042# convert UPPER CASE letters to lower case
43def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000044 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000045
Barry Warsaw226ae6c1999-10-12 19:54:53 +000046 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000047
Barry Warsaw226ae6c1999-10-12 19:54:53 +000048 """
49 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000050
51# Convert lower case letters to UPPER CASE
52def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000053 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000054
Barry Warsaw226ae6c1999-10-12 19:54:53 +000055 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000056
Barry Warsaw226ae6c1999-10-12 19:54:53 +000057 """
58 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000059
60# Swap lower case letters and UPPER CASE
61def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000062 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000063
Barry Warsaw226ae6c1999-10-12 19:54:53 +000064 Return a copy of the string s with upper case characters
65 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000066
Barry Warsaw226ae6c1999-10-12 19:54:53 +000067 """
68 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000069
70# Strip leading and trailing tabs and spaces
71def strip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000072 """strip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000073
Barry Warsaw226ae6c1999-10-12 19:54:53 +000074 Return a copy of the string s with leading and trailing
75 whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000076
Barry Warsaw226ae6c1999-10-12 19:54:53 +000077 """
78 return s.strip()
Guido van Rossumc6360141990-10-13 19:23:40 +000079
Guido van Rossum306a8a61996-08-08 18:40:59 +000080# Strip leading tabs and spaces
81def lstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000082 """lstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000083
Barry Warsaw226ae6c1999-10-12 19:54:53 +000084 Return a copy of the string s with leading whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000085
Barry Warsaw226ae6c1999-10-12 19:54:53 +000086 """
87 return s.lstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000088
89# Strip trailing tabs and spaces
90def rstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091 """rstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000092
Barry Warsaw226ae6c1999-10-12 19:54:53 +000093 Return a copy of the string s with trailing whitespace
94 removed.
Guido van Rossum20032041997-12-29 19:26:28 +000095
Barry Warsaw226ae6c1999-10-12 19:54:53 +000096 """
97 return s.rstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000098
99
Guido van Rossumc6360141990-10-13 19:23:40 +0000100# Split a string into a list of space/tab-separated words
Guido van Rossum306a8a61996-08-08 18:40:59 +0000101def split(s, sep=None, maxsplit=0):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000102 """split(str [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000103
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000104 Return a list of the words in the string s, using sep as the
105 delimiter string. If maxsplit is nonzero, splits into at most
106 maxsplit words If sep is not specified, any whitespace string
107 is a separator. Maxsplit defaults to 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000108
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000109 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000110
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000111 """
112 return s.split(sep, maxsplit)
113splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000114
Guido van Rossum2ab19921995-06-22 18:58:00 +0000115# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000116def join(words, sep = ' '):
117 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000118
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000119 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000120 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000121 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000122
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000123 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000124
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000125 """
126 return sep.join(words)
127joinfields = join
128
129# for a little bit of speed
130_apply = apply
Guido van Rossumfac38b71991-04-07 13:42:19 +0000131
Guido van Rossumd3166071993-05-24 14:16:22 +0000132# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000133def index(s, *args):
134 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000135
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000136 Like find but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000137
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138 """
139 return _apply(s.index, args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000140
Guido van Rossume65cce51993-11-08 15:05:21 +0000141# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000142def rindex(s, *args):
143 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000144
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000145 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000146
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000147 """
148 return _apply(s.rindex, args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149
150# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000151def count(s, *args):
152 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000153
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000154 Return the number of occurrences of substring sub in string
155 s[start:end]. Optional arguments start and end are
156 interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000157
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000158 """
159 return _apply(s.count, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000160
Guido van Rossumd3166071993-05-24 14:16:22 +0000161# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000162def find(s, *args):
163 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000164
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000165 Return the lowest index in s where substring sub is found,
166 such that sub is contained within s[start,end]. Optional
167 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000168
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000169 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000170
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000171 """
172 return _apply(s.find, args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000173
Guido van Rossume65cce51993-11-08 15:05:21 +0000174# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000175def rfind(s, *args):
176 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000177
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178 Return the highest index in s where substring sub is found,
179 such that sub is contained within s[start,end]. Optional
180 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000181
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000182 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000183
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000184 """
185 return _apply(s.rfind, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000186
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000187# for a bit of speed
188_float = float
189_int = int
190_long = long
191_StringType = type('')
Guido van Rossumd0753e21997-12-10 22:59:55 +0000192
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000193# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000194def atof(s):
195 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000196
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000197 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000198
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000199 """
200 if type(s) == _StringType:
Fred Drake13a2c272000-02-10 17:17:14 +0000201 return _float(s)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000202 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000203 raise TypeError('argument 1: expected string, %s found' %
204 type(s).__name__)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000205
Guido van Rossumc6360141990-10-13 19:23:40 +0000206# Convert string to integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000207def atoi(*args):
208 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000209
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000210 Return the integer represented by the string s in the given
211 base, which defaults to 10. The string s must consist of one
212 or more digits, possibly preceded by a sign. If base is 0, it
213 is chosen from the leading characters of s, 0 for octal, 0x or
214 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
215 accepted.
Guido van Rossum20032041997-12-29 19:26:28 +0000216
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000217 """
218 try:
Fred Drake13a2c272000-02-10 17:17:14 +0000219 s = args[0]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000220 except IndexError:
Fred Drake13a2c272000-02-10 17:17:14 +0000221 raise TypeError('function requires at least 1 argument: %d given' %
222 len(args))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000223 # Don't catch type error resulting from too many arguments to int(). The
224 # error message isn't compatible but the error type is, and this function
225 # is complicated enough already.
226 if type(s) == _StringType:
Fred Drake13a2c272000-02-10 17:17:14 +0000227 return _apply(_int, args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000228 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000229 raise TypeError('argument 1: expected string, %s found' %
230 type(s).__name__)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000231
Guido van Rossumc6360141990-10-13 19:23:40 +0000232
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000233# Convert string to long integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000234def atol(*args):
235 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000236
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000237 Return the long integer represented by the string s in the
238 given base, which defaults to 10. The string s must consist
239 of one or more digits, possibly preceded by a sign. If base
240 is 0, it is chosen from the leading characters of s, 0 for
241 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
242 0x or 0X is accepted. A trailing L or l is not accepted,
243 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000244
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000245 """
246 try:
Fred Drake13a2c272000-02-10 17:17:14 +0000247 s = args[0]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000248 except IndexError:
Fred Drake13a2c272000-02-10 17:17:14 +0000249 raise TypeError('function requires at least 1 argument: %d given' %
250 len(args))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000251 # Don't catch type error resulting from too many arguments to long(). The
252 # error message isn't compatible but the error type is, and this function
253 # is complicated enough already.
254 if type(s) == _StringType:
Fred Drake13a2c272000-02-10 17:17:14 +0000255 return _apply(_long, args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000256 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000257 raise TypeError('argument 1: expected string, %s found' %
258 type(s).__name__)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000259
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000260
Guido van Rossumc6360141990-10-13 19:23:40 +0000261# Left-justify a string
262def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000263 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000264
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000265 Return a left-justified version of s, in a field of the
266 specified width, padded with spaces as needed. The string is
267 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000268
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000269 """
270 n = width - len(s)
271 if n <= 0: return s
272 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000273
274# Right-justify a string
275def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000276 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000277
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000278 Return a right-justified version of s, in a field of the
279 specified width, padded with spaces as needed. The string is
280 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000281
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000282 """
283 n = width - len(s)
284 if n <= 0: return s
285 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000286
287# Center a string
288def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000289 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000290
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000291 Return a center version of s, in a field of the specified
292 width. padded with spaces as needed. The string is never
293 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000294
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000295 """
296 n = width - len(s)
297 if n <= 0: return s
298 half = n/2
299 if n%2 and width%2:
Fred Drake13a2c272000-02-10 17:17:14 +0000300 # This ensures that center(center(s, i), j) = center(s, j)
301 half = half+1
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000302 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000303
304# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
305# Decadent feature: the argument may be a string or a number
306# (Use of this is deprecated; it should be a string as with ljust c.s.)
307def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000308 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000309
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000310 Pad a numeric string x with zeros on the left, to fill a field
311 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000312
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000313 """
314 if type(x) == type(''): s = x
315 else: s = `x`
316 n = len(s)
317 if n >= width: return s
318 sign = ''
319 if s[0] in ('-', '+'):
Fred Drake13a2c272000-02-10 17:17:14 +0000320 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000321 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000322
323# Expand tabs in a string.
324# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000325def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000326 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000327
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000328 Return a copy of the string s with all tab characters replaced
329 by the appropriate number of spaces, depending on the current
330 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000331
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000332 """
333 res = line = ''
334 for c in s:
Fred Drake13a2c272000-02-10 17:17:14 +0000335 if c == '\t':
336 c = ' '*(tabsize - len(line) % tabsize)
337 line = line + c
338 if c == '\n':
339 res = res + line
340 line = ''
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000341 return res + line
Guido van Rossum2db91351992-10-18 17:09:59 +0000342
Guido van Rossum25395281996-05-28 23:08:45 +0000343# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000344def translate(s, table, deletions=""):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000345 """translate(s,table [,deletechars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000346
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000347 Return a copy of the string s, where all characters occurring
348 in the optional argument deletechars are removed, and the
349 remaining characters have been mapped through the given
350 translation table, which must be a string of length 256.
Guido van Rossum20032041997-12-29 19:26:28 +0000351
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000352 """
353 return s.translate(table, deletions)
Guido van Rossum2db91351992-10-18 17:09:59 +0000354
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000355# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
356def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000357 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000358
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000359 Return a copy of the string s with only its first character
360 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000361
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000362 """
363 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000364
365# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
366# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000367def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000368 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000369
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000370 Split the argument into words using split, capitalize each
371 word using capitalize, and join the capitalized words using
372 join. Note that this replaces runs of whitespace characters by
373 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000374
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000375 """
376 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000377
Guido van Rossumed7253c1996-07-23 18:12:39 +0000378# Construct a translation string
379_idmapL = None
380def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000381 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000382
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000383 Return a translation table (a string of 256 bytes long)
384 suitable for use in string.translate. The strings frm and to
385 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000386
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000387 """
388 if len(fromstr) != len(tostr):
Fred Drake13a2c272000-02-10 17:17:14 +0000389 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000390 global _idmapL
391 if not _idmapL:
Fred Drake13a2c272000-02-10 17:17:14 +0000392 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000393 L = _idmapL[:]
394 fromstr = map(ord, fromstr)
395 for i in range(len(fromstr)):
Fred Drake13a2c272000-02-10 17:17:14 +0000396 L[fromstr[i]] = tostr[i]
Eric S. Raymonde37340e2001-02-09 16:56:44 +0000397 return join(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000398
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000399# Substring replacement (global)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000400def replace(s, old, new, maxsplit=0):
401 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000402
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000403 Return a copy of string str with all occurrences of substring
404 old replaced by new. If the optional argument maxsplit is
405 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000406
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000407 """
408 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000409
410
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000411# XXX: transitional
412#
413# If string objects do not have methods, then we need to use the old string.py
414# library, which uses strop for many more things than just the few outlined
415# below.
416try:
417 ''.upper
418except AttributeError:
419 from stringold import *
420
Guido van Rossum2db91351992-10-18 17:09:59 +0000421# Try importing optional built-in module "strop" -- if it exists,
422# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000423# It also defines values for whitespace, lowercase and uppercase
424# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000425
426try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000427 from strop import maketrans, lowercase, uppercase, whitespace
428 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000429except ImportError:
Fred Drake13a2c272000-02-10 17:17:14 +0000430 pass # Use the original versions