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: reduce code for scanning tokens



details:   https://anonhg.NetBSD.org/src/rev/62f326fb637b
branches:  trunk
changeset: 375308:62f326fb637b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 14 14:14:07 2023 +0000

description:
indent: reduce code for scanning tokens

The input line is guaranteed to end with '\n', so there's no need to
carry another pointer around.

No functional change.

diffstat:

 usr.bin/indent/indent.h     |   3 +--
 usr.bin/indent/io.c         |  10 ++--------
 usr.bin/indent/lexi.c       |  24 +++++++++++-------------
 usr.bin/indent/pr_comment.c |  24 +++++++++---------------
 4 files changed, 23 insertions(+), 38 deletions(-)

diffs (167 lines):

diff -r 8199fed9ef3f -r 62f326fb637b usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sun May 14 12:45:56 2023 +0000
+++ b/usr.bin/indent/indent.h   Sun May 14 14:14:07 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.130 2023/05/14 12:12:02 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.131 2023/05/14 14:14:07 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -391,7 +391,6 @@ void inp_init(void);
 
 const char *inp_p(void);
 const char *inp_line_start(void);
-const char *inp_line_end(void);
 char inp_peek(void);
 char inp_lookahead(size_t);
 void inp_skip(void);
diff -r 8199fed9ef3f -r 62f326fb637b usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sun May 14 12:45:56 2023 +0000
+++ b/usr.bin/indent/io.c       Sun May 14 14:14:07 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.163 2023/05/14 12:12:02 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.164 2023/05/14 14:14:07 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.163 2023/05/14 12:12:02 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.164 2023/05/14 14:14:07 rillig Exp $");
 
 #include <assert.h>
 #include <stdio.h>
@@ -77,12 +77,6 @@ inp_line_start(void)
     return inp.mem;
 }
 
-const char *
-inp_line_end(void)
-{
-    return inp.e;
-}
-
 char
 inp_peek(void)
 {
diff -r 8199fed9ef3f -r 62f326fb637b usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sun May 14 12:45:56 2023 +0000
+++ b/usr.bin/indent/lexi.c     Sun May 14 14:14:07 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.182 2023/05/14 12:12:02 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.183 2023/05/14 14:14:07 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.182 2023/05/14 12:12:02 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.183 2023/05/14 14:14:07 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -331,16 +331,16 @@ static bool
 probably_looking_at_definition(void)
 {
     int paren_level = 0;
-    for (const char *p = inp_p(), *e = inp_line_end(); p < e; p++) {
+    for (const char *p = inp_p(); *p != '\n'; p++) {
        if (*p == '(')
            paren_level++;
        if (*p == ')' && --paren_level == 0) {
            p++;
 
-           while (p < e && (ch_isspace(*p) || is_identifier_part(*p)))
+           while (*p != '\n' && (ch_isspace(*p) || is_identifier_part(*p)))
                p++;            /* '__dead' or '__unused' */
 
-           if (p == e)         /* func(...) */
+           if (*p == '\n')     /* func(...) */
                break;
            if (*p == ';')      /* func(...); */
                return false;
@@ -472,21 +472,19 @@ lex_asterisk_unary(void)
     }
 
     if (ps.in_decl) {
-       const char *tp = inp_p(), *e = inp_line_end();
-
-       while (tp < e) {
+       for (const char *tp = inp_p(); *tp != '\n';) {
            if (ch_isspace(*tp))
                tp++;
            else if (is_identifier_start(*tp)) {
                tp++;
-               while (tp < e && is_identifier_part(*tp))
+               while (is_identifier_part(*tp))
                    tp++;
-           } else
+           } else {
+               if (*tp == '(')
+                   ps.is_function_definition = true;
                break;
+           }
        }
-
-       if (tp < e && *tp == '(')
-           ps.is_function_definition = true;
     }
 }
 
diff -r 8199fed9ef3f -r 62f326fb637b usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sun May 14 12:45:56 2023 +0000
+++ b/usr.bin/indent/pr_comment.c       Sun May 14 14:14:07 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.133 2023/05/14 12:12:02 rillig Exp $  */
+/*     $NetBSD: pr_comment.c,v 1.134 2023/05/14 14:14:07 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,10 +38,8 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.133 2023/05/14 12:12:02 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.134 2023/05/14 14:14:07 rillig Exp $");
 
-#include <assert.h>
-#include <stdio.h>
 #include <string.h>
 
 #include "indent.h"
@@ -74,15 +72,12 @@ com_terminate(void)
 static bool
 fits_in_one_line(int com_ind, int max_line_length)
 {
-    for (const char *p = inp_p(); *p != '\n'; p++) {
-       assert(*p != '\0');
-       assert(inp_line_end() - p >= 2);
-       if (!(p[0] == '*' && p[1] == '/'))
-           continue;
-
-       int len = ind_add(com_ind + 3, inp_p(), p);
-       len += ch_isblank(p[-1]) ? 2 : 3;
-       return len <= max_line_length;
+    for (const char *start = inp_p(), *p = start; *p != '\n'; p++) {
+       if (p[0] == '*' && p[1] == '/') {
+           int len = ind_add(com_ind + 3, start, p);
+           len += ch_isblank(p[-1]) ? 2 : 3;
+           return len <= max_line_length;
+       }
     }
     return false;
 }
@@ -147,8 +142,7 @@ analyze_comment(bool *p_may_wrap, bool *
         * Find out how much indentation there was originally, because that
         * much will have to be ignored by output_complete_line.
         */
-       const char *start = inp_line_start();
-       ps.n_comment_delta = -ind_add(0, start, inp_p() - 2);
+       ps.n_comment_delta = -ind_add(0, inp_line_start(), inp_p() - 2);
     } else {
        ps.n_comment_delta = 0;
        while (ch_isblank(inp_peek()))



Home | Main Index | Thread Index | Old Index