Patches by Jens B. Jorgensen with small mods by me:

- Converted the templates to use ANSI C prototypes (finally!)
- Use re in stead of deprecated regex
This commit is contained in:
Jack Jansen 2001-12-27 23:35:43 +00:00
parent b9526515b7
commit 9aaee933da
17 changed files with 60 additions and 146 deletions

View file

@ -2,37 +2,33 @@
# Variable substitution. Variables are $delimited$
#
import string
import regex
import regsub
import re
error = 'varsubst.error'
class Varsubst:
def __init__(self, dict):
self.dict = dict
self.prog = regex.compile('\$[a-zA-Z0-9_]*\$')
self.prog = re.compile('\$([a-zA-Z0-9_]*)\$')
self.do_useindent = 0
def useindent(self, onoff):
self.do_useindent = onoff
def subst(self, str):
def subst(self, s):
rv = ''
while 1:
pos = self.prog.search(str)
if pos < 0:
return rv + str
if pos:
rv = rv + str[:pos]
str = str[pos:]
len = self.prog.match(str)
if len == 2:
m = self.prog.search(s)
if not m:
return rv + s
rv = rv + s[:m.start()]
s = s[m.end():]
if m.end() - m.start() == 2:
# Escaped dollar
rv = rv + '$'
str = str[2:]
s = s[2:]
continue
name = str[1:len-1]
str = str[len:]
name = m.group(1)
if not self.dict.has_key(name):
raise error, 'No such variable: '+name
value = self.dict[name]
@ -44,7 +40,7 @@ class Varsubst:
lastnl = string.rfind(old, '\n', 0) + 1
lastnl = len(old) - lastnl
sub = '\n' + (' '*lastnl)
return regsub.gsub('\n', sub, value)
return re.sub('\n', sub, value)
def _test():
import sys