8000 extmod/re1.5: Support escaping within RE classes. · adafruit/circuitpython@ebf8332 · GitHub
[go: up one dir, main page]

Skip to content

Commit ebf8332

Browse files
jimmodpgeorge
authored andcommitted
extmod/re1.5: Support escaping within RE classes.
Fixes issues #3178 and #5220. Tests are added, including all the cases mentioned in both bugs.
1 parent 7a7ee16 commit ebf8332

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

extmod/re1.5/compilecode.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
5353
PC++; // Skip # of pair byte
5454
prog->len++;
5555
for (cnt = 0; *re != ']'; re++, cnt++) {
56+
if (*re == '\\') {
57+
++re;
58+
}
5659
if (!*re) return NULL;
5760
EMIT(PC++, *re);
5861
if (re[1] == '-' && re[2] != ']') {

tests/extmod/ure1.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,23 @@
8888

8989
# bytes objects
9090
m = re.match(rb'a+?', b'ab'); print(m.group(0))
91+
print("===")
92+
93+
# escaping
94+
m = re.match(r'a\.c', 'a.c'); print(m.group(0) if m else '')
95+
m = re.match(r'a\.b', 'abc'); print(m is None)
96+
m = re.match(r'a\.b', 'a\\bc'); print(m is None)
97+
m = re.match(r'[a\-z]', 'abc'); print(m.group(0))
98+
m = re.match(r'[.\]]*', '.].]a'); print(m.group(0))
99+
m = re.match(r'[.\]+]*', '.]+.]a'); print(m.group(0))
100+
m = re.match(r'[a-f0-9x\-yz]*', 'abxcd1-23'); print(m.group(0))
101+
m = re.match(r'[a\\b]*', 'a\\aa\\bb\\bbab'); print(m.group(0))
102+
m = re.search(r'[a\-z]', '-'); print(m.group(0))
103+
m = re.search(r'[a\-z]', 'f'); print(m is None)
104+
m = re.search(r'[a\]z]', 'a'); print(m.group(0))
105+
print(re.compile(r'[-a]').split('foo-bar'))
106+
print(re.compile(r'[a-]').split('foo-bar'))
107+
print(re.compile(r'[ax\-]').split('foo-bar'))
108+
print(re.compile(r'[a\-x]').split('foo-bar'))
109+
print(re.compile(r'[\-ax]').split('foo-bar'))
110+
print("===")

tests/extmod/ure_error.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ def test_re(r):
2323
test_re(r'[')
2424
test_re(r'([')
2525
test_re(r'([)')
26+
test_re(r'[a\]')

0 commit comments

Comments
 (0)
0