Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: merge duplicate code for token buffers



details:   https://anonhg.NetBSD.org/src/rev/def5e7e325ee
branches:  trunk
changeset: 987404:def5e7e325ee
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Sep 25 22:14:21 2021 +0000

description:
indent: merge duplicate code for token buffers

No functional change.

diffstat:

 usr.bin/indent/indent.c     |  37 +++++++++++++++++--------------------
 usr.bin/indent/indent.h     |   4 +++-
 usr.bin/indent/lexi.c       |  15 ++++-----------
 usr.bin/indent/pr_comment.c |  17 ++++-------------
 4 files changed, 28 insertions(+), 45 deletions(-)

diffs (164 lines):

diff -r 731c863c5a8e -r def5e7e325ee usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Sep 25 21:42:43 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Sep 25 22:14:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.80 2021/09/25 21:42:43 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.81 2021/09/25 22:14:21 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.80 2021/09/25 21:42:43 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.81 2021/09/25 22:14:21 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -137,29 +137,15 @@
 static void
 check_size_code(size_t desired_size)
 {
-    if (code.e + desired_size < code.l)
-        return;
-
-    size_t nsize = code.l - code.s + 400 + desired_size;
-    size_t code_len = code.e - code.s;
-    code.buf = xrealloc(code.buf, nsize);
-    code.e = code.buf + code_len + 1;
-    code.l = code.buf + nsize - 5;
-    code.s = code.buf + 1;
+    if (code.e + desired_size >= code.l)
+       buf_expand(&code, desired_size);
 }
 
 static void
 check_size_label(size_t desired_size)
 {
-    if (lab.e + (desired_size) < lab.l)
-        return;
-
-    size_t nsize = lab.l - lab.s + 400 + desired_size;
-    size_t label_len = lab.e - lab.s;
-    lab.buf = xrealloc(lab.buf, nsize);
-    lab.e = lab.buf + label_len + 1;
-    lab.l = lab.buf + nsize - 5;
-    lab.s = lab.buf + 1;
+    if (lab.e + desired_size >= lab.l)
+       buf_expand(&lab, desired_size);
 }
 
 #if HAVE_CAPSICUM
@@ -376,6 +362,17 @@
     buf->l = buf->buf + bufsize - 5;   /* safety margin, though unreliable */
 }
 
+void
+buf_expand(struct buffer *buf, size_t desired_size)
+{
+    size_t nsize = buf->l - buf->s + 400 + desired_size;
+    size_t code_len = buf->e - buf->s;
+    buf->buf = xrealloc(buf->buf, nsize);
+    buf->e = buf->buf + code_len + 1;
+    buf->l = buf->buf + nsize - 5;
+    buf->s = buf->buf + 1;
+}
+
 static void
 main_init_globals(void)
 {
diff -r 731c863c5a8e -r def5e7e325ee usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat Sep 25 21:42:43 2021 +0000
+++ b/usr.bin/indent/indent.h   Sat Sep 25 22:14:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.24 2021/09/25 21:42:43 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.25 2021/09/25 22:14:21 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -70,3 +70,5 @@
 void           *xmalloc(size_t);
 void           *xrealloc(void *, size_t);
 char           *xstrdup(const char *);
+
+void           buf_expand(struct buffer *, size_t);
diff -r 731c863c5a8e -r def5e7e325ee usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sat Sep 25 21:42:43 2021 +0000
+++ b/usr.bin/indent/lexi.c     Sat Sep 25 22:14:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.57 2021/09/25 20:05:55 rillig Exp $ */
+/*     $NetBSD: lexi.c,v 1.58 2021/09/25 22:14:21 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.57 2021/09/25 20:05:55 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.58 2021/09/25 22:14:21 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -205,15 +205,8 @@
 static void
 check_size_token(size_t desired_size)
 {
-    if (token.e + (desired_size) < token.l)
-        return;
-
-    size_t nsize = token.l - token.s + 400 + desired_size;
-    size_t token_len = token.e - token.s;
-    token.buf = xrealloc(token.buf, nsize);
-    token.e = token.buf + token_len + 1;
-    token.l = token.buf + nsize - 5;
-    token.s = token.buf + 1;
+    if (token.e + desired_size >= token.l)
+       buf_expand(&token, desired_size);
 }
 
 static int
diff -r 731c863c5a8e -r def5e7e325ee usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sat Sep 25 21:42:43 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sat Sep 25 22:14:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.45 2021/09/25 20:23:42 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.46 2021/09/25 22:14:21 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,14 +43,12 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.45 2021/09/25 20:23:42 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.46 2021/09/25 22:14:21 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
 
-#include <err.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include "indent.h"
@@ -58,15 +56,8 @@
 static void
 check_size_comment(size_t desired_size)
 {
-    if (com.e + (desired_size) < com.l)
-        return;
-
-    size_t nsize = com.l - com.s + 400 + desired_size;
-    size_t com_len = com.e - com.s;
-    com.buf = xrealloc(com.buf, nsize);
-    com.s = com.buf + 1;
-    com.e = com.s + com_len;
-    com.l = com.buf + nsize - 5;
+    if (com.e + desired_size >= com.l)
+       buf_expand(&com, desired_size);
 }
 
 /*



Home | Main Index | Thread Index | Old Index