Skip to content

Commit 068c427

Browse files
committed
Merge pull request aichaos#14 from FujiMakoto/master
Use formatted string replacements instead of regex substitutions where applicable
2 parents 54a890f + 9319bfe commit 068c427

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

rivescript/rivescript.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ def reply(self, user, msg):
15291529
# Okay to continue?
15301530
if '{ok}' in begin:
15311531
reply = self._getreply(user, msg)
1532-
begin = re.sub('{ok}', reply, begin)
1532+
begin = begin.replace('{ok}', reply)
15331533

15341534
reply = begin
15351535

@@ -1841,13 +1841,13 @@ def _getreply(self, user, msg, context='normal', step=0):
18411841
for match in reTopic:
18421842
self._say("Setting user's topic to " + match)
18431843
self._users[user]["topic"] = match
1844-
reply = re.sub(r'\{topic=' + re.escape(match) + r'\}', '', reply)
1844+
reply = reply.replace('{{topic={match}}}'.format(match=match), '')
18451845

18461846
reSet = re.findall(RE.set_tag, reply)
18471847
for match in reSet:
18481848
self._say("Set uservar " + str(match[0]) + "=" + str(match[1]))
18491849
self._users[user][match[0]] = match[1]
1850-
reply = re.sub('<set ' + re.escape(match[0]) + '=' + re.escape(match[1]) + '>', '', reply)
1850+
reply = reply.replace('<set {key}={value}>'.format(key=match[0], value=match[1]), '')
18511851
else:
18521852
# Process more tags if not in BEGIN.
18531853
reply = self._process_tags(user, msg, reply, stars, thatstars, step)
@@ -1892,7 +1892,7 @@ def _substitute(self, msg, kind):
18921892
for match in placeholders:
18931893
i = int(match)
18941894
result = ph[i]
1895-
msg = re.sub(r'\x00' + match + r'\x00', result, msg)
1895+
msg = msg.replace('\x00' + match + '\x00', result)
18961896

18971897
# Strip & return.
18981898
return msg.strip()
@@ -1967,15 +1967,15 @@ def _reply_regexp(self, user, regexp):
19671967
rep = ''
19681968
if var in self._bvars:
19691969
rep = self._strip_nasties(self._bvars[var])
1970-
regexp = re.sub(r'<bot ' + re.escape(var) + r'>', rep, regexp)
1970+
regexp = regexp.replace('<bot {var}>'.format(var=var), rep)
19711971

19721972
# Filter in user variables.
19731973
uvars = re.findall(RE.get_tag, regexp)
19741974
for var in uvars:
19751975
rep = ''
19761976
if var in self._users[user]:
19771977
rep = self._strip_nasties(self._users[user][var])
1978-
regexp = re.sub(r'<get ' + re.escape(var) + r'>', rep, regexp)
1978+
regexp = regexp.replace('<get {var}>'.format(var=var), rep)
19791979

19801980
# Filter in <input> and <reply> tags. This is a slow process, so only
19811981
# do it if we have to!
@@ -1984,15 +1984,12 @@ def _reply_regexp(self, user, regexp):
19841984
tags = re.findall(r'<' + type + r'([0-9])>', regexp)
19851985
for index in tags:
19861986
rep = self._format_message(self._users[user]['__history__'][type][int(index) - 1])
1987-
regexp = re.sub(r'<' + type + str(index) + r'>', rep, regexp)
1988-
regexp = re.sub(
1989-
'<' + type + '>',
1990-
self._format_message(self._users[user]['__history__'][type][0]),
1991-
regexp
1992-
)
1987+
regexp = regexp.replace('<{type}{index}>'.format(type=type, index=index), rep)
1988+
regexp = regexp.replace('<{type}>'.format(type=type),
1989+
self._format_message(self._users[user]['__history__'][type][0]))
19931990
# TODO: the Perl version doesn't do just <input>/<reply> in trigs!
19941991

1995-
return re.compile(r'^' +regexp + r'$')
1992+
return re.compile(r'^' + regexp + r'$')
19961993

