From 509d40303734a025c0ed6c0376d04d44594f0060 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Sun, 27 Dec 2015 11:10:05 +0100 Subject: add sorting by version --- README.md | 1 + lr.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/README.md b/README.md index afa9c17..6cbf19f 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ Default: `n`. * `p`: directory name. * `s`: file size. * `t`: file type. This sorts all directories before other files. +* `v`: file name as version numbers (sorts "2" before "10"). ## Filter expressions diff --git a/lr.c b/lr.c index bc91f8c..b815ddd 100644 --- a/lr.c +++ b/lr.c @@ -1040,9 +1040,42 @@ dircmp(char *a, char *b) return *a - *b; } +// taken straight from musl@a593414 +int mystrverscmp(const char *l0, const char *r0) +{ + const unsigned char *l = (const void *)l0; + const unsigned char *r = (const void *)r0; + size_t i, dp, j; + int z = 1; + + /* Find maximal matching prefix and track its maximal digit + * suffix and whether those digits are all zeros. */ + for (dp=i=0; l[i]==r[i]; i++) { + int c = l[i]; + if (!c) return 0; + if (!isdigit(c)) dp=i+1, z=1; + else if (c!='0') z=0; + } + + if (l[dp]!='0' && r[dp]!='0') { + /* If we're not looking at a digit sequence that began + * with a zero, longest digit string is greater. */ + for (j=i; isdigit(l[j]); j++) + if (!isdigit(r[j])) return 1; + if (isdigit(r[j])) return -1; + } else if (z && dpfpath), extnam(fa->fpath)); case 'p': DIRCMP(fa->fpath, fb->fpath); case 'P': DIRCMP(fb->fpath, fa->fpath); + case 'v': VERCMP(fa->fpath, fb->fpath); + case 'V': VERCMP(fb->fpath, fa->fpath); default: STRCMP(fa->fpath, fb->fpath); } } -- cgit 1.4.1