Module Name:    src
Committed By:   rillig
Date:           Sat Mar 13 10:20:54 UTC 2021

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

Log Message:
indent: replace column computation with indentation computation

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/indent/io.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.11 src/usr.bin/indent/indent.h:1.12
--- src/usr.bin/indent/indent.h:1.11	Sat Mar 13 10:06:47 2021
+++ src/usr.bin/indent/indent.h	Sat Mar 13 10:20:54 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.11 2021/03/13 10:06:47 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.12 2021/03/13 10:20:54 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -30,7 +30,7 @@
 
 #if 0
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.h,v 1.11 2021/03/13 10:06:47 rillig Exp $");
+__RCSID("$NetBSD: indent.h,v 1.12 2021/03/13 10:20:54 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $");
 #endif
@@ -49,6 +49,8 @@ int	compute_code_indent(void);
 int	compute_label_indent(void);
 int	count_spaces(int, const char *);
 int	count_spaces_until(int, const char *, const char *);
+int	indentation_after_range(int, const char *, const char *);
+int	indentation_after(int, const char *);
 void	init_constant_tt(void);
 #ifdef debug
 void	debug_vis_range(const char *, const char *, const char *, const char *);

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.39 src/usr.bin/indent/io.c:1.40
--- src/usr.bin/indent/io.c:1.39	Sat Mar 13 10:06:47 2021
+++ src/usr.bin/indent/io.c	Sat Mar 13 10:20:54 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.39 2021/03/13 10:06:47 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.40 2021/03/13 10:20:54 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.39 2021/03/13 10:06:47 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.40 2021/03/13 10:20:54 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -425,42 +425,38 @@ fill_buffer(void)
     }
 }
 
-/*
- * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
- *
- * All rights reserved
- */
 int
-count_spaces_until(int col, const char *buffer, const char *end)
+indentation_after_range(int ind, const char *start, const char *end)
 {
-    for (const char *p = buffer; *p != '\0' && p != end; ++p) {
-	switch (*p) {
-
-	case '\n':
-	case 014:		/* form feed */
-	    col = 1;
-	    break;
-
-	case '\t':
-	    col = 1 + opt.tabsize * ((col - 1) / opt.tabsize + 1);
-	    break;
+    for (const char *p = start; *p != '\0' && p != end; ++p) {
+	if (*p == '\n' || *p == '\f')
+	    ind = 0;
+	else if (*p == '\t')
+	    ind = opt.tabsize * (ind / opt.tabsize + 1);
+	else if (*p == '\b')
+	    --ind;
+	else
+	    ++ind;
+    }
+    return ind;
+}
 
-	case 010:		/* backspace */
-	    --col;
-	    break;
+int
+indentation_after(int ind, const char *s)
+{
+    return indentation_after_range(ind, s, NULL);
+}
 
-	default:
-	    ++col;
-	    break;
-	}			/* end of switch */
-    }				/* end of for loop */
-    return col;
+int
+count_spaces_until(int col, const char *s, const char *e)
+{
+    return 1 + indentation_after_range(col - 1, s, e);
 }
 
 int
-count_spaces(int col, const char *buffer)
+count_spaces(int col, const char *s)
 {
-    return count_spaces_until(col, buffer, NULL);
+    return 1 + indentation_after(col - 1, s);
 }
 
 void

Reply via email to