Module Name: src Committed By: rillig Date: Sun Nov 7 08:24:50 UTC 2021
Modified Files: src/usr.bin/indent: indent.h pr_comment.c Log Message: indent: split copy_comment into wrapping and non-wrapping These two cases are processed in an almost entirely different way. In particular, copy_comment_nowrap should copy the comment verbatim, which is not obvious from the current code, due to the many conditions and the complex control flow. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.81 -r1.82 src/usr.bin/indent/indent.h cvs rdiff -u -r1.98 -r1.99 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.h diff -u src/usr.bin/indent/indent.h:1.81 src/usr.bin/indent/indent.h:1.82 --- src/usr.bin/indent/indent.h:1.81 Sun Nov 7 07:44:59 2021 +++ src/usr.bin/indent/indent.h Sun Nov 7 08:24:50 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.81 2021/11/07 07:44:59 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.82 2021/11/07 08:24:50 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -307,7 +307,8 @@ extern struct parser_state { * starting a structure definition or an * initialization list */ - int ind_level; /* the current indentation level */ + int ind_level; /* the indentation level for the line that is + * currently prepared for output */ int ind_level_follow; /* the level to which ind_level should be set * after the current line is printed */ Index: src/usr.bin/indent/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.98 src/usr.bin/indent/pr_comment.c:1.99 --- src/usr.bin/indent/pr_comment.c:1.98 Sun Nov 7 07:06:00 2021 +++ src/usr.bin/indent/pr_comment.c Sun Nov 7 08:24:50 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.98 2021/11/07 07:06:00 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.99 2021/11/07 08:24:50 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.98 2021/11/07 07:06:00 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.99 2021/11/07 08:24:50 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -202,8 +202,146 @@ analyze_comment(int *p_adj_max_line_leng } static void -copy_comment(int adj_max_line_length, bool break_delim, bool may_wrap) +copy_comment_wrap(int adj_max_line_length, bool break_delim) { + bool may_wrap = true; + ssize_t last_blank = -1; /* index of the last blank in com.buf */ + + for (;;) { + switch (*inp.s) { + case '\f': + if (may_wrap) { /* in a text comment, break the line here */ + dump_line_ff(); + last_blank = -1; + com_add_delim(); + inp.s++; + while (ch_isblank(*inp.s)) + inp.s++; + } else { + inp_skip(); + com_add_char('\f'); + } + break; + + case '\n': + if (token.e[-1] == '/') + goto end_of_line_comment; + + if (had_eof) { + diag(1, "Unterminated comment"); + dump_line(); + return; + } + + last_blank = -1; + if (!may_wrap || ps.next_col_1) { /* if this is a boxed comment, + * we handle the newline */ + if (com.s == com.e) + com_add_char(' '); + if (may_wrap && com.e - com.s > 3) { + dump_line(); + com_add_delim(); + } + dump_line(); + if (may_wrap) + com_add_delim(); + + } else { + ps.next_col_1 = true; + if (!ch_isblank(com.e[-1])) + com_add_char(' '); + last_blank = com.e - 1 - com.buf; + } + ++line_no; + if (may_wrap) { + bool skip_asterisk = true; + do { /* flush any blanks and/or tabs at start of + * next line */ + inp_skip(); + if (*inp.s == '*' && skip_asterisk) { + skip_asterisk = false; + inp_skip(); + if (*inp.s == '/') + goto end_of_comment; + } + } while (ch_isblank(*inp.s)); + } else + inp_skip(); + break; /* end of case for newline */ + + case '*': + inp_skip(); + if (*inp.s == '/' && token.e[-1] == '*') { + end_of_comment: + inp_skip(); + + end_of_line_comment: + if (break_delim) { + if (com.e > com.s + 3) + dump_line(); + else + com.s = com.e; /* XXX: why not e = s? */ + com_add_char(' '); + } + + if (!ch_isblank(com.e[-1]) && may_wrap) + com_add_char(' '); + if (token.e[-1] == '*') { + com_add_char('*'); + com_add_char('/'); + } + com_terminate(); + return; + + } else /* handle isolated '*' */ + com_add_char('*'); + break; + + default: /* we have a random char */ + ; + int now_len = ind_add(ps.com_ind, com.s, com.e); + for (;;) { + char ch = inp_next(); + if (ch_isblank(ch)) + last_blank = com.e - com.buf; + com_add_char(ch); + now_len++; + if (memchr("*\n\r\b\t", *inp.s, 6) != NULL) + break; + if (now_len >= adj_max_line_length && last_blank != -1) + break; + } + + ps.next_col_1 = false; + + if (now_len <= adj_max_line_length || !may_wrap) + break; + if (isspace((unsigned char)com.e[-1])) + break; + + if (last_blank == -1) { /* only a single word in this line */ + dump_line(); + com_add_delim(); + break; + } + + const char *last_word_s = com.buf + last_blank + 1; + size_t last_word_len = (size_t)(com.e - last_word_s); + com.e = com.buf + last_blank; + dump_line(); + com_add_delim(); + + memcpy(com.e, last_word_s, last_word_len); + com.e += last_word_len; + last_blank = -1; + } + } +} + +static void +copy_comment_nowrap(int adj_max_line_length, bool break_delim) +{ + bool may_wrap = false; ssize_t last_blank = -1; /* index of the last blank in com.buf */ for (;;) { @@ -369,6 +507,9 @@ process_comment(void) int l_just_saw_decl = ps.just_saw_decl; analyze_comment(&adj_max_line_length, &break_delim, &may_wrap); - copy_comment(adj_max_line_length, break_delim, may_wrap); + if (may_wrap) + copy_comment_wrap(adj_max_line_length, break_delim); + else + copy_comment_nowrap(adj_max_line_length, break_delim); ps.just_saw_decl = l_just_saw_decl; }