home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / STRING.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-10-30  |  18.8 KB  |  644 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Common string manipulations.
  5.  
  6. Public module variables:
  7.  
  8. whitespace -- a string containing all characters considered whitespace
  9. lowercase -- a string containing all characters considered lowercase letters
  10. uppercase -- a string containing all characters considered uppercase letters
  11. letters -- a string containing all characters considered letters
  12. digits -- a string containing all characters considered decimal digits
  13. hexdigits -- a string containing all characters considered hexadecimal digits
  14. octdigits -- a string containing all characters considered octal digits
  15.  
  16. '''
  17. whitespace = ' \t\n\r\x0b\x0c'
  18. lowercase = 'abcdefghijklmnopqrstuvwxyz'
  19. uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  20. letters = lowercase + uppercase
  21. digits = '0123456789'
  22. hexdigits = digits + 'abcdef' + 'ABCDEF'
  23. octdigits = '01234567'
  24. _idmap = ''
  25. for i in range(256):
  26.     _idmap = _idmap + chr(i)
  27.  
  28. _lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z') + 1:]
  29. _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z') + 1:]
  30. _swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z') + 1:]
  31. del i
  32. index_error = ValueError
  33. atoi_error = ValueError
  34. atof_error = ValueError
  35. atol_error = ValueError
  36.  
  37. def lower(s):
  38.     '''lower(s) -> string
  39.  
  40. \tReturn a copy of the string s converted to lowercase.
  41.  
  42. \t'''
  43.     res = ''
  44.     for c in s:
  45.         res = res + _lower[ord(c)]
  46.     
  47.     return res
  48.  
  49.  
  50. def upper(s):
  51.     '''upper(s) -> string
  52.  
  53. \tReturn a copy of the string s converted to uppercase.
  54.  
  55. \t'''
  56.     res = ''
  57.     for c in s:
  58.         res = res + _upper[ord(c)]
  59.     
  60.     return res
  61.  
  62.  
  63. def swapcase(s):
  64.     '''swapcase(s) -> string
  65.  
  66. \tReturn a copy of the string s with upper case characters
  67. \tconverted to lowercase and vice versa.
  68.  
  69. \t'''
  70.     res = ''
  71.     for c in s:
  72.         res = res + _swapcase[ord(c)]
  73.     
  74.     return res
  75.  
  76.  
  77. def strip(s):
  78.     '''strip(s) -> string
  79.  
  80. \tReturn a copy of the string s with leading and trailing
  81. \twhitespace removed.
  82.  
  83. \t'''
  84.     (i, j) = (0, len(s))
  85.     while i < j and s[i] in whitespace:
  86.         i = i + 1
  87.     while i < j and s[j - 1] in whitespace:
  88.         j = j - 1
  89.     return s[i:j]
  90.  
  91.  
  92. def lstrip(s):
  93.     '''lstrip(s) -> string
  94.  
  95. \tReturn a copy of the string s with leading whitespace removed.
  96.  
  97. \t'''
  98.     (i, j) = (0, len(s))
  99.     while i < j and s[i] in whitespace:
  100.         i = i + 1
  101.     return s[i:j]
  102.  
  103.  
  104. def rstrip(s):
  105.     '''rstrip(s) -> string
  106.  
  107. \tReturn a copy of the string s with trailing whitespace
  108. \tremoved.
  109.  
  110. \t'''
  111.     (i, j) = (0, len(s))
  112.     while i < j and s[j - 1] in whitespace:
  113.         j = j - 1
  114.     return s[i:j]
  115.  
  116.  
  117. def split(s, sep = None, maxsplit = 0):
  118.     '''split(str [,sep [,maxsplit]]) -> list of strings
  119.  
  120. \tReturn a list of the words in the string s, using sep as the
  121. \tdelimiter string.  If maxsplit is nonzero, splits into at most
  122. \tmaxsplit words If sep is not specified, any whitespace string
  123. \tis a separator.  Maxsplit defaults to 0.
  124.  
  125. \t(split and splitfields are synonymous)
  126.  
  127. \t'''
  128.     if sep is not None:
  129.         return splitfields(s, sep, maxsplit)
  130.     
  131.     res = []
  132.     (i, n) = (0, len(s))
  133.     if maxsplit <= 0:
  134.         maxsplit = n
  135.     
  136.     count = 0
  137.     while i < n:
  138.         while i < n and s[i] in whitespace:
  139.             i = i + 1
  140.         if i == n:
  141.             break
  142.         
  143.         if count >= maxsplit:
  144.             res.append(s[i:])
  145.             break
  146.         
  147.         j = i
  148.         while j < n and s[j] not in whitespace:
  149.             j = j + 1
  150.         count = count + 1
  151.         res.append(s[i:j])
  152.         i = j
  153.     return res
  154.  
  155.  
  156. def splitfields(s, sep = None, maxsplit = 0):
  157.     '''splitfields(str [,sep [,maxsplit]]) -> list of strings
  158.  
  159. \tReturn a list of the words in the string s, using sep as the
  160. \tdelimiter string.  If maxsplit is nonzero, splits into at most
  161. \tmaxsplit words If sep is not specified, any whitespace string
  162. \tis a separator.  Maxsplit defaults to 0.
  163.  
  164. \t(split and splitfields are synonymous)
  165.  
  166. \t'''
  167.     if sep is None:
  168.         return split(s, None, maxsplit)
  169.     
  170.     res = []
  171.     nsep = len(sep)
  172.     if nsep == 0:
  173.         return [
  174.             s]
  175.     
  176.     ns = len(s)
  177.     if maxsplit <= 0:
  178.         maxsplit = ns
  179.     
  180.     i = j = 0
  181.     count = 0
  182.     while j + nsep <= ns:
  183.         if s[j:j + nsep] == sep:
  184.             count = count + 1
  185.             res.append(s[i:j])
  186.             i = j = j + nsep
  187.             if count >= maxsplit:
  188.                 break
  189.             
  190.         else:
  191.             j = j + 1
  192.     res.append(s[i:])
  193.     return res
  194.  
  195.  
  196. def join(words, sep = ' '):
  197.     '''join(list [,sep]) -> string
  198.  
  199. \tReturn a string composed of the words in list, with
  200. \tintervening occurences of sep.  Sep defaults to a single
  201. \tspace.
  202.  
  203. \t(joinfields and join are synonymous)
  204.  
  205. \t'''
  206.     return joinfields(words, sep)
  207.  
  208.  
  209. def joinfields(words, sep = ' '):
  210.     '''joinfields(list [,sep]) -> string
  211.  
  212. \tReturn a string composed of the words in list, with
  213. \tintervening occurences of sep.  The default separator is a
  214. \tsingle space.
  215.  
  216. \t(joinfields and join are synonymous)
  217.  
  218. \t'''
  219.     res = ''
  220.     for w in words:
  221.         res = res + sep + w
  222.     
  223.     return res[len(sep):]
  224.  
  225.  
  226. def index(s, sub, i = 0, last = None):
  227.     '''index(s, sub [,start [,end]]) -> int
  228.  
  229. \tReturn the lowest index in s where substring sub is found,
  230. \tsuch that sub is contained within s[start,end].  Optional
  231. \targuments start and end are interpreted as in slice notation.
  232.  
  233. \tRaise ValueError if not found.
  234.  
  235. \t'''
  236.     if last is None:
  237.         last = len(s)
  238.     
  239.     res = find(s, sub, i, last)
  240.     if res < 0:
  241.         raise ValueError, 'substring not found in string.index'
  242.     
  243.     return res
  244.  
  245.  
  246. def rindex(s, sub, i = 0, last = None):
  247.     '''rindex(s, sub [,start [,end]]) -> int
  248.  
  249. \tReturn the highest index in s where substring sub is found,
  250. \tsuch that sub is contained within s[start,end].  Optional
  251. \targuments start and end are interpreted as in slice notation.
  252.  
  253. \tRaise ValueError if not found.
  254.  
  255. \t'''
  256.     if last is None:
  257.         last = len(s)
  258.     
  259.     res = rfind(s, sub, i, last)
  260.     if res < 0:
  261.         raise ValueError, 'substring not found in string.index'
  262.     
  263.     return res
  264.  
  265.  
  266. def count(s, sub, i = 0, last = None):
  267.     '''count(s, sub[, start[,end]]) -> int
  268.  
  269. \tReturn the number of occurrences of substring sub in string
  270. \ts[start:end].  Optional arguments start and end are
  271. \tinterpreted as in slice notation.
  272.  
  273. \t'''
  274.     Slen = len(s)
  275.     if last is None:
  276.         last = Slen
  277.     elif last < 0:
  278.         last = max(0, last + Slen)
  279.     elif last > Slen:
  280.         last = Slen
  281.     
  282.     if i < 0:
  283.         i = max(0, i + Slen)
  284.     
  285.     n = len(sub)
  286.     m = last + 1 - n
  287.     if n == 0:
  288.         return m - i
  289.     
  290.     r = 0
  291.     while i < m:
  292.         if sub == s[i:i + n]:
  293.             r = r + 1
  294.             i = i + n
  295.         else:
  296.             i = i + 1
  297.     return r
  298.  
  299.  
  300. def find(s, sub, i = 0, last = None):
  301.     '''find(s, sub [,start [,end]]) -> in
  302.  
  303. \tReturn the lowest index in s where substring sub is found,
  304. \tsuch that sub is contained within s[start,end].  Optional
  305. \targuments start and end are interpreted as in slice notation.
  306.  
  307. \tReturn -1 on failure.
  308.  
  309. \t'''
  310.     Slen = len(s)
  311.     if last is None:
  312.         last = Slen
  313.     elif last < 0:
  314.         last = max(0, last + Slen)
  315.     elif last > Slen:
  316.         last = Slen
  317.     
  318.     if i < 0:
  319.         i = max(0, i + Slen)
  320.     
  321.     n = len(sub)
  322.     m = last + 1 - n
  323.     while i < m:
  324.         if sub == s[i:i + n]:
  325.             return i
  326.         
  327.         i = i + 1
  328.     return -1
  329.  
  330.  
  331. def rfind(s, sub, i = 0, last = None):
  332.     '''rfind(s, sub [,start [,end]]) -> int
  333.  
  334. \tReturn the highest index in s where substring sub is found,
  335. \tsuch that sub is contained within s[start,end].  Optional
  336. \targuments start and end are interpreted as in slice notation.
  337.  
  338. \tReturn -1 on failure.
  339.  
  340. \t'''
  341.     Slen = len(s)
  342.     if last is None:
  343.         last = Slen
  344.     elif last < 0:
  345.         last = max(0, last + Slen)
  346.     elif last > Slen:
  347.         last = Slen
  348.     
  349.     if i < 0:
  350.         i = max(0, i + Slen)
  351.     
  352.     n = len(sub)
  353.     m = last + 1 - n
  354.     r = -1
  355.     while i < m:
  356.         if sub == s[i:i + n]:
  357.             r = i
  358.         
  359.         i = i + 1
  360.     return r
  361.  
  362. _safe_env = {
  363.     '__builtins__': { } }
  364. _re = None
  365.  
  366. def atof(str):
  367.     '''atof(s) -> float
  368.  
  369. \tReturn the floating point number represented by the string s.
  370.  
  371. \t'''
  372.     global _re, _re
  373.     if _re is None:
  374.         
  375.         try:
  376.             import re
  377.         except ImportError:
  378.             _re = 0
  379.  
  380.         _re = re
  381.     
  382.     sign = ''
  383.     s = strip(str)
  384.     if s and s[0] in '+-':
  385.         sign = s[0]
  386.         s = s[1:]
  387.     
  388.     if not s:
  389.         raise ValueError, 'non-float argument to string.atof'
  390.     
  391.     while s[0] == '0' and len(s) > 1 and s[1] in digits:
  392.         s = s[1:]
  393.     if _re and not _re.match('[0-9]*(\\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
  394.         raise ValueError, 'non-float argument to string.atof'
  395.     
  396.     
  397.     try:
  398.         return float(eval(sign + s, _safe_env))
  399.     except SyntaxError:
  400.         raise ValueError, 'non-float argument to string.atof'
  401.  
  402.  
  403.  
  404. def atoi(str, base = 10):
  405.     '''atoi(s [,base]) -> int
  406.  
  407. \tReturn the integer represented by the string s in the given
  408. \tbase, which defaults to 10.  The string s must consist of one
  409. \tor more digits, possibly preceded by a sign.  If base is 0, it
  410. \tis chosen from the leading characters of s, 0 for octal, 0x or
  411. \t0X for hexadecimal.  If base is 16, a preceding 0x or 0X is
  412. \taccepted.
  413.  
  414. \t'''
  415.     if base != 10:
  416.         raise ValueError, "this string.atoi doesn't support base != 10"
  417.     
  418.     sign = ''
  419.     s = strip(str)
  420.     if s and s[0] in '+-':
  421.         sign = s[0]
  422.         s = s[1:]
  423.     
  424.     if not s:
  425.         raise ValueError, 'non-integer argument to string.atoi'
  426.     
  427.     while s[0] == '0' and len(s) > 1:
  428.         s = s[1:]
  429.     for c in s:
  430.         pass
  431.     
  432.     return eval(sign + s, _safe_env)
  433.  
  434.  
  435. def atol(str, base = 10):
  436.     '''atol(s [,base]) -> long
  437.  
  438. \tReturn the long integer represented by the string s in the
  439. \tgiven base, which defaults to 10.  The string s must consist
  440. \tof one or more digits, possibly preceded by a sign.  If base
  441. \tis 0, it is chosen from the leading characters of s, 0 for
  442. \toctal, 0x or 0X for hexadecimal.  If base is 16, a preceding
  443. \t0x or 0X is accepted.  A trailing L or l is not accepted,
  444. \tunless base is 0.
  445.  
  446. \t'''
  447.     if base != 10:
  448.         raise ValueError, "this string.atol doesn't support base != 10"
  449.     
  450.     sign = ''
  451.     s = strip(str)
  452.     if s and s[0] in '+-':
  453.         sign = s[0]
  454.         s = s[1:]
  455.     
  456.     if not s:
  457.         raise ValueError, 'non-integer argument to string.atol'
  458.     
  459.     while s[0] == '0' and len(s) > 1:
  460.         s = s[1:]
  461.     for c in s:
  462.         pass
  463.     
  464.     return eval(sign + s + 'L', _safe_env)
  465.  
  466.  
  467. def ljust(s, width):
  468.     '''ljust(s, width) -> string
  469.  
  470. \tReturn a left-justified version of s, in a field of the
  471. \tspecified width, padded with spaces as needed.  The string is
  472. \tnever truncated.
  473.  
  474. \t'''
  475.     n = width - len(s)
  476.     if n <= 0:
  477.         return s
  478.     
  479.     return s + ' ' * n
  480.  
  481.  
  482. def rjust(s, width):
  483.     '''rjust(s, width) -> string
  484.  
  485. \tReturn a right-justified version of s, in a field of the
  486. \tspecified width, padded with spaces as needed.  The string is
  487. \tnever truncated.
  488.  
  489. \t'''
  490.     n = width - len(s)
  491.     if n <= 0:
  492.         return s
  493.     
  494.     return ' ' * n + s
  495.  
  496.  
  497. def center(s, width):
  498.     '''center(s, width) -> string
  499.  
  500. \tReturn a center version of s, in a field of the specified
  501. \twidth. padded with spaces as needed.  The string is never
  502. \ttruncated.
  503.  
  504. \t'''
  505.     n = width - len(s)
  506.     if n <= 0:
  507.         return s
  508.     
  509.     half = n / 2
  510.     if n % 2 and width % 2:
  511.         half = half + 1
  512.     
  513.     return ' ' * half + s + ' ' * (n - half)
  514.  
  515.  
  516. def zfill(x, width):
  517.     '''zfill(x, width) -> string
  518.  
  519. \tPad a numeric string x with zeros on the left, to fill a field
  520. \tof the specified width.  The string x is never truncated.
  521.  
  522. \t'''
  523.     if type(x) == type(''):
  524.         s = x
  525.     else:
  526.         s = `x`
  527.     n = len(s)
  528.     if n >= width:
  529.         return s
  530.     
  531.     sign = ''
  532.     if s[0] in ('-', '+'):
  533.         (sign, s) = (s[0], s[1:])
  534.     
  535.     return sign + '0' * (width - n) + s
  536.  
  537.  
  538. def expandtabs(s, tabsize = 8):
  539.     '''expandtabs(s [,tabsize]) -> string
  540.  
  541. \tReturn a copy of the string s with all tab characters replaced
  542. \tby the appropriate number of spaces, depending on the current
  543. \tcolumn, and the tabsize (default 8).
  544.  
  545. \t'''
  546.     res = line = ''
  547.     for c in s:
  548.         line = line + c
  549.         if c == '\n':
  550.             res = res + line
  551.             line = ''
  552.         
  553.     
  554.     return res + line
  555.  
  556.  
  557. def translate(s, table, deletions = ''):
  558.     '''translate(s,table [,deletechars]) -> string
  559.  
  560. \tReturn a copy of the string s, where all characters occurring
  561. \tin the optional argument deletechars are removed, and the
  562. \tremaining characters have been mapped through the given
  563. \ttranslation table, which must be a string of length 256.
  564.  
  565. \t'''
  566.     if type(table) != type('') or len(table) != 256:
  567.         raise TypeError, 'translation table must be 256 characters long'
  568.     
  569.     res = ''
  570.     for c in s:
  571.         pass
  572.     
  573.     return res
  574.  
  575.  
  576. def capitalize(s):
  577.     '''capitalize(s) -> string
  578.  
  579. \tReturn a copy of the string s with only its first character
  580. \tcapitalized.
  581.  
  582. \t'''
  583.     return upper(s[:1]) + lower(s[1:])
  584.  
  585.  
  586. def capwords(s, sep = None):
  587.     '''capwords(s, [sep]) -> string
  588.  
  589. \tSplit the argument into words using split, capitalize each
  590. \tword using capitalize, and join the capitalized words using
  591. \tjoin. Note that this replaces runs of whitespace characters by
  592. \ta single space.
  593.  
  594. \t'''
  595.     if not sep:
  596.         pass
  597.     return join(map(capitalize, split(s, sep)), ' ')
  598.  
  599. _idmapL = None
  600.  
  601. def maketrans(fromstr, tostr):
  602.     '''maketrans(frm, to) -> string
  603.  
  604. \tReturn a translation table (a string of 256 bytes long)
  605. \tsuitable for use in string.translate.  The strings frm and to
  606. \tmust be of the same length.
  607.  
  608. \t'''
  609.     global _idmapL
  610.     if len(fromstr) != len(tostr):
  611.         raise ValueError, 'maketrans arguments must have same length'
  612.     
  613.     if not _idmapL:
  614.         _idmapL = map(None, _idmap)
  615.     
  616.     L = _idmapL[:]
  617.     fromstr = map(ord, fromstr)
  618.     for i in range(len(fromstr)):
  619.         L[fromstr[i]] = tostr[i]
  620.     
  621.     return joinfields(L, '')
  622.  
  623.  
  624. def replace(str, old, new, maxsplit = 0):
  625.     '''replace (str, old, new[, maxsplit]) -> string
  626.  
  627. \tReturn a copy of string str with all occurrences of substring
  628. \told replaced by new. If the optional argument maxsplit is
  629. \tgiven, only the first maxsplit occurrences are replaced.
  630.  
  631. \t'''
  632.     return joinfields(splitfields(str, old, maxsplit), new)
  633.  
  634.  
  635. try:
  636.     *
  637. except ImportError:
  638.     0
  639.     0
  640.     range(256)
  641. except:
  642.     0
  643.  
  644.