New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stuff you may want... #19

Closed
wants to merge 2 commits into
base: master
from
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+30 −16
Diff settings

Always

Just for now

Prev

unlimited precision sort -n and replace strptime for sort -M

  • Loading branch information...
izabera committed Aug 10, 2016
commit 6d6252a8f39e3f813cd79dc96ebae61c6507717c
Copy path View file
@@ -205,28 +205,42 @@ static int compare_values(int flags, char *x, char *y)

return dx>dy ? 1 : (dx<dy ? -1 : 0);
} else if (CFG_SORT_BIG && ff == FLAG_M) {
struct tm thyme;
int dx;
char *xx,*yy;
int dx = 0, dy = 0;

xx = strptime(x,"%b",&thyme);
dx = thyme.tm_mon;
yy = strptime(y,"%b",&thyme);
if (!xx) return !yy ? 0 : -1;
else if (!yy) return 1;
else return dx==thyme.tm_mon ? 0 : dx-thyme.tm_mon;
char *months = "jan\0feb\0mar\0apr\0may\0jun\0jul\0aug\0sep\0oct\0nov\0dec";
for (int i = 1; i <= 12; i++) {
if (strncasecmp(months, x, 3) == 0) dx = i;
if (strncasecmp(months, y, 3) == 0) dy = i;
months += strlen(months)+1;
}
return dx && dy ? dx - dy : !dx && !dy ? 0 : dx ? 1 : -1;

} else if (CFG_SORT_BIG && ff == FLAG_x) {
return strtol(x, NULL, 16)-strtol(y, NULL, 16);
// This has to be ff == FLAG_n
} else {
// Full floating point version of -n
if (CFG_SORT_FLOAT) {
double dx = atof(x), dy = atof(y);

return dx>dy ? 1 : (dx<dy ? -1 : 0);
// Integer version of -n for tiny systems
} else return atoi(x)-atoi(y);
// compare as strings instead of numbers to have unlimited precision
int negx = 1, negy = 1, cmp;
if (*x == '-') x++, negx = -1;
if (*y == '-') y++, negy = -1;
if (negx == 1 && negy == -1) return 1; // -x < x
if (negx == -1 && negy == 1) return -1;

while (*x == '0') x++; // skip zeros
while (*y == '0') y++;
size_t lenx = strspn(x, "1234567890"), leny = strspn(y, "1234567890");
if (lenx != leny) return negx * ((lenx > leny) - (lenx < leny)); // long num > short num
if ((cmp = strncmp(x, y, lenx))) return negx * cmp; // simple number comparison

x += lenx + (x[lenx] == '.'); y += lenx + (y[lenx] == '.'); // skip integer part and .
lenx = strspn(x, "1234567890"); leny = strspn(y, "1234567890");
lenx = lenx < leny ? lenx : leny;
if ((cmp = strncmp(x, y, lenx))) return negx * cmp; // different prefixes

x += lenx; y += lenx; // skip prefixes
while (*x == '0') x++;
while (*y == '0') y++; // skip zeros again
return !*x && !*y ? 0 : isdigit(*x) ? 1 : -1;
}
}

ProTip! Use n and p to navigate between commits in a pull request.