blob: c0f51479cd12f45209ad4691d8aa6878d0ab2fb1 [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# module 'string' -- A collection of string operations
2
Guido van Rossume7113b61993-03-29 11:30:50 +00003# Warning: most of the code you see here isn't normally used nowadays.
4# At the end of this file most functions are replaced by built-in
5# functions imported from built-in module "strop".
Guido van Rossumc6360141990-10-13 19:23:40 +00006
7# Some strings for ctype-style character classification
Guido van Rossum8e2ec561993-07-29 09:37:38 +00008whitespace = ' \t\n\r\v\f'
Guido van Rossumc6360141990-10-13 19:23:40 +00009lowercase = 'abcdefghijklmnopqrstuvwxyz'
10uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
11letters = lowercase + uppercase
12digits = '0123456789'
13hexdigits = digits + 'abcdef' + 'ABCDEF'
14octdigits = '01234567'
15
16# Case conversion helpers
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000017_idmap = ''
18for i in range(256): _idmap = _idmap + chr(i)
19_lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:]
20_upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
21_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
Guido van Rossumc6360141990-10-13 19:23:40 +000022del i
23
Guido van Rossum710c3521994-08-17 13:16:11 +000024# Backward compatible names for exceptions
25index_error = ValueError
26atoi_error = ValueError
27atof_error = ValueError
28atol_error = ValueError
29
Guido van Rossumc6360141990-10-13 19:23:40 +000030# convert UPPER CASE letters to lower case
31def lower(s):
32 res = ''
33 for c in s:
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000034 res = res + _lower[ord(c)]
Guido van Rossumc6360141990-10-13 19:23:40 +000035 return res
36
37# Convert lower case letters to UPPER CASE
38def upper(s):
39 res = ''
40 for c in s:
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000041 res = res + _upper[ord(c)]
Guido van Rossumc6360141990-10-13 19:23:40 +000042 return res
43
44# Swap lower case letters and UPPER CASE
45def swapcase(s):
46 res = ''
47 for c in s:
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000048 res = res + _swapcase[ord(c)]
Guido van Rossumc6360141990-10-13 19:23:40 +000049 return res
50
51# Strip leading and trailing tabs and spaces
52def strip(s):
53 i, j = 0, len(s)
54 while i < j and s[i] in whitespace: i = i+1
55 while i < j and s[j-1] in whitespace: j = j-1
56 return s[i:j]
57
Guido van Rossum306a8a61996-08-08 18:40:59 +000058# Strip leading tabs and spaces
59def lstrip(s):
60 i, j = 0, len(s)
61 while i < j and s[i] in whitespace: i = i+1
62 return s[i:j]
63
64# Strip trailing tabs and spaces
65def rstrip(s):
66 i, j = 0, len(s)
67 while i < j and s[j-1] in whitespace: j = j-1
68 return s[i:j]
69
70
Guido van Rossumc6360141990-10-13 19:23:40 +000071# Split a string into a list of space/tab-separated words
72# NB: split(s) is NOT the same as splitfields(s, ' ')!
Guido van Rossum306a8a61996-08-08 18:40:59 +000073def split(s, sep=None, maxsplit=0):
74 if sep is not None: return splitfields(s, sep, maxsplit)
Guido van Rossumc6360141990-10-13 19:23:40 +000075 res = []
76 i, n = 0, len(s)
Guido van Rossum06ba34c1997-12-01 15:25:19 +000077 if maxsplit <= 0: maxsplit = n
78 count = 0
Guido van Rossumc6360141990-10-13 19:23:40 +000079 while i < n:
80 while i < n and s[i] in whitespace: i = i+1
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000081 if i == n: break
Guido van Rossum06ba34c1997-12-01 15:25:19 +000082 if count >= maxsplit:
83 res.append(s[i:])
84 break
Guido van Rossumc6360141990-10-13 19:23:40 +000085 j = i
86 while j < n and s[j] not in whitespace: j = j+1
Guido van Rossum06ba34c1997-12-01 15:25:19 +000087 count = count + 1
Guido van Rossumc6360141990-10-13 19:23:40 +000088 res.append(s[i:j])
89 i = j
90 return res
91
92# Split a list into fields separated by a given string
93# NB: splitfields(s, ' ') is NOT the same as split(s)!
Guido van Rossum7a461e51992-09-20 21:41:09 +000094# splitfields(s, '') returns [s] (in analogy with split() in nawk)
Guido van Rossum306a8a61996-08-08 18:40:59 +000095def splitfields(s, sep=None, maxsplit=0):
96 if sep is None: return split(s, None, maxsplit)
Guido van Rossumc6360141990-10-13 19:23:40 +000097 res = []
Guido van Rossumc6360141990-10-13 19:23:40 +000098 nsep = len(sep)
Guido van Rossumae507a41992-08-19 16:49:58 +000099 if nsep == 0:
Guido van Rossum7a461e51992-09-20 21:41:09 +0000100 return [s]
Guido van Rossumae507a41992-08-19 16:49:58 +0000101 ns = len(s)
Guido van Rossum06ba34c1997-12-01 15:25:19 +0000102 if maxsplit <= 0: maxsplit = ns
Guido van Rossumc6360141990-10-13 19:23:40 +0000103 i = j = 0
Guido van Rossum306a8a61996-08-08 18:40:59 +0000104 count = 0
Guido van Rossumc6360141990-10-13 19:23:40 +0000105 while j+nsep <= ns:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000106 if s[j:j+nsep] == sep:
Guido van Rossum306a8a61996-08-08 18:40:59 +0000107 count = count + 1
Guido van Rossumc6360141990-10-13 19:23:40 +0000108 res.append(s[i:j])
109 i = j = j + nsep
Guido van Rossum06ba34c1997-12-01 15:25:19 +0000110 if count >= maxsplit: break
Guido van Rossum306a8a61996-08-08 18:40:59 +0000111
Guido van Rossumc6360141990-10-13 19:23:40 +0000112 else:
113 j = j + 1
114 res.append(s[i:])
115 return res
116
Guido van Rossumfac38b71991-04-07 13:42:19 +0000117# Join words with spaces between them
Guido van Rossum2ab19921995-06-22 18:58:00 +0000118def join(words, sep = ' '):
119 return joinfields(words, sep)
Guido van Rossumfac38b71991-04-07 13:42:19 +0000120
Guido van Rossum2ab19921995-06-22 18:58:00 +0000121# Join fields with optional separator
122def joinfields(words, sep = ' '):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000123 res = ''
124 for w in words:
125 res = res + (sep + w)
126 return res[len(sep):]
127
Guido van Rossumd3166071993-05-24 14:16:22 +0000128# Find substring, raise exception if not found
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000129def index(s, sub, i = 0, last=None):
Guido van Rossum15105651997-10-20 23:31:15 +0000130 if last is None: last = len(s)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000131 res = find(s, sub, i, last)
Guido van Rossum710c3521994-08-17 13:16:11 +0000132 if res < 0:
133 raise ValueError, 'substring not found in string.index'
134 return res
Guido van Rossumd3166071993-05-24 14:16:22 +0000135
Guido van Rossume65cce51993-11-08 15:05:21 +0000136# Find last substring, raise exception if not found
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000137def rindex(s, sub, i = 0, last=None):
Guido van Rossum15105651997-10-20 23:31:15 +0000138 if last is None: last = len(s)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000139 res = rfind(s, sub, i, last)
Guido van Rossum710c3521994-08-17 13:16:11 +0000140 if res < 0:
141 raise ValueError, 'substring not found in string.index'
142 return res
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143
144# Count non-overlapping occurrences of substring
Guido van Rossum15105651997-10-20 23:31:15 +0000145def count(s, sub, i = 0, last=None):
146 Slen = len(s) # cache this value, for speed
147 if last is None:
148 last = Slen
149 elif last < 0:
150 last = max(0, last + Slen)
151 elif last > Slen:
152 last = Slen
153 if i < 0: i = max(0, i + Slen)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000154 n = len(sub)
Guido van Rossum15105651997-10-20 23:31:15 +0000155 m = last + 1 - n
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156 if n == 0: return m-i
157 r = 0
158 while i < m:
159 if sub == s[i:i+n]:
160 r = r+1
161 i = i+n
162 else:
163 i = i+1
Guido van Rossume65cce51993-11-08 15:05:21 +0000164 return r
165
Guido van Rossumd3166071993-05-24 14:16:22 +0000166# Find substring, return -1 if not found
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000167def find(s, sub, i = 0, last=None):
168 Slen = len(s) # cache this value, for speed
Guido van Rossum15105651997-10-20 23:31:15 +0000169 if last is None:
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000170 last = Slen
171 elif last < 0:
172 last = max(0, last + Slen)
173 elif last > Slen:
174 last = Slen
175 if i < 0: i = max(0, i + Slen)
Guido van Rossum710c3521994-08-17 13:16:11 +0000176 n = len(sub)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000177 m = last + 1 - n
Guido van Rossum710c3521994-08-17 13:16:11 +0000178 while i < m:
179 if sub == s[i:i+n]: return i
180 i = i+1
181 return -1
Guido van Rossumc6360141990-10-13 19:23:40 +0000182
Guido van Rossume65cce51993-11-08 15:05:21 +0000183# Find last substring, return -1 if not found
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000184def rfind(s, sub, i = 0, last=None):
185 Slen = len(s) # cache this value, for speed
Guido van Rossum15105651997-10-20 23:31:15 +0000186 if last is None:
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000187 last = Slen
188 elif last < 0:
189 last = max(0, last + Slen)
190 elif last > Slen:
191 last = Slen
192 if i < 0: i = max(0, i + Slen)
Guido van Rossum710c3521994-08-17 13:16:11 +0000193 n = len(sub)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000194 m = last + 1 - n
Guido van Rossum710c3521994-08-17 13:16:11 +0000195 r = -1
196 while i < m:
197 if sub == s[i:i+n]: r = i
198 i = i+1
199 return r
Guido van Rossume65cce51993-11-08 15:05:21 +0000200
Guido van Rossumd0753e21997-12-10 22:59:55 +0000201# "Safe" environment for eval()
202safe_env = {"__builtins__": {}}
203
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000204# Convert string to float
Guido van Rossum9694fca1997-10-22 21:00:49 +0000205re = None
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000206def atof(str):
Guido van Rossum9694fca1997-10-22 21:00:49 +0000207 global re
208 if re is None:
Guido van Rossum90d62ab1997-12-10 22:35:02 +0000209 # Don't fail if re doesn't exist -- just skip the syntax check
210 try:
211 import re
212 except ImportError:
213 re = 0
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000214 sign = ''
Guido van Rossum9694fca1997-10-22 21:00:49 +0000215 s = strip(str)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000216 if s and s[0] in '+-':
217 sign = s[0]
218 s = s[1:]
Guido van Rossum710c3521994-08-17 13:16:11 +0000219 if not s:
220 raise ValueError, 'non-float argument to string.atof'
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000221 while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
Guido van Rossum90d62ab1997-12-10 22:35:02 +0000222 if re and not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
Guido van Rossum710c3521994-08-17 13:16:11 +0000223 raise ValueError, 'non-float argument to string.atof'
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000224 try:
Guido van Rossumd0753e21997-12-10 22:59:55 +0000225 return float(eval(sign + s, safe_env))
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000226 except SyntaxError:
Guido van Rossum710c3521994-08-17 13:16:11 +0000227 raise ValueError, 'non-float argument to string.atof'
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000228
Guido van Rossumc6360141990-10-13 19:23:40 +0000229# Convert string to integer
Guido van Rossum8c1688e1995-03-14 17:43:02 +0000230def atoi(str, base=10):
231 if base != 10:
232 # We only get here if strop doesn't define atoi()
233 raise ValueError, "this string.atoi doesn't support base != 10"
Guido van Rossum2d4aa4f1992-08-06 22:33:41 +0000234 sign = ''
Guido van Rossumc6360141990-10-13 19:23:40 +0000235 s = str
Guido van Rossumc629d341992-11-05 10:43:02 +0000236 if s and s[0] in '+-':
Guido van Rossum2d4aa4f1992-08-06 22:33:41 +0000237 sign = s[0]
238 s = s[1:]
Guido van Rossum710c3521994-08-17 13:16:11 +0000239 if not s:
240 raise ValueError, 'non-integer argument to string.atoi'
Guido van Rossum2d4aa4f1992-08-06 22:33:41 +0000241 while s[0] == '0' and len(s) > 1: s = s[1:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000242 for c in s:
Guido van Rossum710c3521994-08-17 13:16:11 +0000243 if c not in digits:
244 raise ValueError, 'non-integer argument to string.atoi'
Guido van Rossumd0753e21997-12-10 22:59:55 +0000245 return eval(sign + s, safe_env)
Guido van Rossumc6360141990-10-13 19:23:40 +0000246
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000247# Convert string to long integer
Guido van Rossum8c1688e1995-03-14 17:43:02 +0000248def atol(str, base=10):
249 if base != 10:
250 # We only get here if strop doesn't define atol()
251 raise ValueError, "this string.atol doesn't support base != 10"
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000252 sign = ''
253 s = str
254 if s and s[0] in '+-':
255 sign = s[0]
256 s = s[1:]
Guido van Rossum710c3521994-08-17 13:16:11 +0000257 if not s:
258 raise ValueError, 'non-integer argument to string.atol'
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000259 while s[0] == '0' and len(s) > 1: s = s[1:]
260 for c in s:
Guido van Rossum710c3521994-08-17 13:16:11 +0000261 if c not in digits:
262 raise ValueError, 'non-integer argument to string.atol'
Guido van Rossumd0753e21997-12-10 22:59:55 +0000263 return eval(sign + s + 'L', safe_env)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000264
Guido van Rossumc6360141990-10-13 19:23:40 +0000265# Left-justify a string
266def ljust(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000267 n = width - len(s)
268 if n <= 0: return s
269 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000270
271# Right-justify a string
272def rjust(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000273 n = width - len(s)
274 if n <= 0: return s
275 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000276
277# Center a string
278def center(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000279 n = width - len(s)
280 if n <= 0: return s
281 half = n/2
282 if n%2 and width%2:
283 # This ensures that center(center(s, i), j) = center(s, j)
284 half = half+1
285 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000286
287# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
288# Decadent feature: the argument may be a string or a number
289# (Use of this is deprecated; it should be a string as with ljust c.s.)
290def zfill(x, width):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000291 if type(x) == type(''): s = x
Guido van Rossumc6360141990-10-13 19:23:40 +0000292 else: s = `x`
293 n = len(s)
294 if n >= width: return s
295 sign = ''
Guido van Rossum333c2e01991-08-16 13:29:03 +0000296 if s[0] in ('-', '+'):
297 sign, s = s[0], s[1:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000298 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000299
300# Expand tabs in a string.
301# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000302def expandtabs(s, tabsize=8):
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000303 res = line = ''
304 for c in s:
305 if c == '\t':
306 c = ' '*(tabsize - len(line)%tabsize)
307 line = line + c
308 if c == '\n':
309 res = res + line
310 line = ''
311 return res + line
Guido van Rossum2db91351992-10-18 17:09:59 +0000312
Guido van Rossum25395281996-05-28 23:08:45 +0000313# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000314def translate(s, table, deletions=""):
315 if type(table) != type('') or len(table) != 256:
316 raise TypeError, "translation table must be 256 characters long"
317 res = ""
318 for c in s:
319 if c not in deletions:
320 res = res + table[ord(c)]
321 return res
Guido van Rossum2db91351992-10-18 17:09:59 +0000322
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000323# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
324def capitalize(s):
Guido van Rossumed7253c1996-07-23 18:12:39 +0000325 return upper(s[:1]) + lower(s[1:])
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000326
327# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
328# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000329def capwords(s, sep=None):
Guido van Rossumf480c671996-08-26 15:55:00 +0000330 return join(map(capitalize, split(s, sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000331
Guido van Rossumed7253c1996-07-23 18:12:39 +0000332# Construct a translation string
333_idmapL = None
334def maketrans(fromstr, tostr):
335 if len(fromstr) != len(tostr):
336 raise ValueError, "maketrans arguments must have same length"
337 global _idmapL
338 if not _idmapL:
339 _idmapL = map(None, _idmap)
340 L = _idmapL[:]
341 fromstr = map(ord, fromstr)
342 for i in range(len(fromstr)):
343 L[fromstr[i]] = tostr[i]
344 return joinfields(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000345
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000346# Substring replacement (global)
Guido van Rossum21aa0ef1997-04-02 05:49:46 +0000347def replace(str, old, new, maxsplit=0):
348 return joinfields(splitfields(str, old, maxsplit), new)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000349
350
Guido van Rossum2db91351992-10-18 17:09:59 +0000351# Try importing optional built-in module "strop" -- if it exists,
352# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000353# It also defines values for whitespace, lowercase and uppercase
354# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000355
356try:
357 from strop import *
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000358 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000359except ImportError:
360 pass # Use the original, slow versions