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
details: https://anonhg.NetBSD.org/src/rev/2699e69f609c
branches: trunk
changeset: 376434:2699e69f609c
user: rillig <rillig%NetBSD.org@localhost>
date: Sat Jun 17 23:03:20 2023 +0000
description:
indent: clean up
Extract duplicate code for handling line continuations.
Prevent theoretic undefined behavior in strspn, as inp.s is not
null-terminated.
Remove adding extra space characters when processing comments, as these
are not necessary to force a line of output.
No functional change.
diffstat:
usr.bin/indent/io.c | 7 +++----
usr.bin/indent/lexi.c | 42 +++++++++++++++++++++++-------------------
usr.bin/indent/pr_comment.c | 30 +++++++++++++++---------------
3 files changed, 41 insertions(+), 38 deletions(-)
diffs (206 lines):
diff -r 32273708ec2c -r 2699e69f609c usr.bin/indent/io.c
--- a/usr.bin/indent/io.c Sat Jun 17 22:28:49 2023 +0000
+++ b/usr.bin/indent/io.c Sat Jun 17 23:03:20 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $ */
+/* $NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $");
#include <stdio.h>
@@ -341,8 +341,7 @@ output_indented_line(void)
if (lab.len == 0 && code.len == 0 && com.len == 0)
out.line_kind = lk_blank;
- if (want_blank_line() && newlines < 2
- && out.line_kind != lk_blank)
+ if (want_blank_line() && newlines < 2 && out.line_kind != lk_blank)
add_buffered_newline();
/* This kludge aligns function definitions correctly. */
diff -r 32273708ec2c -r 2699e69f609c usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c Sat Jun 17 22:28:49 2023 +0000
+++ b/usr.bin/indent/lexi.c Sat Jun 17 23:03:20 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.231 2023/06/17 22:28:49 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.232 2023/06/17 23:03:20 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.231 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.232 2023/06/17 23:03:20 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -182,17 +182,25 @@ token_add_char(char ch)
buf_add_char(&token, ch);
}
+static bool
+skip_line_continuation(void)
+{
+ if (inp_p[0] == '\\' && inp_p[1] == '\n') {
+ inp_p++;
+ inp_skip();
+ line_no++;
+ return true;
+ }
+ return false;
+}
+
static void
lex_number(void)
{
for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
unsigned char ch = (unsigned char)*inp_p;
- if (ch == '\\' && inp_p[1] == '\n') {
- inp_p++;
- inp_skip();
- line_no++;
+ if (skip_line_continuation())
continue;
- }
if (ch >= array_length(lex_number_row)
|| lex_number_row[ch] == 0)
break;
@@ -213,13 +221,11 @@ static void
lex_word(void)
{
for (;;) {
- if (is_identifier_part(inp_p[0]))
+ if (is_identifier_part(*inp_p))
token_add_char(*inp_p++);
- else if (inp_p[0] == '\\' && inp_p[1] == '\n') {
- inp_p++;
- inp_skip();
- line_no++;
- } else
+ else if (skip_line_continuation())
+ continue;
+ else
return;
}
}
@@ -533,13 +539,11 @@ lexi(void)
buf_clear(&token);
for (;;) {
- if (ch_isblank(inp_p[0]))
- inp_p++;
- else if (inp_p[0] == '\\' && inp_p[1] == '\n') {
+ if (ch_isblank(*inp_p))
inp_p++;
- inp_skip();
- line_no++;
- } else
+ else if (skip_line_continuation())
+ continue;
+ else
break;
}
diff -r 32273708ec2c -r 2699e69f609c usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c Sat Jun 17 22:28:49 2023 +0000
+++ b/usr.bin/indent/pr_comment.c Sat Jun 17 23:03:20 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pr_comment.c,v 1.167 2023/06/17 22:28:49 rillig Exp $ */
+/* $NetBSD: pr_comment.c,v 1.168 2023/06/17 23:03:20 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.167 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.168 2023/06/17 23:03:20 rillig Exp $");
#include <string.h>
@@ -75,6 +75,15 @@ fits_in_one_line(int max_line_length)
return false;
}
+static bool
+is_block_comment(void)
+{
+ const char *p = inp_p;
+ while (*p == '*')
+ p++;
+ return *p == '\n';
+}
+
static void
analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
{
@@ -91,7 +100,7 @@ analyze_comment(bool *p_may_wrap, bool *
token.s[token.len - 1] == '/' ||
(inp_p[0] == '\n' && !opt.format_block_comments))
may_wrap = false;
- if (code.len == 0 && inp_p[strspn(inp_p, "*")] == '\n')
+ if (is_block_comment())
out.line_kind = lk_block_comment;
if (com.len > 0)
@@ -143,8 +152,7 @@ static void
copy_comment_start(bool may_wrap, bool *delim, int line_length)
{
ps.comment_cont = false;
- com_add_char('/');
- com_add_char(token.s[token.len - 1]); /* either '*' or '/' */
+ buf_add_chars(&com, token.s, token.len); // "/*" or "//"
if (may_wrap) {
if (!ch_isblank(inp_p[0]))
@@ -180,8 +188,7 @@ copy_comment_wrap_text(int line_length,
if (ch_isspace(com.s[com.len - 1]))
return;
- if (*last_blank == -1) {
- /* only a single word in this line */
+ if (*last_blank == -1) { /* only a single word in this line */
output_line();
com_add_star();
return;
@@ -209,8 +216,6 @@ copy_comment_wrap_newline(ssize_t *last_
{
*last_blank = -1;
if (seen_newline) {
- if (com.len == 0)
- com_add_char(' '); /* force empty output line */
if (com.len > 3) {
output_line();
com_add_star();
@@ -245,9 +250,7 @@ copy_comment_wrap_finish(int line_length
if (delim) {
if (com.len > 3)
output_line();
- else
- buf_clear(&com);
- com_add_char(' ');
+ buf_clear(&com);
} else {
size_t len = com.len;
// XXX: This loop differs from the one below.
@@ -316,9 +319,6 @@ copy_comment_nowrap(void)
return;
}
- if (com.len == 0)
- com_add_char(' '); /* force output of an
- * empty line */
output_line();
line_no++;
inp_skip();
Home |
Main Index |
Thread Index |
Old Index