Mercurial > cpython-lru_cache-weakref
annotate Lib/string.py @ 980:ffe2fbc583de legacy-trunk
splitfields(s, '') is illegal
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 19 Aug 1992 16:49:58 +0000 |
| parents | 8725fa8d7ff9 |
| children | 6508ea32c6b0 |
| 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 | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
15 _idmap = '' |
|
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
16 for i in range(256): _idmap = _idmap + chr(i) |
|
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
17 _lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:] |
|
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
18 _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:] |
|
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
19 _swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:] |
| 5 | 20 del i |
| 21 | |
| 22 # convert UPPER CASE letters to lower case | |
| 23 def lower(s): | |
| 24 res = '' | |
| 25 for c in s: | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
26 res = res + _lower[ord(c)] |
| 5 | 27 return res |
| 28 | |
| 29 # Convert lower case letters to UPPER CASE | |
| 30 def upper(s): | |
| 31 res = '' | |
| 32 for c in s: | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
33 res = res + _upper[ord(c)] |
| 5 | 34 return res |
| 35 | |
| 36 # Swap lower case letters and UPPER CASE | |
| 37 def swapcase(s): | |
| 38 res = '' | |
| 39 for c in s: | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
40 res = res + _swapcase[ord(c)] |
| 5 | 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 | |
| 559 | 57 if i == n: break |
| 5 | 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)! | |
|
980
ffe2fbc583de
splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents:
928
diff
changeset
|
66 # splitfields(s, '') is illegal |
|
ffe2fbc583de
splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents:
928
diff
changeset
|
67 splitfields_error = 'string.splitfields called with empty separator' |
| 5 | 68 def splitfields(s, sep): |
| 69 res = [] | |
|
980
ffe2fbc583de
splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents:
928
diff
changeset
|
70 nsep = len(sep) |
|
ffe2fbc583de
splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents:
928
diff
changeset
|
71 if nsep == 0: |
|
ffe2fbc583de
splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents:
928
diff
changeset
|
72 raise splitfields_error |
| 5 | 73 ns = len(s) |
| 74 i = j = 0 | |
| 75 while j+nsep <= ns: | |
| 559 | 76 if s[j:j+nsep] == sep: |
| 5 | 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 | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
84 # Join words with spaces between them |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
85 def join(words): |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
86 res = '' |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
87 for w in words: |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
88 res = res + (' ' + w) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
89 return res[1:] |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
90 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
91 # Join fields with separator |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
92 def joinfields(words, sep): |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
93 res = '' |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
94 for w in words: |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
95 res = res + (sep + w) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
96 return res[len(sep):] |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
97 |
| 5 | 98 # Find substring |
| 99 index_error = 'substring not found in string.index' | |
| 100 def index(s, sub): | |
| 101 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
|
102 for i in range(len(s) + 1 - n): |
| 559 | 103 if sub == s[i:i+n]: return i |
| 5 | 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): | |
|
928
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
109 sign = '' |
| 5 | 110 s = str |
|
928
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
111 if s[:1] in '+-': |
|
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
112 sign = s[0] |
|
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
113 s = s[1:] |
| 5 | 114 if not s: raise atoi_error, str |
|
928
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
115 while s[0] == '0' and len(s) > 1: s = s[1:] |
| 5 | 116 for c in s: |
| 117 if c not in digits: raise atoi_error, str | |
|
928
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
118 return eval(sign + s) |
| 5 | 119 |
| 120 # Left-justify a string | |
| 121 def ljust(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 s + ' '*n |
| 5 | 125 |
| 126 # Right-justify a string | |
| 127 def rjust(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 return ' '*n + s |
| 5 | 131 |
| 132 # Center a string | |
| 133 def center(s, width): | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
134 n = width - len(s) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
135 if n <= 0: return s |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
136 half = n/2 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
137 if n%2 and width%2: |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
138 # 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
|
139 half = half+1 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
140 return ' '*half + s + ' '*(n-half) |
| 5 | 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): | |
| 559 | 146 if type(x) == type(''): s = x |
| 5 | 147 else: s = `x` |
| 148 n = len(s) | |
| 149 if n >= width: return s | |
| 150 sign = '' | |
| 365 | 151 if s[0] in ('-', '+'): |
| 152 sign, s = s[0], s[1:] | |
| 5 | 153 return sign + '0'*(width-n) + s |
|
724
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
154 |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
155 # Expand tabs in a string. |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
156 # Doesn't take non-printing chars into account, but does understand \n. |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
157 def expandtabs(s, tabsize): |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
158 res = line = '' |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
159 for c in s: |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
160 if c == '\t': |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
161 c = ' '*(tabsize - len(line)%tabsize) |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
162 line = line + c |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
163 if c == '\n': |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
164 res = res + line |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
165 line = '' |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
166 return res + line |
