From patchwork Fri Nov 2 16:58:35 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [4/6] utlities: kernelscan: make some regex optimisations From: Colin King X-Patchwork-Id: 196599 Message-Id: <1351875517-19128-5-git-send-email-colin.king@canonical.com> To: fwts-devel@lists.ubuntu.com Date: Fri, 2 Nov 2012 16:58:35 +0000 From: Colin Ian King We only should compile a pattern if it is a regular expression. Also, make the klog_find a little faster. Signed-off-by: Colin Ian King Acked-by: Keng-Yu Lin Acked-by: Alex Hung --- src/utilities/kernelscan.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/utilities/kernelscan.c b/src/utilities/kernelscan.c index 9133ce9..2e0d606 100644 --- a/src/utilities/kernelscan.c +++ b/src/utilities/kernelscan.c @@ -293,14 +293,17 @@ static klog_pattern *klog_load(const char *table) exit(EXIT_FAILURE); } - if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) { - fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error); - patterns[i].re = NULL; - } else { - patterns[i].extra = pcre_study(patterns[i].re, 0, &error); - if (error != NULL) { - fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error); + /* Pre-compile regular expressions to make things run a bit faster */ + if (patterns[i].cm == COMPARE_REGEX) { + if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) { + fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error); patterns[i].re = NULL; + } else { + patterns[i].extra = pcre_study(patterns[i].re, 0, &error); + if (error != NULL) { + fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error); + patterns[i].re = NULL; + } } } } @@ -323,8 +326,7 @@ static bool klog_find(char *str, klog_pattern *patterns) if (strstr(str, patterns[i].pattern)) { return true; } - } - if (patterns[i].cm == COMPARE_REGEX) { + } else if (patterns[i].cm == COMPARE_REGEX) { int vector[1]; if (pcre_exec(patterns[i].re, patterns[i].extra, str, strlen(str), 0, 0, vector, 1) == 0) { return true;