home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / REGSUB.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-05  |  4.9 KB  |  156 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import regex
  5.  
  6. def sub(pat, repl, str):
  7.     prog = compile(pat)
  8.     if prog.search(str) >= 0:
  9.         regs = prog.regs
  10.         (a, b) = regs[0]
  11.         str = str[:a] + expand(repl, regs, str) + str[b:]
  12.     
  13.     return str
  14.  
  15.  
  16. def gsub(pat, repl, str):
  17.     prog = compile(pat)
  18.     new = ''
  19.     start = 0
  20.     first = 1
  21.     while prog.search(str, start) >= 0:
  22.         regs = prog.regs
  23.         (a, b) = regs[0]
  24.         if b == b:
  25.             pass
  26.         elif b == start and not first:
  27.             if start >= len(str) or prog.search(str, start + 1) < 0:
  28.                 break
  29.             
  30.             regs = prog.regs
  31.             (a, b) = regs[0]
  32.         
  33.         new = new + str[start:a] + expand(repl, regs, str)
  34.         start = b
  35.         first = 0
  36.     new = new + str[start:]
  37.     return new
  38.  
  39.  
  40. def split(str, pat, maxsplit = 0):
  41.     return intsplit(str, pat, maxsplit, 0)
  42.  
  43.  
  44. def splitx(str, pat, maxsplit = 0):
  45.     return intsplit(str, pat, maxsplit, 1)
  46.  
  47.  
  48. def intsplit(str, pat, maxsplit, retain):
  49.     prog = compile(pat)
  50.     res = []
  51.     start = next = 0
  52.     splitcount = 0
  53.     while prog.search(str, next) >= 0:
  54.         regs = prog.regs
  55.         (a, b) = regs[0]
  56.         if a == b:
  57.             next = next + 1
  58.             if next >= len(str):
  59.                 break
  60.             
  61.         else:
  62.             res.append(str[start:a])
  63.             if retain:
  64.                 res.append(str[a:b])
  65.             
  66.             start = next = b
  67.             splitcount = splitcount + 1
  68.             if maxsplit and splitcount >= maxsplit:
  69.                 break
  70.             
  71.     res.append(str[start:])
  72.     return res
  73.  
  74.  
  75. def capwords(str, pat = '[^a-zA-Z0-9_]+'):
  76.     import string
  77.     words = splitx(str, pat)
  78.     for i in range(0, len(words), 2):
  79.         words[i] = string.capitalize(words[i])
  80.     
  81.     return string.joinfields(words, '')
  82.  
  83. cache = { }
  84.  
  85. def compile(pat):
  86.     if type(pat) != type(''):
  87.         return pat
  88.     
  89.     key = (pat, regex.get_syntax())
  90.     if cache.has_key(key):
  91.         prog = cache[key]
  92.     else:
  93.         prog = cache[key] = regex.compile(pat)
  94.     return prog
  95.  
  96.  
  97. def clear_cache():
  98.     global cache
  99.     cache = { }
  100.  
  101.  
  102. def expand(repl, regs, str):
  103.     if '\\' not in repl:
  104.         return repl
  105.     
  106.     new = ''
  107.     i = 0
  108.     ord0 = ord('0')
  109.     while i < len(repl):
  110.         c = repl[i]
  111.         i = i + 1
  112.         if c != '\\' or i >= len(repl):
  113.             new = new + c
  114.         else:
  115.             c = repl[i]
  116.             i = i + 1
  117.             if c <= c:
  118.                 pass
  119.             elif c <= '9':
  120.                 (a, b) = regs[ord(c) - ord0]
  121.                 new = new + str[a:b]
  122.             elif c == '\\':
  123.                 new = new + c
  124.             else:
  125.                 new = new + '\\' + c
  126.     return new
  127.  
  128.  
  129. def test():
  130.     import sys
  131.     if sys.argv[1:]:
  132.         delpat = sys.argv[1]
  133.     else:
  134.         delpat = '[ \t\n]+'
  135.     while 1:
  136.         if sys.stdin.isatty():
  137.             sys.stderr.write('--> ')
  138.         
  139.         line = sys.stdin.readline()
  140.         if not line:
  141.             break
  142.         
  143.         if line[-1] == '\n':
  144.             line = line[:-1]
  145.         
  146.         fields = split(line, delpat)
  147.         if len(fields) != 3:
  148.             print 'Sorry, not three fields'
  149.             print 'split:', `fields`
  150.             continue
  151.         
  152.         (pat, repl, str) = split(line, delpat)
  153.         print 'sub :', `sub(pat, repl, str)`
  154.         print 'gsub:', `gsub(pat, repl, str)`
  155.  
  156.