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 |
| 15 | _caseswap = {} |
| 16 | for i in range(26): |
| 17 | _caseswap[lowercase[i]] = uppercase[i] |
| 18 | _caseswap[uppercase[i]] = lowercase[i] |
| 19 | del i |
| 20 | |
| 21 | # convert UPPER CASE letters to lower case |
| 22 | def lower(s): |
| 23 | res = '' |
| 24 | for c in s: |
| 25 | if 'A' <= c <= 'Z': c = _caseswap[c] |
| 26 | res = res + c |
| 27 | return res |
| 28 | |
| 29 | # Convert lower case letters to UPPER CASE |
| 30 | def upper(s): |
| 31 | res = '' |
| 32 | for c in s: |
| 33 | if 'a' <= c <= 'z': c = _caseswap[c] |
| 34 | res = res + c |
| 35 | return res |
| 36 | |
| 37 | # Swap lower case letters and UPPER CASE |
| 38 | def swapcase(s): |
| 39 | res = '' |
| 40 | for c in s: |
| 41 | if 'a' <= c <= 'z' or 'A' <= c <= 'Z': c = _caseswap[c] |
| 42 | res = res + c |
| 43 | return res |
| 44 | |
| 45 | # Strip leading and trailing tabs and spaces |
| 46 | def strip(s): |
| 47 | i, j = 0, len(s) |
| 48 | while i < j and s[i] in whitespace: i = i+1 |
| 49 | while i < j and s[j-1] in whitespace: j = j-1 |
| 50 | return s[i:j] |
| 51 | |
| 52 | # Split a string into a list of space/tab-separated words |
| 53 | # NB: split(s) is NOT the same as splitfields(s, ' ')! |
| 54 | def split(s): |
| 55 | res = [] |
| 56 | i, n = 0, len(s) |
| 57 | while i < n: |
| 58 | while i < n and s[i] in whitespace: i = i+1 |
| 59 | if i = n: break |
| 60 | j = i |
| 61 | while j < n and s[j] not in whitespace: j = j+1 |
| 62 | res.append(s[i:j]) |
| 63 | i = j |
| 64 | return res |
| 65 | |
| 66 | # Split a list into fields separated by a given string |
| 67 | # NB: splitfields(s, ' ') is NOT the same as split(s)! |
| 68 | def splitfields(s, sep): |
| 69 | res = [] |
| 70 | ns = len(s) |
| 71 | nsep = len(sep) |
| 72 | i = j = 0 |
| 73 | while j+nsep <= ns: |
| 74 | if s[j:j+nsep] = sep: |
| 75 | res.append(s[i:j]) |
| 76 | i = j = j + nsep |
| 77 | else: |
| 78 | j = j + 1 |
| 79 | res.append(s[i:]) |
| 80 | return res |
| 81 | |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 82 | # Join words with spaces between them |
| 83 | def join(words): |
| 84 | res = '' |
| 85 | for w in words: |
| 86 | res = res + (' ' + w) |
| 87 | return res[1:] |
| 88 | |
| 89 | # Join fields with separator |
| 90 | def joinfields(words, sep): |
| 91 | res = '' |
| 92 | for w in words: |
| 93 | res = res + (sep + w) |
| 94 | return res[len(sep):] |
| 95 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 96 | # Find substring |
| 97 | index_error = 'substring not found in string.index' |
| 98 | def index(s, sub): |
| 99 | n = len(sub) |
Guido van Rossum | 66a07c0 | 1990-12-26 15:39:06 +0000 | [diff] [blame] | 100 | for i in range(len(s) + 1 - n): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 101 | if sub = s[i:i+n]: return i |
| 102 | raise index_error, (s, sub) |
| 103 | |
| 104 | # Convert string to integer |
| 105 | atoi_error = 'non-numeric argument to string.atoi' |
| 106 | def atoi(str): |
| 107 | s = str |
| 108 | if s[:1] in '+-': s = s[1:] |
| 109 | if not s: raise atoi_error, str |
| 110 | for c in s: |
| 111 | if c not in digits: raise atoi_error, str |
| 112 | return eval(str) |
| 113 | |
| 114 | # Left-justify a string |
| 115 | def ljust(s, width): |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 116 | n = width - len(s) |
| 117 | if n <= 0: return s |
| 118 | return s + ' '*n |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 119 | |
| 120 | # Right-justify a string |
| 121 | def rjust(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 ' '*n + s |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 125 | |
| 126 | # Center a string |
| 127 | def center(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 | half = n/2 |
| 131 | if n%2 and width%2: |
| 132 | # This ensures that center(center(s, i), j) = center(s, j) |
| 133 | half = half+1 |
| 134 | return ' '*half + s + ' '*(n-half) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 135 | |
| 136 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 137 | # Decadent feature: the argument may be a string or a number |
| 138 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 139 | def zfill(x, width): |
| 140 | if type(x) = type(''): s = x |
| 141 | else: s = `x` |
| 142 | n = len(s) |
| 143 | if n >= width: return s |
| 144 | sign = '' |
| 145 | if s[0] = '-': |
| 146 | sign, s = '-', s[1:] |
| 147 | return sign + '0'*(width-n) + s |