Skip Montanaro | 0b87444 | 2003-10-03 14:05:26 +0000 | [diff] [blame] | 1 | """A collection of string operations (most are no longer used). |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | 0b87444 | 2003-10-03 14:05:26 +0000 | [diff] [blame] | 3 | Warning: most of the code you see here isn't normally used nowadays. |
| 4 | Beginning with Python 1.6, many of these functions are implemented as |
| 5 | methods on the standard string object. They used to be implemented by |
| 6 | a built-in module called strop, but strop is now obsolete itself. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 7 | |
| 8 | Public module variables: |
| 9 | |
| 10 | whitespace -- a string containing all characters considered whitespace |
| 11 | lowercase -- a string containing all characters considered lowercase letters |
| 12 | uppercase -- a string containing all characters considered uppercase letters |
| 13 | letters -- a string containing all characters considered letters |
| 14 | digits -- a string containing all characters considered decimal digits |
| 15 | hexdigits -- a string containing all characters considered hexadecimal digits |
| 16 | octdigits -- a string containing all characters considered octal digits |
Fred Drake | fd64c59 | 2000-09-18 19:38:11 +0000 | [diff] [blame] | 17 | punctuation -- a string containing all characters considered punctuation |
| 18 | printable -- a string containing all characters considered printable |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 19 | |
| 20 | """ |
| 21 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 22 | # Some strings for ctype-style character classification |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 23 | whitespace = ' \t\n\r\v\f' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 24 | lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 25 | uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 26 | letters = lowercase + uppercase |
Fred Drake | 960fdf9 | 2001-07-20 18:38:26 +0000 | [diff] [blame] | 27 | ascii_lowercase = lowercase |
| 28 | ascii_uppercase = uppercase |
| 29 | ascii_letters = ascii_lowercase + ascii_uppercase |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 30 | digits = '0123456789' |
| 31 | hexdigits = digits + 'abcdef' + 'ABCDEF' |
| 32 | octdigits = '01234567' |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 33 | punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
Fred Drake | 6b2320f | 2000-09-18 16:46:17 +0000 | [diff] [blame] | 34 | printable = digits + letters + punctuation + whitespace |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 35 | |
| 36 | # Case conversion helpers |
Martin v. Löwis | 5357c65 | 2002-10-14 20:03:40 +0000 | [diff] [blame] | 37 | # Use str to convert Unicode literal in case of -U |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame^] | 38 | # Note that Cookie.py bogusly uses _idmap :( |
Martin v. Löwis | 5357c65 | 2002-10-14 20:03:40 +0000 | [diff] [blame] | 39 | l = map(chr, xrange(256)) |
| 40 | _idmap = str('').join(l) |
| 41 | del l |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 42 | |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame^] | 43 | # Functions which aren't available as string methods. |
| 44 | |
| 45 | # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
| 46 | # See also regsub.capwords(). |
| 47 | def capwords(s, sep=None): |
| 48 | """capwords(s, [sep]) -> string |
| 49 | |
| 50 | Split the argument into words using split, capitalize each |
| 51 | word using capitalize, and join the capitalized words using |
| 52 | join. Note that this replaces runs of whitespace characters by |
| 53 | a single space. |
| 54 | |
| 55 | """ |
| 56 | return (sep or ' ').join([x.capitalize() for x in s.split(sep)]) |
| 57 | |
| 58 | |
| 59 | # Construct a translation string |
| 60 | _idmapL = None |
| 61 | def maketrans(fromstr, tostr): |
| 62 | """maketrans(frm, to) -> string |
| 63 | |
| 64 | Return a translation table (a string of 256 bytes long) |
| 65 | suitable for use in string.translate. The strings frm and to |
| 66 | must be of the same length. |
| 67 | |
| 68 | """ |
| 69 | if len(fromstr) != len(tostr): |
| 70 | raise ValueError, "maketrans arguments must have same length" |
| 71 | global _idmapL |
| 72 | if not _idmapL: |
| 73 | _idmapL = map(None, _idmap) |
| 74 | L = _idmapL[:] |
| 75 | fromstr = map(ord, fromstr) |
| 76 | for i in range(len(fromstr)): |
| 77 | L[fromstr[i]] = tostr[i] |
| 78 | return ''.join(L) |
| 79 | |
| 80 | |
| 81 | |
| 82 | import re as _re |
| 83 | |
| 84 | class Template(unicode): |
| 85 | """A string class for supporting $-substitutions.""" |
| 86 | __slots__ = [] |
| 87 | |
| 88 | # Search for $$, $identifier, ${identifier}, and any bare $'s |
| 89 | pattern = _re.compile(r""" |
| 90 | # Match exactly two $'s -- this is the escape sequence |
| 91 | (?P<escaped>\${2})| |
| 92 | # Match a $ followed by a Python identifier |
| 93 | \$(?P<named>[_a-z][_a-z0-9]*)| |
| 94 | # Match a $ followed by a brace delimited identifier |
| 95 | \${(?P<braced>[_a-z][_a-z0-9]*)}| |
| 96 | # Match any other $'s |
| 97 | (?P<bogus>\$) |
| 98 | """, _re.IGNORECASE | _re.VERBOSE) |
| 99 | |
| 100 | def __mod__(self, mapping): |
| 101 | def convert(mo): |
| 102 | groups = mo.groupdict() |
| 103 | if groups.get('escaped') is not None: |
| 104 | return '$' |
| 105 | if groups.get('bogus') is not None: |
| 106 | raise ValueError('Invalid placeholder at index %d' % |
| 107 | mo.start('bogus')) |
| 108 | val = mapping[groups.get('named') or groups.get('braced')] |
| 109 | return unicode(val) |
| 110 | return self.pattern.sub(convert, self) |
| 111 | |
| 112 | |
| 113 | class SafeTemplate(Template): |
| 114 | """A string class for supporting $-substitutions. |
| 115 | |
| 116 | This class is 'safe' in the sense that you will never get KeyErrors if |
| 117 | there are placeholders missing from the interpolation dictionary. In that |
| 118 | case, you will get the original placeholder in the value string. |
| 119 | """ |
| 120 | __slots__ = [] |
| 121 | |
| 122 | def __mod__(self, mapping): |
| 123 | def convert(mo): |
| 124 | groups = mo.groupdict() |
| 125 | if groups.get('escaped') is not None: |
| 126 | return '$' |
| 127 | if groups.get('bogus') is not None: |
| 128 | raise ValueError('Invalid placeholder at index %d' % |
| 129 | mo.start('bogus')) |
| 130 | named = groups.get('named') |
| 131 | if named is not None: |
| 132 | try: |
| 133 | return unicode(mapping[named]) |
| 134 | except KeyError: |
| 135 | return '$' + named |
| 136 | braced = groups.get('braced') |
| 137 | try: |
| 138 | return unicode(mapping[braced]) |
| 139 | except KeyError: |
| 140 | return '${' + braced + '}' |
| 141 | return self.pattern.sub(convert, self) |
| 142 | |
| 143 | |
| 144 | |
| 145 | # NOTE: Everything below here is deprecated. Use string methods instead. |
| 146 | # This stuff will go away in Python 3.0. |
| 147 | |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 148 | # Backward compatible names for exceptions |
| 149 | index_error = ValueError |
| 150 | atoi_error = ValueError |
| 151 | atof_error = ValueError |
| 152 | atol_error = ValueError |
| 153 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 154 | # convert UPPER CASE letters to lower case |
| 155 | def lower(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 156 | """lower(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 157 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 158 | Return a copy of the string s converted to lowercase. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 159 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 160 | """ |
| 161 | return s.lower() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 162 | |
| 163 | # Convert lower case letters to UPPER CASE |
| 164 | def upper(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 165 | """upper(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 166 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 167 | Return a copy of the string s converted to uppercase. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 168 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 169 | """ |
| 170 | return s.upper() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 171 | |
| 172 | # Swap lower case letters and UPPER CASE |
| 173 | def swapcase(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 174 | """swapcase(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 175 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 176 | Return a copy of the string s with upper case characters |
| 177 | converted to lowercase and vice versa. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 178 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 179 | """ |
| 180 | return s.swapcase() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 181 | |
| 182 | # Strip leading and trailing tabs and spaces |
Martin v. Löwis | 1f04610 | 2002-11-08 12:09:59 +0000 | [diff] [blame] | 183 | def strip(s, chars=None): |
Neal Norwitz | a4864a2 | 2002-11-14 03:31:32 +0000 | [diff] [blame] | 184 | """strip(s [,chars]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 185 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 186 | Return a copy of the string s with leading and trailing |
| 187 | whitespace removed. |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 188 | If chars is given and not None, remove characters in chars instead. |
Neal Norwitz | a4864a2 | 2002-11-14 03:31:32 +0000 | [diff] [blame] | 189 | If chars is unicode, S will be converted to unicode before stripping. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 190 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 191 | """ |
Martin v. Löwis | 1f04610 | 2002-11-08 12:09:59 +0000 | [diff] [blame] | 192 | return s.strip(chars) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 194 | # Strip leading tabs and spaces |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 195 | def lstrip(s, chars=None): |
| 196 | """lstrip(s [,chars]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 197 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 198 | Return a copy of the string s with leading whitespace removed. |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 199 | If chars is given and not None, remove characters in chars instead. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 200 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 201 | """ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 202 | return s.lstrip(chars) |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 203 | |
| 204 | # Strip trailing tabs and spaces |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 205 | def rstrip(s, chars=None): |
| 206 | """rstrip(s [,chars]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 207 | |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 208 | Return a copy of the string s with trailing whitespace removed. |
| 209 | If chars is given and not None, remove characters in chars instead. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 210 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 211 | """ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 212 | return s.rstrip(chars) |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 213 | |
| 214 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 215 | # Split a string into a list of space/tab-separated words |
Guido van Rossum | 8f0c5a7 | 2000-03-10 23:22:10 +0000 | [diff] [blame] | 216 | def split(s, sep=None, maxsplit=-1): |
Fred Drake | e4f1366 | 1999-11-04 19:19:48 +0000 | [diff] [blame] | 217 | """split(s [,sep [,maxsplit]]) -> list of strings |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 218 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 219 | Return a list of the words in the string s, using sep as the |
Fred Drake | 1453754 | 2002-01-30 16:15:13 +0000 | [diff] [blame] | 220 | delimiter string. If maxsplit is given, splits at no more than |
| 221 | maxsplit places (resulting in at most maxsplit+1 words). If sep |
| 222 | is not specified, any whitespace string is a separator. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 223 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 224 | (split and splitfields are synonymous) |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 225 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 226 | """ |
| 227 | return s.split(sep, maxsplit) |
| 228 | splitfields = split |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 229 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 230 | # Split a string into a list of space/tab-separated words |
| 231 | def rsplit(s, sep=None, maxsplit=-1): |
| 232 | """rsplit(s [,sep [,maxsplit]]) -> list of strings |
| 233 | |
| 234 | Return a list of the words in the string s, using sep as the |
| 235 | delimiter string, starting at the end of the string and working |
| 236 | to the front. If maxsplit is given, at most maxsplit splits are |
| 237 | done. If sep is not specified or is None, any whitespace string |
| 238 | is a separator. |
| 239 | """ |
| 240 | return s.rsplit(sep, maxsplit) |
| 241 | |
Guido van Rossum | 2ab1992 | 1995-06-22 18:58:00 +0000 | [diff] [blame] | 242 | # Join fields with optional separator |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 243 | def join(words, sep = ' '): |
| 244 | """join(list [,sep]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 245 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 246 | Return a string composed of the words in list, with |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 247 | intervening occurrences of sep. The default separator is a |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 248 | single space. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 249 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 250 | (joinfields and join are synonymous) |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 251 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 252 | """ |
| 253 | return sep.join(words) |
| 254 | joinfields = join |
| 255 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 256 | # Find substring, raise exception if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 257 | def index(s, *args): |
| 258 | """index(s, sub [,start [,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 259 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 260 | Like find but raises ValueError when the substring is not found. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 261 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 262 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 263 | return s.index(*args) |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 265 | # Find last substring, raise exception if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 266 | def rindex(s, *args): |
| 267 | """rindex(s, sub [,start [,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 268 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 269 | Like rfind but raises ValueError when the substring is not found. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 270 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 271 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 272 | return s.rindex(*args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | |
| 274 | # Count non-overlapping occurrences of substring |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 275 | def count(s, *args): |
| 276 | """count(s, sub[, start[,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 277 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 278 | Return the number of occurrences of substring sub in string |
| 279 | s[start:end]. Optional arguments start and end are |
| 280 | interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 281 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 282 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 283 | return s.count(*args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 285 | # Find substring, return -1 if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 286 | def find(s, *args): |
| 287 | """find(s, sub [,start [,end]]) -> in |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 288 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 289 | Return the lowest index in s where substring sub is found, |
| 290 | such that sub is contained within s[start,end]. Optional |
| 291 | arguments start and end are interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 292 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 293 | Return -1 on failure. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 294 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 295 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 296 | return s.find(*args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 298 | # Find last substring, return -1 if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 299 | def rfind(s, *args): |
| 300 | """rfind(s, sub [,start [,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 301 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 302 | Return the highest index in s where substring sub is found, |
| 303 | such that sub is contained within s[start,end]. Optional |
| 304 | arguments start and end are interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 305 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 306 | Return -1 on failure. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 307 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 308 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 309 | return s.rfind(*args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 310 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 311 | # for a bit of speed |
| 312 | _float = float |
| 313 | _int = int |
| 314 | _long = long |
Guido van Rossum | d0753e2 | 1997-12-10 22:59:55 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 316 | # Convert string to float |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 317 | def atof(s): |
| 318 | """atof(s) -> float |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 319 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 320 | Return the floating point number represented by the string s. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 321 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 322 | """ |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 323 | return _float(s) |
| 324 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 326 | # Convert string to integer |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 327 | def atoi(s , base=10): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 328 | """atoi(s [,base]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 329 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 330 | Return the integer represented by the string s in the given |
| 331 | base, which defaults to 10. The string s must consist of one |
| 332 | or more digits, possibly preceded by a sign. If base is 0, it |
| 333 | is chosen from the leading characters of s, 0 for octal, 0x or |
| 334 | 0X for hexadecimal. If base is 16, a preceding 0x or 0X is |
| 335 | accepted. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 336 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 337 | """ |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 338 | return _int(s, base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 341 | # Convert string to long integer |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 342 | def atol(s, base=10): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 343 | """atol(s [,base]) -> long |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 344 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 345 | Return the long integer represented by the string s in the |
| 346 | given base, which defaults to 10. The string s must consist |
| 347 | of one or more digits, possibly preceded by a sign. If base |
| 348 | is 0, it is chosen from the leading characters of s, 0 for |
| 349 | octal, 0x or 0X for hexadecimal. If base is 16, a preceding |
| 350 | 0x or 0X is accepted. A trailing L or l is not accepted, |
| 351 | unless base is 0. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 352 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 353 | """ |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 354 | return _long(s, base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 357 | # Left-justify a string |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 358 | def ljust(s, width, *args): |
| 359 | """ljust(s, width[, fillchar]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 360 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 361 | Return a left-justified version of s, in a field of the |
| 362 | specified width, padded with spaces as needed. The string is |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 363 | never truncated. If specified the fillchar is used instead of spaces. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 364 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 365 | """ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 366 | return s.ljust(width, *args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 367 | |
| 368 | # Right-justify a string |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 369 | def rjust(s, width, *args): |
| 370 | """rjust(s, width[, fillchar]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 371 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 372 | Return a right-justified version of s, in a field of the |
| 373 | specified width, padded with spaces as needed. The string is |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 374 | never truncated. If specified the fillchar is used instead of spaces. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 375 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 376 | """ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 377 | return s.rjust(width, *args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 378 | |
| 379 | # Center a string |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 380 | def center(s, width, *args): |
| 381 | """center(s, width[, fillchar]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 382 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 383 | Return a center version of s, in a field of the specified |
| 384 | width. padded with spaces as needed. The string is never |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 385 | truncated. If specified the fillchar is used instead of spaces. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 386 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 387 | """ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 388 | return s.center(width, *args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 389 | |
| 390 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 391 | # Decadent feature: the argument may be a string or a number |
| 392 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 393 | def zfill(x, width): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 394 | """zfill(x, width) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 395 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 396 | Pad a numeric string x with zeros on the left, to fill a field |
| 397 | of the specified width. The string x is never truncated. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 398 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 399 | """ |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 400 | if not isinstance(x, basestring): |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 401 | x = repr(x) |
| 402 | return x.zfill(width) |
Guido van Rossum | 6ff2e90 | 1992-03-27 15:13:31 +0000 | [diff] [blame] | 403 | |
| 404 | # Expand tabs in a string. |
| 405 | # Doesn't take non-printing chars into account, but does understand \n. |
Guido van Rossum | 894a7bb | 1995-08-10 19:42:05 +0000 | [diff] [blame] | 406 | def expandtabs(s, tabsize=8): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 407 | """expandtabs(s [,tabsize]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 408 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 409 | Return a copy of the string s with all tab characters replaced |
| 410 | by the appropriate number of spaces, depending on the current |
| 411 | column, and the tabsize (default 8). |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 412 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 413 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 414 | return s.expandtabs(tabsize) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | 2539528 | 1996-05-28 23:08:45 +0000 | [diff] [blame] | 416 | # Character translation through look-up table. |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 417 | def translate(s, table, deletions=""): |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 418 | """translate(s,table [,deletions]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 419 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 420 | Return a copy of the string s, where all characters occurring |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 421 | in the optional argument deletions are removed, and the |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 422 | remaining characters have been mapped through the given |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 423 | translation table, which must be a string of length 256. The |
| 424 | deletions argument is not allowed for Unicode strings. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 425 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 426 | """ |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 427 | if deletions: |
| 428 | return s.translate(table, deletions) |
| 429 | else: |
| 430 | # Add s[:0] so that if s is Unicode and table is an 8-bit string, |
| 431 | # table is converted to Unicode. This means that table *cannot* |
| 432 | # be a dictionary -- for that feature, use u.translate() directly. |
| 433 | return s.translate(table + s[:0]) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 435 | # Capitalize a string, e.g. "aBc dEf" -> "Abc def". |
| 436 | def capitalize(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 437 | """capitalize(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 438 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 439 | Return a copy of the string s with only its first character |
| 440 | capitalized. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 441 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 442 | """ |
| 443 | return s.capitalize() |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 444 | |
Guido van Rossum | 1eb9a81 | 1997-03-25 16:50:31 +0000 | [diff] [blame] | 445 | # Substring replacement (global) |
Guido van Rossum | 8f0c5a7 | 2000-03-10 23:22:10 +0000 | [diff] [blame] | 446 | def replace(s, old, new, maxsplit=-1): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 447 | """replace (str, old, new[, maxsplit]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 448 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 449 | Return a copy of string str with all occurrences of substring |
| 450 | old replaced by new. If the optional argument maxsplit is |
| 451 | given, only the first maxsplit occurrences are replaced. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 452 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 453 | """ |
| 454 | return s.replace(old, new, maxsplit) |
Guido van Rossum | 1eb9a81 | 1997-03-25 16:50:31 +0000 | [diff] [blame] | 455 | |
| 456 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 457 | # Try importing optional built-in module "strop" -- if it exists, |
| 458 | # it redefines some string operations that are 100-1000 times faster. |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 459 | # It also defines values for whitespace, lowercase and uppercase |
| 460 | # that match <ctype.h>'s definitions. |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 461 | |
| 462 | try: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 463 | from strop import maketrans, lowercase, uppercase, whitespace |
| 464 | letters = lowercase + uppercase |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 465 | except ImportError: |
Fred Drake | 857c4c3 | 2000-02-10 16:21:11 +0000 | [diff] [blame] | 466 | pass # Use the original versions |