Skip to content

Commit

Permalink
lib/re1.5: Minor improvement in code size.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Leech <andrew@alelec.net>
  • Loading branch information
pi-anl committed Jul 31, 2023
1 parent 5deef80 commit 8e354bf
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/re1.5/compilecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define INSERT_CODE(at, num, pc) \
((code ? memmove(code + at + num, code + at, pc - at) : 0), pc += num)
#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 PC (prog->bytelen)

Expand All @@ -29,8 +29,9 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
int term = PC;
int alt_label = 0;
const char *re_top = re + len;

while (re < re_top && *re != ')') {
int remain;

while ((remain = re_top - re) && *re != ')') {
switch (*re) {
case '\\':
re++;
Expand Down Expand Up @@ -80,8 +81,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
goto emit_char_pair;
}
}
if (!c) return NULL;
if (re_top - re > 2 && re[1] == '-' && re[2] != ']') {
if (remain > 2 && re[1] == '-' && re[2] != ']') {
re += 2;
}
emit_char_pair:
Expand All @@ -94,7 +94,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
case '(': {
term = PC;
int sub = 0;
int capture = re_top - re > 2 && (re[1] != '?' || re[2] != ':');
int capture = remain > 2 && (re[1] != '?' || re[2] != ':');

if (capture) {
sub = ++prog->sub;
Expand All @@ -107,8 +107,8 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int

re++;
if (re >= re_top) return NULL; // Trailing bracket
re = _compilecode(re, re_top - re, prog, sizecode);
if (re == NULL || re >= re_top || *re != ')') return NULL; // error, or no matching paren
re = _compilecode(re, remain, prog, sizecode);
if (re == NULL || *re != ')') return NULL; // error, or no matching paren

if (capture) {
EMIT(PC++, Save);
Expand All @@ -121,7 +121,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
case '?':
if (PC == term) return NULL; // nothing to repeat
INSERT_CODE(term, 2, PC);
if (re_top - re > 1 && re[1] == '?') {
if (remain > 1 && re[1] == '?') {
EMIT(term, RSplit);
re++;
} else {
Expand All @@ -137,7 +137,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
EMIT(PC, Jmp);
EMIT_CHECKED(PC + 1, REL(PC, term));
PC += 2;
if (re_top - re > 1 && re[1] == '?') {
if (remain > 1 && re[1] == '?') {
EMIT(term, RSplit);
re++;
} else {
Expand All @@ -149,7 +149,7 @@ static const char *_compilecode(const char *re, size_t len, ByteProg *prog, int
break;
case '+':
if (PC == term) return NULL; // nothing to repeat
if (re_top - re > 1 && re[1] == '?') {
if (remain > 1 && re[1] == '?') {
EMIT(PC, Split);
re++;
} else {
Expand Down

0 comments on commit 8e354bf

Please sign in to comment.