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: replace pad_output with output_indent



details:   https://anonhg.NetBSD.org/src/rev/9426ee4a831a
branches:  trunk
changeset: 960250:9426ee4a831a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 13 00:26:56 2021 +0000

description:
indent: replace pad_output with output_indent

Calculating the indentation is simpler than calculating the column,
since that saves the constant addition and subtraction of the 1.

No functional change.

diffstat:

 usr.bin/indent/indent.c     |   6 +-
 usr.bin/indent/indent.h     |   8 ++--
 usr.bin/indent/io.c         |  74 ++++++++++++++++++--------------------------
 usr.bin/indent/pr_comment.c |   8 ++--
 4 files changed, 41 insertions(+), 55 deletions(-)

diffs (239 lines):

diff -r 2b33c3489dfa -r 9426ee4a831a usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Mar 13 00:03:29 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Mar 13 00:26:56 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.46 2021/03/12 23:16:00 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.47 2021/03/13 00:26:56 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.46 2021/03/12 23:16:00 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.47 2021/03/13 00:26:56 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -1150,7 +1150,7 @@
                if (ps.block_init_level <= 0)
                    ps.block_init = 0;
                if (break_comma && (!opt.leave_comma ||
-                   count_spaces_until(compute_code_indent(), s_code, e_code) >
+                   count_spaces_until(compute_code_column(), s_code, e_code) >
                    opt.max_col - opt.tabsize))
                    force_nl = true;
            }
diff -r 2b33c3489dfa -r 9426ee4a831a usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat Mar 13 00:03:29 2021 +0000
+++ b/usr.bin/indent/indent.h   Sat Mar 13 00:26:56 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.7 2021/03/12 23:27:41 rillig Exp $        */
+/*     $NetBSD: indent.h,v 1.8 2021/03/13 00:26:56 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -30,7 +30,7 @@
 
 #if 0
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.h,v 1.7 2021/03/12 23:27:41 rillig Exp $");
+__RCSID("$NetBSD: indent.h,v 1.8 2021/03/13 00:26:56 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $");
 #endif
@@ -45,8 +45,8 @@
 
 void   add_typename(const char *);
 void   alloc_typenames(void);
-int    compute_code_indent(void);
-int    compute_label_indent(void);
+int    compute_code_column(void);
+int    compute_label_column(void);
 int    count_spaces(int, const char *);
 int    count_spaces_until(int, const char *, const char *);
 void   init_constant_tt(void);
diff -r 2b33c3489dfa -r 9426ee4a831a usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat Mar 13 00:03:29 2021 +0000
+++ b/usr.bin/indent/io.c       Sat Mar 13 00:26:56 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.33 2021/03/13 00:03:29 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.34 2021/03/13 00:26:56 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.33 2021/03/13 00:03:29 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.34 2021/03/13 00:26:56 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -63,7 +63,6 @@
 
 int         comment_open;
 static int  paren_indent;
-static int pad_output(int current, int target);
 
 static void
 output_char(char ch)
@@ -89,6 +88,28 @@
     fprintf(output, "%d", i);
 }
 
+static int
+output_indent(int old_ind, int new_ind)
+{
+    int ind = old_ind;
+
+    if (opt.use_tabs) {
+       int tabsize = opt.tabsize;
+       int n = new_ind / tabsize - ind / tabsize;
+       if (n > 0)
+           ind -= ind % tabsize;
+       for (int i = 0; i < n; i++) {
+           output_char('\t');
+           ind += tabsize;
+       }
+    }
+
+    for (; ind < new_ind; ind++)
+        output_char(' ');
+
+    return ind;
+}
+
 /*
  * dump_line is the routine that actually effects the printing of the new
  * source. It prints the label section, followed by the code section with
@@ -144,7 +165,7 @@
            while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
                e_lab--;
            *e_lab = '\0';
-           cur_col = pad_output(1, compute_label_indent());
+           cur_col = 1 + output_indent(0, compute_label_column() - 1);
            if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
                                    || strncmp(s_lab, "#endif", 6) == 0)) {
                char *s = s_lab;
@@ -179,7 +200,7 @@
                comment_open = 0;
                output_string(".*/\n");
            }
-           target_col = compute_code_indent();
+           target_col = compute_code_column();
            {
                int i;
 
@@ -187,7 +208,7 @@
                    if (ps.paren_indents[i] >= 0)
                        ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
            }
-           cur_col = pad_output(cur_col, target_col);
+           cur_col = 1 + output_indent(cur_col - 1, target_col - 1);
            for (p = s_code; p < e_code; p++)
                if (*p == (char) 0200)
                    output_int(target_col * 7);
@@ -219,7 +240,7 @@
            }
            while (e_com > com_st && isspace((unsigned char)e_com[-1]))
                e_com--;
-           (void)pad_output(cur_col, target);
+           (void)output_indent(cur_col - 1, target - 1);
            output_range(com_st, e_com);
            ps.comment_delta = ps.n_comment_delta;
            ++ps.com_lines;     /* count lines with comments */
@@ -262,7 +283,7 @@
 }
 
 int
-compute_code_indent(void)
+compute_code_column(void)
 {
     int target_col = opt.ind_size * ps.ind_level + 1;
 
@@ -290,7 +311,7 @@
 }
 
 int
-compute_label_indent(void)
+compute_label_column(void)
 {
     return
        ps.pcase ? (int) (case_ind * opt.ind_size) + 1
@@ -398,41 +419,6 @@
  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
  *
  * All rights reserved
- *
- * Writes tabs and spaces to move the current column up to the desired
- * position.
- */
-static int
-pad_output(int current, int target)
-                               /* writes tabs and blanks (if necessary) to
-                                * get the current output position up to the
-                                * target column */
-    /* current: the current column value */
-    /* target: position we want it at */
-{
-    int curr;                  /* internal column pointer */
-
-    if (current >= target)
-       return current;         /* line is already long enough */
-    curr = current;
-    if (opt.use_tabs) {
-       int tcur;
-
-       while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) {
-           output_char('\t');
-           curr = tcur;
-       }
-    }
-    while (curr++ < target)
-       output_char(' ');       /* pad with final blanks */
-
-    return target;
-}
-
-/*
- * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
- *
- * All rights reserved
  */
 int
 count_spaces_until(int col, const char *buffer, const char *end)
diff -r 2b33c3489dfa -r 9426ee4a831a usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sat Mar 13 00:03:29 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sat Mar 13 00:26:56 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.20 2021/03/12 23:16:00 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.21 2021/03/13 00:26:56 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.20 2021/03/12 23:16:00 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.21 2021/03/13 00:26:56 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -162,11 +162,11 @@
            int target_col;
            break_delim = false;
            if (s_code != e_code)
-               target_col = count_spaces(compute_code_indent(), s_code);
+               target_col = count_spaces(compute_code_column(), s_code);
            else {
                target_col = 1;
                if (s_lab != e_lab)
-                   target_col = count_spaces(compute_label_indent(), s_lab);
+                   target_col = count_spaces(compute_label_column(), s_lab);
            }
            ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
            if (ps.com_col <= target_col)



Home | Main Index | Thread Index | Old Index