lib/re1.5: Minor improvement in code size.

Signed-off-by: Andrew Leech <andrew@alelec.net>
pull/8152/head
Andrew Leech 2023-06-07 10:07:14 +10:00
rodzic 5deef8005b
commit 8e354bf903
1 zmienionych plików z 11 dodań i 11 usunięć

Wyświetl plik

@ -10,7 +10,7 @@
#define INSERT_CODE(at, num, pc) \ #define INSERT_CODE(at, num, pc) \
((code ? memmove(code + at + num, code + at, pc - at) : 0), pc += num) ((code ? memmove(code + at + num, code + at, pc - at) : 0), pc += num)
#define REL(at, to) (to - at - 2) #define REL(at, to) (to - at - 2)
#define EMIT(at, byte) (code ? (code[at] = byte) : (at)) #define EMIT(at, byte) {int _at = at; code ? (code[_at] = byte) : (0);}
#define EMIT_CHECKED(at, byte) (_emit_checked(at, code, byte, &err)) #define EMIT_CHECKED(at, byte) (_emit_checked(at, code, byte, &err))
#define PC (prog->bytelen) #define PC (prog->bytelen)
@ -29,8 +29,9 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
int term = PC; int term = PC;
int alt_label = 0; int alt_label = 0;
const char *re_top = re + len; const char *re_top = re + len;
int remain;
while (re < re_top && *re != ')') {
while ((remain = re_top - re) && *re != ')') {
switch (*re) { switch (*re) {
case '\\': case '\\':
re++; re++;
@ -80,8 +81,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
goto emit_char_pair; goto emit_char_pair;
} }
} }
if (!c) return NULL; if (remain > 2 && re[1] == '-' && re[2] != ']') {
if (re_top - re > 2 && re[1] == '-' && re[2] != ']') {
re += 2; re += 2;
} }
emit_char_pair: emit_char_pair:
@ -94,7 +94,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
case '(': { case '(': {
term = PC; term = PC;
int sub = 0; int sub = 0;
int capture = re_top - re > 2 && (re[1] != '?' || re[2] != ':'); int capture = remain > 2 && (re[1] != '?' || re[2] != ':');
if (capture) { if (capture) {
sub = ++prog->sub; sub = ++prog->sub;
@ -107,8 +107,8 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
re++; re++;
if (re >= re_top) return NULL; // Trailing bracket if (re >= re_top) return NULL; // Trailing bracket
re = _compilecode(re, re_top - re, prog, sizecode); re = _compilecode(re, remain, prog, sizecode);
if (re == NULL || re >= re_top || *re != ')') return NULL; // error, or no matching paren if (re == NULL || *re != ')') return NULL; // error, or no matching paren
if (capture) { if (capture) {
EMIT(PC++, Save); EMIT(PC++, Save);
@ -121,7 +121,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
case '?': case '?':
if (PC == term) return NULL; // nothing to repeat if (PC == term) return NULL; // nothing to repeat
INSERT_CODE(term, 2, PC); INSERT_CODE(term, 2, PC);
if (re_top - re > 1 && re[1] == '?') { if (remain > 1 && re[1] == '?') {
EMIT(term, RSplit); EMIT(term, RSplit);
re++; re++;
} else { } else {
@ -137,7 +137,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
EMIT(PC, Jmp); EMIT(PC, Jmp);
EMIT_CHECKED(PC + 1, REL(PC, term)); EMIT_CHECKED(PC + 1, REL(PC, term));
PC += 2; PC += 2;
if (re_top - re > 1 && re[1] == '?') { if (remain > 1 && re[1] == '?') {
EMIT(term, RSplit); EMIT(term, RSplit);
re++; re++;
} else { } else {
@ -149,7 +149,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
break; break;
case '+': case '+':
if (PC == term) return NULL; // nothing to repeat if (PC == term) return NULL; // nothing to repeat
if (re_top - re > 1 && re[1] == '?') { if (remain > 1 && re[1] == '?') {
EMIT(PC, Split); EMIT(PC, Split);
re++; re++;
} else { } else {