Module Name: src Committed By: rillig Date: Wed Oct 13 22:38:03 UTC 2021
Modified Files: src/usr.bin/indent: pr_comment.c Log Message: indent: extract fits_in_one_line from process_comment No functional change. To generate a diff of this commit: cvs rdiff -u -r1.76 -r1.77 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/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.76 src/usr.bin/indent/pr_comment.c:1.77 --- src/usr.bin/indent/pr_comment.c:1.76 Tue Oct 12 22:22:35 2021 +++ src/usr.bin/indent/pr_comment.c Wed Oct 13 22:38:02 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.76 2021/10/12 22:22:35 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.77 2021/10/13 22:38:02 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.76 2021/10/12 22:22:35 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.77 2021/10/13 22:38:02 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -82,6 +82,23 @@ com_terminate(void) *com.e = '\0'; } +static bool +fits_in_one_line(int max_line_length) +{ + for (const char *p = inp.s; *p != '\n'; p++) { + assert(*p != '\0'); + assert(inp.e - p >= 2); + if (!(p[0] == '*' && p[1] == '/')) + continue; + + int len = indentation_after_range(ps.com_ind + 3, inp.s, p); + len += is_hspace(p[-1]) ? 2 : 3; + if (len <= max_line_length) + return true; + } + return false; +} + /* * Scan, reformat and output a single comment, which is either a block comment * starting with '/' '*' or an end-of-line comment starting with '//'. @@ -186,21 +203,8 @@ process_comment(void) if (*inp.s != ' ' && may_wrap) com_add_char(' '); - /* Don't put a break delimiter if this is a one-liner that won't wrap. */ - if (break_delim) { - for (const char *p = inp.s; *p != '\n'; p++) { - assert(*p != '\0'); - assert(inp.e - p >= 2); - if (!(p[0] == '*' && p[1] == '/')) - continue; - - int len = indentation_after_range(ps.com_ind + 3, inp.s, p) + - (is_hspace(p[-1]) ? 2 : 3); - if (len <= adj_max_line_length) - break_delim = false; - break; - } - } + if (break_delim && fits_in_one_line(adj_max_line_length)) + break_delim = false; if (break_delim) { char *t = com.e;