svn commit: r303596 - head/usr.bin/indent
Pedro F. Giffuni
pfg at FreeBSD.org
Sun Jul 31 20:04:19 UTC 2016
Author: pfg
Date: Sun Jul 31 20:04:18 2016
New Revision: 303596
URL: https://svnweb.freebsd.org/changeset/base/303596
Log:
indent(1): Simplify pr_comment().
Modify count_spaces() to take a third parameter "end" that will make the
function return when the end is reached. This lets the caller pass a
pointer to non nul-terminated sequence of characters. Rename
count_spaces() to count_spaces_until() and reinstate count_spaces(), this
time based on count_spaces_until().
Use count_spaces_until() to recalculate current column when going through
a comment just before the fragment which decides if current line of the
comment should be wrapped. This move simplifies this code by eliminating
the need for keeping the column counter up to date every time e_com is
advanced and also reduces spread of code that has to know how many columns
a tab will produce.
Deduplicate code that decided if a comment needs a blank line at the top.
References:
https://github.com/pstef/freebsd_indent/commit/d9fa3b481532a448095f8ddd14fd0797ce59230c
https://github.com/pstef/freebsd_indent/commit/27185b4b336b0e2108a3d96aee6df80cced94192
Differential Revision: https://reviews.freebsd.org/D6966 (Partial)
Submitted by: Piotr Stefaniak
Modified:
head/usr.bin/indent/indent.h
head/usr.bin/indent/io.c
head/usr.bin/indent/pr_comment.c
Modified: head/usr.bin/indent/indent.h
==============================================================================
--- head/usr.bin/indent/indent.h Sun Jul 31 19:02:19 2016 (r303595)
+++ head/usr.bin/indent/indent.h Sun Jul 31 20:04:18 2016 (r303596)
@@ -32,6 +32,7 @@ void addkey(char *, int);
int compute_code_target(void);
int compute_label_target(void);
int count_spaces(int, char *);
+int count_spaces_until(int, char *, char *);
int lexi(void);
void diag2(int, const char *);
void diag3(int, const char *, int);
Modified: head/usr.bin/indent/io.c
==============================================================================
--- head/usr.bin/indent/io.c Sun Jul 31 19:02:19 2016 (r303595)
+++ head/usr.bin/indent/io.c Sun Jul 31 20:04:18 2016 (r303596)
@@ -506,18 +506,15 @@ pad_output(int current, int target)
*
*/
int
-count_spaces(int current, char *buffer)
+count_spaces_until(int cur, char *buffer, char *end)
/*
* this routine figures out where the character position will be after
* printing the text in buffer starting at column "current"
*/
{
char *buf; /* used to look thru buffer */
- int cur; /* current character counter */
- cur = current;
-
- for (buf = buffer; *buf != '\0'; ++buf) {
+ for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
switch (*buf) {
case '\n':
@@ -541,6 +538,12 @@ count_spaces(int current, char *buffer)
return (cur);
}
+int
+count_spaces(int cur, char *buffer)
+{
+ return (count_spaces_until(cur, buffer, NULL));
+}
+
void
diag4(int level, const char *msg, int a, int b)
{
Modified: head/usr.bin/indent/pr_comment.c
==============================================================================
--- head/usr.bin/indent/pr_comment.c Sun Jul 31 19:02:19 2016 (r303595)
+++ head/usr.bin/indent/pr_comment.c Sun Jul 31 20:04:18 2016 (r303596)
@@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "indent_globs.h"
#include "indent.h"
/*
@@ -89,11 +90,6 @@ pr_comment(void)
char *t_ptr; /* used for moving string */
int break_delim = comment_delimiter_on_blankline;
int l_just_saw_decl = ps.just_saw_decl;
- /*
- * int ps.last_nl = 0; true iff the last significant thing
- * weve seen is a newline
- */
- int one_liner = 1; /* true iff this comment is a one-liner */
adj_max_col = max_col;
ps.just_saw_decl = 0;
last_bl = NULL; /* no blanks found so far */
@@ -107,6 +103,7 @@ pr_comment(void)
if (ps.col_1 && !format_col1_comments) { /* if comment starts in column
* 1 it should not be touched */
ps.box_com = true;
+ break_delim = false;
ps.com_col = 1;
}
else {
@@ -119,7 +116,7 @@ pr_comment(void)
* be a block comment and is treated as a
* box comment unless format_block_comments
* is nonzero (the default). */
- break_delim = 0;
+ break_delim = false;
}
if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
/* klg: check only if this line is blank */
@@ -134,7 +131,7 @@ pr_comment(void)
}
else {
int target_col;
- break_delim = 0;
+ break_delim = false;
if (s_code != e_code)
target_col = count_spaces(compute_code_target(), s_code);
else {
@@ -165,23 +162,35 @@ pr_comment(void)
if (*buf_ptr != ' ' && !ps.box_com)
*e_com++ = ' ';
+ /* Don't put a break delimiter if this comment is a one-liner */
+ for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
+ if (t_ptr >= buf_end)
+ fill_buffer();
+ if (t_ptr[0] == '*' && t_ptr[1] == '/') {
+ break_delim = false;
+ break;
+ }
+ }
+
+ if (break_delim) {
+ char *t = e_com;
+ e_com = s_com + 2;
+ *e_com = 0;
+ if (blanklines_before_blockcomments)
+ prefix_blankline_requested = 1;
+ dump_line();
+ e_com = t;
+ s_com[0] = s_com[1] = s_com[2] = ' ';
+ }
+
*e_com = '\0';
- if (troff) {
- now_col = 1;
+ if (troff)
adj_max_col = 80;
- }
- else
- now_col = count_spaces(ps.com_col, s_com); /* figure what column we
- * would be in if we
- * printed the comment
- * now */
/* Start to copy the comment */
while (1) { /* this loop will go until the comment is
* copied */
- if (*buf_ptr > 040 && *buf_ptr != '*')
- ps.last_nl = 0;
CHECK_SIZE_COM;
switch (*buf_ptr) { /* this checks for various spcl cases */
case 014: /* check for a form feed */
@@ -209,7 +218,6 @@ pr_comment(void)
dump_line();
return;
}
- one_liner = 0;
if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
* we dont ignore the newline */
if (s_com == e_com) {
@@ -218,25 +226,12 @@ pr_comment(void)
}
*e_com = '\0';
if (!ps.box_com && e_com - s_com > 3) {
- if (break_delim == 1 && s_com[0] == '/'
- && s_com[1] == '*' && s_com[2] == ' ') {
- char *t = e_com;
- break_delim = 2;
- e_com = s_com + 2;
- *e_com = 0;
- if (blanklines_before_blockcomments)
- prefix_blankline_requested = 1;
- dump_line();
- e_com = t;
- s_com[0] = s_com[1] = s_com[2] = ' ';
- }
dump_line();
CHECK_SIZE_COM;
*e_com++ = ' ';
*e_com++ = ' ';
}
dump_line();
- now_col = ps.com_col;
}
else {
ps.last_nl = 1;
@@ -250,7 +245,6 @@ pr_comment(void)
last_bl = e_com;
CHECK_SIZE_COM;
*e_com++ = ' ';
- ++now_col;
}
}
++line_no; /* keep track of input line number */
@@ -281,29 +275,15 @@ pr_comment(void)
end_of_comment:
if (++buf_ptr >= buf_end)
fill_buffer();
-
- if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before
- * end */
+ /* ensure blank before end */
+ if (e_com[-1] != ' ' && !ps.box_com) {
*e_com++ = ' ';
- ++now_col;
- }
- if (break_delim == 1 && !one_liner && s_com[0] == '/'
- && s_com[1] == '*' && s_com[2] == ' ') {
- char *t = e_com;
- break_delim = 2;
- e_com = s_com + 2;
- *e_com = 0;
- if (blanklines_before_blockcomments)
- prefix_blankline_requested = 1;
- dump_line();
- e_com = t;
- s_com[0] = s_com[1] = s_com[2] = ' ';
}
- if (break_delim == 2 && e_com > s_com + 3
- /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
- *e_com = '\0';
- dump_line();
- now_col = ps.com_col;
+ if (break_delim) {
+ if (e_com > s_com + 3) {
+ *e_com = '\0';
+ dump_line();
+ }
}
CHECK_SIZE_COM;
*e_com++ = '*';
@@ -312,44 +292,26 @@ pr_comment(void)
ps.just_saw_decl = l_just_saw_decl;
return;
}
- else { /* handle isolated '*' */
+ else /* handle isolated '*' */
*e_com++ = '*';
- ++now_col;
- }
break;
default: /* we have a random char */
- *e_com = *buf_ptr++;
- if (buf_ptr >= buf_end)
- fill_buffer();
-
- if (*e_com == '\t') /* keep track of column */
- now_col = ((now_col - 1) & tabmask) + tabsize + 1;
- else if (*e_com == '\b') /* this is a backspace */
- --now_col;
- else
- ++now_col;
-
- if (*e_com == ' ' || *e_com == '\t')
- last_bl = e_com;
- /* remember we saw a blank */
-
- ++e_com;
+ now_col = count_spaces_until(ps.com_col, s_com, e_com);
+ do {
+ *e_com = *buf_ptr++;
+ if (buf_ptr >= buf_end)
+ fill_buffer();
+ if (*e_com == ' ' || *e_com == '\t')
+ last_bl = e_com; /* remember we saw a blank */
+ ++e_com;
+ now_col++;
+ } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
+ now_col <= adj_max_col);
+ ps.last_nl = false;
if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
/*
* the comment is too long, it must be broken up
*/
- if (break_delim == 1 && s_com[0] == '/'
- && s_com[1] == '*' && s_com[2] == ' ') {
- char *t = e_com;
- break_delim = 2;
- e_com = s_com + 2;
- *e_com = 0;
- if (blanklines_before_blockcomments)
- prefix_blankline_requested = 1;
- dump_line();
- e_com = t;
- s_com[0] = s_com[1] = s_com[2] = ' ';
- }
if (last_bl == NULL) { /* we have seen no blanks */
last_bl = e_com; /* fake it */
*e_com++ = ' ';
@@ -378,8 +340,6 @@ pr_comment(void)
}
}
*e_com = '\0';
- now_col = count_spaces(ps.com_col, s_com); /* recompute current
- * position */
}
break;
}
More information about the svn-src-head
mailing list