19971994
def _precompile_regexp(self, trigger):
19981995
"""Precompile the regex for most triggers.
@@ -2036,23 +2033,25 @@ def _process_tags(self, user, msg, reply, st=[], bst=[], depth=0):
20362033
reStars = re.findall(RE.star_tags, reply)
20372034
for match in reStars:
20382035
if int(match) < len(stars):
2039-
reply = re.sub(r'<star' + match + '>', stars[int(match)], reply)
2036+
reply = reply.replace('<star{match}>'.format(match=match), stars[int(match)])
20402037
if len(botstars) > 0:
20412038
reply = reply.replace('<botstar>', botstars[1])
20422039
reStars = re.findall(RE.botstars, reply)
20432040
for match in reStars:
20442041
if int(match) < len(botstars):
2045-
reply = re.sub(r'<botstar' + match + '>', botstars[int(match)], reply)
2042+
reply = reply.replace('<botstar{match}>'.format(match=match), botstars[int(match)])
20462043

20472044
# <input> and <reply>
20482045
reply = reply.replace('<input>', self._users[user]['__history__']['input'][0])
20492046
reply = reply.replace('<reply>', self._users[user]['__history__']['reply'][0])
20502047
reInput = re.findall(RE.input_tags, reply)
20512048
for match in reInput:
2052-
reply = re.sub(r'<input' + match + r'>', self._users[user]['__history__']['input'][int(match) - 1], reply)
2049+
reply = reply.replace('<input{match}>'.format(match=match),
2050+
self._users[user]['__history__']['input'][int(match) - 1])
20532051
reReply = re.findall(RE.reply_tags, reply)
20542052
for match in reReply:
2055-
reply = re.sub(r'<reply' + match + r'>', self._users[user]['__history__']['reply'][int(match) - 1], reply)
2053+
reply = reply.replace('<reply{match}>'.format(match=match),
2054+
self._users[user]['__history__']['reply'][int(match) - 1])
20562055

20572056
# <id> and escape codes.
20582057
reply = reply.replace('<id>', user)
@@ -2068,7 +2067,7 @@ def _process_tags(self, user, msg, reply, st=[], bst=[], depth=0):
20682067
output = random.choice(match.split('|'))
20692068
else:
20702069
output = random.choice(match.split(' '))
2071-
reply = re.sub(r'\{random\}' + re.escape(match) + r'\{/random\}', output, reply)
2070+
reply = reply.replace('{{random}}{match}{{/random}}'.format(match=match), output)
20722071

20732072
# Person Substitutions and String Formatting.
20742073
for item in ['person', 'formal', 'sentence', 'uppercase', 'lowercase']:
@@ -2080,7 +2079,7 @@ def _process_tags(self, user, msg, reply, st=[], bst=[], depth=0):
20802079
output = self._substitute(match, "person")
20812080
else:
20822081
output = self._string_format(match, item)
2083-
reply = re.sub(r'\{' + item + r'\}' + re.escape(match) + '\{/' + item + r'\}', output, reply)
2082+
reply = reply.replace('{{{item}}}{match}{{/{item}}}'.format(item=item, match=match), output)
20842083

20852084
# Handle all variable-related tags with an iterative regex approach,
20862085
# to allow for nesting of tags in arbitrary ways (think <set a=<get b>>)
@@ -2169,15 +2168,15 @@ def _process_tags(self, user, msg, reply, st=[], bst=[], depth=0):
21692168
for match in reTopic:
21702169
self._say("Setting user's topic to " + match)
21712170
self._users[user]["topic"] = match
2172-
reply = re.sub(r'\{topic=' + re.escape(match) + r'\}', '', reply)
2171+
reply = reply.replace('{{topic={match}}}'.format(match=match), '')
21732172

21742173
# Inline redirecter.
21752174
reRedir = re.findall(RE.redir_tag, reply)
21762175
for match in reRedir:
21772176
self._say("Redirect to " + match)
21782177
at = match.strip()
21792178
subreply = self._getreply(user, at, step=(depth + 1))
2180-
reply = re.sub(r'\{@' + re.escape(match) + r'\}', subreply, reply)
2179+
reply = reply.replace('{{@{match}}}'.format(match=match), subreply)
21812180

21822181
# Object caller.
21832182
reply = reply.replace("{__call__}", "<call>")
@@ -2203,7 +2202,7 @@ def _process_tags(self, user, msg, reply, st=[], bst=[], depth=0):
22032202
else:
22042203
output = '[ERR: Object Not Found]'
22052204

2206-
reply = re.sub('<call>' + re.escape(match) + r'</call>', output, reply)
2205+
reply = reply.replace('<call>{match}</call>'.format(match=match), output)
22072206

22082207
return reply
22092208

0 commit comments

Comments
 (0)