Module Name:    src
Committed By:   rillig
Date:           Sat Oct  9 11:13:25 UTC 2021

Modified Files:
        src/usr.bin/indent: indent.c io.c

Log Message:
indent: condense code for calculating indentations

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.136 -r1.137 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.91 -r1.92 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.136 src/usr.bin/indent/indent.c:1.137
--- src/usr.bin/indent/indent.c:1.136	Sat Oct  9 11:00:27 2021
+++ src/usr.bin/indent/indent.c	Sat Oct  9 11:13:25 2021
@@ -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 @@ static char sccsid[] = "@(#)indent.c	5.1
 
 #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 @@ main_prepare_parsing(void)
 
     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 @@ main_prepare_parsing(void)
 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 @@ indent_declaration(int cur_decl_ind, boo
      * 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++ = ' ';

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.91 src/usr.bin/indent/io.c:1.92
--- src/usr.bin/indent/io.c:1.91	Sat Oct  9 11:00:27 2021
+++ src/usr.bin/indent/io.c	Sat Oct  9 11:13:25 2021
@@ -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 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 
 #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 @@ static void
 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 @@ dump_line_comment(int ind)
 	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++;

Reply via email to