sort: two small optimizations

This commit is contained in:
Denis Vlasenko 2006-11-26 15:48:54 +00:00
parent 0d42ddff70
commit 664733f1a3

View File

@ -70,8 +70,7 @@ static char *get_key(char *str, struct sort_key *key, int flags)
for (i = 1; i < key->range[2*j] + j; i++) { for (i = 1; i < key->range[2*j] + j; i++) {
/* Skip leading blanks or first separator */ /* Skip leading blanks or first separator */
if (str[end]) { if (str[end]) {
if (!key_separator && isspace(str[end])) if (!key_separator)
/* TODO: remove "&& isspace(str[end])" */
while (isspace(str[end])) end++; while (isspace(str[end])) end++;
} }
/* Skip body of key */ /* Skip body of key */
@ -158,7 +157,7 @@ static int compare_keys(const void *xarg, const void *yarg)
struct sort_key *key; struct sort_key *key;
for (key = key_list; !retval && key; key = key->next_key) { for (key = key_list; !retval && key; key = key->next_key) {
flags = (key->flags) ? key->flags : global_flags; flags = key->flags ? key->flags : global_flags;
/* Chop out and modify key chunks, handling -dfib */ /* Chop out and modify key chunks, handling -dfib */
x = get_key(*(char **)xarg, key, flags); x = get_key(*(char **)xarg, key, flags);
y = get_key(*(char **)yarg, key, flags); y = get_key(*(char **)yarg, key, flags);
@ -181,7 +180,8 @@ static int compare_keys(const void *xarg, const void *yarg)
#if ENABLE_FEATURE_SORT_BIG #if ENABLE_FEATURE_SORT_BIG
case FLAG_g: { case FLAG_g: {
char *xx, *yy; char *xx, *yy;
double dx = strtod(x, &xx), dy = strtod(y, &yy); double dx = strtod(x, &xx);
double dy = strtod(y, &yy);
/* not numbers < NaN < -infinity < numbers < +infinity) */ /* not numbers < NaN < -infinity < numbers < +infinity) */
if (x == xx) if (x == xx)
retval = (y == yy ? 0 : -1); retval = (y == yy ? 0 : -1);
@ -217,26 +217,27 @@ static int compare_keys(const void *xarg, const void *yarg)
else if (!yy) else if (!yy)
retval = 1; retval = 1;
else else
retval = (dx==thyme.tm_mon) ? 0 : dx-thyme.tm_mon; retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon;
break; break;
} }
/* Full floating point version of -n */ /* Full floating point version of -n */
case FLAG_n: { case FLAG_n: {
double dx = atof(x), dy =atof(y); double dx = atof(x);
double dy = atof(y);
retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0); retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
break; break;
} }
} } /* switch */
/* Free key copies. */ /* Free key copies. */
if (x != *(char **)xarg) free(x); if (x != *(char **)xarg) free(x);
if (y != *(char **)yarg) free(y); if (y != *(char **)yarg) free(y);
if (retval) break; /* if (retval) break; - done by for() anyway */
#else #else
/* Integer version of -n for tiny systems */ /* Integer version of -n for tiny systems */
case FLAG_n: case FLAG_n:
retval = atoi(x) - atoi(y); retval = atoi(x) - atoi(y);
break; break;
} } /* switch */
#endif #endif
} }
/* Perform fallback sort if necessary */ /* Perform fallback sort if necessary */