string.py revision 06ba34c5d47751f9a5ae8e16bf1a7cf12c871609
1# module 'string' -- A collection of string operations
2
3# 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".
6
7# Some strings for ctype-style character classification
8whitespace = ' \t\n\r\v\f'
9lowercase = 'abcdefghijklmnopqrstuvwxyz'
10uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
11letters = lowercase + uppercase
12digits = '0123456789'
13hexdigits = digits + 'abcdef' + 'ABCDEF'
14octdigits = '01234567'
15
16# Case conversion helpers
17_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:]
22del i
23
24# Backward compatible names for exceptions
25index_error = ValueError
26atoi_error = ValueError
27atof_error = ValueError
28atol_error = ValueError
29
30# convert UPPER CASE letters to lower case
31def lower(s):
32	res = ''
33	for c in s:
34		res = res + _lower[ord(c)]
35	return res
36
37# Convert lower case letters to UPPER CASE
38def upper(s):
39	res = ''
40	for c in s:
41		res = res + _upper[ord(c)]
42	return res
43
44# Swap lower case letters and UPPER CASE
45def swapcase(s):
46	res = ''
47	for c in s:
48		res = res + _swapcase[ord(c)]
49	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
58# 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
71# Split a string into a list of space/tab-separated words
72# NB: split(s) is NOT the same as splitfields(s, ' ')!
73def split(s, sep=None, maxsplit=0):
74	if sep is not None: return splitfields(s, sep, maxsplit)
75	res = []
76	i, n = 0, len(s)
77	if maxsplit <= 0: maxsplit = n
78	count = 0
79	while i < n:
80		while i < n and s[i] in whitespace: i = i+1
81		if i == n: break
82		if count >= maxsplit:
83		    res.append(s[i:])
84		    break
85		j = i
86		while j < n and s[j] not in whitespace: j = j+1
87		count = count + 1
88		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)!
94# splitfields(s, '') returns [s] (in analogy with split() in nawk)
95def splitfields(s, sep=None, maxsplit=0):
96	if sep is None: return split(s, None, maxsplit)
97	res = []
98	nsep = len(sep)
99	if nsep == 0:
100		return [s]
101	ns = len(s)
102	if maxsplit <= 0: maxsplit = ns
103	i = j = 0
104	count = 0
105	while j+nsep <= ns:
106		if s[j:j+nsep] == sep:
107			count = count + 1
108			res.append(s[i:j])
109			i = j = j + nsep
110			if count >= maxsplit: break
111
112		else:
113			j = j + 1
114	res.append(s[i:])
115	return res
116
117# Join words with spaces between them
118def join(words, sep = ' '):
119	return joinfields(words, sep)
120
121# Join fields with optional separator
122def joinfields(words, sep = ' '):
123	res = ''
124	for w in words:
125		res = res + (sep + w)
126	return res[len(sep):]
127
128# Find substring, raise exception if not found
129def index(s, sub, i = 0, last=None):
130	if last is None: last = len(s)
131	res = find(s, sub, i, last)
132	if res < 0:
133		raise ValueError, 'substring not found in string.index'
134	return res
135
136# Find last substring, raise exception if not found
137def rindex(s, sub, i = 0, last=None):
138	if last is None: last = len(s)
139	res = rfind(s, sub, i, last)
140	if res < 0:
141		raise ValueError, 'substring not found in string.index'
142	return res
143
144# Count non-overlapping occurrences of substring
145def 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)
154	n = len(sub)
155	m = last + 1 - n
156	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
164	return r
165
166# Find substring, return -1 if not found
167def find(s, sub, i = 0, last=None):
168	Slen = len(s)  # cache this value, for speed
169	if last is None:
170		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)
176	n = len(sub)
177	m = last + 1 - n
178	while i < m:
179		if sub == s[i:i+n]: return i
180		i = i+1
181	return -1
182
183# Find last substring, return -1 if not found
184def rfind(s, sub, i = 0, last=None):
185	Slen = len(s)  # cache this value, for speed
186	if last is None:
187		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)
193	n = len(sub)
194	m = last + 1 - n
195	r = -1
196	while i < m:
197		if sub == s[i:i+n]: r = i
198		i = i+1
199	return r
200
201# Convert string to float
202re = None
203def atof(str):
204	global re
205	if re is None:
206		import re
207	sign = ''
208	s = strip(str)
209	if s and s[0] in '+-':
210		sign = s[0]
211		s = s[1:]
212	if not s:
213		raise ValueError, 'non-float argument to string.atof'
214	while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
215	if not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
216		raise ValueError, 'non-float argument to string.atof'
217	try:
218		return float(eval(sign + s))
219	except SyntaxError:
220		raise ValueError, 'non-float argument to string.atof'
221
222# Convert string to integer
223def atoi(str, base=10):
224	if base != 10:
225		# We only get here if strop doesn't define atoi()
226		raise ValueError, "this string.atoi doesn't support base != 10"
227	sign = ''
228	s = str
229	if s and s[0] in '+-':
230		sign = s[0]
231		s = s[1:]
232	if not s:
233		raise ValueError, 'non-integer argument to string.atoi'
234	while s[0] == '0' and len(s) > 1: s = s[1:]
235	for c in s:
236		if c not in digits:
237			raise ValueError, 'non-integer argument to string.atoi'
238	return eval(sign + s)
239
240# Convert string to long integer
241def atol(str, base=10):
242	if base != 10:
243		# We only get here if strop doesn't define atol()
244		raise ValueError, "this string.atol doesn't support base != 10"
245	sign = ''
246	s = str
247	if s and s[0] in '+-':
248		sign = s[0]
249		s = s[1:]
250	if not s:
251		raise ValueError, 'non-integer argument to string.atol'
252	while s[0] == '0' and len(s) > 1: s = s[1:]
253	for c in s:
254		if c not in digits:
255			raise ValueError, 'non-integer argument to string.atol'
256	return eval(sign + s + 'L')
257
258# Left-justify a string
259def ljust(s, width):
260	n = width - len(s)
261	if n <= 0: return s
262	return s + ' '*n
263
264# Right-justify a string
265def rjust(s, width):
266	n = width - len(s)
267	if n <= 0: return s
268	return ' '*n + s
269
270# Center a string
271def center(s, width):
272	n = width - len(s)
273	if n <= 0: return s
274	half = n/2
275	if n%2 and width%2:
276		# This ensures that center(center(s, i), j) = center(s, j)
277		half = half+1
278	return ' '*half +  s + ' '*(n-half)
279
280# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
281# Decadent feature: the argument may be a string or a number
282# (Use of this is deprecated; it should be a string as with ljust c.s.)
283def zfill(x, width):
284	if type(x) == type(''): s = x
285	else: s = `x`
286	n = len(s)
287	if n >= width: return s
288	sign = ''
289	if s[0] in ('-', '+'):
290		sign, s = s[0], s[1:]
291	return sign + '0'*(width-n) + s
292
293# Expand tabs in a string.
294# Doesn't take non-printing chars into account, but does understand \n.
295def expandtabs(s, tabsize=8):
296	res = line = ''
297	for c in s:
298		if c == '\t':
299			c = ' '*(tabsize - len(line)%tabsize)
300		line = line + c
301		if c == '\n':
302			res = res + line
303			line = ''
304	return res + line
305
306# Character translation through look-up table.
307def translate(s, table, deletions=""):
308	if type(table) != type('') or len(table) != 256:
309	    raise TypeError, "translation table must be 256 characters long"
310	res = ""
311	for c in s:
312		if c not in deletions:
313			res = res + table[ord(c)]
314	return res
315
316# Capitalize a string, e.g. "aBc  dEf" -> "Abc  def".
317def capitalize(s):
318	return upper(s[:1]) + lower(s[1:])
319
320# Capitalize the words in a string, e.g. " aBc  dEf " -> "Abc Def".
321# See also regsub.capwords().
322def capwords(s, sep=None):
323	return join(map(capitalize, split(s, sep)), sep or ' ')
324
325# Construct a translation string
326_idmapL = None
327def maketrans(fromstr, tostr):
328	if len(fromstr) != len(tostr):
329		raise ValueError, "maketrans arguments must have same length"
330	global _idmapL
331	if not _idmapL:
332		_idmapL = map(None, _idmap)
333	L = _idmapL[:]
334	fromstr = map(ord, fromstr)
335	for i in range(len(fromstr)):
336		L[fromstr[i]] = tostr[i]
337	return joinfields(L, "")
338
339# Substring replacement (global)
340def replace(str, old, new, maxsplit=0):
341	return joinfields(splitfields(str, old, maxsplit), new)
342
343
344# Try importing optional built-in module "strop" -- if it exists,
345# it redefines some string operations that are 100-1000 times faster.
346# It also defines values for whitespace, lowercase and uppercase
347# that match <ctype.h>'s definitions.
348
349try:
350	from strop import *
351	letters = lowercase + uppercase
352except ImportError:
353	pass # Use the original, slow versions
354