diff options
Diffstat (limited to 'str.c')
-rw-r--r-- | str.c | 157 |
1 files changed, 72 insertions, 85 deletions
@@ -1,4 +1,4 @@ -/* $NetBSD: str.c,v 1.48 2020/06/15 14:46:28 rillig Exp $ */ +/* $NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $ */ /*- * Copyright (c) 1988, 1989, 1990, 1993 @@ -69,14 +69,14 @@ */ #ifndef MAKE_NATIVE -static char rcsid[] = "$NetBSD: str.c,v 1.48 2020/06/15 14:46:28 rillig Exp $"; +static char rcsid[] = "$NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $"; #else #include <sys/cdefs.h> #ifndef lint #if 0 static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90"; #else -__RCSID("$NetBSD: str.c,v 1.48 2020/06/15 14:46:28 rillig Exp $"); +__RCSID("$NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $"); #endif #endif /* not lint */ #endif @@ -320,123 +320,110 @@ Str_FindSubstring(const char *string, const char *substring) } /* - * Str_Match -- - * - * See if a particular string matches a particular pattern. - * - * Results: Non-zero is returned if string matches pattern, 0 otherwise. The - * matching operation permits the following special characters in the - * pattern: *?\[] (see the man page for details on what these mean). + * Str_Match -- Test if a string matches a pattern like "*.[ch]". * * XXX this function does not detect or report malformed patterns. * + * Results: + * Non-zero is returned if string matches the pattern, 0 otherwise. The + * matching operation permits the following special characters in the + * pattern: *?\[] (as in fnmatch(3)). + * * Side effects: None. */ -int -Str_Match(const char *string, const char *pattern) +Boolean +Str_Match(const char *str, const char *pat) { - char c2; - for (;;) { /* * See if we're at the end of both the pattern and the * string. If, we succeeded. If we're at the end of the * pattern but not at the end of the string, we failed. */ - if (*pattern == 0) - return !*string; - if (*string == 0 && *pattern != '*') - return 0; + if (*pat == 0) + return *str == 0; + if (*str == 0 && *pat != '*') + return FALSE; + /* - * Check for a "*" as the next pattern character. It matches - * any substring. We handle this by calling ourselves - * recursively for each postfix of string, until either we - * match or we reach the end of the string. + * A '*' in the pattern matches any substring. We handle this + * by calling ourselves for each suffix of the string. */ - if (*pattern == '*') { - pattern++; - while (*pattern == '*') - pattern++; - if (*pattern == 0) - return 1; - while (*string != 0) { - if (Str_Match(string, pattern)) - return 1; - ++string; + if (*pat == '*') { + pat++; + while (*pat == '*') + pat++; + if (*pat == 0) + return TRUE; + while (*str != 0) { + if (Str_Match(str, pat)) + return TRUE; + str++; } - return 0; + return FALSE; } - /* - * Check for a "?" as the next pattern character. It matches - * any single character. - */ - if (*pattern == '?') + + /* A '?' in the pattern matches any single character. */ + if (*pat == '?') goto thisCharOK; + /* - * Check for a "[" as the next pattern character. It is - * followed by a list of characters that are acceptable, or - * by a range (two characters separated by "-"). + * A '[' in the pattern matches a character from a list. + * The '[' is followed by the list of acceptable characters, + * or by ranges (two characters separated by '-'). In these + * character lists, the backslash is an ordinary character. */ - if (*pattern == '[') { - int nomatch; - - ++pattern; - if (*pattern == '^') { - ++pattern; - nomatch = 1; - } else - nomatch = 0; + if (*pat == '[') { + Boolean neg = pat[1] == '^'; + pat += 1 + neg; + for (;;) { - if ((*pattern == ']') || (*pattern == 0)) { - if (nomatch) + if (*pat == ']' || *pat == 0) { + if (neg) break; - return 0; + return FALSE; } - if (*pattern == *string) + if (*pat == *str) break; - if (pattern[1] == '-') { - c2 = pattern[2]; - if (c2 == 0) - return nomatch; - if ((*pattern <= *string) && - (c2 >= *string)) + if (pat[1] == '-') { + if (pat[2] == 0) + return neg; + if (*pat <= *str && pat[2] >= *str) break; - if ((*pattern >= *string) && - (c2 <= *string)) + if (*pat >= *str && pat[2] <= *str) break; - pattern += 2; + pat += 2; } - ++pattern; + pat++; } - if (nomatch && (*pattern != ']') && (*pattern != 0)) - return 0; - while ((*pattern != ']') && (*pattern != 0)) - ++pattern; - if (*pattern == 0) - --pattern; + if (neg && *pat != ']' && *pat != 0) + return FALSE; + while (*pat != ']' && *pat != 0) + pat++; + if (*pat == 0) + pat--; goto thisCharOK; } + /* - * If the next pattern character is a backslash, just strip it - * off so we do exact matching on the character that follows. + * A backslash in the pattern matches the character following + * it exactly. */ - if (*pattern == '\\') { - ++pattern; - if (*pattern == 0) - return 0; + if (*pat == '\\') { + pat++; + if (*pat == 0) + return FALSE; } - /* - * There's no special character. Just make sure that the - * next characters of each string match. - */ - if (*pattern != *string) - return 0; -thisCharOK: ++pattern; - ++string; + + if (*pat != *str) + return FALSE; + + thisCharOK: + pat++; + str++; } } - /*- *----------------------------------------------------------------------- * Str_SYSVMatch -- |