Module Name:    src
Committed By:   rillig
Date:           Sat Mar 13 00:26:56 UTC 2021

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

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/indent/io.c
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/indent/pr_comment.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.46 src/usr.bin/indent/indent.c:1.47
--- src/usr.bin/indent/indent.c:1.46	Fri Mar 12 23:16:00 2021
+++ src/usr.bin/indent/indent.c	Sat Mar 13 00:26:56 2021
@@ -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 @@ static char sccsid[] = "@(#)indent.c	5.1
 #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 @@ main(int argc, char **argv)
 		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;
 	    }

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.7 src/usr.bin/indent/indent.h:1.8
--- src/usr.bin/indent/indent.h:1.7	Fri Mar 12 23:27:41 2021
+++ src/usr.bin/indent/indent.h	Sat Mar 13 00:26:56 2021
@@ -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 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 
 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);

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.33 src/usr.bin/indent/io.c:1.34
--- src/usr.bin/indent/io.c:1.33	Sat Mar 13 00:03:29 2021
+++ src/usr.bin/indent/io.c	Sat Mar 13 00:26:56 2021
@@ -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 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 #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 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 
 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 @@ output_int(int i)
     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 @@ dump_line(void)
 	    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 @@ dump_line(void)
 		comment_open = 0;
 		output_string(".*/\n");
 	    }
-	    target_col = compute_code_indent();
+	    target_col = compute_code_column();
 	    {
 		int i;
 
@@ -187,7 +208,7 @@ dump_line(void)
 		    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 @@ dump_line(void)
 	    }
 	    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 @@ dump_line(void)
 }
 
 int
-compute_code_indent(void)
+compute_code_column(void)
 {
     int target_col = opt.ind_size * ps.ind_level + 1;
 
@@ -290,7 +311,7 @@ compute_code_indent(void)
 }
 
 int
-compute_label_indent(void)
+compute_label_column(void)
 {
     return
 	ps.pcase ? (int) (case_ind * opt.ind_size) + 1
@@ -398,41 +419,6 @@ fill_buffer(void)
  * 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)

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.20 src/usr.bin/indent/pr_comment.c:1.21
--- src/usr.bin/indent/pr_comment.c:1.20	Fri Mar 12 23:16:00 2021
+++ src/usr.bin/indent/pr_comment.c	Sat Mar 13 00:26:56 2021
@@ -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 @@ static char sccsid[] = "@(#)pr_comment.c
 #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 @@ pr_comment(void)
 	    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)

Reply via email to