1 #define _GNU_SOURCE
2 #include <ctype.h>
3 #include <string.h>
4
strverscmp(const char * l0,const char * r0)5 int strverscmp(const char* l0, const char* r0) {
6 const unsigned char* l = (const void*)l0;
7 const unsigned char* r = (const void*)r0;
8 size_t i, dp, j;
9 int z = 1;
10
11 /* Find maximal matching prefix and track its maximal digit
12 * suffix and whether those digits are all zeros. */
13 for (dp = i = 0; l[i] == r[i]; i++) {
14 int c = l[i];
15 if (!c)
16 return 0;
17 if (!isdigit(c))
18 dp = i + 1, z = 1;
19 else if (c != '0')
20 z = 0;
21 }
22
23 if (l[dp] != '0' && r[dp] != '0') {
24 /* If we're not looking at a digit sequence that began
25 * with a zero, longest digit string is greater. */
26 for (j = i; isdigit(l[j]); j++)
27 if (!isdigit(r[j]))
28 return 1;
29 if (isdigit(r[j]))
30 return -1;
31 } else if (z && dp < i && (isdigit(l[i]) || isdigit(r[i]))) {
32 /* Otherwise, if common prefix of digit sequence is
33 * all zeros, digits order less than non-digits. */
34 return (unsigned char)(l[i] - '0') - (unsigned char)(r[i] - '0');
35 }
36
37 return l[i] - r[i];
38 }
39