annotate Lib/string.py @ 5792:dffabc620e4e legacy-trunk

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