Module Name:    src
Committed By:   rillig
Date:           Fri Jul  3 07:40:13 UTC 2020

Modified Files:
        src/usr.bin/make: nonints.h str.c

Log Message:
make(1): refactor Str_Match

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


To generate a diff of this commit:
cvs rdiff -u -r1.77 -r1.78 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.50 -r1.51 src/usr.bin/make/str.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.77 src/usr.bin/make/nonints.h:1.78
--- src/usr.bin/make/nonints.h:1.77	Thu Jul  2 15:47:38 2020
+++ src/usr.bin/make/nonints.h	Fri Jul  3 07:40:13 2020
@@ -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 @@ Lst Parse_MainName(void);
 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);
 

Index: src/usr.bin/make/str.c
diff -u src/usr.bin/make/str.c:1.50 src/usr.bin/make/str.c:1.51
--- src/usr.bin/make/str.c:1.50	Fri Jul  3 06:30:02 2020
+++ src/usr.bin/make/str.c	Fri Jul  3 07:40:13 2020
@@ -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_FindSubstring(const char *string, co
 }
 
 /*
- * 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.
+		 * 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;
+		if (*pat == '[') {
+			Boolean neg = pat[1] == '^';
+			pat += 1 + neg;
 
-			++pattern;
-			if (*pattern == '^') {
-				++pattern;
-				nomatch = 1;
-			} else
-				nomatch = 0;
 			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 --

Reply via email to