mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
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:
parent
b9526515b7
commit
9aaee933da
17 changed files with 60 additions and 146 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue