annotate Lib/string.py @ 3216:d00a79ebb027 legacy-trunk

Added missing imgpbm and imgsgi modules
author Jack Jansen <jack.jansen@cwi.nl>
date Thu, 12 Oct 1995 10:23:27 +0000
parents 4f9d4e73057c
children 70183592370e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # module 'string' -- A collection of string operations
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 lowercase = 'abcdefghijklmnopqrstuvwxyz'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 letters = lowercase + uppercase
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 digits = '0123456789'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 hexdigits = digits + 'abcdef' + 'ABCDEF'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 octdigits = '01234567'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 del i
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
24 # Backward compatible names for exceptions
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
25 index_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
26 atoi_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
27 atof_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
28 atol_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
29
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # convert UPPER CASE letters to lower case
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 def lower(s):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 res = ''
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 for c in s:
585
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
34 res = res + _lower[ord(c)]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # Convert lower case letters to UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 def upper(s):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 res = ''
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 for c in s:
585
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
41 res = res + _upper[ord(c)]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # Swap lower case letters and UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def swapcase(s):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 res = ''
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 for c in s:
585
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
48 res = res + _swapcase[ord(c)]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # Strip leading and trailing tabs and spaces
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 def strip(s):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 i, j = 0, len(s)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 while i < j and s[i] in whitespace: i = i+1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 while i < j and s[j-1] in whitespace: j = j-1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 return s[i:j]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # Split a string into a list of space/tab-separated words
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # NB: split(s) is NOT the same as splitfields(s, ' ')!
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
60 def split(s, sep=None):
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
61 if sep is not None: return splitfields(s, sep)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 res = []
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 i, n = 0, len(s)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 while i < n:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 while i < n and s[i] in whitespace: i = i+1
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
66 if i == n: break
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 j = i
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 while j < n and s[j] not in whitespace: j = j+1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 res.append(s[i:j])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 i = j
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # Split a list into fields separated by a given string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # 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
75 # splitfields(s, '') returns [s] (in analogy with split() in nawk)
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
76 def splitfields(s, sep=None):
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
77 if sep is None: return split(s)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 res = []
980
ffe2fbc583de splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents: 928
diff changeset
79 nsep = len(sep)
ffe2fbc583de splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents: 928
diff changeset
80 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
81 return [s]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 ns = len(s)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 i = j = 0
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 while j+nsep <= ns:
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
85 if s[j:j+nsep] == sep:
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 res.append(s[i:j])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 i = j = j + nsep
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 else:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 j = j + 1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 res.append(s[i:])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
93 # Join words with spaces between them
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
94 def join(words, sep = ' '):
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
95 return joinfields(words, sep)
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
96
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
97 # Join fields with optional separator
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
98 def joinfields(words, sep = ' '):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
99 res = ''
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
100 for w in words:
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
101 res = res + (sep + w)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
102 return res[len(sep):]
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
103
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
104 # Find substring, raise exception if not found
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
105 def index(s, sub, i = 0):
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
106 res = find(s, sub, i)
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
107 if res < 0:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
108 raise ValueError, 'substring not found in string.index'
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
109 return res
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
110
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
111 # Find last substring, raise exception if not found
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
112 def rindex(s, sub, i = 0):
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
113 res = rfind(s, sub, i)
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
114 if res < 0:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
115 raise ValueError, 'substring not found in string.index'
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
116 return res
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
117
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
118 # Count non-overlapping occurrences of substring
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
119 def count(s, sub, i = 0):
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
120 if i < 0: i = max(0, i + len(s))
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
121 n = len(sub)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
122 m = len(s) + 1 - n
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
123 if n == 0: return m-i
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
124 r = 0
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
125 while i < m:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
126 if sub == s[i:i+n]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
127 r = r+1
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
128 i = i+n
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
129 else:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
130 i = i+1
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
131 return r
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
132
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
133 # Find substring, return -1 if not found
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
134 def find(s, sub, i = 0):
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
135 if i < 0: i = max(0, i + len(s))
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
136 n = len(sub)
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
137 m = len(s) + 1 - n
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
138 while i < m:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
139 if sub == s[i:i+n]: return i
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
140 i = i+1
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
141 return -1
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
143 # Find last substring, return -1 if not found
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
144 def rfind(s, sub, i = 0):
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
145 if i < 0: i = max(0, i + len(s))
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
146 n = len(sub)
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
147 m = len(s) + 1 - n
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
148 r = -1
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
149 while i < m:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
150 if sub == s[i:i+n]: r = i
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
151 i = i+1
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
152 return r
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
153
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
154 # Convert string to float
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
155 def atof(str):
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
156 import regex
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
157 sign = ''
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
158 s = str
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
159 if s and s[0] in '+-':
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
160 sign = s[0]
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
161 s = s[1:]
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
162 if not s:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
163 raise ValueError, 'non-float argument to string.atof'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
164 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
165 if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s):
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
166 raise ValueError, 'non-float argument to string.atof'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
167 try:
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
168 return float(eval(sign + s))
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
169 except SyntaxError:
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
170 raise ValueError, 'non-float argument to string.atof'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
171
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 # Convert string to integer
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
173 def atoi(str, base=10):
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
174 if base != 10:
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
175 # We only get here if strop doesn't define atoi()
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
176 raise ValueError, "this string.atoi doesn't support base != 10"
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
177 sign = ''
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 s = str
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
179 if s and s[0] in '+-':
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
180 sign = s[0]
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
181 s = s[1:]
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
182 if not s:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
183 raise ValueError, 'non-integer argument to string.atoi'
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
184 while s[0] == '0' and len(s) > 1: s = s[1:]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 for c in s:
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
186 if c not in digits:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
187 raise ValueError, 'non-integer argument to string.atoi'
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
188 return eval(sign + s)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
190 # Convert string to long integer
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
191 def atol(str, base=10):
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
192 if base != 10:
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
193 # We only get here if strop doesn't define atol()
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
194 raise ValueError, "this string.atol doesn't support base != 10"
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
195 sign = ''
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
196 s = str
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
197 if s and s[0] in '+-':
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
198 sign = s[0]
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
199 s = s[1:]
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
200 if not s:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
201 raise ValueError, 'non-integer argument to string.atol'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
202 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
203 for c in s:
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
204 if c not in digits:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
205 raise ValueError, 'non-integer argument to string.atol'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
206 return eval(sign + s + 'L')
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
207
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 # Left-justify a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 def ljust(s, width):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
210 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
211 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
212 return s + ' '*n
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 # Right-justify a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 def rjust(s, width):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
216 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
217 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
218 return ' '*n + s
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 # Center a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 def center(s, width):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
222 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
223 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
224 half = n/2
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
225 if n%2 and width%2:
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
226 # 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
227 half = half+1
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
228 return ' '*half + s + ' '*(n-half)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 # Decadent feature: the argument may be a string or a number
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 # (Use of this is deprecated; it should be a string as with ljust c.s.)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 def zfill(x, width):
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
234 if type(x) == type(''): s = x
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 else: s = `x`
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 n = len(s)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 if n >= width: return s
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 sign = ''
365
4e3073bb0e3e A plus is also a sign!
Guido van Rossum <guido@python.org>
parents: 196
diff changeset
239 if s[0] in ('-', '+'):
4e3073bb0e3e A plus is also a sign!
Guido van Rossum <guido@python.org>
parents: 196
diff changeset
240 sign, s = s[0], s[1:]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 return sign + '0'*(width-n) + s
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
242
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
243 # Expand tabs in a string.
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
244 # Doesn't take non-printing chars into account, but does understand \n.
2958
4f9d4e73057c default tabsize to 8
Guido van Rossum <guido@python.org>
parents: 2778
diff changeset
245 def expandtabs(s, tabsize=8):
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
246 res = line = ''
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
247 for c in s:
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
248 if c == '\t':
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
249 c = ' '*(tabsize - len(line)%tabsize)
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
250 line = line + c
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
251 if c == '\n':
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
252 res = res + line
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
253 line = ''
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
254 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
255
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
256
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
257 # 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
258 # 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
259 # 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
260 # 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
261
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
262 try:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
263 from strop import *
1331
07900834cc97 * pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents: 1291
diff changeset
264 letters = lowercase + uppercase
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
265 except ImportError:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
266 pass # Use the original, slow versions