Mercurial > cpython-lru_cache-weakref
annotate Lib/stringold.py @ 1590:ac2e5cc29949 legacy-trunk
Added tests for missing host to open_http and open_gopher
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 18 Apr 1994 09:39:56 +0000 |
| parents | 6278933d02e3 |
| children | 1940892d2fe3 |
| rev | line source |
|---|---|
| 5 | 1 # module 'string' -- A collection of string operations |
| 2 | |
|
1260
b120cdd30fbc
* Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents:
1095
diff
changeset
|
3 # Warning: most of the code you see here isn't normally used nowadays. |
|
b120cdd30fbc
* Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents:
1095
diff
changeset
|
4 # At the end of this file most functions are replaced by built-in |
|
b120cdd30fbc
* Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents:
1095
diff
changeset
|
5 # functions imported from built-in module "strop". |
| 5 | 6 |
| 7 # Some strings for ctype-style character classification | |
|
1331
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
8 whitespace = ' \t\n\r\v\f' |
| 5 | 9 lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 10 uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
| 11 letters = lowercase + uppercase | |
| 12 digits = '0123456789' | |
| 13 hexdigits = digits + 'abcdef' + 'ABCDEF' | |
| 14 octdigits = '01234567' | |
| 15 | |
| 16 # Case conversion helpers | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
17 _idmap = '' |
|
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
18 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
|
19 _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
|
20 _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
|
21 _swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:] |
| 5 | 22 del i |
| 23 | |
| 24 # convert UPPER CASE letters to lower case | |
| 25 def lower(s): | |
| 26 res = '' | |
| 27 for c in s: | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
28 res = res + _lower[ord(c)] |
| 5 | 29 return res |
| 30 | |
| 31 # Convert lower case letters to UPPER CASE | |
| 32 def upper(s): | |
| 33 res = '' | |
| 34 for c in s: | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
35 res = res + _upper[ord(c)] |
| 5 | 36 return res |
| 37 | |
| 38 # Swap lower case letters and UPPER CASE | |
| 39 def swapcase(s): | |
| 40 res = '' | |
| 41 for c in s: | |
|
585
37905fe1892a
New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
42 res = res + _swapcase[ord(c)] |
| 5 | 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 | |
| 559 | 59 if i == n: break |
| 5 | 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)! | |
|
1052
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
980
diff
changeset
|
68 # splitfields(s, '') returns [s] (in analogy with split() in nawk) |
| 5 | 69 def splitfields(s, sep): |
| 70 res = [] | |
|
980
ffe2fbc583de
splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents:
928
diff
changeset
|
71 nsep = len(sep) |
|
ffe2fbc583de
splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents:
928
diff
changeset
|
72 if nsep == 0: |
|
1052
6508ea32c6b0
New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents:
980
diff
changeset
|
73 return [s] |
| 5 | 74 ns = len(s) |
| 75 i = j = 0 | |
| 76 while j+nsep <= ns: | |
| 559 | 77 if s[j:j+nsep] == sep: |
| 5 | 78 res.append(s[i:j]) |
| 79 i = j = j + nsep | |
| 80 else: | |
| 81 j = j + 1 | |
| 82 res.append(s[i:]) | |
| 83 return res | |
| 84 | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
85 # Join words with spaces between them |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
86 def join(words): |
|
1095
ac0828ef68f2
* mainloop.py: added facility for calling select(). Also added
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
87 return joinfields(words, ' ') |
|
196
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 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
96 # Find substring, raise exception if not found |
| 5 | 97 index_error = 'substring not found in string.index' |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
98 def index(s, sub, *args): |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
99 if args: |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
100 if len(args) > 1: |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
101 raise TypeError, 'string.index(): too many args' |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
102 i = args[0] |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
103 if i < 0: i = i + len(s) |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
104 else: |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
105 i = 0 |
| 5 | 106 n = len(sub) |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
107 m = len(s) + 1 - n |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
108 while i < m: |
| 559 | 109 if sub == s[i:i+n]: return i |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
110 i = i+1 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
111 raise index_error, (s, sub) + args |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
112 |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
113 # Find last substring, raise exception if not found |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
114 def rindex(s, sub, *args): |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
115 if args: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
116 if len(args) > 1: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
117 raise TypeError, 'string.rindex(): too many args' |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
118 i = args[0] |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
119 if i < 0: i = i + len(s) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
120 else: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
121 i = 0 |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
122 n = len(sub) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
123 m = len(s) + 1 - n |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
124 r = None |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
125 while i < m: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
126 if sub == s[i:i+n]: r = i |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
127 i = i+1 |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
128 if r is None: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
129 raise index_error, (s, sub) + args |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
130 return r |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
131 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
132 # Find substring, return -1 if not found |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
133 def find(*args): |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
134 try: |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
135 return apply(index, args) |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
136 except index_error: |
|
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
137 return -1 |
| 5 | 138 |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
139 # Find last substring, return -1 if not found |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
140 def rfind(*args): |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
141 try: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
142 return apply(rindex, args) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
143 except index_error: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
144 return -1 |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
145 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
146 # Convert string to float |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
147 atof_error = 'non-float argument to string.atof' |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
148 def atof(str): |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
149 import regex |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
150 sign = '' |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
151 s = str |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
152 if s and s[0] in '+-': |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
153 sign = s[0] |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
154 s = s[1:] |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
155 if not s: raise atof_error, str |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
156 while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:] |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
157 if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s): |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
158 raise atof_error, str |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
159 try: |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
160 return eval(sign + s) |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
161 except SyntaxError: |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
162 raise atof_error, str |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
163 |
| 5 | 164 # Convert string to integer |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
165 atoi_error = 'non-integer argument to string.atoi' |
| 5 | 166 def atoi(str): |
|
928
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
167 sign = '' |
| 5 | 168 s = str |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
1078
diff
changeset
|
169 if s and s[0] in '+-': |
|
928
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
170 sign = s[0] |
|
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
171 s = s[1:] |
| 5 | 172 if not s: raise atoi_error, str |
|
928
8725fa8d7ff9
Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents:
724
diff
changeset
|
173 while s[0] == '0' and len(s) > 1: s = s[1:] |
| 5 | 174 for c in s: |
| 175 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
|
176 return eval(sign + s) |
| 5 | 177 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
178 # Convert string to long integer |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
179 atol_error = 'non-integer argument to string.atol' |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
180 def atol(str): |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
181 sign = '' |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
182 s = str |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
183 if s and s[0] in '+-': |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
184 sign = s[0] |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
185 s = s[1:] |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
186 if not s: raise atoi_error, str |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
187 while s[0] == '0' and len(s) > 1: s = s[1:] |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
188 for c in s: |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
189 if c not in digits: raise atoi_error, str |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
190 return eval(sign + s + 'L') |
|
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
191 |
| 5 | 192 # Left-justify a string |
| 193 def ljust(s, width): | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
194 n = width - len(s) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
195 if n <= 0: return s |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
196 return s + ' '*n |
| 5 | 197 |
| 198 # Right-justify a string | |
| 199 def rjust(s, width): | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
200 n = width - len(s) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
201 if n <= 0: return s |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
202 return ' '*n + s |
| 5 | 203 |
| 204 # Center a string | |
| 205 def center(s, width): | |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
206 n = width - len(s) |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
207 if n <= 0: return s |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
208 half = n/2 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
209 if n%2 and width%2: |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
210 # 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
|
211 half = half+1 |
|
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
212 return ' '*half + s + ' '*(n-half) |
| 5 | 213 |
| 214 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' | |
| 215 # Decadent feature: the argument may be a string or a number | |
| 216 # (Use of this is deprecated; it should be a string as with ljust c.s.) | |
| 217 def zfill(x, width): | |
| 559 | 218 if type(x) == type(''): s = x |
| 5 | 219 else: s = `x` |
| 220 n = len(s) | |
| 221 if n >= width: return s | |
| 222 sign = '' | |
| 365 | 223 if s[0] in ('-', '+'): |
| 224 sign, s = s[0], s[1:] | |
| 5 | 225 return sign + '0'*(width-n) + s |
|
724
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
226 |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
227 # Expand tabs in a string. |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
228 # 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
|
229 def expandtabs(s, tabsize): |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
230 res = line = '' |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
231 for c in s: |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
232 if c == '\t': |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
233 c = ' '*(tabsize - len(line)%tabsize) |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
234 line = line + c |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
235 if c == '\n': |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
236 res = res + line |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
237 line = '' |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
238 return res + line |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
239 |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
240 |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
241 # Try importing optional built-in module "strop" -- if it exists, |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
242 # it redefines some string operations that are 100-1000 times faster. |
|
1331
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
243 # It also defines values for whitespace, lowercase and uppercase |
|
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
244 # that match <ctype.h>'s definitions. |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
245 # The manipulation with index_error is needed for compatibility. |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
246 |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
247 try: |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
248 from strop import * |
|
1331
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
249 letters = lowercase + uppercase |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
250 from strop import index |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
251 index_error = ValueError |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
252 except ImportError: |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
253 pass # Use the original, slow versions |
