diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-04-25 19:33:05 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-04-25 19:33:05 -0400 |
commit | 0c72419d193ca13edc8302cd8c151bbe80a4716f (patch) | |
tree | 650e3d9994c06336930f959c62fc89055661ed35 | |
parent | 341c8545ca2e2b1af908af506d0e472d68327593 (diff) |
only compile the regex once
-rw-r--r-- | includes.c | 17 |
1 files changed, 8 insertions, 9 deletions
@@ -51,19 +51,18 @@ static struct majefile * find_inc(const struct majefile *file, const regex_t * r struct majefile *find_includes(const struct majefile *source) { - regex_t re; - int rc = regcomp(&re, - "#[[:space:]]*include[[:space:]]+.*\"(.*)\"", - REG_EXTENDED | REG_NEWLINE); - if (rc != 0) { - fprintf(stderr, "maje: regcomp() failed\n"); - abort(); + static regex_t re = { .re_nsub = (size_t)-1 }; + if (re.re_nsub == (size_t)-1) { + if (regcomp(&re, + "#[[:space:]]*include[[:space:]]+.*\"(.*)\"", + REG_EXTENDED | REG_NEWLINE) != 0) { + fprintf(stderr, "maje: regcomp(#include) failed\n"); + abort(); + } } struct majefile *ret = find_inc(source, &re); - regfree(&re); - if (ret != NULL) { while (ret->prev != NULL) { ret = ret->prev; |