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: condense code for calculating indenta...



details:   https://anonhg.NetBSD.org/src/rev/321e61f03e6b
branches:  trunk
changeset: 1024082:321e61f03e6b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Oct 09 11:13:25 2021 +0000

description:
indent: condense code for calculating indentations

No functional change.

diffstat:

 usr.bin/indent/indent.c |  24 ++++++++----------------
 usr.bin/indent/io.c     |  27 ++++++++++++++-------------
 2 files changed, 22 insertions(+), 29 deletions(-)

diffs (140 lines):

diff -r 5bb8dd4955af -r 321e61f03e6b usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Oct 09 11:00:27 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Oct 09 11:13:25 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.136 2021/10/09 11:00:27 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.137 2021/10/09 11:13:25 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.136 2021/10/09 11:00:27 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.137 2021/10/09 11:13:25 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -572,18 +572,16 @@
 
     parse(semicolon);
 
-    char *p = inp.s;
     int ind = 0;
-
-    for (;;) {
+    for (const char *p = inp.s;; p++) {
        if (*p == ' ')
            ind++;
        else if (*p == '\t')
            ind = next_tab(ind);
        else
            break;
-       p++;
     }
+
     if (ind >= opt.indent_size)
        ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
 }
@@ -591,7 +589,7 @@
 static void
 indent_declaration(int cur_decl_ind, bool tabs_to_var)
 {
-    int pos = (int)buf_len(&code);
+    int ind = (int)buf_len(&code);
     char *orig_code_e = code.e;
 
     /*
@@ -599,23 +597,17 @@
      * tabsize
      */
     if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) {
-       pos += (ps.ind_level * opt.indent_size) % opt.tabsize;
+       ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
        cur_decl_ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
     }
 
     if (tabs_to_var) {
-       int tpos;
-
-       while ((tpos = next_tab(pos)) <= cur_decl_ind) {
+       for (int next; (next = next_tab(ind)) <= cur_decl_ind; ind = next)
            buf_add_char(&code, '\t');
-           pos = tpos;
-       }
     }
 
-    while (pos < cur_decl_ind) {
+    for (; ind < cur_decl_ind; ind++)
        buf_add_char(&code, ' ');
-       pos++;
-    }
 
     if (code.e == orig_code_e && ps.want_blank) {
        *code.e++ = ' ';
diff -r 5bb8dd4955af -r 321e61f03e6b usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat Oct 09 11:00:27 2021 +0000
+++ b/usr.bin/indent/io.c       Sat Oct 09 11:13:25 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.91 2021/10/09 11:00:27 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.92 2021/10/09 11:13:25 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.91 2021/10/09 11:00:27 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.92 2021/10/09 11:13:25 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -177,22 +177,23 @@
 dump_line_comment(int ind)
 {
     int target_ind = ps.com_ind;
-    char *com_st = com.s;
+    const char *p = com.s;
 
     target_ind += ps.comment_delta;
 
     /* consider original indentation in case this is a box comment */
-    while (*com_st == '\t')
-       com_st++, target_ind += opt.tabsize;
+    for (; *p == '\t'; p++)
+       target_ind += opt.tabsize;
 
-    while (target_ind < 0) {
-       if (*com_st == ' ')
-           target_ind++, com_st++;
-       else if (*com_st == '\t') {
+    for (; target_ind < 0; p++) {
+       if (*p == ' ')
+           target_ind++;
+       else if (*p == '\t')
            target_ind = next_tab(target_ind);
-           com_st++;
-       } else
+       else {
            target_ind = 0;
+           break;
+       }
     }
 
     /* if comment can't fit on this line, put it on next line */
@@ -202,11 +203,11 @@
        ps.stats.lines++;
     }
 
-    while (com.e > com_st && isspace((unsigned char)com.e[-1]))
+    while (com.e > p && isspace((unsigned char)com.e[-1]))
        com.e--;
 
     (void)output_indent(ind, target_ind);
-    output_range(com_st, com.e);
+    output_range(p, com.e);
 
     ps.comment_delta = ps.n_comment_delta;
     ps.stats.comment_lines++;



Home | Main Index | Thread Index | Old Index