sort: fix ENDCHAR handling in "-kSTART,N.ENDCHAR"
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 19 Oct 2015 17:27:26 +0000 (19:27 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 19 Oct 2015 17:27:26 +0000 (19:27 +0200)
function                                             old     new   delta
get_key                                              505     503      -2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/sort.c
testsuite/sort.tests

index f2bc5335f6b9a2078e0d783107062a176154bd0f..07d903388a61458936d9341a1acb9c5cd89580fb 100644 (file)
@@ -160,17 +160,18 @@ static char *get_key(char *str, struct sort_key *key, int flags)
                if (!j) start = end;
        }
        /* Strip leading whitespace if necessary */
-//XXX: skip_whitespace()
        if (flags & FLAG_b)
+               /* not using skip_whitespace() for speed */
                while (isspace(str[start])) start++;
        /* Strip trailing whitespace if necessary */
        if (flags & FLAG_bb)
                while (end > start && isspace(str[end-1])) end--;
-       /* Handle offsets on start and end */
+       /* -kSTART,N.ENDCHAR: honor ENDCHAR (1-based) */
        if (key->range[3]) {
-               end += key->range[3] - 1;
+               end = key->range[3];
                if (end > len) end = len;
        }
+       /* -kN.STARTCHAR[,...]: honor STARTCHAR (1-based) */
        if (key->range[1]) {
                start += key->range[1] - 1;
                if (start > len) start = len;
index 39c7af7389553347a518fb235eff0a6b270a5337..c51a8e475b11b980cca060978fa34d535c6c9082 100755 (executable)
@@ -106,6 +106,14 @@ a/a:a
 a:b
 " ""
 
+testing "sort with ENDCHAR" "sort -t. -k1,1.1 -k2 input" "\
+ab.1
+aa.2
+" "\
+aa.2
+ab.1
+" ""
+
 testing "glibc build sort" "sort -t. -k 1,1 -k 2n,2n -k 3 input" "\
 GLIBC_2.1
 GLIBC_2.1.1