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): clean up SubstVarShort in .for loops



details:   https://anonhg.NetBSD.org/src/rev/418c3075406b
branches:  trunk
changeset: 979526:418c3075406b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Dec 31 13:37:33 2020 +0000

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

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

diffstat:

 usr.bin/make/for.c |  49 +++++++++++++++++++++----------------------------
 1 files changed, 21 insertions(+), 28 deletions(-)

diffs (84 lines):

diff -r 90c7ef1dfd43 -r 418c3075406b usr.bin/make/for.c
--- a/usr.bin/make/for.c        Thu Dec 31 13:23:43 2020 +0000
+++ b/usr.bin/make/for.c        Thu Dec 31 13:37:33 2020 +0000
@@ -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 @@
  * 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;
-       }
-
-       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, '}');
+       vars = Vector_Get(&f->vars, 0);
+       for (i = 0; i < f->vars.len; i++) {
+               const char *varname = vars[i].name;
+               if (varname[0] == ch && varname[1] == '\0')
+                       goto found;
+       }
+       return;
 
-               *inout_mark = ++p;
-               break;
-       }
-
-       *pp = p;
+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 @@
                        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;
        }



Home | Main Index | Thread Index | Old Index