From 0c72419d193ca13edc8302cd8c151bbe80a4716f Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 25 Apr 2022 19:33:05 -0400 Subject: only compile the regex once --- includes.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'includes.c') diff --git a/includes.c b/includes.c index 103c8fd..cbef99a 100644 --- a/includes.c +++ b/includes.c @@ -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; -- cgit v1.2.1