Module Name: src
Committed By: rillig
Date: Wed Nov 3 21:47:35 UTC 2021
Modified Files:
src/usr.bin/indent: indent.c indent.h io.c pr_comment.c
Log Message:
indent: inline indentation_after, shorten function name to ind_add
There were only few calls to indentation_after, so inlining it spares
the need to look at yet another function definition. Another effect is
that code.s and code.e appear in the code as a pair now, instead of a
single code.s, making the scope of the function call obvious.
In ind_add, there is no need to check for '\0' anymore since none of the
buffers can ever contain a null character, these are filtered out by
inbuf_read_line.
No functional change.
To generate a diff of this commit:
cvs rdiff -u -r1.204 -r1.205 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.75 -r1.76 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.108 -r1.109 src/usr.bin/indent/io.c
cvs rdiff -u -r1.93 -r1.94 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.204 src/usr.bin/indent/indent.c:1.205
--- src/usr.bin/indent/indent.c:1.204 Mon Nov 1 23:44:08 2021
+++ src/usr.bin/indent/indent.c Wed Nov 3 21:47:35 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.204 2021/11/01 23:44:08 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.205 2021/11/03 21:47:35 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.204 2021/11/01 23:44:08 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.205 2021/11/03 21:47:35 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif
@@ -772,8 +772,7 @@ process_lparen_or_lbracket(int decl_ind,
ps.want_blank = false;
*code.e++ = token.s[0];
- ps.paren_indents[ps.p_l_follow - 1] =
- (short)indentation_after_range(0, code.s, code.e);
+ ps.paren_indents[ps.p_l_follow - 1] = (short)ind_add(0, code.s, code.e);
debug_println("paren_indents[%d] is now %d",
ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
@@ -1217,7 +1216,7 @@ process_comma(int decl_ind, bool tabs_to
int varname_len = 8; /* rough estimate for the length of a typical
* variable name */
if (break_comma && (opt.break_after_comma ||
- indentation_after_range(compute_code_indent(), code.s, code.e)
+ ind_add(compute_code_indent(), code.s, code.e)
>= opt.max_line_length - varname_len))
*force_nl = true;
}
Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.75 src/usr.bin/indent/indent.h:1.76
--- src/usr.bin/indent/indent.h:1.75 Mon Nov 1 23:44:08 2021
+++ src/usr.bin/indent/indent.h Wed Nov 3 21:47:35 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.75 2021/11/01 23:44:08 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.76 2021/11/03 21:47:35 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -364,8 +364,7 @@ void debug_println(const char *, ...)__p
void register_typename(const char *);
int compute_code_indent(void);
int compute_label_indent(void);
-int indentation_after_range(int, const char *, const char *);
-int indentation_after(int, const char *);
+int ind_add(int, const char *, const char *);
void inbuf_skip(void);
char inbuf_next(void);
Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.108 src/usr.bin/indent/io.c:1.109
--- src/usr.bin/indent/io.c:1.108 Sat Oct 30 11:49:38 2021
+++ src/usr.bin/indent/io.c Wed Nov 3 21:47:35 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.108 2021/10/30 11:49:38 rillig Exp $ */
+/* $NetBSD: io.c,v 1.109 2021/11/03 21:47:35 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Be
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.108 2021/10/30 11:49:38 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.109 2021/11/03 21:47:35 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
#endif
@@ -136,7 +136,7 @@ dump_line_label(void)
}
} else
output_range(lab.s, lab.e);
- ind = indentation_after(ind, lab.s);
+ ind = ind_add(ind, lab.s, lab.e);
ps.is_case_label = false;
return ind;
@@ -160,7 +160,7 @@ dump_line_code(int ind)
ind = output_indent(ind, target_ind);
output_range(code.s, code.e);
- return indentation_after(ind, code.s);
+ return ind_add(ind, code.s, code.e);
}
static void
@@ -315,11 +315,11 @@ compute_code_indent(void)
target_ind = paren_indent - 1;
} else {
- int w;
- int t = paren_indent;
+ int w; /* TODO: remove '+ 1' and '- 1' */
+ int t = paren_indent; /* TODO: remove '+ 1' and '- 1' */
- if ((w = 1 + indentation_after(t - 1, code.s) - opt.max_line_length) > 0
- && 1 + indentation_after(target_ind, code.s) <= opt.max_line_length) {
+ if ((w = 1 + ind_add(t - 1, code.s, code.e) - opt.max_line_length) > 0
+ && 1 + ind_add(target_ind, code.s, code.e) <= opt.max_line_length) {
t -= w + 1;
if (t > target_ind + 1)
target_ind = t - 1;
@@ -460,9 +460,9 @@ inbuf_read_line(void)
}
int
-indentation_after_range(int ind, const char *start, const char *end)
+ind_add(int ind, const char *start, const char *end)
{
- for (const char *p = start; *p != '\0' && p != end; ++p) {
+ for (const char *p = start; p != end; ++p) {
if (*p == '\n' || *p == '\f')
ind = 0;
else if (*p == '\t')
@@ -474,9 +474,3 @@ indentation_after_range(int ind, const c
}
return ind;
}
-
-int
-indentation_after(int ind, const char *s)
-{
- return indentation_after_range(ind, s, NULL);
-}
Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.93 src/usr.bin/indent/pr_comment.c:1.94
--- src/usr.bin/indent/pr_comment.c:1.93 Sat Oct 30 22:36:07 2021
+++ src/usr.bin/indent/pr_comment.c Wed Nov 3 21:47:35 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: pr_comment.c,v 1.93 2021/10/30 22:36:07 rillig Exp $ */
+/* $NetBSD: pr_comment.c,v 1.94 2021/11/03 21:47:35 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.93 2021/10/30 22:36:07 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.94 2021/11/03 21:47:35 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
#endif
@@ -92,7 +92,7 @@ fits_in_one_line(int max_line_length)
if (!(p[0] == '*' && p[1] == '/'))
continue;
- int len = indentation_after_range(ps.com_ind + 3, inp.s, p);
+ int len = ind_add(ps.com_ind + 3, inp.s, p);
len += ch_isblank(p[-1]) ? 2 : 3;
return len <= max_line_length;
}
@@ -158,9 +158,9 @@ process_comment(void)
int target_ind;
if (code.s != code.e)
- target_ind = indentation_after(compute_code_indent(), code.s);
+ target_ind = ind_add(compute_code_indent(), code.s, code.e);
else if (lab.s != lab.e)
- target_ind = indentation_after(compute_label_indent(), lab.s);
+ target_ind = ind_add(compute_label_indent(), lab.s, lab.e);
else
target_ind = 0;
@@ -192,7 +192,7 @@ process_comment(void)
*/
start = inp.s >= sc_buf && inp.s < sc_buf + sc_size ?
sc_buf : inp.buf;
- ps.n_comment_delta = -indentation_after_range(0, start, inp.s - 2);
+ ps.n_comment_delta = -ind_add(0, start, inp.s - 2);
} else {
ps.n_comment_delta = 0;
while (ch_isblank(*inp.s))
@@ -316,7 +316,7 @@ process_comment(void)
default: /* we have a random char */
;
- int now_len = indentation_after_range(ps.com_ind, com.s, com.e);
+ int now_len = ind_add(ps.com_ind, com.s, com.e);
for (;;) {
char ch = inbuf_next();
if (ch_isblank(ch))