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 # convert UPPER CASE letters to lower case
28 res
= res
+ _lower
[ord(c
)]
31 # Convert lower case letters to UPPER CASE
35 res
= res
+ _upper
[ord(c
)]
38 # Swap lower case letters and UPPER CASE
42 res
= res
+ _swapcase
[ord(c
)]
45 # Strip leading and trailing tabs and spaces
48 while i
< j
and s
[i
] in whitespace
: i
= i
+1
49 while i
< j
and s
[j
-1] in whitespace
: j
= j
-1
52 # Split a string into a list of space/tab-separated words
53 # NB: split(s) is NOT the same as splitfields(s, ' ')!
58 while i
< n
and s
[i
] in whitespace
: i
= i
+1
61 while j
< n
and s
[j
] not in whitespace
: j
= j
+1
66 # Split a list into fields separated by a given string
67 # NB: splitfields(s, ' ') is NOT the same as split(s)!
68 # splitfields(s, '') returns [s] (in analogy with split() in nawk)
69 def splitfields(s
, sep
):
77 if s
[j
:j
+nsep
] == sep
:
85 # Join words with spaces between them
87 return joinfields(words
, ' ')
89 # Join fields with separator
90 def joinfields(words
, sep
):
96 # Find substring, raise exception if not found
97 index_error
= 'substring not found in string.index'
98 def index(s
, sub
, *args
):
101 raise TypeError, 'string.index(): too many args'
103 if i
< 0: i
= i
+ len(s
)
109 if sub
== s
[i
:i
+n
]: return i
111 raise index_error
, (s
, sub
) + args
113 # Find last substring, raise exception if not found
114 def rindex(s
, sub
, *args
):
117 raise TypeError, 'string.rindex(): too many args'
119 if i
< 0: i
= i
+ len(s
)
126 if sub
== s
[i
:i
+n
]: r
= i
129 raise index_error
, (s
, sub
) + args
132 # Find substring, return -1 if not found
135 return apply(index
, args
)
139 # Find last substring, return -1 if not found
142 return apply(rindex
, args
)
146 # Convert string to float
147 atof_error
= 'non-float argument to string.atof'
152 if s
and s
[0] in '+-':
155 if not s
: raise atof_error
, str
156 while s
[0] == '0' and len(s
) > 1 and s
[1] in digits
: s
= s
[1:]
157 if regex
.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s
) != len(s
):
158 raise atof_error
, str
160 return float(eval(sign
+ s
))
162 raise atof_error
, str
164 # Convert string to integer
165 atoi_error
= 'non-integer argument to string.atoi'
169 if s
and s
[0] in '+-':
172 if not s
: raise atoi_error
, str
173 while s
[0] == '0' and len(s
) > 1: s
= s
[1:]
175 if c
not in digits
: raise atoi_error
, str
176 return eval(sign
+ s
)
178 # Convert string to long integer
179 atol_error
= 'non-integer argument to string.atol'
183 if s
and s
[0] in '+-':
186 if not s
: raise atoi_error
, str
187 while s
[0] == '0' and len(s
) > 1: s
= s
[1:]
189 if c
not in digits
: raise atoi_error
, str
190 return eval(sign
+ s
+ 'L')
192 # Left-justify a string
198 # Right-justify a string
205 def center(s
, width
):
210 # This ensures that center(center(s, i), j) = center(s, j)
212 return ' '*half
+ s
+ ' '*(n
-half
)
214 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
215 # Decadent feature: the argument may be a string or a number
216 # (Use of this is deprecated; it should be a string as with ljust c.s.)
218 if type(x
) == type(''): s
= x
221 if n
>= width
: return s
223 if s
[0] in ('-', '+'):
224 sign
, s
= s
[0], s
[1:]
225 return sign
+ '0'*(width
-n
) + s
227 # Expand tabs in a string.
228 # Doesn't take non-printing chars into account, but does understand \n.
229 def expandtabs(s
, tabsize
):
233 c
= ' '*(tabsize
- len(line
)%tabsize
)
241 # Try importing optional built-in module "strop" -- if it exists,
242 # it redefines some string operations that are 100-1000 times faster.
243 # It also defines values for whitespace, lowercase and uppercase
244 # that match <ctype.h>'s definitions.
248 letters
= lowercase
+ uppercase
250 pass # Use the original, slow versions
252 # If certain functions are found, redefine the corresponding exceptions
256 from strop
import index
257 index_error
= ValueError
259 pass # Use the original, slow versions
262 from strop
import atoi
263 atoi_error
= ValueError
265 pass # Use the original, slow versions
268 from strop
import atof
269 atof_error
= ValueError
271 pass # Use the original, slow versions
274 from strop
import atol
275 atol_error
= ValueError
277 pass # Use the original, slow versions