lib/re1.5: Add support for named classes in class sets.

Total code size change of this and previous commit:

   bare-arm:    +0 +0.000%
minimal x86:    +0 +0.000%
   unix x64:   +32 +0.004% standard
      stm32:   +24 +0.006% PYBV10
     cc3200:   +16 +0.009%
    esp8266:   +20 +0.003% GENERIC
      esp32:   +44 +0.003% GENERIC[incl +8(data)]
     mimxrt:   +32 +0.009% TEENSY40
 renesas-ra:   +24 +0.004% RA6M2_EK
        nrf:    +0 +0.000% pca10040
        rp2:   +24 +0.005% PICO
       samd:   +32 +0.012% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Addresses issue #7920.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-10-22 22:23:47 +11:00
parent bd86ce5f82
commit 64193c7de9
3 changed files with 20 additions and 4 deletions

View File

@ -6,7 +6,15 @@ int _re1_5_classmatch(const char *pc, const char *sp)
int is_positive = (pc[-1] == Class); int is_positive = (pc[-1] == Class);
int cnt = *pc++; int cnt = *pc++;
while (cnt--) { while (cnt--) {
if (*sp >= *pc && *sp <= pc[1]) return is_positive; if (*pc == RE15_CLASS_NAMED_CLASS_INDICATOR) {
if (_re1_5_namedclassmatch(pc + 1, sp)) {
return is_positive;
}
} else {
if (*sp >= *pc && *sp <= pc[1]) {
return is_positive;
}
}
pc += 2; pc += 2;
} }
return !is_positive; return !is_positive;

View File

@ -66,14 +66,21 @@ static const char *_compilecode(const char *re, ByteProg *prog, int sizecode)
PC++; // Skip # of pair byte PC++; // Skip # of pair byte
prog->len++; prog->len++;
for (cnt = 0; *re != ']'; re++, cnt++) { for (cnt = 0; *re != ']'; re++, cnt++) {
if (*re == '\\') { char c = *re;
if (c == '\\') {
++re; ++re;
c = *re;
if (MATCH_NAMED_CLASS_CHAR(c)) {
c = RE15_CLASS_NAMED_CLASS_INDICATOR;
goto emit_char_pair;
}
} }
if (!*re) return NULL; if (!c) return NULL;
EMIT(PC++, *re);
if (re[1] == '-' && re[2] != ']') { if (re[1] == '-' && re[2] != ']') {
re += 2; re += 2;
} }
emit_char_pair:
EMIT(PC++, c);
EMIT(PC++, *re); EMIT(PC++, *re);
} }
EMIT_CHECKED(term + 1, cnt); EMIT_CHECKED(term + 1, cnt);

View File

@ -138,6 +138,7 @@ struct Subject {
#define NON_ANCHORED_PREFIX 5 #define NON_ANCHORED_PREFIX 5
#define HANDLE_ANCHORED(bytecode, is_anchored) ((is_anchored) ? (bytecode) + NON_ANCHORED_PREFIX : (bytecode)) #define HANDLE_ANCHORED(bytecode, is_anchored) ((is_anchored) ? (bytecode) + NON_ANCHORED_PREFIX : (bytecode))
#define RE15_CLASS_NAMED_CLASS_INDICATOR 0
int re1_5_backtrack(ByteProg*, Subject*, const char**, int, int); int re1_5_backtrack(ByteProg*, Subject*, const char**, int, int);
int re1_5_pikevm(ByteProg*, Subject*, const char**, int, int); int re1_5_pikevm(ByteProg*, Subject*, const char**, int, int);