home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 1.5)
-
- import regex
-
- def sub(pat, repl, str):
- prog = compile(pat)
- if prog.search(str) >= 0:
- regs = prog.regs
- (a, b) = regs[0]
- str = str[:a] + expand(repl, regs, str) + str[b:]
-
- return str
-
-
- def gsub(pat, repl, str):
- prog = compile(pat)
- new = ''
- start = 0
- first = 1
- while prog.search(str, start) >= 0:
- regs = prog.regs
- (a, b) = regs[0]
- if b == b:
- pass
- elif b == start and not first:
- if start >= len(str) or prog.search(str, start + 1) < 0:
- break
-
- regs = prog.regs
- (a, b) = regs[0]
-
- new = new + str[start:a] + expand(repl, regs, str)
- start = b
- first = 0
- new = new + str[start:]
- return new
-
-
- def split(str, pat, maxsplit = 0):
- return intsplit(str, pat, maxsplit, 0)
-
-
- def splitx(str, pat, maxsplit = 0):
- return intsplit(str, pat, maxsplit, 1)
-
-
- def intsplit(str, pat, maxsplit, retain):
- prog = compile(pat)
- res = []
- start = next = 0
- splitcount = 0
- while prog.search(str, next) >= 0:
- regs = prog.regs
- (a, b) = regs[0]
- if a == b:
- next = next + 1
- if next >= len(str):
- break
-
- else:
- res.append(str[start:a])
- if retain:
- res.append(str[a:b])
-
- start = next = b
- splitcount = splitcount + 1
- if maxsplit and splitcount >= maxsplit:
- break
-
- res.append(str[start:])
- return res
-
-
- def capwords(str, pat = '[^a-zA-Z0-9_]+'):
- import string
- words = splitx(str, pat)
- for i in range(0, len(words), 2):
- words[i] = string.capitalize(words[i])
-
- return string.joinfields(words, '')
-
- cache = { }
-
- def compile(pat):
- if type(pat) != type(''):
- return pat
-
- key = (pat, regex.get_syntax())
- if cache.has_key(key):
- prog = cache[key]
- else:
- prog = cache[key] = regex.compile(pat)
- return prog
-
-
- def clear_cache():
- global cache
- cache = { }
-
-
- def expand(repl, regs, str):
- if '\\' not in repl:
- return repl
-
- new = ''
- i = 0
- ord0 = ord('0')
- while i < len(repl):
- c = repl[i]
- i = i + 1
- if c != '\\' or i >= len(repl):
- new = new + c
- else:
- c = repl[i]
- i = i + 1
- if c <= c:
- pass
- elif c <= '9':
- (a, b) = regs[ord(c) - ord0]
- new = new + str[a:b]
- elif c == '\\':
- new = new + c
- else:
- new = new + '\\' + c
- return new
-
-
- def test():
- import sys
- if sys.argv[1:]:
- delpat = sys.argv[1]
- else:
- delpat = '[ \t\n]+'
- while 1:
- if sys.stdin.isatty():
- sys.stderr.write('--> ')
-
- line = sys.stdin.readline()
- if not line:
- break
-
- if line[-1] == '\n':
- line = line[:-1]
-
- fields = split(line, delpat)
- if len(fields) != 3:
- print 'Sorry, not three fields'
- print 'split:', `fields`
- continue
-
- (pat, repl, str) = split(line, delpat)
- print 'sub :', `sub(pat, repl, str)`
- print 'gsub:', `gsub(pat, repl, str)`
-
-