Sun Mar 14 05:26:42 2021 UTC ()
indent: clean up check_size_comment

The additional parameter last_bl_ptr was only necessary because the last
blank was stored as a pointer into the buffer.  By storing the index in
the buffer instead, it doesn't need to be updated all the time.

No functional change.


(rillig)
diff -r1.34 -r1.35 src/usr.bin/indent/pr_comment.c

cvs diff -r1.34 -r1.35 src/usr.bin/indent/pr_comment.c (expand / switch to unified diff)

--- src/usr.bin/indent/pr_comment.c 2021/03/14 04:52:10 1.34
+++ src/usr.bin/indent/pr_comment.c 2021/03/14 05:26:42 1.35
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pr_comment.c,v 1.34 2021/03/14 04:52:10 rillig Exp $ */ 1/* $NetBSD: pr_comment.c,v 1.35 2021/03/14 05:26:42 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -36,90 +36,86 @@ @@ -36,90 +36,86 @@
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#if 0 40#if 0
41#ifndef lint 41#ifndef lint
42static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93"; 42static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
43#endif /* not lint */ 43#endif /* not lint */
44#endif 44#endif
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47#ifndef lint 47#ifndef lint
48#if defined(__NetBSD__) 48#if defined(__NetBSD__)
49__RCSID("$NetBSD: pr_comment.c,v 1.34 2021/03/14 04:52:10 rillig Exp $"); 49__RCSID("$NetBSD: pr_comment.c,v 1.35 2021/03/14 05:26:42 rillig Exp $");
50#elif defined(__FreeBSD__) 50#elif defined(__FreeBSD__)
51__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); 51__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
52#endif 52#endif
53#endif 53#endif
54 54
55#include <err.h> 55#include <err.h>
56#include <stdio.h> 56#include <stdio.h>
57#include <stdlib.h> 57#include <stdlib.h>
58#include <string.h> 58#include <string.h>
59 59
60#include "indent.h" 60#include "indent.h"
61 61
62static void 62static void
63check_size_comment(size_t desired_size, char **last_bl_ptr) 63check_size_comment(size_t desired_size)
64{ 64{
65 if (e_com + (desired_size) < l_com) 65 if (e_com + (desired_size) < l_com)
66 return; 66 return;
67 67
68 size_t nsize = l_com - s_com + 400 + desired_size; 68 size_t nsize = l_com - s_com + 400 + desired_size;
69 size_t com_len = e_com - s_com; 69 size_t com_len = e_com - s_com;
70 ssize_t blank_pos = *last_bl_ptr != NULL ? *last_bl_ptr - combuf : -1; 
71 combuf = realloc(combuf, nsize); 70 combuf = realloc(combuf, nsize);
72 if (combuf == NULL) 71 if (combuf == NULL)
73 err(1, NULL); 72 err(1, NULL);
74 e_com = combuf + com_len + 1; 
75 if (blank_pos > 0) 
76 *last_bl_ptr = combuf + blank_pos; 
77 l_com = combuf + nsize - 5; 
78 s_com = combuf + 1; 73 s_com = combuf + 1;
 74 e_com = s_com + com_len;
 75 l_com = combuf + nsize - 5;
79} 76}
80 77
81/* 78/*
82 * Scan, reformat and output a single comment, which is either a block comment 79 * Scan, reformat and output a single comment, which is either a block comment
83 * starting with '/' '*' or an end-of-line comment starting with '//'. 80 * starting with '/' '*' or an end-of-line comment starting with '//'.
84 * 81 *
85 * Try to keep comments from going over the maximum line length. If a line is 82 * Try to keep comments from going over the maximum line length. If a line is
86 * too long, move everything starting from the last blank to the next comment 83 * too long, move everything starting from the last blank to the next comment
87 * line. Blanks and tabs from the beginning of the input line are removed. 84 * line. Blanks and tabs from the beginning of the input line are removed.
88 * 85 *
89 * ALGORITHM: 86 * ALGORITHM:
90 * 1) Decide where the comment should be aligned, and if lines should 87 * 1) Decide where the comment should be aligned, and if lines should
91 * be broken. 88 * be broken.
92 * 2) If lines should not be broken and filled, just copy up to end of 89 * 2) If lines should not be broken and filled, just copy up to end of
93 * comment. 90 * comment.
94 * 3) If lines should be filled, then scan through the input buffer, 91 * 3) If lines should be filled, then scan through the input buffer,
95 * copying characters to com_buf. Remember where the last blank, 92 * copying characters to com_buf. Remember where the last blank,
96 * tab, or newline was. When line is filled, print up to last blank 93 * tab, or newline was. When line is filled, print up to last blank
97 * and continue copying. 94 * and continue copying.
98 */ 95 */
99void 96void
100process_comment(void) 97process_comment(void)
101{ 98{
102 int adj_max_line_length; /* Adjusted max_line_length for comments 99 int adj_max_line_length; /* Adjusted max_line_length for comments
103 * that spill over the right margin */ 100 * that spill over the right margin */
104 char *last_bl; /* points to the last blank in the output 101 ssize_t last_blank; /* index of the last blank in combuf */
105 * buffer */ 
106 char *t_ptr; /* used for moving string */ 102 char *t_ptr; /* used for moving string */
107 int break_delim = opt.comment_delimiter_on_blankline; 103 int break_delim = opt.comment_delimiter_on_blankline;
108 int l_just_saw_decl = ps.just_saw_decl; 104 int l_just_saw_decl = ps.just_saw_decl;
109 105
110 adj_max_line_length = opt.max_line_length; 106 adj_max_line_length = opt.max_line_length;
111 ps.just_saw_decl = 0; 107 ps.just_saw_decl = 0;
112 last_bl = NULL; /* no blanks found so far */ 108 last_blank = -1; /* no blanks found so far */
113 ps.box_com = false; /* at first, assume that we are not in 109 ps.box_com = false; /* at first, assume that we are not in
114 * a boxed comment or some other 110 * a boxed comment or some other
115 * comment that should not be touched */ 111 * comment that should not be touched */
116 ++ps.out_coms; /* keep track of number of comments */ 112 ++ps.out_coms; /* keep track of number of comments */
117 113
118 /* Figure where to align and how to treat the comment */ 114 /* Figure where to align and how to treat the comment */
119 115
120 if (ps.col_1 && !opt.format_col1_comments) { /* if the comment starts in 116 if (ps.col_1 && !opt.format_col1_comments) { /* if the comment starts in
121 * column 1, it should not be touched */ 117 * column 1, it should not be touched */
122 ps.box_com = true; 118 ps.box_com = true;
123 break_delim = false; 119 break_delim = false;
124 ps.com_col = 1; 120 ps.com_col = 1;
125 } else { 121 } else {
@@ -222,163 +218,156 @@ process_comment(void) @@ -222,163 +218,156 @@ process_comment(void)
222 prefix_blankline_requested = 1; 218 prefix_blankline_requested = 1;
223 dump_line(); 219 dump_line();
224 e_com = s_com = t; 220 e_com = s_com = t;
225 if (!ps.box_com && opt.star_comment_cont) 221 if (!ps.box_com && opt.star_comment_cont)
226 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 222 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
227 } 223 }
228 224
229 /* Start to copy the comment */ 225 /* Start to copy the comment */
230 226
231 for (;;) { /* this loop will go until the comment is 227 for (;;) { /* this loop will go until the comment is
232 * copied */ 228 * copied */
233 switch (*buf_ptr) { /* this checks for various special cases */ 229 switch (*buf_ptr) { /* this checks for various special cases */
234 case 014: /* check for a form feed */ 230 case 014: /* check for a form feed */
235 check_size_comment(3, &last_bl); 231 check_size_comment(3);
236 if (!ps.box_com) { /* in a text comment, break the line here */ 232 if (!ps.box_com) { /* in a text comment, break the line here */
237 ps.use_ff = true; 233 ps.use_ff = true;
238 /* fix so dump_line uses a form feed */ 234 /* fix so dump_line uses a form feed */
239 dump_line(); 235 dump_line();
240 last_bl = NULL; 236 last_blank = -1;
241 if (!ps.box_com && opt.star_comment_cont) 237 if (!ps.box_com && opt.star_comment_cont)
242 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 238 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
243 while (*++buf_ptr == ' ' || *buf_ptr == '\t') 239 while (*++buf_ptr == ' ' || *buf_ptr == '\t')
244 ; 240 ;
245 } else { 241 } else {
246 if (++buf_ptr >= buf_end) 242 if (++buf_ptr >= buf_end)
247 fill_buffer(); 243 fill_buffer();
248 *e_com++ = 014; 244 *e_com++ = 014;
249 } 245 }
250 break; 246 break;
251 247
252 case '\n': 248 case '\n':
253 if (e_token[-1] == '/') { 249 if (e_token[-1] == '/') {
254 ++line_no; 250 ++line_no;
255 goto end_of_comment; 251 goto end_of_comment;
256 } 252 }
257 if (had_eof) { /* check for unexpected eof */ 253 if (had_eof) { /* check for unexpected eof */
258 printf("Unterminated comment\n"); 254 printf("Unterminated comment\n");
259 dump_line(); 255 dump_line();
260 return; 256 return;
261 } 257 }
262 last_bl = NULL; 258 last_blank = -1;
263 check_size_comment(4, &last_bl); 259 check_size_comment(4);
264 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, 260 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
265 * we dont ignore the newline */ 261 * we dont ignore the newline */
266 if (s_com == e_com) 262 if (s_com == e_com)
267 *e_com++ = ' '; 263 *e_com++ = ' ';
268 if (!ps.box_com && e_com - s_com > 3) { 264 if (!ps.box_com && e_com - s_com > 3) {
269 dump_line(); 265 dump_line();
270 if (opt.star_comment_cont) 266 if (opt.star_comment_cont)
271 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 267 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
272 } 268 }
273 dump_line(); 269 dump_line();
274 if (!ps.box_com && opt.star_comment_cont) 270 if (!ps.box_com && opt.star_comment_cont)
275 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 271 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
276 } else { 272 } else {
277 ps.last_nl = 1; 273 ps.last_nl = 1;
278 if (e_com[-1] == ' ' || e_com[-1] == '\t') 274 if (!(e_com[-1] == ' ' || e_com[-1] == '\t'))
279 last_bl = e_com - 1; 
280 /* 
281 * if there was a space at the end of the last line, remember 
282 * where it was 
283 */ 
284 else { /* otherwise, insert one */ 
285 last_bl = e_com; 
286 *e_com++ = ' '; 275 *e_com++ = ' ';
287 } 276 last_blank = e_com - 1 - combuf;
288 } 277 }
289 ++line_no; /* keep track of input line number */ 278 ++line_no; /* keep track of input line number */
290 if (!ps.box_com) { 279 if (!ps.box_com) {
291 int nstar = 1; 280 int nstar = 1;
292 do { /* flush any blanks and/or tabs at start of 281 do { /* flush any blanks and/or tabs at start of
293 * next line */ 282 * next line */
294 if (++buf_ptr >= buf_end) 283 if (++buf_ptr >= buf_end)
295 fill_buffer(); 284 fill_buffer();
296 if (*buf_ptr == '*' && --nstar >= 0) { 285 if (*buf_ptr == '*' && --nstar >= 0) {
297 if (++buf_ptr >= buf_end) 286 if (++buf_ptr >= buf_end)
298 fill_buffer(); 287 fill_buffer();
299 if (*buf_ptr == '/') 288 if (*buf_ptr == '/')
300 goto end_of_comment; 289 goto end_of_comment;
301 } 290 }
302 } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 291 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
303 } else if (++buf_ptr >= buf_end) 292 } else if (++buf_ptr >= buf_end)
304 fill_buffer(); 293 fill_buffer();
305 break; /* end of case for newline */ 294 break; /* end of case for newline */
306 295
307 case '*': /* must check for possibility of being at end 296 case '*': /* must check for possibility of being at end
308 * of comment */ 297 * of comment */
309 if (++buf_ptr >= buf_end) /* get to next char after * */ 298 if (++buf_ptr >= buf_end) /* get to next char after * */
310 fill_buffer(); 299 fill_buffer();
311 check_size_comment(4, &last_bl); 300 check_size_comment(4);
312 if (*buf_ptr == '/') { /* it is the end!!! */ 301 if (*buf_ptr == '/') { /* it is the end!!! */
313 end_of_comment: 302 end_of_comment:
314 if (++buf_ptr >= buf_end) 303 if (++buf_ptr >= buf_end)
315 fill_buffer(); 304 fill_buffer();
316 if (break_delim) { 305 if (break_delim) {
317 if (e_com > s_com + 3) 306 if (e_com > s_com + 3)
318 dump_line(); 307 dump_line();
319 else 308 else
320 s_com = e_com; 309 s_com = e_com;
321 *e_com++ = ' '; 310 *e_com++ = ' ';
322 } 311 }
323 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com) 312 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
324 *e_com++ = ' '; /* ensure blank before end */ 313 *e_com++ = ' '; /* ensure blank before end */
325 if (e_token[-1] == '/') 314 if (e_token[-1] == '/')
326 *e_com++ = '\n', *e_com = '\0'; 315 *e_com++ = '\n', *e_com = '\0';
327 else 316 else
328 *e_com++ = '*', *e_com++ = '/', *e_com = '\0'; 317 *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
329 ps.just_saw_decl = l_just_saw_decl; 318 ps.just_saw_decl = l_just_saw_decl;
330 return; 319 return;
331 } else /* handle isolated '*' */ 320 } else /* handle isolated '*' */
332 *e_com++ = '*'; 321 *e_com++ = '*';
333 break; 322 break;
334 default: /* we have a random char */ 323 default: /* we have a random char */
335 ; 324 ;
336 int now_len = indentation_after_range(ps.com_col - 1, s_com, e_com); 325 int now_len = indentation_after_range(ps.com_col - 1, s_com, e_com);
337 do { 326 do {
338 check_size_comment(1, &last_bl); 327 check_size_comment(1);
339 *e_com = *buf_ptr++; 328 *e_com = *buf_ptr++;
340 if (buf_ptr >= buf_end) 329 if (buf_ptr >= buf_end)
341 fill_buffer(); 330 fill_buffer();
342 if (*e_com == ' ' || *e_com == '\t') 331 if (*e_com == ' ' || *e_com == '\t')
343 last_bl = e_com; /* remember we saw a blank */ 332 last_blank = e_com - combuf; /* remember we saw a blank */
344 ++e_com; 333 ++e_com;
345 now_len++; 334 now_len++;
346 } while (!memchr("*\n\r\b\t", *buf_ptr, 6) && 335 } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
347 (now_len < adj_max_line_length || !last_bl)); 336 (now_len < adj_max_line_length || last_blank == -1));
348 ps.last_nl = false; 337 ps.last_nl = false;
349 /* XXX: signed character comparison '>' does not work for UTF-8 */ 338 /* XXX: signed character comparison '>' does not work for UTF-8 */
350 if (now_len > adj_max_line_length && 339 if (now_len > adj_max_line_length &&
351 !ps.box_com && e_com[-1] > ' ') { 340 !ps.box_com && e_com[-1] > ' ') {
352 /* 341 /*
353 * the comment is too long, it must be broken up 342 * the comment is too long, it must be broken up
354 */ 343 */
355 if (last_bl == NULL) { 344 if (last_blank == -1) {
356 dump_line(); 345 dump_line();
357 if (!ps.box_com && opt.star_comment_cont) 346 if (!ps.box_com && opt.star_comment_cont)
358 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 347 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
359 break; 348 break;
360 } 349 }
361 *e_com = '\0'; 350 *e_com = '\0';
362 e_com = last_bl; 351 e_com = combuf + last_blank;
363 dump_line(); 352 dump_line();
364 if (!ps.box_com && opt.star_comment_cont) 353 if (!ps.box_com && opt.star_comment_cont)
365 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 354 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
366 for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t'; 355 for (t_ptr = combuf + last_blank + 1;
367 t_ptr++) 356 *t_ptr == ' ' || *t_ptr == '\t'; t_ptr++)
368 ; 357 continue;
369 last_bl = NULL; 358 last_blank = -1;
370 /* 359 /*
371 * t_ptr will be somewhere between e_com (dump_line() reset) 360 * t_ptr will be somewhere between e_com (dump_line() reset)
372 * and l_com. So it's safe to copy byte by byte from t_ptr 361 * and l_com. So it's safe to copy byte by byte from t_ptr
373 * to e_com without any check_size_comment(). 362 * to e_com without any check_size_comment().
374 */ 363 */
375 while (*t_ptr != '\0') { 364 while (*t_ptr != '\0') {
376 if (*t_ptr == ' ' || *t_ptr == '\t') 365 if (*t_ptr == ' ' || *t_ptr == '\t')
377 last_bl = e_com; 366 last_blank = e_com - combuf;
378 *e_com++ = *t_ptr++; 367 *e_com++ = *t_ptr++;
379 } 368 }
380 } 369 }
381 break; 370 break;
382 } 371 }
383 } 372 }
384} 373}