Mercurial > cpython-lru_cache-weakref
annotate Lib/string.py @ 365:4e3073bb0e3e legacy-trunk
A plus is also a sign!
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 16 Aug 1991 13:29:03 +0000 |
| parents | 48fc0b8f33d9 |
| children | 683edb0c4463 |
| rev | line source |
|---|---|
| 5 | 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 | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
82 # Join words with spaces between them |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
83 def join(words): |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
84 res = '' |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
85 for w in words: |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
86 res = res + (' ' + w) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
87 return res[1:] |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
88 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
89 # Join fields with separator |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
90 def joinfields(words, sep): |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
91 res = '' |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
92 for w in words: |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
93 res = res + (sep + w) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
94 return res[len(sep):] |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
95 |
| 5 | 96 # Find substring |
| 97 index_error = 'substring not found in string.index' | |
| 98 def index(s, sub): | |
| 99 n = len(sub) | |
|
102
f2cacb90c917
Fix bugf in index -- last char would not be checked.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
100 for i in range(len(s) + 1 - n): |
| 5 | 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): | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
116 n = width - len(s) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
117 if n <= 0: return s |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
118 return s + ' '*n |
| 5 | 119 |
| 120 # Right-justify a string | |
| 121 def rjust(s, width): | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
122 n = width - len(s) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
123 if n <= 0: return s |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
124 return ' '*n + s |
| 5 | 125 |
| 126 # Center a string | |
| 127 def center(s, width): | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
128 n = width - len(s) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
129 if n <= 0: return s |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
130 half = n/2 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
131 if n%2 and width%2: |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
132 # This ensures that center(center(s, i), j) = center(s, j) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
133 half = half+1 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
134 return ' '*half + s + ' '*(n-half) |
| 5 | 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 = '' | |
| 365 | 145 if s[0] in ('-', '+'): |
| 146 sign, s = s[0], s[1:] | |
| 5 | 147 return sign + '0'*(width-n) + s |
