10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# module 'string' -- A collection of string operations
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Warning: most of the code you see here isn't normally used nowadays.  With
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Python 1.6, many of these functions are implemented as methods on the
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# standard string object. They used to be implemented by a built-in module
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# called strop, but strop is now obsolete itself.
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Common string manipulations.
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
100a8c90248264a8b26970b4473770bcc3df8515fJosh GaoPublic module variables:
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowhitespace -- a string containing all characters considered whitespace
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaolowercase -- a string containing all characters considered lowercase letters
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaouppercase -- a string containing all characters considered uppercase letters
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoletters -- a string containing all characters considered letters
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodigits -- a string containing all characters considered decimal digits
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohexdigits -- a string containing all characters considered hexadecimal digits
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaooctdigits -- a string containing all characters considered octal digits
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom warnings import warnpy3k
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowarnpy3k("the stringold module has been removed in Python 3.0", stacklevel=2)
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel warnpy3k
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Some strings for ctype-style character classification
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowhitespace = ' \t\n\r\v\f'
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaolowercase = 'abcdefghijklmnopqrstuvwxyz'
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaouppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoletters = lowercase + uppercase
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodigits = '0123456789'
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohexdigits = digits + 'abcdef' + 'ABCDEF'
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaooctdigits = '01234567'
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Case conversion helpers
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_idmap = ''
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofor i in range(256): _idmap = _idmap + chr(i)
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel i
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Backward compatible names for exceptions
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoindex_error = ValueError
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoatoi_error = ValueError
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoatof_error = ValueError
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoatol_error = ValueError
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# convert UPPER CASE letters to lower case
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef lower(s):
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """lower(s) -> string
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s converted to lowercase.
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.lower()
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert lower case letters to UPPER CASE
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef upper(s):
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """upper(s) -> string
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s converted to uppercase.
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.upper()
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Swap lower case letters and UPPER CASE
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef swapcase(s):
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """swapcase(s) -> string
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s with upper case characters
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    converted to lowercase and vice versa.
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.swapcase()
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Strip leading and trailing tabs and spaces
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef strip(s):
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """strip(s) -> string
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s with leading and trailing
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    whitespace removed.
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.strip()
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Strip leading tabs and spaces
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef lstrip(s):
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """lstrip(s) -> string
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s with leading whitespace removed.
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.lstrip()
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Strip trailing tabs and spaces
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rstrip(s):
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """rstrip(s) -> string
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s with trailing whitespace
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    removed.
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.rstrip()
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Split a string into a list of space/tab-separated words
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef split(s, sep=None, maxsplit=0):
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """split(str [,sep [,maxsplit]]) -> list of strings
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a list of the words in the string s, using sep as the
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    delimiter string.  If maxsplit is nonzero, splits into at most
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    maxsplit words If sep is not specified, any whitespace string
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    is a separator.  Maxsplit defaults to 0.
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    (split and splitfields are synonymous)
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.split(sep, maxsplit)
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaosplitfields = split
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Join fields with optional separator
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef join(words, sep = ' '):
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """join(list [,sep]) -> string
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a string composed of the words in list, with
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    intervening occurrences of sep.  The default separator is a
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    single space.
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    (joinfields and join are synonymous)
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return sep.join(words)
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojoinfields = join
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# for a little bit of speed
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_apply = apply
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find substring, raise exception if not found
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef index(s, *args):
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """index(s, sub [,start [,end]]) -> int
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Like find but raises ValueError when the substring is not found.
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _apply(s.index, args)
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find last substring, raise exception if not found
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rindex(s, *args):
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """rindex(s, sub [,start [,end]]) -> int
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Like rfind but raises ValueError when the substring is not found.
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _apply(s.rindex, args)
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Count non-overlapping occurrences of substring
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef count(s, *args):
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """count(s, sub[, start[,end]]) -> int
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return the number of occurrences of substring sub in string
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    s[start:end].  Optional arguments start and end are
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    interpreted as in slice notation.
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _apply(s.count, args)
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find substring, return -1 if not found
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef find(s, *args):
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """find(s, sub [,start [,end]]) -> in
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return the lowest index in s where substring sub is found,
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    such that sub is contained within s[start,end].  Optional
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    arguments start and end are interpreted as in slice notation.
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return -1 on failure.
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _apply(s.find, args)
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find last substring, return -1 if not found
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rfind(s, *args):
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """rfind(s, sub [,start [,end]]) -> int
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return the highest index in s where substring sub is found,
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    such that sub is contained within s[start,end].  Optional
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    arguments start and end are interpreted as in slice notation.
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return -1 on failure.
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _apply(s.rfind, args)
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# for a bit of speed
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_float = float
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_int = int
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_long = long
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_StringType = type('')
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert string to float
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef atof(s):
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """atof(s) -> float
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return the floating point number represented by the string s.
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(s) == _StringType:
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return _float(s)
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise TypeError('argument 1: expected string, %s found' %
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        type(s).__name__)
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert string to integer
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef atoi(*args):
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """atoi(s [,base]) -> int
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return the integer represented by the string s in the given
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    base, which defaults to 10.  The string s must consist of one
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    or more digits, possibly preceded by a sign.  If base is 0, it
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    is chosen from the leading characters of s, 0 for octal, 0x or
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    0X for hexadecimal.  If base is 16, a preceding 0x or 0X is
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    accepted.
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    try:
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        s = args[0]
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    except IndexError:
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise TypeError('function requires at least 1 argument: %d given' %
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        len(args))
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Don't catch type error resulting from too many arguments to int().  The
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # error message isn't compatible but the error type is, and this function
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # is complicated enough already.
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(s) == _StringType:
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return _apply(_int, args)
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise TypeError('argument 1: expected string, %s found' %
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        type(s).__name__)
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert string to long integer
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef atol(*args):
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """atol(s [,base]) -> long
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return the long integer represented by the string s in the
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    given base, which defaults to 10.  The string s must consist
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    of one or more digits, possibly preceded by a sign.  If base
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    is 0, it is chosen from the leading characters of s, 0 for
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    octal, 0x or 0X for hexadecimal.  If base is 16, a preceding
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    0x or 0X is accepted.  A trailing L or l is not accepted,
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    unless base is 0.
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    try:
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        s = args[0]
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    except IndexError:
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise TypeError('function requires at least 1 argument: %d given' %
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        len(args))
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Don't catch type error resulting from too many arguments to long().  The
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # error message isn't compatible but the error type is, and this function
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # is complicated enough already.
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(s) == _StringType:
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return _apply(_long, args)
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise TypeError('argument 1: expected string, %s found' %
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        type(s).__name__)
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Left-justify a string
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef ljust(s, width):
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """ljust(s, width) -> string
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a left-justified version of s, in a field of the
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    specified width, padded with spaces as needed.  The string is
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    never truncated.
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    n = width - len(s)
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if n <= 0: return s
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s + ' '*n
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Right-justify a string
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rjust(s, width):
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """rjust(s, width) -> string
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a right-justified version of s, in a field of the
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    specified width, padded with spaces as needed.  The string is
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    never truncated.
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    n = width - len(s)
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if n <= 0: return s
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return ' '*n + s
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Center a string
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef center(s, width):
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """center(s, width) -> string
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a center version of s, in a field of the specified
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    width. padded with spaces as needed.  The string is never
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    truncated.
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    n = width - len(s)
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if n <= 0: return s
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    half = n/2
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if n%2 and width%2:
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # This ensures that center(center(s, i), j) = center(s, j)
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        half = half+1
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return ' '*half +  s + ' '*(n-half)
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Decadent feature: the argument may be a string or a number
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# (Use of this is deprecated; it should be a string as with ljust c.s.)
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef zfill(x, width):
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """zfill(x, width) -> string
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Pad a numeric string x with zeros on the left, to fill a field
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    of the specified width.  The string x is never truncated.
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(x) == type(''): s = x
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else: s = repr(x)
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    n = len(s)
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if n >= width: return s
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    sign = ''
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if s[0] in ('-', '+'):
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sign, s = s[0], s[1:]
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return sign + '0'*(width-n) + s
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Expand tabs in a string.
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Doesn't take non-printing chars into account, but does understand \n.
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef expandtabs(s, tabsize=8):
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """expandtabs(s [,tabsize]) -> string
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s with all tab characters replaced
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    by the appropriate number of spaces, depending on the current
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    column, and the tabsize (default 8).
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    res = line = ''
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for c in s:
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if c == '\t':
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            c = ' '*(tabsize - len(line) % tabsize)
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        line = line + c
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if c == '\n':
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            res = res + line
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            line = ''
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return res + line
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Character translation through look-up table.
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef translate(s, table, deletions=""):
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """translate(s,table [,deletechars]) -> string
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s, where all characters occurring
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    in the optional argument deletechars are removed, and the
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    remaining characters have been mapped through the given
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    translation table, which must be a string of length 256.
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.translate(table, deletions)
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Capitalize a string, e.g. "aBc  dEf" -> "Abc  def".
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef capitalize(s):
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """capitalize(s) -> string
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of the string s with only its first character
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    capitalized.
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.capitalize()
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Capitalize the words in a string, e.g. " aBc  dEf " -> "Abc Def".
3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef capwords(s, sep=None):
3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """capwords(s, [sep]) -> string
3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Split the argument into words using split, capitalize each
3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    word using capitalize, and join the capitalized words using
3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    join. Note that this replaces runs of whitespace characters by
3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    a single space.
3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return join(map(capitalize, s.split(sep)), sep or ' ')
3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Construct a translation string
3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_idmapL = None
3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef maketrans(fromstr, tostr):
3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """maketrans(frm, to) -> string
3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a translation table (a string of 256 bytes long)
3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    suitable for use in string.translate.  The strings frm and to
3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    must be of the same length.
3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if len(fromstr) != len(tostr):
3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise ValueError, "maketrans arguments must have same length"
3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    global _idmapL
3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if not _idmapL:
3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _idmapL = list(_idmap)
3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    L = _idmapL[:]
3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    fromstr = map(ord, fromstr)
3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for i in range(len(fromstr)):
3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L[fromstr[i]] = tostr[i]
3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return join(L, "")
4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Substring replacement (global)
4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef replace(s, old, new, maxsplit=0):
4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """replace (str, old, new[, maxsplit]) -> string
4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Return a copy of string str with all occurrences of substring
4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    old replaced by new. If the optional argument maxsplit is
4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    given, only the first maxsplit occurrences are replaced.
4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return s.replace(old, new, maxsplit)
4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# XXX: transitional
4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#
4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# If string objects do not have methods, then we need to use the old string.py
4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# library, which uses strop for many more things than just the few outlined
4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# below.
4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry:
4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ''.upper
4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept AttributeError:
4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    from stringold import *
4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Try importing optional built-in module "strop" -- if it exists,
4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# it redefines some string operations that are 100-1000 times faster.
4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# It also defines values for whitespace, lowercase and uppercase
4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# that match <ctype.h>'s definitions.
4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry:
4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    from strop import maketrans, lowercase, uppercase, whitespace
4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    letters = lowercase + uppercase
4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ImportError:
4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass                                          # Use the original versions
433