summaryrefslogtreecommitdiffstats
path: root/Tools/modulator/varsubst.py
diff options
context:
space:
mode:
authorJack Jansen <jack.jansen@cwi.nl>2001-12-27 23:35:43 (GMT)
committerJack Jansen <jack.jansen@cwi.nl>2001-12-27 23:35:43 (GMT)
commit9aaee933da8ce390c8c34fc227054b2651264368 (patch)
tree6ae5e879867bf1977da12f9bf050366c2f5087ca /Tools/modulator/varsubst.py
parentb9526515b7303e9d09a67f8438902ff01a55abc4 (diff)
downloadcpython-9aaee933da8ce390c8c34fc227054b2651264368.zip
cpython-9aaee933da8ce390c8c34fc227054b2651264368.tar.gz
cpython-9aaee933da8ce390c8c34fc227054b2651264368.tar.bz2
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
Diffstat (limited to 'Tools/modulator/varsubst.py')
-rw-r--r--Tools/modulator/varsubst.py28
1 files changed, 12 insertions, 16 deletions
diff --git a/Tools/modulator/varsubst.py b/Tools/modulator/varsubst.py
index 06e9683..71a2b26 100644
--- a/Tools/modulator/varsubst.py
+++ b/Tools/modulator/varsubst.py
@@ -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