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: clean up code for appending to buffers



details:   https://anonhg.NetBSD.org/src/rev/af3b1eb1fcc2
branches:  trunk
changeset: 1023951:af3b1eb1fcc2
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Oct 05 05:56:49 2021 +0000

description:
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.

diffstat:

 usr.bin/indent/indent.c     |  25 ++++++++++++-------------
 usr.bin/indent/lexi.c       |   6 +++---
 usr.bin/indent/pr_comment.c |  15 +++++++--------
 3 files changed, 22 insertions(+), 24 deletions(-)

diffs (144 lines):

diff -r a9a4f1cfabfb -r af3b1eb1fcc2 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue Oct 05 05:39:14 2021 +0000
+++ b/usr.bin/indent/indent.c   Tue Oct 05 05:56:49 2021 +0000
@@ -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 @@
 
 #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 @@
     *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 @@
        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 @@
        *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 @@
 
        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 @@
            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++ = ' ';
diff -r a9a4f1cfabfb -r af3b1eb1fcc2 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Tue Oct 05 05:39:14 2021 +0000
+++ b/usr.bin/indent/lexi.c     Tue Oct 05 05:56:49 2021 +0000
@@ -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 @@
 
 #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 @@
        if (*buf_ptr == '=') {  /* == */
            *token.e++ = '=';   /* Flip =+ to += */
            buf_ptr++;
-           *token.e = 0;
+           *token.e = '\0';
        }
        ttype = binary_op;
        unary_delim = true;
diff -r a9a4f1cfabfb -r af3b1eb1fcc2 usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Tue Oct 05 05:39:14 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Tue Oct 05 05:56:49 2021 +0000
@@ -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 @@
 
 #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 @@
     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 @@
            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));



Home | Main Index | Thread Index | Old Index