sort: fix key with delimiters breakage

function                                             old     new   delta
get_key                                              509     505      -4

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2015-10-19 14:44:51 +02:00
parent 3d0805e9e7
commit 2e86a5c98d
2 changed files with 52 additions and 5 deletions

View File

@ -106,7 +106,9 @@ static struct sort_key {
static char *get_key(char *str, struct sort_key *key, int flags) static char *get_key(char *str, struct sort_key *key, int flags)
{ {
int start = 0, end = 0, len, j; int start = start; /* for compiler */
int end;
int len, j;
unsigned i; unsigned i;
/* Special case whole string, so we don't have to make a copy */ /* Special case whole string, so we don't have to make a copy */
@ -123,12 +125,15 @@ static char *get_key(char *str, struct sort_key *key, int flags)
end = len; end = len;
/* Loop through fields */ /* Loop through fields */
else { else {
unsigned char ch = 0;
end = 0; end = 0;
for (i = 1; i < key->range[2*j] + j; i++) { for (i = 1; i < key->range[2*j] + j; i++) {
if (key_separator) { if (key_separator) {
/* Skip body of key and separator */ /* Skip body of key and separator */
while (str[end]) { while ((ch = str[end]) != '\0') {
if (str[end++] == key_separator) end++;
if (ch == key_separator)
break; break;
} }
} else { } else {
@ -136,7 +141,7 @@ static char *get_key(char *str, struct sort_key *key, int flags)
while (isspace(str[end])) while (isspace(str[end]))
end++; end++;
/* Skip body of key */ /* Skip body of key */
while (str[end]) { while (str[end] != '\0') {
if (isspace(str[end])) if (isspace(str[end]))
break; break;
end++; end++;
@ -144,11 +149,17 @@ static char *get_key(char *str, struct sort_key *key, int flags)
} }
} }
/* Remove last delim: "abc:def:" => "abc:def" */ /* Remove last delim: "abc:def:" => "abc:def" */
if (key_separator && j && end != 0) if (j && ch) {
//if (str[end-1] != key_separator)
// bb_error_msg(_and_die("BUG! "
// "str[start:%d,end:%d]:'%.*s'",
// start, end, (int)(end-start), &str[start]);
end--; end--;
}
} }
if (!j) start = end; if (!j) start = end;
} }
//bb_error_msg("start:%d,end:%d", start, end);
/* Strip leading whitespace if necessary */ /* Strip leading whitespace if necessary */
//XXX: skip_whitespace() //XXX: skip_whitespace()
if (flags & FLAG_b) if (flags & FLAG_b)

View File

@ -106,6 +106,42 @@ a/a:a
a:b a:b
" "" " ""
testing "glibc build sort" "sort -t. -k 1,1 -k 2n,2n -k 3 input" "\
GLIBC_2.1
GLIBC_2.1.1
GLIBC_2.2
GLIBC_2.2.1
GLIBC_2.10
GLIBC_2.20
GLIBC_2.21
" "\
GLIBC_2.21
GLIBC_2.1.1
GLIBC_2.2.1
GLIBC_2.2
GLIBC_2.20
GLIBC_2.10
GLIBC_2.1
" ""
testing "glibc build sort unique" "sort -u -t. -k 1,1 -k 2n,2n -k 3 input" "\
GLIBC_2.1
GLIBC_2.1.1
GLIBC_2.2
GLIBC_2.2.1
GLIBC_2.10
GLIBC_2.20
GLIBC_2.21
" "\
GLIBC_2.10
GLIBC_2.2.1
GLIBC_2.1.1
GLIBC_2.20
GLIBC_2.2
GLIBC_2.1
GLIBC_2.21
" ""
testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\ testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\
a c a c
" "\ " "\