Hello,
please consider the attached patch, improving sort -V to ignore leading white
spaces. The patch makes it possible to sort strings mixed with numbers. A
simple test case has been merged directly into tests/misc/sort-version.
Thanks in advance!
Kamil
From 5493d92bca31724b9d26d04f87dd3b9f13d1b123 Mon Sep 17 00:00:00 2001
From: Kamil Dudka
Date: Wed, 3 Mar 2010 12:36:43 +0100
Subject: [PATCH] sort -V now ignores leading white spaces
* src/sort.c (compare_version): Skip all white spaces before the call of
filevercmp ().
* tests/misc/sort-version: Add a related test case.
* NEWS: Mention the change as a change in behavior.
---
NEWS|2 ++
src/sort.c | 10 +-
tests/misc/sort-version |4
3 files changed, 15 insertions(+), 1 deletions(-)
diff --git a/NEWS b/NEWS
index 2a3ca63..7eb2a0d 100644
--- a/NEWS
+++ b/NEWS
@@ -35,6 +35,8 @@ GNU coreutils NEWS-*- outline -*-
join -t '' no longer emits an error and instead operates on
each line as a whole (even if they contain NUL characters).
+ sort -V now ignores leading white spaces
+
* Noteworthy changes in release 8.4 (2010-01-13) [stable]
diff --git a/src/sort.c b/src/sort.c
index 39cb6d6..c9663eb 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1979,6 +1979,8 @@ static int
compare_version (char *restrict texta, size_t lena,
char *restrict textb, size_t lenb)
{
+ char const *a = texta;
+ char const *b = textb;
int diff;
/* It is necessary to save the character after the end of the field.
@@ -1990,7 +1992,13 @@ compare_version (char *restrict texta, size_t lena,
texta[lena] = '\0';
textb[lenb] = '\0';
- diff = filevercmp (texta, textb);
+ /* skip all blanks */
+ while (blanks[to_uchar (*a)])
+a++;
+ while (blanks[to_uchar (*b)])
+b++;
+
+ diff = filevercmp (a, b);
texta[lena] = sv_a;
textb[lenb] = sv_b;
diff --git a/tests/misc/sort-version b/tests/misc/sort-version
index 741ee8b..60e3e90 100755
--- a/tests/misc/sort-version
+++ b/tests/misc/sort-version
@@ -24,6 +24,8 @@ fi
. $srcdir/test-lib.sh
cat > in << _EOF_
+11
+ 1
gcc-c++-10.fc9.tar.gz
gcc-c++-10.8.12-0.7rc2.fc9.tar.bz2
glibc-2-0.1.beta1.fc10.rpm
@@ -65,6 +67,8 @@ string start 5.90.0 end of str
_EOF_
cat > exp << _EOF_
+ 1
+11
gcc-c++-10.fc9.tar.gz
gcc-c++-10.8.12-0.7rc2.fc9.tar.bz2
glibc-2-0.1.beta1.fc10.rpm
--
1.6.2.5