Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): refactor Str_Match



details:   https://anonhg.NetBSD.org/src/rev/c24391db2b78
branches:  trunk
changeset: 973509:c24391db2b78
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jul 03 07:40:13 2020 +0000

description:
make(1): refactor Str_Match

- use shorter variable names to avoid line breaks
- eliminate c2 variable
- change return type to Boolean

diffstat:

 usr.bin/make/nonints.h |    4 +-
 usr.bin/make/str.c     |  156 ++++++++++++++++++++++--------------------------
 2 files changed, 74 insertions(+), 86 deletions(-)

diffs (236 lines):

diff -r 20f03273075a -r c24391db2b78 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Fri Jul 03 07:03:14 2020 +0000
+++ b/usr.bin/make/nonints.h    Fri Jul 03 07:40:13 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.77 2020/07/02 15:47:38 rillig Exp $      */
+/*     $NetBSD: nonints.h,v 1.78 2020/07/03 07:40:13 rillig Exp $      */
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -137,7 +137,7 @@
 char *str_concat(const char *, const char *, int);
 char **brk_string(const char *, int *, Boolean, char **);
 char *Str_FindSubstring(const char *, const char *);
-int Str_Match(const char *, const char *);
+Boolean Str_Match(const char *, const char *);
 char *Str_SYSVMatch(const char *, const char *, size_t *, Boolean *);
 void Str_SYSVSubst(Buffer *, char *, char *, size_t, Boolean);
 
diff -r 20f03273075a -r c24391db2b78 usr.bin/make/str.c
--- a/usr.bin/make/str.c        Fri Jul 03 07:03:14 2020 +0000
+++ b/usr.bin/make/str.c        Fri Jul 03 07:40:13 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: str.c,v 1.50 2020/07/03 06:30:02 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.50 2020/07/03 06:30:02 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.50 2020/07/03 06:30:02 rillig Exp $");
+__RCSID("$NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $");
 #endif
 #endif                         /* not lint */
 #endif
@@ -320,122 +320,110 @@
 }
 
 /*
- * Str_Match -- Test if a string matches a pattern.
+ * 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 glob(3)).
+ *     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 == 0;
-               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.
-                */
-               if (*pattern == '*') {
-                       pattern++;
-                       while (*pattern == '*')
-                               pattern++;
-                       if (*pattern == 0)
-                               return 1;
-                       while (*string != 0) {
-                               if (Str_Match(string, pattern))
-                                       return 1;
-                               ++string;
-                       }
-                       return 0;
-               }
-               /*
-                * Check for a "?" as the next pattern character.  It matches
-                * any single character.
-                */
-               if (*pattern == '?')
-                       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 any substring.  We handle this
+                * by calling ourselves for each suffix of the string.
                 */
-               if (*pattern == '[') {
-                       int nomatch;
+               if (*pat == '*') {
+                       pat++;
+                       while (*pat == '*')
+                               pat++;
+                       if (*pat == 0)
+                               return TRUE;
+                       while (*str != 0) {
+                               if (Str_Match(str, pat))
+                                       return TRUE;
+                               str++;
+                       }
+                       return FALSE;
+               }
+
+               /* A '?' in the pattern matches any single character. */
+               if (*pat == '?')
+                       goto thisCharOK;
 
-                       ++pattern;
-                       if (*pattern == '^') {
-                               ++pattern;
-                               nomatch = 1;
-                       } else
-                               nomatch = 0;
+               /*
+                * 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 (*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 --



Home | Main Index | Thread Index | Old Index