From 85e9fcde9455b3884d547ff15b8e6296d5a29d5b Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 9 Jul 2022 15:59:58 +0200 Subject: [PATCH] Change strverscmp(3) non-digit behavior to match GNU sort -V. This behavior differs from glibc strverscmp(3) but is useful and matches the behavior of GNU sort -V and musl strverscmp(3. --- libc/string/strverscmp.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libc/string/strverscmp.c b/libc/string/strverscmp.c index affc0ced..ef7403f6 100644 --- a/libc/string/strverscmp.c +++ b/libc/string/strverscmp.c @@ -41,7 +41,12 @@ int strverscmp(const char* a, const char* b) continue; // Be a regular strcmp if no digits are involed when they differ. - bool version_string = is_number(a[i]) && is_number(b[i]); + // Note: This implementation uses version number comparison if *either* + // of the first differing characters are digits, unlike glibc + // which is documented to require *both*. This behavior matches + // GNU sort -V and musl. It's also useful as "1.2.txt" compares + // before "1.2.3.txt". + bool version_string = is_number(a[i]) || is_number(b[i]); if ( !version_string && a[i] < b[i] ) return -1; if ( !version_string && a[i] > b[i] )