Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 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 |
| 6 | whitespace = ' \t\n' |
| 7 | lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 8 | uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 9 | letters = lowercase + uppercase |
| 10 | digits = '0123456789' |
| 11 | hexdigits = digits + 'abcdef' + 'ABCDEF' |
| 12 | octdigits = '01234567' |
| 13 | |
| 14 | # Case conversion helpers |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 15 | _idmap = '' |
| 16 | for 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 Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 20 | del i |
| 21 | |
| 22 | # convert UPPER CASE letters to lower case |
| 23 | def lower(s): |
| 24 | res = '' |
| 25 | for c in s: |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 26 | res = res + _lower[ord(c)] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 27 | return res |
| 28 | |
| 29 | # Convert lower case letters to UPPER CASE |
| 30 | def upper(s): |
| 31 | res = '' |
| 32 | for c in s: |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 33 | res = res + _upper[ord(c)] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 34 | return res |
| 35 | |
| 36 | # Swap lower case letters and UPPER CASE |
| 37 | def swapcase(s): |
| 38 | res = '' |
| 39 | for c in s: |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 40 | res = res + _swapcase[ord(c)] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 41 | return res |
| 42 | |
| 43 | # Strip leading and trailing tabs and spaces |
| 44 | def 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, ' ')! |
| 52 | def 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 Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 57 | if i == n: break |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 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)! |
Guido van Rossum | ae507a4 | 1992-08-19 16:49:58 +0000 | [diff] [blame^] | 66 | # splitfields(s, '') is illegal |
| 67 | splitfields_error = 'string.splitfields called with empty separator' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 68 | def splitfields(s, sep): |
| 69 | res = [] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 70 | nsep = len(sep) |
Guido van Rossum | ae507a4 | 1992-08-19 16:49:58 +0000 | [diff] [blame^] | 71 | if nsep == 0: |
| 72 | raise splitfields_error |
| 73 | ns = len(s) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 74 | i = j = 0 |
| 75 | while j+nsep <= ns: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 76 | if s[j:j+nsep] == sep: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 77 | res.append(s[i:j]) |
| 78 | i = j = j + nsep |
| 79 | else: |
| 80 | j = j + 1 |
| 81 | res.append(s[i:]) |
| 82 | return res |
| 83 | |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 84 | # Join words with spaces between them |
| 85 | def join(words): |
| 86 | res = '' |
| 87 | for w in words: |
| 88 | res = res + (' ' + w) |
| 89 | return res[1:] |
| 90 | |
| 91 | # Join fields with separator |
| 92 | def joinfields(words, sep): |
| 93 | res = '' |
| 94 | for w in words: |
| 95 | res = res + (sep + w) |
| 96 | return res[len(sep):] |
| 97 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 98 | # Find substring |
| 99 | index_error = 'substring not found in string.index' |
| 100 | def index(s, sub): |
| 101 | n = len(sub) |
Guido van Rossum | 66a07c0 | 1990-12-26 15:39:06 +0000 | [diff] [blame] | 102 | for i in range(len(s) + 1 - n): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 103 | if sub == s[i:i+n]: return i |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 104 | raise index_error, (s, sub) |
| 105 | |
| 106 | # Convert string to integer |
| 107 | atoi_error = 'non-numeric argument to string.atoi' |
| 108 | def atoi(str): |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 109 | sign = '' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 110 | s = str |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 111 | if s[:1] in '+-': |
| 112 | sign = s[0] |
| 113 | s = s[1:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 114 | if not s: raise atoi_error, str |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 115 | while s[0] == '0' and len(s) > 1: s = s[1:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 116 | for c in s: |
| 117 | if c not in digits: raise atoi_error, str |
Guido van Rossum | 2d4aa4f | 1992-08-06 22:33:41 +0000 | [diff] [blame] | 118 | return eval(sign + s) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 119 | |
| 120 | # Left-justify a string |
| 121 | def ljust(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 122 | n = width - len(s) |
| 123 | if n <= 0: return s |
| 124 | return s + ' '*n |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 125 | |
| 126 | # Right-justify a string |
| 127 | def rjust(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 128 | n = width - len(s) |
| 129 | if n <= 0: return s |
| 130 | return ' '*n + s |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 131 | |
| 132 | # Center a string |
| 133 | def center(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 134 | n = width - len(s) |
| 135 | if n <= 0: return s |
| 136 | half = n/2 |
| 137 | if n%2 and width%2: |
| 138 | # This ensures that center(center(s, i), j) = center(s, j) |
| 139 | half = half+1 |
| 140 | return ' '*half + s + ' '*(n-half) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 141 | |
| 142 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 143 | # Decadent feature: the argument may be a string or a number |
| 144 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 145 | def zfill(x, width): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 146 | if type(x) == type(''): s = x |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 147 | else: s = `x` |
| 148 | n = len(s) |
| 149 | if n >= width: return s |
| 150 | sign = '' |
Guido van Rossum | 333c2e0 | 1991-08-16 13:29:03 +0000 | [diff] [blame] | 151 | if s[0] in ('-', '+'): |
| 152 | sign, s = s[0], s[1:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 153 | return sign + '0'*(width-n) + s |
Guido van Rossum | 6ff2e90 | 1992-03-27 15:13:31 +0000 | [diff] [blame] | 154 | |
| 155 | # Expand tabs in a string. |
| 156 | # Doesn't take non-printing chars into account, but does understand \n. |
| 157 | def expandtabs(s, tabsize): |
| 158 | res = line = '' |
| 159 | for c in s: |
| 160 | if c == '\t': |
| 161 | c = ' '*(tabsize - len(line)%tabsize) |
| 162 | line = line + c |
| 163 | if c == '\n': |
| 164 | res = res + line |
| 165 | line = '' |
| 166 | return res + line |