Skip to content

Commit d09a5b5

Browse files
committed
extmod: Pull in upstream changes to re1.5; fixes bug, adds named class.
1 parent 2e24034 commit d09a5b5

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

extmod/re1.5/charclass.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,23 @@ int _re1_5_classmatch(const char *pc, const char *sp)
1111
}
1212
return !is_positive;
1313
}
14+
15+
int _re1_5_namedclassmatch(const char *pc, const char *sp)
16+
{
17+
// pc points to name of class
18+
int off = (*pc >> 5) & 1;
19+
if ((*pc | 0x20) == 'd') {
20+
if (!(*sp >= '0' && *sp <= '9')) {
21+
off ^= 1;
22+
}
23+
} else if ((*pc | 0x20) == 's') {
24+
if (!(*sp == ' ' || (*sp >= '\t' && *sp <= '\r'))) {
25+
off ^= 1;
26+
}
27+
} else { // w
28+
if (!((*sp >= 'A' && *sp <= 'Z') || (*sp >= 'a' && *sp <= 'z') || (*sp >= '0' && *sp <= '9') || *sp == '_')) {
29+
off ^= 1;
30+
}
31+
}
32+
return off;
33+
}

extmod/re1.5/compilecode.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ const char *_compilecode(const char *re, ByteProg *prog)
7878
switch (*re) {
7979
case '\\':
8080
re++;
81+
if ((*re | 0x20) == 'd' || (*re | 0x20) == 's' || (*re | 0x20) == 'w') {
82+
term = pc;
83+
EMIT(pc++, NamedClass);
84+
EMIT(pc++, *re);
85+
prog->len++;
86+
break;
87+
}
8188
default:
8289
term = pc;
8390
EMIT(pc++, Char);
@@ -112,22 +119,24 @@ const char *_compilecode(const char *re, ByteProg *prog)
112119
EMIT(term + 1, cnt);
113120
break;
114121
}
115-
case '(':
122+
case '(': {
116123
term = pc;
124+
int sub = ++prog->sub;
117125

118126
EMIT(pc++, Save);
119-
EMIT(pc++, 2 * ++prog->sub);
127+
EMIT(pc++, 2 * sub);
120128
prog->len++;
121129

122130
prog->bytelen = pc;
123131
re = _compilecode(re + 1, prog);
124132
pc = prog->bytelen;
125133

126134
EMIT(pc++, Save);
127-
EMIT(pc++, 2 * prog->sub + 1);
135+
EMIT(pc++, 2 * sub + 1);
128136
prog->len++;
129137

130138
break;
139+
}
131140
case '?':
132141
insert_code(code, term, 2, &pc);
133142
EMIT(term, Split);

extmod/re1.5/re1.5.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ enum /* Inst.opcode */
8282
Any,
8383
Class,
8484
ClassNot,
85+
NamedClass,
8586

8687
ASSERTS = 0x50,
8788
Bol = ASSERTS,
@@ -145,5 +146,6 @@ int re1_5_compilecode(ByteProg *prog, const char *re);
145146
void re1_5_dumpcode(ByteProg *prog);
146147
void cleanmarks(ByteProg *prog);
147148
int _re1_5_classmatch(const char *pc, const char *sp);
149+
int _re1_5_namedclassmatch(const char *pc, const char *sp);
148150

149151
#endif /*_RE1_5_REGEXP__H*/

extmod/re1.5/recursiveloop.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ recursiveloop(char *pc, const char *sp, Subject *input, const char **subp, int n
3030
pc += *(unsigned char*)pc * 2 + 1;
3131
sp++;
3232
continue;
33+
case NamedClass:
34+
if (!_re1_5_namedclassmatch(pc, sp))
35+
return 0;
36+
pc++;
37+
sp++;
38+
continue;
3339
case Match:
3440
return 1;
3541
case Jmp:

0 commit comments

Comments
 (0)