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, ' ')! |
| 60 | def split(s): |
| 61 | res = [] |
| 62 | i, n = 0, len(s) |
| 63 | while i < n: |
| 64 | while i < n and s[i] in whitespace: i = i+1 |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 65 | if i == n: break |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 66 | j = i |
| 67 | while j < n and s[j] not in whitespace: j = j+1 |
| 68 | res.append(s[i:j]) |
| 69 | i = j |
| 70 | return res |
| 71 | |
| 72 | # Split a list into fields separated by a given string |
| 73 | # NB: splitfields(s, ' ') is NOT the same as split(s)! |
Guido van Rossum | 7a461e5 | 1992-09-20 21:41:09 +0000 | [diff] [blame] | 74 | # splitfields(s, '') returns [s] (in analogy with split() in nawk) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 75 | def splitfields(s, sep): |
| 76 | res = [] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 77 | nsep = len(sep) |
Guido van Rossum | ae507a4 | 1992-08-19 16:49:58 +0000 | [diff] [blame] | 78 | if nsep == 0: |
Guido van Rossum | 7a461e5 | 1992-09-20 21:41:09 +0000 | [diff] [blame] | 79 | return [s] |
Guido van Rossum | ae507a4 | 1992-08-19 16:49:58 +0000 | [diff] [blame] | 80 | ns = len(s) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 81 | i = j = 0 |
| 82 | while j+nsep <= ns: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 83 | if s[j:j+nsep] == sep: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 84 | res.append(s[i:j]) |
| 85 | i = j = j + nsep |
| 86 | else: |
| 87 | j = j + 1 |
| 88 | res.append(s[i:]) |
| 89 | return res |
| 90 | |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 91 | # Join words with spaces between them |
| 92 | def join(words): |
Guido van Rossum | 18fc569 | 1992-11-26 09:17:19 +0000 | [diff] [blame] | 93 | return joinfields(words, ' ') |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 94 | |
| 95 | # Join fields with separator |
| 96 | def joinfields(words, sep): |
| 97 | res = '' |
| 98 | for w in words: |
| 99 | res = res + (sep + w) |
| 100 | return res[len(sep):] |
| 101 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 102 | # Find substring, raise exception if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 103 | def index(s, sub, i = 0): |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 104 | res = find(s, sub, i) |
| 105 | if res < 0: |
| 106 | raise ValueError, 'substring not found in string.index' |
| 107 | return res |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 109 | # Find last substring, raise exception if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 110 | def rindex(s, sub, i = 0): |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 111 | res = rfind(s, sub, i) |
| 112 | if res < 0: |
| 113 | raise ValueError, 'substring not found in string.index' |
| 114 | return res |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 115 | |
| 116 | # Count non-overlapping occurrences of substring |
| 117 | def count(s, sub, i = 0): |
| 118 | if i < 0: i = i + len(s) |
| 119 | n = len(sub) |
| 120 | m = len(s) + 1 - n |
| 121 | if n == 0: return m-i |
| 122 | r = 0 |
| 123 | while i < m: |
| 124 | if sub == s[i:i+n]: |
| 125 | r = r+1 |
| 126 | i = i+n |
| 127 | else: |
| 128 | i = i+1 |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 129 | return r |
| 130 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 131 | # Find substring, return -1 if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 132 | def find(s, sub, i = 0): |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 133 | if i < 0: i = i + len(s) |
| 134 | n = len(sub) |
| 135 | m = len(s) + 1 - n |
| 136 | while i < m: |
| 137 | if sub == s[i:i+n]: return i |
| 138 | i = i+1 |
| 139 | return -1 |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 141 | # Find last substring, return -1 if not found |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 142 | def rfind(s, sub, i = 0): |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 143 | if i < 0: i = i + len(s) |
| 144 | n = len(sub) |
| 145 | m = len(s) + 1 - n |
| 146 | r = -1 |
| 147 | while i < m: |
| 148 | if sub == s[i:i+n]: r = i |
| 149 | i = i+1 |
| 150 | return r |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 152 | # Convert string to float |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 153 | def atof(str): |
| 154 | import regex |
| 155 | sign = '' |
| 156 | s = str |
| 157 | if s and s[0] in '+-': |
| 158 | sign = s[0] |
| 159 | s = s[1:] |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 160 | if not s: |
| 161 | raise ValueError, 'non-float argument to string.atof' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 162 | while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:] |
| 163 | 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^] | 164 | raise ValueError, 'non-float argument to string.atof' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 165 | try: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 166 | return float(eval(sign + s)) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 167 | except SyntaxError: |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 168 | raise ValueError, 'non-float argument to string.atof' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 170 | # Convert string to integer |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 171 | def atoi(str): |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 172 | sign = '' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 173 | s = str |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 174 | if s and s[0] in '+-': |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 175 | sign = s[0] |
| 176 | s = s[1:] |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 177 | if not s: |
| 178 | raise ValueError, 'non-integer argument to string.atoi' |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 179 | while s[0] == '0' and len(s) > 1: s = s[1:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 180 | for c in s: |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 181 | if c not in digits: |
| 182 | raise ValueError, 'non-integer argument to string.atoi' |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 183 | return eval(sign + s) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 185 | # Convert string to long integer |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 186 | def atol(str): |
| 187 | sign = '' |
| 188 | s = str |
| 189 | if s and s[0] in '+-': |
| 190 | sign = s[0] |
| 191 | s = s[1:] |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 192 | if not s: |
| 193 | raise ValueError, 'non-integer argument to string.atol' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 194 | while s[0] == '0' and len(s) > 1: s = s[1:] |
| 195 | for c in s: |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame^] | 196 | if c not in digits: |
| 197 | raise ValueError, 'non-integer argument to string.atol' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 198 | return eval(sign + s + 'L') |
| 199 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 200 | # Left-justify a string |
| 201 | def ljust(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 202 | n = width - len(s) |
| 203 | if n <= 0: return s |
| 204 | return s + ' '*n |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 205 | |
| 206 | # Right-justify a string |
| 207 | def rjust(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 208 | n = width - len(s) |
| 209 | if n <= 0: return s |
| 210 | return ' '*n + s |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 211 | |
| 212 | # Center a string |
| 213 | def center(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 214 | n = width - len(s) |
| 215 | if n <= 0: return s |
| 216 | half = n/2 |
| 217 | if n%2 and width%2: |
| 218 | # This ensures that center(center(s, i), j) = center(s, j) |
| 219 | half = half+1 |
| 220 | return ' '*half + s + ' '*(n-half) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 221 | |
| 222 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 223 | # Decadent feature: the argument may be a string or a number |
| 224 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 225 | def zfill(x, width): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 226 | if type(x) == type(''): s = x |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 227 | else: s = `x` |
| 228 | n = len(s) |
| 229 | if n >= width: return s |
| 230 | sign = '' |
Guido van Rossum | 333c2e0 | 1991-08-16 13:29:03 +0000 | [diff] [blame] | 231 | if s[0] in ('-', '+'): |
| 232 | sign, s = s[0], s[1:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 233 | return sign + '0'*(width-n) + s |
Guido van Rossum | 6ff2e90 | 1992-03-27 15:13:31 +0000 | [diff] [blame] | 234 | |
| 235 | # Expand tabs in a string. |
| 236 | # Doesn't take non-printing chars into account, but does understand \n. |
| 237 | def expandtabs(s, tabsize): |
| 238 | res = line = '' |
| 239 | for c in s: |
| 240 | if c == '\t': |
| 241 | c = ' '*(tabsize - len(line)%tabsize) |
| 242 | line = line + c |
| 243 | if c == '\n': |
| 244 | res = res + line |
| 245 | line = '' |
| 246 | return res + line |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 247 | |
| 248 | |
| 249 | # Try importing optional built-in module "strop" -- if it exists, |
| 250 | # it redefines some string operations that are 100-1000 times faster. |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 251 | # It also defines values for whitespace, lowercase and uppercase |
| 252 | # that match <ctype.h>'s definitions. |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 253 | |
| 254 | try: |
| 255 | from strop import * |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 256 | letters = lowercase + uppercase |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | except ImportError: |
| 258 | pass # Use the original, slow versions |