Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 1 | # module 'string' -- A collection of string operations |
| 2 | |
Guido van Rossum | e7113b6 | 1993-03-29 11:30:50 +0000 | [diff] [blame] | 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". |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 6 | |
| 7 | # Some strings for ctype-style character classification |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 8 | whitespace = ' \t\n\r\v\f' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 9 | lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 10 | uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 11 | letters = lowercase + uppercase |
| 12 | digits = '0123456789' |
| 13 | hexdigits = digits + 'abcdef' + 'ABCDEF' |
| 14 | octdigits = '01234567' |
| 15 | |
| 16 | # Case conversion helpers |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 17 | _idmap = '' |
| 18 | for 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 Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 22 | del i |
| 23 | |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 24 | # Backward compatible names for exceptions |
| 25 | index_error = ValueError |
| 26 | atoi_error = ValueError |
| 27 | atof_error = ValueError |
| 28 | atol_error = ValueError |
| 29 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 30 | # convert UPPER CASE letters to lower case |
| 31 | def lower(s): |
| 32 | res = '' |
| 33 | for c in s: |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 34 | res = res + _lower[ord(c)] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 35 | return res |
| 36 | |
| 37 | # Convert lower case letters to UPPER CASE |
| 38 | def upper(s): |
| 39 | res = '' |
| 40 | for c in s: |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 41 | res = res + _upper[ord(c)] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 42 | return res |
| 43 | |
| 44 | # Swap lower case letters and UPPER CASE |
| 45 | def swapcase(s): |
| 46 | res = '' |
| 47 | for c in s: |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 48 | res = res + _swapcase[ord(c)] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 49 | return res |
| 50 | |
| 51 | # Strip leading and trailing tabs and spaces |
| 52 | def 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 | # Split a string into a list of space/tab-separated words |
| 59 | # NB: split(s) is NOT the same as splitfields(s, ' ')! |
Guido van Rossum | 2ab1992 | 1995-06-22 18:58:00 +0000 | [diff] [blame] | 60 | def split(s, sep=None): |
| 61 | if sep is not None: return splitfields(s, sep) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 62 | res = [] |
| 63 | i, n = 0, len(s) |
| 64 | while i < n: |
| 65 | while i < n and s[i] in whitespace: i = i+1 |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 66 | if i == n: break |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 67 | j = i |
| 68 | while j < n and s[j] not in whitespace: j = j+1 |
| 69 | res.append(s[i:j]) |
| 70 | i = j |
| 71 | return res |
| 72 | |
| 73 | # Split a list into fields separated by a given string |
| 74 | # NB: splitfields(s, ' ') is NOT the same as split(s)! |
Guido van Rossum | 7a461e5 | 1992-09-20 21:41:09 +0000 | [diff] [blame] | 75 | # splitfields(s, '') returns [s] (in analogy with split() in nawk) |
Guido van Rossum | 2ab1992 | 1995-06-22 18:58:00 +0000 | [diff] [blame] | 76 | def splitfields(s, sep=None): |
| 77 | if sep is None: return split(s) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 78 | res = [] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 79 | nsep = len(sep) |
Guido van Rossum | ae507a4 | 1992-08-19 16:49:58 +0000 | [diff] [blame] | 80 | if nsep == 0: |
Guido van Rossum | 7a461e5 | 1992-09-20 21:41:09 +0000 | [diff] [blame] | 81 | return [s] |
Guido van Rossum | ae507a4 | 1992-08-19 16:49:58 +0000 | [diff] [blame] | 82 | ns = len(s) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 83 | i = j = 0 |
| 84 | while j+nsep <= ns: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 85 | if s[j:j+nsep] == sep: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 86 | res.append(s[i:j]) |
| 87 | i = j = j + nsep |
| 88 | else: |
| 89 | j = j + 1 |
| 90 | res.append(s[i:]) |
| 91 | return res |
| 92 | |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 93 | # Join words with spaces between them |
Guido van Rossum | 2ab1992 | 1995-06-22 18:58:00 +0000 | [diff] [blame] | 94 | def join(words, sep = ' '): |
| 95 | return joinfields(words, sep) |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 2ab1992 | 1995-06-22 18:58:00 +0000 | [diff] [blame] | 97 | # Join fields with optional separator |
| 98 | def joinfields(words, sep = ' '): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 99 | res = '' |
| 100 | for w in words: |
| 101 | res = res + (sep + w) |
| 102 | return res[len(sep):] |
| 103 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 104 | # Find substring, raise exception if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 105 | def index(s, sub, i = 0): |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 106 | res = find(s, sub, i) |
| 107 | if res < 0: |
| 108 | raise ValueError, 'substring not found in string.index' |
| 109 | return res |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 111 | # Find last substring, raise exception if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 112 | def rindex(s, sub, i = 0): |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 113 | res = rfind(s, sub, i) |
| 114 | if res < 0: |
| 115 | raise ValueError, 'substring not found in string.index' |
| 116 | return res |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 117 | |
| 118 | # Count non-overlapping occurrences of substring |
| 119 | def count(s, sub, i = 0): |
Guido van Rossum | 8c1688e | 1995-03-14 17:43:02 +0000 | [diff] [blame] | 120 | if i < 0: i = max(0, i + len(s)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 121 | n = len(sub) |
| 122 | m = len(s) + 1 - n |
| 123 | if n == 0: return m-i |
| 124 | r = 0 |
| 125 | while i < m: |
| 126 | if sub == s[i:i+n]: |
| 127 | r = r+1 |
| 128 | i = i+n |
| 129 | else: |
| 130 | i = i+1 |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 131 | return r |
| 132 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 133 | # Find substring, return -1 if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 134 | def find(s, sub, i = 0): |
Guido van Rossum | 8c1688e | 1995-03-14 17:43:02 +0000 | [diff] [blame] | 135 | if i < 0: i = max(0, i + len(s)) |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 136 | n = len(sub) |
| 137 | m = len(s) + 1 - n |
| 138 | while i < m: |
| 139 | if sub == s[i:i+n]: return i |
| 140 | i = i+1 |
| 141 | return -1 |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 143 | # Find last substring, return -1 if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 144 | def rfind(s, sub, i = 0): |
Guido van Rossum | 8c1688e | 1995-03-14 17:43:02 +0000 | [diff] [blame] | 145 | if i < 0: i = max(0, i + len(s)) |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 146 | n = len(sub) |
| 147 | m = len(s) + 1 - n |
| 148 | r = -1 |
| 149 | while i < m: |
| 150 | if sub == s[i:i+n]: r = i |
| 151 | i = i+1 |
| 152 | return r |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 154 | # Convert string to float |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 155 | def atof(str): |
| 156 | import regex |
| 157 | sign = '' |
| 158 | s = str |
| 159 | if s and s[0] in '+-': |
| 160 | sign = s[0] |
| 161 | s = s[1:] |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 162 | if not s: |
| 163 | raise ValueError, 'non-float argument to string.atof' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 164 | while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:] |
| 165 | if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s): |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 166 | raise ValueError, 'non-float argument to string.atof' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 167 | try: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | return float(eval(sign + s)) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 169 | except SyntaxError: |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 170 | raise ValueError, 'non-float argument to string.atof' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 172 | # Convert string to integer |
Guido van Rossum | 8c1688e | 1995-03-14 17:43:02 +0000 | [diff] [blame] | 173 | def atoi(str, base=10): |
| 174 | if base != 10: |
| 175 | # We only get here if strop doesn't define atoi() |
| 176 | raise ValueError, "this string.atoi doesn't support base != 10" |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 177 | sign = '' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 178 | s = str |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 179 | if s and s[0] in '+-': |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 180 | sign = s[0] |
| 181 | s = s[1:] |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 182 | if not s: |
| 183 | raise ValueError, 'non-integer argument to string.atoi' |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 184 | while s[0] == '0' and len(s) > 1: s = s[1:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 185 | for c in s: |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 186 | if c not in digits: |
| 187 | raise ValueError, 'non-integer argument to string.atoi' |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 188 | return eval(sign + s) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 190 | # Convert string to long integer |
Guido van Rossum | 8c1688e | 1995-03-14 17:43:02 +0000 | [diff] [blame] | 191 | def atol(str, base=10): |
| 192 | if base != 10: |
| 193 | # We only get here if strop doesn't define atol() |
| 194 | raise ValueError, "this string.atol doesn't support base != 10" |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 195 | sign = '' |
| 196 | s = str |
| 197 | if s and s[0] in '+-': |
| 198 | sign = s[0] |
| 199 | s = s[1:] |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 200 | if not s: |
| 201 | raise ValueError, 'non-integer argument to string.atol' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 202 | while s[0] == '0' and len(s) > 1: s = s[1:] |
| 203 | for c in s: |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 204 | if c not in digits: |
| 205 | raise ValueError, 'non-integer argument to string.atol' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 206 | return eval(sign + s + 'L') |
| 207 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 208 | # Left-justify a string |
| 209 | def ljust(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 210 | n = width - len(s) |
| 211 | if n <= 0: return s |
| 212 | return s + ' '*n |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 213 | |
| 214 | # Right-justify a string |
| 215 | def rjust(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 216 | n = width - len(s) |
| 217 | if n <= 0: return s |
| 218 | return ' '*n + s |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 219 | |
| 220 | # Center a string |
| 221 | def center(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 222 | n = width - len(s) |
| 223 | if n <= 0: return s |
| 224 | half = n/2 |
| 225 | if n%2 and width%2: |
| 226 | # This ensures that center(center(s, i), j) = center(s, j) |
| 227 | half = half+1 |
| 228 | return ' '*half + s + ' '*(n-half) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 229 | |
| 230 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 231 | # Decadent feature: the argument may be a string or a number |
| 232 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 233 | def zfill(x, width): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 234 | if type(x) == type(''): s = x |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 235 | else: s = `x` |
| 236 | n = len(s) |
| 237 | if n >= width: return s |
| 238 | sign = '' |
Guido van Rossum | 333c2e0 | 1991-08-16 13:29:03 +0000 | [diff] [blame] | 239 | if s[0] in ('-', '+'): |
| 240 | sign, s = s[0], s[1:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 241 | return sign + '0'*(width-n) + s |
Guido van Rossum | 6ff2e90 | 1992-03-27 15:13:31 +0000 | [diff] [blame] | 242 | |
| 243 | # Expand tabs in a string. |
| 244 | # Doesn't take non-printing chars into account, but does understand \n. |
Guido van Rossum | 894a7bb | 1995-08-10 19:42:05 +0000 | [diff] [blame] | 245 | def expandtabs(s, tabsize=8): |
Guido van Rossum | 6ff2e90 | 1992-03-27 15:13:31 +0000 | [diff] [blame] | 246 | res = line = '' |
| 247 | for c in s: |
| 248 | if c == '\t': |
| 249 | c = ' '*(tabsize - len(line)%tabsize) |
| 250 | line = line + c |
| 251 | if c == '\n': |
| 252 | res = res + line |
| 253 | line = '' |
| 254 | return res + line |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 255 | |
Guido van Rossum | 2539528 | 1996-05-28 23:08:45 +0000 | [diff] [blame] | 256 | # Character translation through look-up table. |
| 257 | def translate(s, table): |
| 258 | if type(table) != type('') or len(table) != 256: |
| 259 | raise TypeError, "translation table must be 256-char string" |
| 260 | res = "" |
| 261 | for c in s: |
| 262 | res = res + table[ord(c)] |
| 263 | return res |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 265 | # Capitalize a string, e.g. "aBc dEf" -> "Abc def". |
| 266 | def capitalize(s): |
| 267 | return upper(s[:1]) + lower(s[1:]) |
| 268 | |
| 269 | # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
| 270 | # See also regsub.capwords(). |
| 271 | def capwords(s): |
| 272 | return join(map(capitalize, split(s))) |
| 273 | |
| 274 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 275 | # Try importing optional built-in module "strop" -- if it exists, |
| 276 | # it redefines some string operations that are 100-1000 times faster. |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 277 | # It also defines values for whitespace, lowercase and uppercase |
| 278 | # that match <ctype.h>'s definitions. |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 279 | |
| 280 | try: |
| 281 | from strop import * |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 282 | letters = lowercase + uppercase |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | except ImportError: |
| 284 | pass # Use the original, slow versions |