Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/indent indent: split bsearch comparison function



details:   https://anonhg.NetBSD.org/src/rev/9c00fd5f2c1c
branches:  trunk
changeset: 960133:9c00fd5f2c1c
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Mar 08 21:13:33 2021 +0000

description:
indent: split bsearch comparison function

It may have been a clever trick to use the same memory layout for struct
templ and a string pointer, but it's not worth the extra comment and
difficulty in understanding the code.

No functional change.

diffstat:

 usr.bin/indent/lexi.c |  27 +++++++++++++++------------
 1 files changed, 15 insertions(+), 12 deletions(-)

diffs (71 lines):

diff -r e3cde05c5fa3 -r 9c00fd5f2c1c usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Mon Mar 08 20:55:34 2021 +0000
+++ b/usr.bin/indent/lexi.c     Mon Mar 08 21:13:33 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.26 2021/03/08 20:20:11 rillig Exp $ */
+/*     $NetBSD: lexi.c,v 1.27 2021/03/08 21:13:33 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.26 2021/03/08 20:20:11 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.27 2021/03/08 21:13:33 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -75,9 +75,9 @@
 
 /*
  * This table has to be sorted alphabetically, because it'll be used in binary
- * search. For the same reason, string must be the first thing in struct templ.
+ * search.
  */
-struct templ specials[] =
+const struct templ specials[] =
 {
     {"_Bool", rw_type},
     {"_Complex", rw_type},
@@ -187,9 +187,15 @@
 }
 
 static int
-strcmp_type(const void *e1, const void *e2)
+compare_templ_array(const void *key, const void *elem)
 {
-    return strcmp(e1, *(const char *const *)e2);
+    return strcmp(key, ((const struct templ *)elem)->rwd);
+}
+
+static int
+compare_string_array(const void *key, const void *elem)
+{
+    return strcmp(key, *((const char *const *)elem));
 }
 
 #ifdef debug
@@ -341,11 +347,8 @@
         */
        state->last_u_d = (state->last_token == structure);
 
-       p = bsearch(s_token,
-           specials,
-           sizeof(specials) / sizeof(specials[0]),
-           sizeof(specials[0]),
-           strcmp_type);
+       p = bsearch(s_token, specials, sizeof specials / sizeof specials[0],
+           sizeof specials[0], compare_templ_array);
        if (p == NULL) {        /* not a special keyword... */
            char *u;
 
@@ -353,7 +356,7 @@
            if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
                strcmp(u, "_t") == 0) || (typename_top >= 0 &&
                  bsearch(s_token, typenames, typename_top + 1,
-                   sizeof(typenames[0]), strcmp_type))) {
+                   sizeof typenames[0], compare_string_array))) {
                state->keyword = rw_type;
                state->last_u_d = true;
                goto found_typename;



Home | Main Index | Thread Index | Old Index