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 | |
| 82 | # Find substring |
| 83 | index_error = 'substring not found in string.index' |
| 84 | def index(s, sub): |
| 85 | n = len(sub) |
Guido van Rossum | 66a07c0 | 1990-12-26 15:39:06 +0000 | [diff] [blame] | 86 | for i in range(len(s) + 1 - n): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 87 | if sub = s[i:i+n]: return i |
| 88 | raise index_error, (s, sub) |
| 89 | |
| 90 | # Convert string to integer |
| 91 | atoi_error = 'non-numeric argument to string.atoi' |
| 92 | def atoi(str): |
| 93 | s = str |
| 94 | if s[:1] in '+-': s = s[1:] |
| 95 | if not s: raise atoi_error, str |
| 96 | for c in s: |
| 97 | if c not in digits: raise atoi_error, str |
| 98 | return eval(str) |
| 99 | |
| 100 | # Left-justify a string |
| 101 | def ljust(s, width): |
| 102 | n = len(s) |
| 103 | if n >= width: return s |
| 104 | return s + ' '*(width-n) |
| 105 | |
| 106 | # Right-justify a string |
| 107 | def rjust(s, width): |
| 108 | n = len(s) |
| 109 | if n >= width: return s |
| 110 | return ' '*(width-n) + s |
| 111 | |
| 112 | # Center a string |
| 113 | def center(s, width): |
| 114 | n = len(s) |
| 115 | if n >= width: return s |
| 116 | return ' '*((width-n)/2) + s + ' '*(width -(width-n)/2) |
| 117 | |
| 118 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 119 | # Decadent feature: the argument may be a string or a number |
| 120 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 121 | def zfill(x, width): |
| 122 | if type(x) = type(''): s = x |
| 123 | else: s = `x` |
| 124 | n = len(s) |
| 125 | if n >= width: return s |
| 126 | sign = '' |
| 127 | if s[0] = '-': |
| 128 | sign, s = '-', s[1:] |
| 129 | return sign + '0'*(width-n) + s |