annotate Lib/stringold.py @ 6719:faebc1940432 legacy-trunk

Understand -Xlinker option.
author Guido van Rossum <guido@python.org>
date Tue, 02 Dec 1997 20:38:11 +0000
parents d790fe25e148
children 8ef481c7c3af
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
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
58 # Strip leading tabs and spaces
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
59 def lstrip(s):
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
60 i, j = 0, len(s)
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
61 while i < j and s[i] in whitespace: i = i+1
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
62 return s[i:j]
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
63
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
64 # Strip trailing tabs and spaces
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
65 def rstrip(s):
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
66 i, j = 0, len(s)
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
67 while i < j and s[j-1] in whitespace: j = j-1
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
68 return s[i:j]
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
69
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
70
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # Split a string into a list of space/tab-separated words
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 # NB: split(s) is NOT the same as splitfields(s, ' ')!
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
73 def split(s, sep=None, maxsplit=0):
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
74 if sep is not None: return splitfields(s, sep, maxsplit)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 res = []
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 i, n = 0, len(s)
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
77 if maxsplit <= 0: maxsplit = n
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
78 count = 0
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 while i < n:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 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
81 if i == n: break
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
82 if count >= maxsplit:
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
83 res.append(s[i:])
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
84 break
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 j = i
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 while j < n and s[j] not in whitespace: j = j+1
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
87 count = count + 1
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 res.append(s[i:j])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 i = j
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 # Split a list into fields separated by a given string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # 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
94 # splitfields(s, '') returns [s] (in analogy with split() in nawk)
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
95 def splitfields(s, sep=None, maxsplit=0):
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
96 if sep is None: return split(s, None, maxsplit)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 res = []
980
ffe2fbc583de splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents: 928
diff changeset
98 nsep = len(sep)
ffe2fbc583de splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents: 928
diff changeset
99 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
100 return [s]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 ns = len(s)
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
102 if maxsplit <= 0: maxsplit = ns
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 i = j = 0
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
104 count = 0
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 while j+nsep <= ns:
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
106 if s[j:j+nsep] == sep:
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
107 count = count + 1
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 res.append(s[i:j])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 i = j = j + nsep
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
110 if count >= maxsplit: break
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
111
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 else:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 j = j + 1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 res.append(s[i:])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
117 # 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
118 def join(words, sep = ' '):
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
119 return joinfields(words, sep)
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
120
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
121 # Join fields with optional separator
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
122 def joinfields(words, sep = ' '):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
123 res = ''
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
124 for w in words:
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
125 res = res + (sep + w)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
126 return res[len(sep):]
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
127
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
128 # Find substring, raise exception if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
129 def index(s, sub, i = 0, last=None):
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
130 if last is None: last = len(s)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
131 res = find(s, sub, i, last)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
132 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
133 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
134 return res
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
135
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
136 # Find last substring, raise exception if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
137 def rindex(s, sub, i = 0, last=None):
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
138 if last is None: last = len(s)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
139 res = rfind(s, sub, i, last)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
140 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
141 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
142 return res
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
143
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
144 # Count non-overlapping occurrences of substring
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
145 def count(s, sub, i = 0, last=None):
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
146 Slen = len(s) # cache this value, for speed
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
147 if last is None:
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
148 last = Slen
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
149 elif last < 0:
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
150 last = max(0, last + Slen)
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
151 elif last > Slen:
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
152 last = Slen
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
153 if i < 0: i = max(0, i + Slen)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
154 n = len(sub)
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
155 m = last + 1 - n
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
156 if n == 0: return m-i
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
157 r = 0
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
158 while i < m:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
159 if sub == s[i:i+n]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
160 r = r+1
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
161 i = i+n
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
162 else:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
163 i = i+1
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
164 return r
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
165
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
166 # Find substring, return -1 if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
167 def find(s, sub, i = 0, last=None):
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
168 Slen = len(s) # cache this value, for speed
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
169 if last is None:
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
170 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
171 elif last < 0:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
172 last = max(0, last + Slen)
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
173 elif last > Slen:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
174 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
175 if i < 0: i = max(0, i + Slen)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
176 n = len(sub)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
177 m = last + 1 - n
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
178 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
179 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
180 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
181 return -1
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
183 # Find last substring, return -1 if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
184 def rfind(s, sub, i = 0, last=None):
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
185 Slen = len(s) # cache this value, for speed
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
186 if last is None:
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
187 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
188 elif last < 0:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
189 last = max(0, last + Slen)
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
190 elif last > Slen:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
191 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
192 if i < 0: i = max(0, i + Slen)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
193 n = len(sub)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
194 m = last + 1 - n
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
195 r = -1
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
196 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
197 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
198 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
199 return r
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
200
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
201 # Convert string to float
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
202 re = None
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
203 def atof(str):
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
204 global re
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
205 if re is None:
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
206 import re
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
207 sign = ''
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
208 s = strip(str)
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
209 if s and s[0] in '+-':
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
210 sign = s[0]
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
211 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
212 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
213 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
214 while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
215 if not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
216 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
217 try:
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
218 return float(eval(sign + s))
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
219 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
220 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
221
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 # 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
223 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
224 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
225 # 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
226 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
227 sign = ''
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 s = str
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
229 if s and s[0] in '+-':
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
230 sign = s[0]
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
231 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
232 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
233 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
234 while s[0] == '0' and len(s) > 1: s = s[1:]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 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
236 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
237 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
238 return eval(sign + s)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
240 # 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
241 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
242 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
243 # 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
244 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
245 sign = ''
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
246 s = str
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
247 if s and s[0] in '+-':
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
248 sign = s[0]
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 return eval(sign + s + 'L')
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
257
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 # Left-justify a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 def ljust(s, width):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
260 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
261 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
262 return s + ' '*n
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 # Right-justify a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 def rjust(s, width):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
266 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
267 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
268 return ' '*n + s
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 # Center a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 def center(s, width):
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
272 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
273 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
274 half = n/2
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
275 if n%2 and width%2:
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
276 # 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
277 half = half+1
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
278 return ' '*half + s + ' '*(n-half)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 # 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
281 # Decadent feature: the argument may be a string or a number
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 # (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
283 def zfill(x, width):
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
284 if type(x) == type(''): s = x
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 else: s = `x`
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 n = len(s)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 if n >= width: return s
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 sign = ''
365
4e3073bb0e3e A plus is also a sign!
Guido van Rossum <guido@python.org>
parents: 196
diff changeset
289 if s[0] in ('-', '+'):
4e3073bb0e3e A plus is also a sign!
Guido van Rossum <guido@python.org>
parents: 196
diff changeset
290 sign, s = s[0], s[1:]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 return sign + '0'*(width-n) + s
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
292
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
293 # Expand tabs in a string.
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
294 # 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
295 def expandtabs(s, tabsize=8):
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
296 res = line = ''
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
297 for c in s:
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
298 if c == '\t':
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
299 c = ' '*(tabsize - len(line)%tabsize)
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
300 line = line + c
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
301 if c == '\n':
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
302 res = res + line
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
303 line = ''
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
304 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
305
3639
70183592370e add translate() -- which was in strop per release 1.3
Guido van Rossum <guido@python.org>
parents: 2958
diff changeset
306 # Character translation through look-up table.
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
307 def translate(s, table, deletions=""):
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
308 if type(table) != type('') or len(table) != 256:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
309 raise TypeError, "translation table must be 256 characters long"
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
310 res = ""
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
311 for c in s:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
312 if c not in deletions:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
313 res = res + table[ord(c)]
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
314 return res
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
315
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
316 # Capitalize a string, e.g. "aBc dEf" -> "Abc def".
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
317 def capitalize(s):
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
318 return upper(s[:1]) + lower(s[1:])
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
319
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
320 # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
321 # See also regsub.capwords().
4076
eda29bef880c Add optional separator character to capwords(), for completeness.
Guido van Rossum <guido@python.org>
parents: 3957
diff changeset
322 def capwords(s, sep=None):
4145
951ed9a83f57 Use correct separator for capwords(s, sep).
Guido van Rossum <guido@python.org>
parents: 4076
diff changeset
323 return join(map(capitalize, split(s, sep)), sep or ' ')
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
324
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
325 # Construct a translation string
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
326 _idmapL = None
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
327 def maketrans(fromstr, tostr):
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
328 if len(fromstr) != len(tostr):
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
329 raise ValueError, "maketrans arguments must have same length"
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
330 global _idmapL
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
331 if not _idmapL:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
332 _idmapL = map(None, _idmap)
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
333 L = _idmapL[:]
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
334 fromstr = map(ord, fromstr)
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
335 for i in range(len(fromstr)):
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
336 L[fromstr[i]] = tostr[i]
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
337 return joinfields(L, "")
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
338
5213
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
339 # Substring replacement (global)
5244
247139d200e8 Changed my mind on replace().
Guido van Rossum <guido@python.org>
parents: 5213
diff changeset
340 def replace(str, old, new, maxsplit=0):
247139d200e8 Changed my mind on replace().
Guido van Rossum <guido@python.org>
parents: 5213
diff changeset
341 return joinfields(splitfields(str, old, maxsplit), new)
5213
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
342
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
343
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
344 # 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
345 # 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
346 # 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
347 # 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
348
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
349 try:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
350 from strop import *
1331
07900834cc97 * pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents: 1291
diff changeset
351 letters = lowercase + uppercase
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
352 except ImportError:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
353 pass # Use the original, slow versions