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): merge duplicate code for the :H :T :R ...



details:   https://anonhg.NetBSD.org/src/rev/6262989a3461
branches:  trunk
changeset: 936908:6262989a3461
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Aug 08 19:13:39 2020 +0000

description:
make(1): merge duplicate code for the :H :T :R :E modifiers

By setting modifyWord first, the rest of the code becomes exactly the
same and is merged by the compiler.

diffstat:

 usr.bin/make/var.c |  53 ++++++++++++++++++++++-------------------------------
 1 files changed, 22 insertions(+), 31 deletions(-)

diffs (93 lines):

diff -r 635f378f24c8 -r 6262989a3461 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sat Aug 08 19:08:48 2020 +0000
+++ b/usr.bin/make/var.c        Sat Aug 08 19:13:39 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.433 2020/08/08 18:54:04 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.434 2020/08/08 19:13:39 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.433 2020/08/08 18:54:04 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.434 2020/08/08 19:13:39 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.433 2020/08/08 18:54:04 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.434 2020/08/08 19:13:39 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2898,6 +2898,21 @@
     return AMR_OK;
 }
 
+/* Apply the given function to each word of the variable value. */
+static ApplyModifierResult
+ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st,
+                      ModifyWordsCallback modifyWord)
+{
+    char delim = (*pp)[1];
+    if (delim != st->endc && delim != ':')
+       return AMR_UNKNOWN;
+
+    st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord,
+                           st->val, modifyWord, NULL);
+    (*pp)++;
+    return AMR_OK;
+}
+
 #ifdef SYSVVARSUB
 /* :from=to */
 static ApplyModifierResult
@@ -3122,40 +3137,16 @@
                res = AMR_UNKNOWN;
            break;
        case 'T':
-           if (p[1] == st.endc || p[1] == ':') {
-               st.newVal = ModifyWords(st.ctxt, st.sep, st.oneBigWord,
-                                       st.val, ModifyWord_Tail, NULL);
-               p++;
-               res = AMR_OK;
-           } else
-               res = AMR_UNKNOWN;
+           res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Tail);
            break;
        case 'H':
-           if (p[1] == st.endc || p[1] == ':') {
-               st.newVal = ModifyWords(st.ctxt, st.sep, st.oneBigWord,
-                                       st.val, ModifyWord_Head, NULL);
-               p++;
-               res = AMR_OK;
-           } else
-               res = AMR_UNKNOWN;
+           res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Head);
            break;
        case 'E':
-           if (p[1] == st.endc || p[1] == ':') {
-               st.newVal = ModifyWords(st.ctxt, st.sep, st.oneBigWord,
-                                       st.val, ModifyWord_Suffix, NULL);
-               p++;
-               res = AMR_OK;
-           } else
-               res = AMR_UNKNOWN;
+           res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Suffix);
            break;
        case 'R':
-           if (p[1] == st.endc || p[1] == ':') {
-               st.newVal = ModifyWords(st.ctxt, st.sep, st.oneBigWord,
-                                       st.val, ModifyWord_Root, NULL);
-               p++;
-               res = AMR_OK;
-           } else
-               res = AMR_UNKNOWN;
+           res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Root);
            break;
        case 'r':
            res = ApplyModifier_Range(&p, &st);



Home | Main Index | Thread Index | Old Index