Module Name: src
Committed By: rillig
Date: Tue Oct 5 05:56:49 UTC 2021
Modified Files:
src/usr.bin/indent: indent.c lexi.c pr_comment.c
Log Message:
indent: clean up code for appending to buffers
Use *e++ for appending and e[-1] for testing the previously appended
character, like in other places in the code.
No functional change.
To generate a diff of this commit:
cvs rdiff -u -r1.99 -r1.100 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.66 -r1.67 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.48 -r1.49 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.99 src/usr.bin/indent/indent.c:1.100
--- src/usr.bin/indent/indent.c:1.99 Tue Oct 5 05:39:14 2021
+++ src/usr.bin/indent/indent.c Tue Oct 5 05:56:49 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.99 2021/10/05 05:39:14 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.100 2021/10/05 05:56:49 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.99 2021/10/05 05:39:14 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.100 2021/10/05 05:56:49 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif
@@ -199,8 +199,8 @@ search_brace_comment(bool *inout_comment
*sc_end++ = '/'; /* copy in start of comment */
*sc_end++ = '*';
for (;;) { /* loop until the end of the comment */
- *sc_end = inbuf_next();
- if (*sc_end++ == '*' && *buf_ptr == '/')
+ *sc_end++ = inbuf_next();
+ if (sc_end[-1] == '*' && *buf_ptr == '/')
break; /* we are at end of comment */
if (sc_end >= &save_com[sc_size]) { /* check for temp buffer
* overflow */
@@ -483,7 +483,7 @@ main_parse_command_line(int argc, char *
opt.comment_column = 2; /* don't put normal comments before column 2 */
if (opt.block_comment_max_line_length <= 0)
opt.block_comment_max_line_length = opt.max_line_length;
- if (opt.local_decl_indent < 0) /* if not specified by user, set this */
+ if (opt.local_decl_indent < 0) /* if not specified by user, set this */
opt.local_decl_indent = opt.decl_indent;
if (opt.decl_comment_column <= 0) /* if not specified by user, set this */
opt.decl_comment_column = opt.ljust_decl
@@ -772,11 +772,10 @@ process_colon(int *inout_squest, bool *i
*lab.e = '\0';
code.e = code.s;
}
- *inout_force_nl = ps.pcase = *inout_scase; /* ps.pcase will be used by
- * dump_line to decide how to
- * indent the label. force_nl
- * will force a case n: to be
- * on a line by itself */
+ ps.pcase = *inout_scase; /* will be used by dump_line to decide how to
+ * indent the label. */
+ *inout_force_nl = *inout_scase; /* will force a 'case n:' to be on a
+ * line by itself */
*inout_scase = false;
ps.want_blank = false;
}
@@ -1109,8 +1108,8 @@ process_preprocessing(void)
while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
check_size_label(2);
- *lab.e = inbuf_next();
- switch (*lab.e++) {
+ *lab.e++ = inbuf_next();
+ switch (lab.e[-1]) {
case '\\':
if (!in_comment)
*lab.e++ = inbuf_next();
@@ -1151,7 +1150,7 @@ process_preprocessing(void)
if (sc_end == NULL) { /* if this is the first comment, we
* must set up the buffer */
save_com = sc_buf;
- sc_end = &save_com[0];
+ sc_end = save_com;
} else {
*sc_end++ = '\n'; /* add newline between comments */
*sc_end++ = ' ';
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.66 src/usr.bin/indent/lexi.c:1.67
--- src/usr.bin/indent/lexi.c:1.66 Tue Oct 5 05:39:14 2021
+++ src/usr.bin/indent/lexi.c Tue Oct 5 05:56:49 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.66 2021/10/05 05:39:14 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.67 2021/10/05 05:56:49 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 (
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.66 2021/10/05 05:39:14 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.67 2021/10/05 05:56:49 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
#endif
@@ -589,7 +589,7 @@ lexi(struct parser_state *state)
if (*buf_ptr == '=') { /* == */
*token.e++ = '='; /* Flip =+ to += */
buf_ptr++;
- *token.e = 0;
+ *token.e = '\0';
}
ttype = binary_op;
unary_delim = true;
Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.48 src/usr.bin/indent/pr_comment.c:1.49
--- src/usr.bin/indent/pr_comment.c:1.48 Tue Oct 5 05:39:14 2021
+++ src/usr.bin/indent/pr_comment.c Tue Oct 5 05:56:49 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: pr_comment.c,v 1.48 2021/10/05 05:39:14 rillig Exp $ */
+/* $NetBSD: pr_comment.c,v 1.49 2021/10/05 05:56:49 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)pr_comment.c
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.48 2021/10/05 05:39:14 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.49 2021/10/05 05:56:49 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
#endif
@@ -193,7 +193,7 @@ process_comment(void)
if (break_delim) {
char *t = com.e;
com.e = com.s + 2;
- *com.e = 0;
+ *com.e = '\0';
if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
prefix_blankline_requested = true;
dump_line();
@@ -300,11 +300,10 @@ process_comment(void)
int now_len = indentation_after_range(ps.com_col - 1, com.s, com.e);
do {
check_size_comment(1);
- *com.e = inbuf_next();
- if (*com.e == ' ' || *com.e == '\t')
- last_blank = com.e - com.buf; /* remember we saw a
- * blank */
- ++com.e;
+ char ch = inbuf_next();
+ if (ch == ' ' || ch == '\t')
+ last_blank = com.e - com.buf;
+ *com.e++ = ch;
now_len++;
} while (memchr("*\n\r\b\t", *buf_ptr, 6) == NULL &&
(now_len < adj_max_line_length || last_blank == -1));