1 # module 'string' -- A collection of string operations
3 # Warning: most of the code you see here isn't normally used nowadays.
4 # At the end of this file most functions are replaced by built-in
5 # functions imported from built-in module "strop".
7 # Some strings for ctype-style character classification
8 whitespace
= ' \t\n\r\v\f'
9 lowercase
= 'abcdefghijklmnopqrstuvwxyz'
10 uppercase
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
11 letters
= lowercase
+ uppercase
13 hexdigits
= digits
+ 'abcdef' + 'ABCDEF'
14 octdigits
= '01234567'
16 # Case conversion helpers
18 for i
in range(256): _idmap
= _idmap
+ chr(i
)
19 _lower
= _idmap
[:ord('A')] + lowercase
+ _idmap
[ord('Z')+1:]
20 _upper
= _idmap
[:ord('a')] + uppercase
+ _idmap
[ord('z')+1:]
21 _swapcase
= _upper
[:ord('A')] + lowercase
+ _upper
[ord('Z')+1:]
24 # Backward compatible names for exceptions
25 index_error
= ValueError
26 atoi_error
= ValueError
27 atof_error
= ValueError
28 atol_error
= ValueError
30 # convert UPPER CASE letters to lower case
34 res
= res
+ _lower
[ord(c
)]
37 # Convert lower case letters to UPPER CASE
41 res
= res
+ _upper
[ord(c
)]
44 # Swap lower case letters and UPPER CASE
48 res
= res
+ _swapcase
[ord(c
)]
51 # Strip leading and trailing tabs and spaces
54 while i
< j
and s
[i
] in whitespace
: i
= i
+1
55 while i
< j
and s
[j
-1] in whitespace
: j
= j
-1
58 # Split a string into a list of space/tab-separated words
59 # NB: split(s) is NOT the same as splitfields(s, ' ')!
60 def split(s
, sep
=None):
61 if sep
is not None: return splitfields(s
, sep
)
65 while i
< n
and s
[i
] in whitespace
: i
= i
+1
68 while j
< n
and s
[j
] not in whitespace
: j
= j
+1
73 # Split a list into fields separated by a given string
74 # NB: splitfields(s, ' ') is NOT the same as split(s)!
75 # splitfields(s, '') returns [s] (in analogy with split() in nawk)
76 def splitfields(s
, sep
=None):
77 if sep
is None: return split(s
)
85 if s
[j
:j
+nsep
] == sep
:
93 # Join words with spaces between them
94 def join(words
, sep
= ' '):
95 return joinfields(words
, sep
)
97 # Join fields with optional separator
98 def joinfields(words
, sep
= ' '):
101 res
= res
+ (sep
+ w
)
102 return res
[len(sep
):]
104 # Find substring, raise exception if not found
105 def index(s
, sub
, i
= 0):
106 res
= find(s
, sub
, i
)
108 raise ValueError, 'substring not found in string.index'
111 # Find last substring, raise exception if not found
112 def rindex(s
, sub
, i
= 0):
113 res
= rfind(s
, sub
, i
)
115 raise ValueError, 'substring not found in string.index'
118 # Count non-overlapping occurrences of substring
119 def count(s
, sub
, i
= 0):
120 if i
< 0: i
= max(0, i
+ len(s
))
123 if n
== 0: return m
-i
133 # Find substring, return -1 if not found
134 def find(s
, sub
, i
= 0):
135 if i
< 0: i
= max(0, i
+ len(s
))
139 if sub
== s
[i
:i
+n
]: return i
143 # Find last substring, return -1 if not found
144 def rfind(s
, sub
, i
= 0):
145 if i
< 0: i
= max(0, i
+ len(s
))
150 if sub
== s
[i
:i
+n
]: r
= i
154 # Convert string to float
159 if s
and s
[0] in '+-':
163 raise ValueError, 'non-float argument to string.atof'
164 while s
[0] == '0' and len(s
) > 1 and s
[1] in digits
: s
= s
[1:]
165 if regex
.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s
) != len(s
):
166 raise ValueError, 'non-float argument to string.atof'
168 return float(eval(sign
+ s
))
170 raise ValueError, 'non-float argument to string.atof'
172 # Convert string to integer
173 def atoi(str, base
=10):
175 # We only get here if strop doesn't define atoi()
176 raise ValueError, "this string.atoi doesn't support base != 10"
179 if s
and s
[0] in '+-':
183 raise ValueError, 'non-integer argument to string.atoi'
184 while s
[0] == '0' and len(s
) > 1: s
= s
[1:]
187 raise ValueError, 'non-integer argument to string.atoi'
188 return eval(sign
+ s
)
190 # Convert string to long integer
191 def atol(str, base
=10):
193 # We only get here if strop doesn't define atol()
194 raise ValueError, "this string.atol doesn't support base != 10"
197 if s
and s
[0] in '+-':
201 raise ValueError, 'non-integer argument to string.atol'
202 while s
[0] == '0' and len(s
) > 1: s
= s
[1:]
205 raise ValueError, 'non-integer argument to string.atol'
206 return eval(sign
+ s
+ 'L')
208 # Left-justify a string
214 # Right-justify a string
221 def center(s
, width
):
226 # This ensures that center(center(s, i), j) = center(s, j)
228 return ' '*half
+ s
+ ' '*(n
-half
)
230 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
231 # Decadent feature: the argument may be a string or a number
232 # (Use of this is deprecated; it should be a string as with ljust c.s.)
234 if type(x
) == type(''): s
= x
237 if n
>= width
: return s
239 if s
[0] in ('-', '+'):
240 sign
, s
= s
[0], s
[1:]
241 return sign
+ '0'*(width
-n
) + s
243 # Expand tabs in a string.
244 # Doesn't take non-printing chars into account, but does understand \n.
245 def expandtabs(s
, tabsize
=8):
249 c
= ' '*(tabsize
- len(line
)%tabsize
)
257 # Try importing optional built-in module "strop" -- if it exists,
258 # it redefines some string operations that are 100-1000 times faster.
259 # It also defines values for whitespace, lowercase and uppercase
260 # that match <ctype.h>'s definitions.
264 letters
= lowercase
+ uppercase
266 pass # Use the original, slow versions