]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
sort: make -h work with -k and blank used as thousands separator
authorKamil Dudka <kdudka@redhat.com>
Mon, 18 Jul 2016 17:04:44 +0000 (19:04 +0200)
committerPádraig Brady <P@draigBrady.com>
Mon, 18 Jul 2016 21:12:32 +0000 (22:12 +0100)
* src/sort.c (traverse_raw_number): Allow to skip only one occurrence
of thousands_sep to avoid finding the unit in the next column in case
thousands_sep matches as blank and is used as column delimiter.
* tests/misc/sort-h-thousands-sep.sh: Add regression test for this bug.
* tests/local.mk: Reference the test.
* NEWS: Mention the bug fix.
Reported at https://bugzilla.redhat.com/1355780
Fixes http://bugs.gnu.org/24015

NEWS
src/sort.c
tests/local.mk
tests/misc/sort-h-thousands-sep.sh [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index 4d8fb45acc6373ad4a9c899e0768fe369bbf3bb6..736b95ed6fc14bd1d1097b73021df789489fc48c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   nl now resets numbering for each page section rather than just for each page.
   [This bug was present in "the beginning".]
 
+  sort -h -k now works even in locales that use blank as thousands separator.
+
   stty --help no longer outputs extraneous gettext header lines
   for translated languages. [bug introduced in coreutils-8.24]
 
index 58c1167473482a287e2116bc7c03b2c394bc8e73..038f6aee3270b8a0718926667deb7dd266e18f68 100644 (file)
@@ -1902,13 +1902,17 @@ traverse_raw_number (char const **number)
      to be lacking in units.
      FIXME: add support for multibyte thousands_sep and decimal_point.  */
 
-  do
+  while (ISDIGIT (ch = *p++))
     {
-      while (ISDIGIT (ch = *p++))
-        if (max_digit < ch)
-          max_digit = ch;
+      if (max_digit < ch)
+        max_digit = ch;
+
+      /* Allow to skip only one occurrence of thousands_sep to avoid finding
+         the unit in the next column in case thousands_sep matches as blank
+         and is used as column delimiter.  */
+      if (*p == thousands_sep)
+        ++p;
     }
-  while (ch == thousands_sep);
 
   if (ch == decimal_point)
     while (ISDIGIT (ch = *p++))
index 27cbf6e5dbdd3c18f1164f7635d12c4260327480..889142af49eca21581066d9de91c51c06752126d 100644 (file)
@@ -348,6 +348,7 @@ all_tests =                                 \
   tests/misc/sort-discrim.sh                   \
   tests/misc/sort-files0-from.pl               \
   tests/misc/sort-float.sh                     \
+  tests/misc/sort-h-thousands-sep.sh           \
   tests/misc/sort-merge.pl                     \
   tests/misc/sort-merge-fdlimit.sh             \
   tests/misc/sort-month.sh                     \
diff --git a/tests/misc/sort-h-thousands-sep.sh b/tests/misc/sort-h-thousands-sep.sh
new file mode 100755 (executable)
index 0000000..17f1b6c
--- /dev/null
@@ -0,0 +1,47 @@
+#!/bin/sh
+# exercise 'sort -h' in locales where thousands separator is blank
+
+# Copyright (C) 2016 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ sort
+test "$(LC_ALL=sv_SE locale thousands_sep)" = ' ' \
+  || skip_ 'The Swedish locale with blank thousands separator is unavailable.'
+
+tee exp1 > in << _EOF_
+1       1k      4 003   1M
+2k      2M      4 002   2
+3M      3       4 001   3k
+_EOF_
+
+cat > exp2 << _EOF_
+3M      3       4 001   3k
+1       1k      4 003   1M
+2k      2M      4 002   2
+_EOF_
+
+cat > exp3 << _EOF_
+3M      3       4 001   3k
+2k      2M      4 002   2
+1       1k      4 003   1M
+_EOF_
+
+for i in 1 2 3; do
+  LC_ALL="sv_SE.utf8" sort -h -k $i "in" > "out${i}" || fail=1
+  compare "exp${i}" "out${i}" || fail=1
+done
+
+Exit $fail