re-pcre: sub(): support functional replacement arg.

asyncio-segfault
Paul Sokolovsky 2014-04-11 06:05:39 +03:00
rodzic 72b5eedd3d
commit 9ad8f7090d
1 zmienionych plików z 6 dodań i 3 usunięć

Wyświetl plik

@ -62,6 +62,7 @@ class PCREPattern:
return self.search(s, PCRE_ANCHORED) return self.search(s, PCRE_ANCHORED)
def sub(self, repl, s): def sub(self, repl, s):
if not callable(repl):
assert "\\" not in repl, "Backrefs not implemented" assert "\\" not in repl, "Backrefs not implemented"
res = "" res = ""
while s: while s:
@ -70,7 +71,9 @@ class PCREPattern:
return res + s return res + s
beg, end = m.span() beg, end = m.span()
res += s[:beg] res += s[:beg]
assert not callable(repl) if callable(repl):
res += repl(m)
else:
res += repl res += repl
s = s[end:] s = s[end:]