From 354438e7586fb0678f4178426eba70a7afc2ae03 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Wed, 9 Feb 2022 17:16:41 +0100 Subject: [PATCH] match: Reformat match_pattern Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider --- src/match.c | 127 ++++++++++++++++++++++++++-------------------------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/src/match.c b/src/match.c index 1a60d732..45ab8189 100644 --- a/src/match.c +++ b/src/match.c @@ -51,74 +51,75 @@ */ static int match_pattern(const char *s, const char *pattern, size_t limit) { - bool had_asterisk = false; - if (s == NULL || pattern == NULL || limit <= 0) { - return 0; - } + bool had_asterisk = false; - for (;;) { - /* If at end of pattern, accept if also at end of string. */ - if (*pattern == '\0') { - return (*s == '\0'); - } - - while (*pattern == '*') { - /* Skip the asterisk. */ - had_asterisk = true; - pattern++; - } - - if (had_asterisk) { - /* If at end of pattern, accept immediately. */ - if (!*pattern) - return 1; - - /* If next character in pattern is known, optimize. */ - if (*pattern != '?') { - /* - * Look instances of the next character in - * pattern, and try to match starting from - * those. - */ - for (; *s; s++) - if (*s == *pattern && match_pattern(s + 1, pattern + 1, limit - 1)) { - return 1; - } - /* Failed. */ + if (s == NULL || pattern == NULL || limit <= 0) { return 0; - } - /* - * Move ahead one character at a time and try to - * match at each position. - */ - for (; *s; s++) { - if (match_pattern(s, pattern, limit - 1)) { - return 1; + } + + for (;;) { + /* If at end of pattern, accept if also at end of string. */ + if (*pattern == '\0') { + return (*s == '\0'); } - } - /* Failed. */ - return 0; - } - /* - * There must be at least one more character in the string. - * If we are at the end, fail. - */ - if (!*s) { - return 0; + + while (*pattern == '*') { + /* Skip all the asterisks. */ + had_asterisk = true; + pattern++; + } + + if (had_asterisk) { + /* If at end of pattern, accept immediately. */ + if (!*pattern) + return 1; + + /* If next character in pattern is known, optimize. */ + if (*pattern != '?') { + /* + * Look instances of the next character in + * pattern, and try to match starting from + * those. + */ + for (; *s; s++) + if (*s == *pattern && match_pattern(s + 1, pattern + 1, limit - 1)) { + return 1; + } + /* Failed. */ + return 0; + } + /* + * Move ahead one character at a time and try to + * match at each position. + */ + for (; *s; s++) { + if (match_pattern(s, pattern, limit - 1)) { + return 1; + } + } + /* Failed. */ + return 0; + } + /* + * There must be at least one more character in the string. + * If we are at the end, fail. + */ + if (!*s) { + return 0; + } + + /* Check if the next character of the string is acceptable. */ + if (*pattern != '?' && *pattern != *s) { + return 0; + } + + /* Move to the next character, both in string and in pattern. */ + s++; + pattern++; } - /* Check if the next character of the string is acceptable. */ - if (*pattern != '?' && *pattern != *s) { - return 0; - } - - /* Move to the next character, both in string and in pattern. */ - s++; - pattern++; - } - - /* NOTREACHED */ - return 0; + /* NOTREACHED */ + return 0; } /*