diff options
Diffstat (limited to 'win32/strverscmp.c')
| -rw-r--r-- | win32/strverscmp.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/win32/strverscmp.c b/win32/strverscmp.c new file mode 100644 index 000000000..05dc60c39 --- /dev/null +++ b/win32/strverscmp.c | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | /* | ||
| 2 | strverscmp from musl (https://www.musl-libc.org/). | ||
| 3 | |||
| 4 | MIT licensed: | ||
| 5 | |||
| 6 | ---------------------------------------------------------------------- | ||
| 7 | Copyright © 2005-2020 Rich Felker, et al. | ||
| 8 | |||
| 9 | Permission is hereby granted, free of charge, to any person obtaining | ||
| 10 | a copy of this software and associated documentation files (the | ||
| 11 | "Software"), to deal in the Software without restriction, including | ||
| 12 | without limitation the rights to use, copy, modify, merge, publish, | ||
| 13 | distribute, sublicense, and/or sell copies of the Software, and to | ||
| 14 | permit persons to whom the Software is furnished to do so, subject to | ||
| 15 | the following conditions: | ||
| 16 | |||
| 17 | The above copyright notice and this permission notice shall be | ||
| 18 | included in all copies or substantial portions of the Software. | ||
| 19 | |||
| 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 21 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| 23 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
| 24 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
| 25 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
| 26 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 27 | ---------------------------------------------------------------------- | ||
| 28 | */ | ||
| 29 | #include "libbb.h" | ||
| 30 | #include <ctype.h> | ||
| 31 | #include <string.h> | ||
| 32 | |||
| 33 | int strverscmp(const char *l0, const char *r0) | ||
| 34 | { | ||
| 35 | const unsigned char *l = (const void *)l0; | ||
| 36 | const unsigned char *r = (const void *)r0; | ||
| 37 | size_t i, dp, j; | ||
| 38 | int z = 1; | ||
| 39 | |||
| 40 | /* Find maximal matching prefix and track its maximal digit | ||
| 41 | * suffix and whether those digits are all zeros. */ | ||
| 42 | for (dp=i=0; l[i]==r[i]; i++) { | ||
| 43 | int c = l[i]; | ||
| 44 | if (!c) return 0; | ||
| 45 | if (!isdigit(c)) dp=i+1, z=1; | ||
| 46 | else if (c!='0') z=0; | ||
| 47 | } | ||
| 48 | |||
| 49 | if (l[dp]-'1'<9U && r[dp]-'1'<9U) { | ||
| 50 | /* If we're looking at non-degenerate digit sequences starting | ||
| 51 | * with nonzero digits, longest digit string is greater. */ | ||
| 52 | for (j=i; isdigit(l[j]); j++) | ||
| 53 | if (!isdigit(r[j])) return 1; | ||
| 54 | if (isdigit(r[j])) return -1; | ||
| 55 | } else if (z && dp<i && (isdigit(l[i]) || isdigit(r[i]))) { | ||
| 56 | /* Otherwise, if common prefix of digit sequence is | ||
| 57 | * all zeros, digits order less than non-digits. */ | ||
| 58 | return (unsigned char)(l[i]-'0') - (unsigned char)(r[i]-'0'); | ||
| 59 | } | ||
| 60 | |||
| 61 | return l[i] - r[i]; | ||
| 62 | } | ||
