]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
col: flip all comparisions to numerical order
authorSami Kerola <kerolasa@iki.fi>
Sun, 28 Jun 2020 12:24:28 +0000 (13:24 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sat, 12 Sep 2020 17:32:54 +0000 (18:32 +0100)
Left side is always smaller or equal to right side.  This makes reading code
quicker when not having to constantly swap where is the greater value.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
text-utils/col.c

index 2b1195784ce42dba8cc16bda6ae5424b71e99cf7..2cf6cb39e9869aa37e8f545087855090f7bb45b6 100644 (file)
@@ -183,7 +183,8 @@ static inline void col_putchar(wchar_t ch)
  */
 static void flush_blanks(struct col_ctl *ctl)
 {
-       int half = 0, i, nb = ctl->nblank_lines;
+       int half = 0;
+       ssize_t i, nb = ctl->nblank_lines;
 
        if (nb & 1) {
                if (ctl->fine)
@@ -194,6 +195,7 @@ static void flush_blanks(struct col_ctl *ctl)
        nb /= 2;
        for (i = nb; --i >= 0;)
                col_putchar(NL);
+
        if (half) {
                col_putchar(ESC);
                col_putchar('9');
@@ -221,11 +223,11 @@ static void flush_line(struct col_ctl *ctl, LINE *l)
                 * Do an O(n) sort on l->l_line by column being careful to
                 * preserve the order of characters in the same column.
                 */
-               if (l->l_lsize > sorted_size) {
+               if (sorted_size < l->l_lsize) {
                        sorted_size = l->l_lsize;
                        sorted = xrealloc(sorted, sizeof(CHAR) * sorted_size);
                }
-               if (l->l_max_col >= count_size) {
+               if (count_size <= l->l_max_col) {
                        count_size = l->l_max_col + 1;
                        count = xrealloc((void *)count, sizeof(size_t) * count_size);
                }
@@ -248,35 +250,35 @@ static void flush_line(struct col_ctl *ctl, LINE *l)
                c = sorted;
        } else
                c = l->l_line;
-       while (nchars > 0) {
+
+       while (0 < nchars) {
                this_col = c->c_column;
                endc = c;
                do {
                        ++endc;
-               } while (--nchars > 0 && this_col == endc->c_column);
+               } while (0 < --nchars && this_col == endc->c_column);
 
                /* if -b only print last character */
                if (ctl->no_backspaces) {
                        c = endc - 1;
-                       if (nchars > 0 &&
-                           this_col + c->c_width > endc->c_column)
+                       if (0 < nchars && endc->c_column < this_col + c->c_width)
                                continue;
                }
 
-               if (this_col > last_col) {
+               if (last_col < this_col) {
                        ssize_t nspace = this_col - last_col;
 
-                       if (ctl->compress_spaces && nspace > 1) {
+                       if (ctl->compress_spaces && 1 < nspace) {
                                ssize_t ntabs;
 
                                ntabs = this_col / 8 - last_col / 8;
-                               if (ntabs > 0) {
+                               if (0 < ntabs) {
                                        nspace = this_col & 7;
-                                       while (--ntabs >= 0)
+                                       while (0 <= --ntabs)
                                                col_putchar(TAB);
                                }
                        }
-                       while (--nspace >= 0)
+                       while (0 <= --nspace)
                                col_putchar(SPACE);
                        last_col = this_col;
                }
@@ -293,12 +295,14 @@ static void flush_line(struct col_ctl *ctl, LINE *l)
                                ctl->last_set = c->c_set;
                        }
                        col_putchar(c->c_char);
-                       if ((c + 1) < endc) {
+                       if (c + 1 < endc) {
                                int i;
-                               for (i=0; i < c->c_width; i++)
+
+                               for (i = 0; i < c->c_width; i++)
                                        col_putchar(BS);
                        }
-                       if (++c >= endc)
+
+                       if (endc <= ++c)
                                break;
                }
                last_col += (c - 1)->c_width;
@@ -334,7 +338,7 @@ static void flush_lines(struct col_ctl *ctl, ssize_t nflush)
 {
        LINE *l;
 
-       while (--nflush >= 0) {
+       while (0 <= --nflush) {
                l = ctl->lines;
                ctl->lines = l->l_next;
                if (l->l_line) {
@@ -401,7 +405,7 @@ static int handle_not_graphic(struct col_ctl *ctl, struct col_lines *lns)
                return 1;
        }
        if (iswspace(lns->ch)) {
-               if (wcwidth(lns->ch) > 0)
+               if (0 < wcwidth(lns->ch))
                        lns->cur_col += wcwidth(lns->ch);
                return 1;
        }
@@ -413,7 +417,6 @@ static int handle_not_graphic(struct col_ctl *ctl, struct col_lines *lns)
 
 static void update_cur_line(struct col_ctl *ctl, struct col_lines *lns)
 {
-       LINE *lnew;
        ssize_t nmove;
 
        lns->adjust = 0;
@@ -428,6 +431,7 @@ static void update_cur_line(struct col_ctl *ctl, struct col_lines *lns)
        if (nmove < 0) {
                for (; nmove < 0 && ctl->l->l_prev; nmove++)
                        ctl->l = ctl->l->l_prev;
+
                if (nmove) {
                        if (lns->nflushd_lines == 0) {
                                /*
@@ -435,7 +439,7 @@ static void update_cur_line(struct col_ctl *ctl, struct col_lines *lns)
                                 * has been flushed yet.
                                 */
                                for (; nmove < 0; nmove++) {
-                                       lnew = alloc_line(ctl);
+                                       LINE *lnew = alloc_line(ctl);
                                        ctl->l->l_prev = lnew;
                                        lnew->l_next = ctl->l;
                                        ctl->l = ctl->lines = lnew;
@@ -454,18 +458,20 @@ static void update_cur_line(struct col_ctl *ctl, struct col_lines *lns)
                }
        } else {
                /* may need to allocate here */
-               for (; nmove > 0 && ctl->l->l_next; nmove--)
+               for (; 0 < nmove && ctl->l->l_next; nmove--)
                        ctl->l = ctl->l->l_next;
-               for (; nmove > 0; nmove--) {
-                       lnew = alloc_line(ctl);
+
+               for (; 0 < nmove; nmove--) {
+                       LINE *lnew = alloc_line(ctl);
                        lnew->l_prev = ctl->l;
                        ctl->l->l_next = lnew;
                        ctl->l = lnew;
                }
        }
+
        lns->this_line = lns->cur_line + lns->adjust;
        nmove = lns->this_line - lns->nflushd_lines;
-       if (nmove > 0 && (size_t)nmove >= ctl->max_bufd_lines + BUFFER_MARGIN) {
+       if (0 < nmove && ctl->max_bufd_lines + BUFFER_MARGIN <= (size_t)nmove) {
                lns->nflushd_lines += nmove - ctl->max_bufd_lines;
                flush_lines(ctl, nmove - ctl->max_bufd_lines);
        }
@@ -572,7 +578,7 @@ int main(int argc, char **argv)
                        update_cur_line(&ctl, &lns);
 
                /* grow line's buffer? */
-               if (ctl.l->l_line_len + 1 >= ctl.l->l_lsize) {
+               if (ctl.l->l_lsize <= ctl.l->l_line_len + 1) {
                        size_t need;
 
                        need = ctl.l->l_lsize ? ctl.l->l_lsize * 2 : NALLOC;
@@ -595,7 +601,7 @@ int main(int argc, char **argv)
                        ctl.l->l_needs_sort = 1;
                else
                        ctl.l->l_max_col = lns.cur_col;
-               if (lns.c->c_width > 0)
+               if (0 < lns.c->c_width)
                        lns.cur_col += lns.c->c_width;
        }
        /* goto the last line that had a character on it */