stringold.py revision 2d4aa4f5d43842bc049752ab8d5ffa3d2880cbe6
1# module 'string' -- A collection of string operations
2
3# XXX Some of these operations are incredibly slow and should be built in
4
5# Some strings for ctype-style character classification
6whitespace = ' \t\n'
7lowercase = 'abcdefghijklmnopqrstuvwxyz'
8uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
9letters = lowercase + uppercase
10digits = '0123456789'
11hexdigits = digits + 'abcdef' + 'ABCDEF'
12octdigits = '01234567'
13
14# Case conversion helpers
15_idmap = ''
16for i in range(256): _idmap = _idmap + chr(i)
17_lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:]
18_upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
19_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
20del i
21
22# convert UPPER CASE letters to lower case
23def lower(s):
24	res = ''
25	for c in s:
26		res = res + _lower[ord(c)]
27	return res
28
29# Convert lower case letters to UPPER CASE
30def upper(s):
31	res = ''
32	for c in s:
33		res = res + _upper[ord(c)]
34	return res
35
36# Swap lower case letters and UPPER CASE
37def swapcase(s):
38	res = ''
39	for c in s:
40		res = res + _swapcase[ord(c)]
41	return res
42
43# Strip leading and trailing tabs and spaces
44def strip(s):
45	i, j = 0, len(s)
46	while i < j and s[i] in whitespace: i = i+1
47	while i < j and s[j-1] in whitespace: j = j-1
48	return s[i:j]
49
50# Split a string into a list of space/tab-separated words
51# NB: split(s) is NOT the same as splitfields(s, ' ')!
52def split(s):
53	res = []
54	i, n = 0, len(s)
55	while i < n:
56		while i < n and s[i] in whitespace: i = i+1
57		if i == n: break
58		j = i
59		while j < n and s[j] not in whitespace: j = j+1
60		res.append(s[i:j])
61		i = j
62	return res
63
64# Split a list into fields separated by a given string
65# NB: splitfields(s, ' ') is NOT the same as split(s)!
66def splitfields(s, sep):
67	res = []
68	ns = len(s)
69	nsep = len(sep)
70	i = j = 0
71	while j+nsep <= ns:
72		if s[j:j+nsep] == sep:
73			res.append(s[i:j])
74			i = j = j + nsep
75		else:
76			j = j + 1
77	res.append(s[i:])
78	return res
79
80# Join words with spaces between them
81def join(words):
82	res = ''
83	for w in words:
84		res = res + (' ' + w)
85	return res[1:]
86
87# Join fields with separator
88def joinfields(words, sep):
89	res = ''
90	for w in words:
91		res = res + (sep + w)
92	return res[len(sep):]
93
94# Find substring
95index_error = 'substring not found in string.index'
96def index(s, sub):
97	n = len(sub)
98	for i in range(len(s) + 1 - n):
99		if sub == s[i:i+n]: return i
100	raise index_error, (s, sub)
101
102# Convert string to integer
103atoi_error = 'non-numeric argument to string.atoi'
104def atoi(str):
105	sign = ''
106	s = str
107	if s[:1] in '+-':
108		sign = s[0]
109		s = s[1:]
110	if not s: raise atoi_error, str
111	while s[0] == '0' and len(s) > 1: s = s[1:]
112	for c in s:
113		if c not in digits: raise atoi_error, str
114	return eval(sign + s)
115
116# Left-justify a string
117def ljust(s, width):
118	n = width - len(s)
119	if n <= 0: return s
120	return s + ' '*n
121
122# Right-justify a string
123def rjust(s, width):
124	n = width - len(s)
125	if n <= 0: return s
126	return ' '*n + s
127
128# Center a string
129def center(s, width):
130	n = width - len(s)
131	if n <= 0: return s
132	half = n/2
133	if n%2 and width%2:
134		# This ensures that center(center(s, i), j) = center(s, j)
135		half = half+1
136	return ' '*half +  s + ' '*(n-half)
137
138# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
139# Decadent feature: the argument may be a string or a number
140# (Use of this is deprecated; it should be a string as with ljust c.s.)
141def zfill(x, width):
142	if type(x) == type(''): s = x
143	else: s = `x`
144	n = len(s)
145	if n >= width: return s
146	sign = ''
147	if s[0] in ('-', '+'):
148		sign, s = s[0], s[1:]
149	return sign + '0'*(width-n) + s
150
151# Expand tabs in a string.
152# Doesn't take non-printing chars into account, but does understand \n.
153def expandtabs(s, tabsize):
154	res = line = ''
155	for c in s:
156		if c == '\t':
157			c = ' '*(tabsize - len(line)%tabsize)
158		line = line + c
159		if c == '\n':
160			res = res + line
161			line = ''
162	return res + line
163