Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/unexpand Deduplicate some code I'd duplicated, short...



details:   https://anonhg.NetBSD.org/src/rev/18d7d478e664
branches:  trunk
changeset: 965355:18d7d478e664
user:      dyoung <dyoung%NetBSD.org@localhost>
date:      Fri Sep 13 17:32:29 2019 +0000

description:
Deduplicate some code I'd duplicated, shorten a couple of staircases,
repair the indentation in usage().  NFCI.

diffstat:

 usr.bin/unexpand/unexpand.c |  49 ++++++++++++++++++++------------------------
 1 files changed, 22 insertions(+), 27 deletions(-)

diffs (103 lines):

diff -r 30d9044667f2 -r 18d7d478e664 usr.bin/unexpand/unexpand.c
--- a/usr.bin/unexpand/unexpand.c       Fri Sep 13 17:26:27 2019 +0000
+++ b/usr.bin/unexpand/unexpand.c       Fri Sep 13 17:32:29 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: unexpand.c,v 1.17 2019/09/13 17:26:27 dyoung Exp $     */
+/*     $NetBSD: unexpand.c,v 1.18 2019/09/13 17:32:29 dyoung Exp $     */
 
 /*-
  * Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: unexpand.c,v 1.17 2019/09/13 17:26:27 dyoung Exp $");
+__RCSID("$NetBSD: unexpand.c,v 1.18 2019/09/13 17:32:29 dyoung Exp $");
 #endif /* not lint */
 
 /*
@@ -67,9 +67,9 @@
 static void
 usage(void)
 {
-    (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
-       getprogname());
-    exit(EXIT_FAILURE);
+       (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
+           getprogname());
+       exit(EXIT_FAILURE);
 }
 
 int
@@ -132,6 +132,7 @@
 static void
 tabify(const char *line, size_t len)
 {
+       const size_t dstop = (nstops == 0) ? DSTOP : tabstops[0];
        const char *e, *p;
        size_t dcol, ocol, limit, n;
 
@@ -144,33 +145,25 @@
                        continue;
                } else if (*p == '\t') {
                        if (nstops <= 1) {
-                               size_t stop = (nstops == 0)
-                                   ? DSTOP
-                                   : tabstops[0];
-                               dcol = (1 + dcol / stop) * stop;
+                               dcol = (1 + dcol / dstop) * dstop;
                                continue;
-                       } else {
-                               for (n = 0; n < nstops &&
-                                   tabstops[n] - 1 < dcol; n++)
-                                       continue;
-                               if (n < nstops - 1 && tabstops[n] - 1 < limit) {
-                                       dcol = tabstops[n];
-                                       continue;
-                               }
+                       }
+                       for (n = 0; n < nstops && tabstops[n] <= dcol; n++)
+                               continue;
+                       if (n < nstops - 1 && tabstops[n] - 1 < limit) {
+                               dcol = tabstops[n];
+                               continue;
                        }
                }
 
                /* Output our tabs */
                if (nstops <= 1) {
-                       size_t stop = (nstops == 0)
-                           ? DSTOP
-                           : tabstops[0];
-                       while (((ocol + stop) / stop) <= (dcol / stop)) {
+                       while (((ocol + dstop) / dstop) <= (dcol / dstop)) {
                                if (dcol - ocol < 2)
                                        break;
                                if (putchar('\t') == EOF)
                                        goto out;
-                               ocol = (1 + ocol / stop) * stop;
+                               ocol = (1 + ocol / dstop) * dstop;
                        }
                } else {
                        for (n = 0; n < nstops && tabstops[n] <= ocol; n++)
@@ -203,13 +196,15 @@
                        dcol++;
                }
 
+               if (all && dcol < limit)
+                       continue; /* Collapse the rest of the line. */
+
                /* Output remainder of line */
-               if (!all || dcol >= limit) {
-                       for (p++; p < e; p++)
-                               if (putchar(*p) == EOF)
-                                       goto out;
-                       return;
+               for (p++; p < e; p++) {
+                       if (putchar(*p) == EOF)
+                               goto out;
                }
+               return;
        }
        return;
 out:



Home | Main Index | Thread Index | Old Index