blob: aed3eafb544ecb6a1b963541b41d1b08e8cb251e [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# 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
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000015_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:]
Guido van Rossumc6360141990-10-13 19:23:40 +000020del i
21
22# convert UPPER CASE letters to lower case
23def lower(s):
24 res = ''
25 for c in s:
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000026 res = res + _lower[ord(c)]
Guido van Rossumc6360141990-10-13 19:23:40 +000027 return res
28
29# Convert lower case letters to UPPER CASE
30def upper(s):
31 res = ''
32 for c in s:
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000033 res = res + _upper[ord(c)]
Guido van Rossumc6360141990-10-13 19:23:40 +000034 return res
35
36# Swap lower case letters and UPPER CASE
37def swapcase(s):
38 res = ''
39 for c in s:
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000040 res = res + _swapcase[ord(c)]
Guido van Rossumc6360141990-10-13 19:23:40 +000041 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
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000057 if i == n: break
Guido van Rossumc6360141990-10-13 19:23:40 +000058 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)!
Guido van Rossum7a461e51992-09-20 21:41:09 +000066# splitfields(s, '') returns [s] (in analogy with split() in nawk)
Guido van Rossumc6360141990-10-13 19:23:40 +000067def splitfields(s, sep):
68 res = []
Guido van Rossumc6360141990-10-13 19:23:40 +000069 nsep = len(sep)
Guido van Rossumae507a41992-08-19 16:49:58 +000070 if nsep == 0:
Guido van Rossum7a461e51992-09-20 21:41:09 +000071 return [s]
Guido van Rossumae507a41992-08-19 16:49:58 +000072 ns = len(s)
Guido van Rossumc6360141990-10-13 19:23:40 +000073 i = j = 0
74 while j+nsep <= ns:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000075 if s[j:j+nsep] == sep:
Guido van Rossumc6360141990-10-13 19:23:40 +000076 res.append(s[i:j])
77 i = j = j + nsep
78 else:
79 j = j + 1
80 res.append(s[i:])
81 return res
82
Guido van Rossumfac38b71991-04-07 13:42:19 +000083# Join words with spaces between them
84def join(words):
Guido van Rossum18fc5691992-11-26 09:17:19 +000085 return joinfields(words, ' ')
Guido van Rossumfac38b71991-04-07 13:42:19 +000086
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
Guido van Rossumc6360141990-10-13 19:23:40 +000094# Find substring
95index_error = 'substring not found in string.index'
Guido van Rossumc629d341992-11-05 10:43:02 +000096def index(s, sub, *args):
97 if args:
98 if len(args) > 1:
99 raise TypeError, 'string.index(): too many args'
100 i = args[0]
101 else:
102 i = 0
Guido van Rossumc6360141990-10-13 19:23:40 +0000103 n = len(sub)
Guido van Rossumc629d341992-11-05 10:43:02 +0000104 m = len(s) + 1 - n
105 while i < m:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000106 if sub == s[i:i+n]: return i
Guido van Rossumc629d341992-11-05 10:43:02 +0000107 i = i+1
Guido van Rossumc6360141990-10-13 19:23:40 +0000108 raise index_error, (s, sub)
109
110# Convert string to integer
111atoi_error = 'non-numeric argument to string.atoi'
112def atoi(str):
Guido van Rossum2d4aa4f1992-08-06 22:33:41 +0000113 sign = ''
Guido van Rossumc6360141990-10-13 19:23:40 +0000114 s = str
Guido van Rossumc629d341992-11-05 10:43:02 +0000115 if s and s[0] in '+-':
Guido van Rossum2d4aa4f1992-08-06 22:33:41 +0000116 sign = s[0]
117 s = s[1:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000118 if not s: raise atoi_error, str
Guido van Rossum2d4aa4f1992-08-06 22:33:41 +0000119 while s[0] == '0' and len(s) > 1: s = s[1:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000120 for c in s:
121 if c not in digits: raise atoi_error, str
Guido van Rossum2d4aa4f1992-08-06 22:33:41 +0000122 return eval(sign + s)
Guido van Rossumc6360141990-10-13 19:23:40 +0000123
124# Left-justify a string
125def ljust(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000126 n = width - len(s)
127 if n <= 0: return s
128 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000129
130# Right-justify a string
131def rjust(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000132 n = width - len(s)
133 if n <= 0: return s
134 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000135
136# Center a string
137def center(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000138 n = width - len(s)
139 if n <= 0: return s
140 half = n/2
141 if n%2 and width%2:
142 # This ensures that center(center(s, i), j) = center(s, j)
143 half = half+1
144 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000145
146# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
147# Decadent feature: the argument may be a string or a number
148# (Use of this is deprecated; it should be a string as with ljust c.s.)
149def zfill(x, width):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000150 if type(x) == type(''): s = x
Guido van Rossumc6360141990-10-13 19:23:40 +0000151 else: s = `x`
152 n = len(s)
153 if n >= width: return s
154 sign = ''
Guido van Rossum333c2e01991-08-16 13:29:03 +0000155 if s[0] in ('-', '+'):
156 sign, s = s[0], s[1:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000157 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000158
159# Expand tabs in a string.
160# Doesn't take non-printing chars into account, but does understand \n.
161def expandtabs(s, tabsize):
162 res = line = ''
163 for c in s:
164 if c == '\t':
165 c = ' '*(tabsize - len(line)%tabsize)
166 line = line + c
167 if c == '\n':
168 res = res + line
169 line = ''
170 return res + line
Guido van Rossum2db91351992-10-18 17:09:59 +0000171
172
173# Try importing optional built-in module "strop" -- if it exists,
174# it redefines some string operations that are 100-1000 times faster.
175# The manipulation with index_error is needed for compatibility.
176
177try:
178 from strop import *
179 from strop import index
180 index_error = ValueError
181except ImportError:
182 pass # Use the original, slow versions