X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstring%2Fstrverscmp.c;h=4daf276d0cfeb0725649141b8a16a52f89794710;hb=2404d9d643763e6eceafa9a1918925d80a84ad44;hp=7054967837f895301d5472ec9303ff63905db6ed;hpb=a6540174be064806b6eb4dc6abff48a9b4facfdd;p=musl diff --git a/src/string/strverscmp.c b/src/string/strverscmp.c index 70549678..4daf276d 100644 --- a/src/string/strverscmp.c +++ b/src/string/strverscmp.c @@ -1,7 +1,34 @@ +#define _GNU_SOURCE +#include #include -int strverscmp(const char *l, const char *r) +int strverscmp(const char *l0, const char *r0) { - /* FIXME */ - return strcmp(l, r); + 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 && dp