]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: fix greedy mode on -l
authorKarel Zak <kzak@redhat.com>
Thu, 4 Aug 2022 08:13:49 +0000 (10:13 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 4 Aug 2022 08:13:49 +0000 (10:13 +0200)
In the 'greedy' mode strtok() skips leading white chars, but code for
-l (merge remaining data to the last column) do not count the skipped
chars.

Old version:

$ printf '  a b c\n1 2 3\n' | column -t -o '-' -l2
a-a
1-2 3

Fixed version:

$ printf '  a b c\n1 2 3\n' | column -t -o '-' -l2
a-b c
1-2 3

Note, see leading white chars '  a b c'.

Fexes: https://github.com/util-linux/util-linux/issues/1763
Signed-off-by: Karel Zak <kzak@redhat.com>
text-utils/column.c

index f17d1a975270b4778335f42ed4933c2412369695..ba2acd1c4564d610f1691622418227fdf57520df 100644 (file)
@@ -443,7 +443,7 @@ static void modify_table(struct column_control *ctl)
 static int add_line_to_table(struct column_control *ctl, wchar_t *wcs0)
 {
        wchar_t *wcdata, *sv = NULL, *wcs = wcs0;
-       size_t n = 0, nchars = 0, len;
+       size_t n = 0, nchars = 0, skip = 0, len;
        struct libscols_line *ln = NULL;
 
        if (!ctl->tab)
@@ -455,13 +455,22 @@ static int add_line_to_table(struct column_control *ctl, wchar_t *wcs0)
                char *data;
 
                if (ctl->maxncols && n + 1 == ctl->maxncols) {
-                       if (nchars < len)
-                               wcdata = wcs0 + nchars;
+                       if (nchars + skip < len)
+                               wcdata = wcs0 + (nchars + skip);
                        else
                                wcdata = NULL;
-               } else
+               } else {
                        wcdata = local_wcstok(ctl, wcs, &sv);
 
+                       /* For the default separator ('greedy' mode) it uses
+                        * strtok() and it skips leading white chars. In this
+                        * case we need to remember size of the ignored white
+                        * chars due to wcdata calculation in maxncols case */
+                       if (wcdata && ctl->greedy
+                           && n == 0 && nchars == 0 && wcdata > wcs)
+                               skip = wcdata - wcs;
+               }
+
                if (!wcdata)
                        break;
                if (scols_table_get_ncols(ctl->tab) < n + 1) {