re-pcre: Implement count arg to re.sub().

pull/118/head^2
Paul Sokolovsky 2014-06-08 00:16:34 +03:00
rodzic c4c29b4f57
commit 5262fb8237
4 zmienionych plików z 11 dodań i 4 usunięć

Wyświetl plik

@ -1,5 +1,5 @@
name = re
srctype = micropython-lib
type = module
version = 0.1.7
version = 0.1.8
author = Paul Sokolovsky

Wyświetl plik

@ -80,7 +80,7 @@ class PCREPattern:
def match(self, s, pos=0, endpos=-1):
return self.search(s, pos, endpos, PCRE_ANCHORED)
def sub(self, repl, s):
def sub(self, repl, s, count=0):
if not callable(repl):
assert "\\" not in repl, "Backrefs not implemented"
res = ""
@ -95,6 +95,10 @@ class PCREPattern:
else:
res += repl
s = s[end:]
if count != 0:
count -= 1
if count == 0:
return res + s
return res
def split(self, s, maxsplit=0):
@ -139,7 +143,7 @@ def match(pattern, string, flags=0):
def sub(pattern, repl, s, count=0, flags=0):
r = compile(pattern, flags)
return r.sub(repl, s)
return r.sub(repl, s, count)
def split(pattern, s, maxsplit=0, flags=0):

Wyświetl plik

@ -6,7 +6,7 @@ from setuptools import setup
setup(name='micropython-re-pcre',
version='0.1.7',
version='0.1.8',
description='re-pcre module for MicroPython',
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
url='https://github.com/micropython/micropython/issues/405',

Wyświetl plik

@ -12,6 +12,9 @@ assert m.group(0) == "aaaa"
assert re.sub("a", "z", "caaab") == "czzzb"
assert re.sub("a+", "z", "caaab") == "czb"
assert re.sub("a", "z", "caaab", 1) == "czaab"
assert re.sub("a", "z", "caaab", 2) == "czzab"
assert re.sub("a", "z", "caaab", 10) == "czzzb"
assert re.sub("a", lambda m: m.group(0) * 2, "caaab") == "caaaaaab"