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: inline indentation_after, shorten fun...



details:   https://anonhg.NetBSD.org/src/rev/d127920acb08
branches:  trunk
changeset: 991024:d127920acb08
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Nov 03 21:47:35 2021 +0000

description:
indent: inline indentation_after, shorten function name to ind_add

There were only few calls to indentation_after, so inlining it spares
the need to look at yet another function definition. Another effect is
that code.s and code.e appear in the code as a pair now, instead of a
single code.s, making the scope of the function call obvious.

In ind_add, there is no need to check for '\0' anymore since none of the
buffers can ever contain a null character, these are filtered out by
inbuf_read_line.

No functional change.

diffstat:

 usr.bin/indent/indent.c     |   9 ++++-----
 usr.bin/indent/indent.h     |   5 ++---
 usr.bin/indent/io.c         |  26 ++++++++++----------------
 usr.bin/indent/pr_comment.c |  14 +++++++-------
 4 files changed, 23 insertions(+), 31 deletions(-)

diffs (187 lines):

diff -r 9cadbb3bc00d -r d127920acb08 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Wed Nov 03 16:18:09 2021 +0000
+++ b/usr.bin/indent/indent.c   Wed Nov 03 21:47:35 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.204 2021/11/01 23:44:08 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.205 2021/11/03 21:47:35 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.204 2021/11/01 23:44:08 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.205 2021/11/03 21:47:35 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -772,8 +772,7 @@
     ps.want_blank = false;
     *code.e++ = token.s[0];
 
-    ps.paren_indents[ps.p_l_follow - 1] =
-       (short)indentation_after_range(0, code.s, code.e);
+    ps.paren_indents[ps.p_l_follow - 1] = (short)ind_add(0, code.s, code.e);
     debug_println("paren_indents[%d] is now %d",
        ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
 
@@ -1217,7 +1216,7 @@
        int varname_len = 8;    /* rough estimate for the length of a typical
                                 * variable name */
        if (break_comma && (opt.break_after_comma ||
-               indentation_after_range(compute_code_indent(), code.s, code.e)
+               ind_add(compute_code_indent(), code.s, code.e)
                >= opt.max_line_length - varname_len))
            *force_nl = true;
     }
diff -r 9cadbb3bc00d -r d127920acb08 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Wed Nov 03 16:18:09 2021 +0000
+++ b/usr.bin/indent/indent.h   Wed Nov 03 21:47:35 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.75 2021/11/01 23:44:08 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.76 2021/11/03 21:47:35 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -364,8 +364,7 @@
 void register_typename(const char *);
 int compute_code_indent(void);
 int compute_label_indent(void);
-int indentation_after_range(int, const char *, const char *);
-int indentation_after(int, const char *);
+int ind_add(int, const char *, const char *);
 
 void inbuf_skip(void);
 char inbuf_next(void);
diff -r 9cadbb3bc00d -r d127920acb08 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Wed Nov 03 16:18:09 2021 +0000
+++ b/usr.bin/indent/io.c       Wed Nov 03 21:47:35 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.108 2021/10/30 11:49:38 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.109 2021/11/03 21:47:35 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.108 2021/10/30 11:49:38 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.109 2021/11/03 21:47:35 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -136,7 +136,7 @@
        }
     } else
        output_range(lab.s, lab.e);
-    ind = indentation_after(ind, lab.s);
+    ind = ind_add(ind, lab.s, lab.e);
 
     ps.is_case_label = false;
     return ind;
@@ -160,7 +160,7 @@
 
     ind = output_indent(ind, target_ind);
     output_range(code.s, code.e);
-    return indentation_after(ind, code.s);
+    return ind_add(ind, code.s, code.e);
 }
 
 static void
@@ -315,11 +315,11 @@
            target_ind = paren_indent - 1;
 
        } else {
-           int w;
-           int t = paren_indent;
+           int w;                      /* TODO: remove '+ 1' and '- 1' */
+           int t = paren_indent;       /* TODO: remove '+ 1' and '- 1' */
 
-           if ((w = 1 + indentation_after(t - 1, code.s) - opt.max_line_length) > 0
-               && 1 + indentation_after(target_ind, code.s) <= opt.max_line_length) {
+           if ((w = 1 + ind_add(t - 1, code.s, code.e) - opt.max_line_length) > 0
+               && 1 + ind_add(target_ind, code.s, code.e) <= opt.max_line_length) {
                t -= w + 1;
                if (t > target_ind + 1)
                    target_ind = t - 1;
@@ -460,9 +460,9 @@
 }
 
 int
-indentation_after_range(int ind, const char *start, const char *end)
+ind_add(int ind, const char *start, const char *end)
 {
-    for (const char *p = start; *p != '\0' && p != end; ++p) {
+    for (const char *p = start; p != end; ++p) {
        if (*p == '\n' || *p == '\f')
            ind = 0;
        else if (*p == '\t')
@@ -474,9 +474,3 @@
     }
     return ind;
 }
-
-int
-indentation_after(int ind, const char *s)
-{
-    return indentation_after_range(ind, s, NULL);
-}
diff -r 9cadbb3bc00d -r d127920acb08 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Wed Nov 03 16:18:09 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Wed Nov 03 21:47:35 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.93 2021/10/30 22:36:07 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.94 2021/11/03 21:47:35 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.93 2021/10/30 22:36:07 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.94 2021/11/03 21:47:35 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -92,7 +92,7 @@
        if (!(p[0] == '*' && p[1] == '/'))
            continue;
 
-       int len = indentation_after_range(ps.com_ind + 3, inp.s, p);
+       int len = ind_add(ps.com_ind + 3, inp.s, p);
        len += ch_isblank(p[-1]) ? 2 : 3;
        return len <= max_line_length;
     }
@@ -158,9 +158,9 @@
 
            int target_ind;
            if (code.s != code.e)
-               target_ind = indentation_after(compute_code_indent(), code.s);
+               target_ind = ind_add(compute_code_indent(), code.s, code.e);
            else if (lab.s != lab.e)
-               target_ind = indentation_after(compute_label_indent(), lab.s);
+               target_ind = ind_add(compute_label_indent(), lab.s, lab.e);
            else
                target_ind = 0;
 
@@ -192,7 +192,7 @@
         */
        start = inp.s >= sc_buf && inp.s < sc_buf + sc_size ?
            sc_buf : inp.buf;
-       ps.n_comment_delta = -indentation_after_range(0, start, inp.s - 2);
+       ps.n_comment_delta = -ind_add(0, start, inp.s - 2);
     } else {
        ps.n_comment_delta = 0;
        while (ch_isblank(*inp.s))
@@ -316,7 +316,7 @@
 
        default:                /* we have a random char */
            ;
-           int now_len = indentation_after_range(ps.com_ind, com.s, com.e);
+           int now_len = ind_add(ps.com_ind, com.s, com.e);
            for (;;) {
                char ch = inbuf_next();
                if (ch_isblank(ch))



Home | Main Index | Thread Index | Old Index