Module Name:    src
Committed By:   rillig
Date:           Fri Nov 26 15:18:19 UTC 2021

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

Log Message:
indent: add buf_add_range for adding characters to a buffer

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.233 -r1.234 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.102 -r1.103 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.124 -r1.125 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.233 src/usr.bin/indent/indent.c:1.234
--- src/usr.bin/indent/indent.c:1.233	Fri Nov 26 14:17:01 2021
+++ src/usr.bin/indent/indent.c	Fri Nov 26 15:18:18 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.233 2021/11/26 14:17:01 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.234 2021/11/26 15:18:18 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.233 2021/11/26 14:17:01 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.234 2021/11/26 15:18:18 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -170,23 +170,29 @@ buf_reserve(struct buffer *buf, size_t n
 	buf_expand(buf, n);
 }
 
-static void
+void
 buf_add_char(struct buffer *buf, char ch)
 {
     buf_reserve(buf, 1);
     *buf->e++ = ch;
 }
 
-static void
-buf_add_buf(struct buffer *buf, const struct buffer *add)
+void
+buf_add_range(struct buffer *buf, const char *s, const char *e)
 {
-    size_t len = buf_len(add);
+    size_t len = (size_t)(e - s);
     buf_reserve(buf, len);
-    memcpy(buf->e, add->s, len);
+    memcpy(buf->e, s, len);
     buf->e += len;
 }
 
 static void
+buf_add_buf(struct buffer *buf, const struct buffer *add)
+{
+    buf_add_range(buf, add->s, add->e);
+}
+
+static void
 buf_terminate(struct buffer *buf)
 {
     buf_reserve(buf, 1);
@@ -335,8 +341,8 @@ search_stmt_other(lexer_symbol lsym, boo
 	    diag(0, "Line broken");
     }
 
-    for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr)
-	inp_comment_add_char(*t_ptr);
+    inp_comment_add_range(token.s, token.e);
+
     debug_inp("search_stmt_other end");
     return true;
 }
@@ -379,12 +385,12 @@ search_stmt_lookahead(lexer_symbol *lsym
     }
 
     struct parser_state backup_ps = ps;
-    debug_println("made backup of parser state");
+    debug_println("backed up parser state");
     *lsym = lexi();
     if (*lsym == lsym_newline || *lsym == lsym_form_feed ||
 	*lsym == lsym_comment || ps.search_stmt) {
 	ps = backup_ps;
-	debug_println("rolled back parser state");
+	debug_println("restored parser state");
     }
 }
 

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.102 src/usr.bin/indent/indent.h:1.103
--- src/usr.bin/indent/indent.h:1.102	Thu Nov 25 18:48:37 2021
+++ src/usr.bin/indent/indent.h	Fri Nov 26 15:18:18 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.102 2021/11/25 18:48:37 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.103 2021/11/26 15:18:18 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -405,6 +405,8 @@ void *xrealloc(void *, size_t);
 char *xstrdup(const char *);
 
 void buf_expand(struct buffer *, size_t);
+void buf_add_char(struct buffer *, char);
+void buf_add_range(struct buffer *, const char *, const char *);
 
 static inline bool
 ch_isalnum(char ch)

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.124 src/usr.bin/indent/pr_comment.c:1.125
--- src/usr.bin/indent/pr_comment.c:1.124	Thu Nov 25 21:01:32 2021
+++ src/usr.bin/indent/pr_comment.c	Fri Nov 26 15:18:18 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.124 2021/11/25 21:01:32 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.125 2021/11/26 15:18:18 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.124 2021/11/25 21:01:32 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.125 2021/11/26 15:18:18 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -67,11 +67,8 @@ com_add_delim(void)
 {
     if (!opt.star_comment_cont)
 	return;
-    size_t len = 3;
-    if (len >= (size_t)(com.l - com.e))
-	buf_expand(&com, len);
-    memcpy(com.e, " * ", len);
-    com.e += len;
+    const char *delim = " * ";
+    buf_add_range(&com, delim, delim + 3);
 }
 
 static void

Reply via email to