Module Name:    src
Committed By:   rillig
Date:           Sun May 14 14:14:07 UTC 2023

Modified Files:
        src/usr.bin/indent: indent.h io.c lexi.c pr_comment.c

Log Message:
indent: reduce code for scanning tokens

The input line is guaranteed to end with '\n', so there's no need to
carry another pointer around.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.130 -r1.131 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.163 -r1.164 src/usr.bin/indent/io.c
cvs rdiff -u -r1.182 -r1.183 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.133 -r1.134 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.130 src/usr.bin/indent/indent.h:1.131
--- src/usr.bin/indent/indent.h:1.130	Sun May 14 12:12:02 2023
+++ src/usr.bin/indent/indent.h	Sun May 14 14:14:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.130 2023/05/14 12:12:02 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.131 2023/05/14 14:14:07 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -391,7 +391,6 @@ void inp_init(void);
 
 const char *inp_p(void);
 const char *inp_line_start(void);
-const char *inp_line_end(void);
 char inp_peek(void);
 char inp_lookahead(size_t);
 void inp_skip(void);

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.163 src/usr.bin/indent/io.c:1.164
--- src/usr.bin/indent/io.c:1.163	Sun May 14 12:12:02 2023
+++ src/usr.bin/indent/io.c	Sun May 14 14:14:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.163 2023/05/14 12:12:02 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.164 2023/05/14 14:14:07 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.163 2023/05/14 12:12:02 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.164 2023/05/14 14:14:07 rillig Exp $");
 
 #include <assert.h>
 #include <stdio.h>
@@ -77,12 +77,6 @@ inp_line_start(void)
     return inp.mem;
 }
 
-const char *
-inp_line_end(void)
-{
-    return inp.e;
-}
-
 char
 inp_peek(void)
 {

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.182 src/usr.bin/indent/lexi.c:1.183
--- src/usr.bin/indent/lexi.c:1.182	Sun May 14 12:12:02 2023
+++ src/usr.bin/indent/lexi.c	Sun May 14 14:14:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.182 2023/05/14 12:12:02 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.183 2023/05/14 14:14:07 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.182 2023/05/14 12:12:02 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.183 2023/05/14 14:14:07 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -331,16 +331,16 @@ static bool
 probably_looking_at_definition(void)
 {
     int paren_level = 0;
-    for (const char *p = inp_p(), *e = inp_line_end(); p < e; p++) {
+    for (const char *p = inp_p(); *p != '\n'; p++) {
 	if (*p == '(')
 	    paren_level++;
 	if (*p == ')' && --paren_level == 0) {
 	    p++;
 
-	    while (p < e && (ch_isspace(*p) || is_identifier_part(*p)))
+	    while (*p != '\n' && (ch_isspace(*p) || is_identifier_part(*p)))
 		p++;		/* '__dead' or '__unused' */
 
-	    if (p == e)		/* func(...) */
+	    if (*p == '\n')	/* func(...) */
 		break;
 	    if (*p == ';')	/* func(...); */
 		return false;
@@ -472,21 +472,19 @@ lex_asterisk_unary(void)
     }
 
     if (ps.in_decl) {
-	const char *tp = inp_p(), *e = inp_line_end();
-
-	while (tp < e) {
+	for (const char *tp = inp_p(); *tp != '\n';) {
 	    if (ch_isspace(*tp))
 		tp++;
 	    else if (is_identifier_start(*tp)) {
 		tp++;
-		while (tp < e && is_identifier_part(*tp))
+		while (is_identifier_part(*tp))
 		    tp++;
-	    } else
+	    } else {
+		if (*tp == '(')
+		    ps.is_function_definition = true;
 		break;
+	    }
 	}
-
-	if (tp < e && *tp == '(')
-	    ps.is_function_definition = true;
     }
 }
 

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.133 src/usr.bin/indent/pr_comment.c:1.134
--- src/usr.bin/indent/pr_comment.c:1.133	Sun May 14 12:12:02 2023
+++ src/usr.bin/indent/pr_comment.c	Sun May 14 14:14:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.133 2023/05/14 12:12:02 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.134 2023/05/14 14:14:07 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,10 +38,8 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.133 2023/05/14 12:12:02 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.134 2023/05/14 14:14:07 rillig Exp $");
 
-#include <assert.h>
-#include <stdio.h>
 #include <string.h>
 
 #include "indent.h"
@@ -74,15 +72,12 @@ com_terminate(void)
 static bool
 fits_in_one_line(int com_ind, int max_line_length)
 {
-    for (const char *p = inp_p(); *p != '\n'; p++) {
-	assert(*p != '\0');
-	assert(inp_line_end() - p >= 2);
-	if (!(p[0] == '*' && p[1] == '/'))
-	    continue;
-
-	int len = ind_add(com_ind + 3, inp_p(), p);
-	len += ch_isblank(p[-1]) ? 2 : 3;
-	return len <= max_line_length;
+    for (const char *start = inp_p(), *p = start; *p != '\n'; p++) {
+	if (p[0] == '*' && p[1] == '/') {
+	    int len = ind_add(com_ind + 3, start, p);
+	    len += ch_isblank(p[-1]) ? 2 : 3;
+	    return len <= max_line_length;
+	}
     }
     return false;
 }
@@ -147,8 +142,7 @@ analyze_comment(bool *p_may_wrap, bool *
 	 * Find out how much indentation there was originally, because that
 	 * much will have to be ignored by output_complete_line.
 	 */
-	const char *start = inp_line_start();
-	ps.n_comment_delta = -ind_add(0, start, inp_p() - 2);
+	ps.n_comment_delta = -ind_add(0, inp_line_start(), inp_p() - 2);
     } else {
 	ps.n_comment_delta = 0;
 	while (ch_isblank(inp_peek()))

Reply via email to