Module Name:    src
Committed By:   rillig
Date:           Thu Dec 31 13:37:33 UTC 2020

Modified Files:
        src/usr.bin/make: for.c

Log Message:
make(1): clean up SubstVarShort in .for loops

This function does not need to advance the parsing position, which
removes duplicate code.


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/usr.bin/make/for.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/for.c
diff -u src/usr.bin/make/for.c:1.129 src/usr.bin/make/for.c:1.130
--- src/usr.bin/make/for.c:1.129	Thu Dec 31 04:38:55 2020
+++ src/usr.bin/make/for.c	Thu Dec 31 13:37:33 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: for.c,v 1.129 2020/12/31 04:38:55 rillig Exp $	*/
+/*	$NetBSD: for.c,v 1.130 2020/12/31 13:37:33 rillig Exp $	*/
 
 /*
  * Copyright (c) 1992, The Regents of the University of California.
@@ -58,7 +58,7 @@
 #include "make.h"
 
 /*	"@(#)for.c	8.1 (Berkeley) 6/6/93"	*/
-MAKE_RCSID("$NetBSD: for.c,v 1.129 2020/12/31 04:38:55 rillig Exp $");
+MAKE_RCSID("$NetBSD: for.c,v 1.130 2020/12/31 13:37:33 rillig Exp $");
 
 static int forLevel = 0;	/* Nesting level */
 
@@ -393,37 +393,30 @@ SubstVarLong(For *f, const char **pp, co
  * variable expressions like $i with their ${:U...} expansion.
  */
 static void
-SubstVarShort(For *f, const char **pp, const char **inout_mark)
+SubstVarShort(For *f, const char *p, const char **inout_mark)
 {
-	const char *p = *pp;
 	const char ch = *p;
+	ForVar *vars;
 	size_t i;
 
-	/* Probably a single character name, ignore $$ and stupid ones. */
-	if (!f->short_var || strchr("}):$", ch) != NULL) {
-		p++;
-		*pp = p;
+	/* Skip $$ and stupid ones. */
+	if (!f->short_var || strchr("}):$", ch) != NULL)
 		return;
-	}
 
+	vars = Vector_Get(&f->vars, 0);
 	for (i = 0; i < f->vars.len; i++) {
-		ForVar *var = Vector_Get(&f->vars, i);
-		const char *varname = var->name;
-		if (varname[0] != ch || varname[1] != '\0')
-			continue;
-
-		/* Found a variable match. Replace with ${:U<value>} */
-		Buf_AddBytesBetween(&f->curBody, *inout_mark, p);
-		Buf_AddStr(&f->curBody, "{:U");
-		Buf_AddEscaped(&f->curBody,
-		    f->items.words[f->sub_next + i], '}');
-		Buf_AddByte(&f->curBody, '}');
-
-		*inout_mark = ++p;
-		break;
-	}
-
-	*pp = p;
+		const char *varname = vars[i].name;
+		if (varname[0] == ch && varname[1] == '\0')
+			goto found;
+	}
+	return;
+
+found:
+	/* Replace $<ch> with ${:U<value>} */
+	Buf_AddBytesBetween(&f->curBody, *inout_mark, p), *inout_mark = p + 1;
+	Buf_AddStr(&f->curBody, "{:U");
+	Buf_AddEscaped(&f->curBody, f->items.words[f->sub_next + i], '}');
+	Buf_AddByte(&f->curBody, '}');
 }
 
 /*
@@ -453,8 +446,8 @@ ForSubstBody(For *f)
 			p += 2;
 			SubstVarLong(f, &p, &mark, p[-1] == '{' ? '}' : ')');
 		} else if (p[1] != '\0') {
-			p++;
-			SubstVarShort(f, &p, &mark);
+			SubstVarShort(f, p + 1, &mark);
+			p += 2;
 		} else
 			break;
 	}

Reply via email to