source: trunk/libs/newlib/src/newlib/libc/string/strverscmp.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.7 KB
Line 
1/*
2FUNCTION
3        <<strverscmp>>---version string compare
4
5INDEX
6        strverscmp
7
8SYNOPSIS
9        #define _GNU_SOURCE
10        #include <string.h>
11        int strverscmp(const char *<[a]>, const char *<[b]>);
12
13DESCRIPTION
14        <<strverscmp>> compares the string at <[a]> to
15        the string at <[b]> in a version-logical order.
16
17RETURNS
18
19        If <<*<[a]>>> version-sorts after <<*<[b]>>>, <<strverscmp>> returns
20        a number greater than zero.  If the two strings match, <<strverscmp>>
21        returns zero.  If <<*<[a]>>> version-sorts before <<*<[b]>>>,
22        <<strverscmp>> returns a number less than zero.
23
24PORTABILITY
25<<strverscmp>> is a GNU extension.
26
27<<strverscmp>> requires no supporting OS subroutines. It uses
28isdigit() from elsewhere in this library.
29
30QUICKREF
31        strverscmp
32*/
33
34/*
35From musl src/string/strverscmp.c
36
37Copyright © 2005-2014 Rich Felker, et al.
38
39Permission is hereby granted, free of charge, to any person obtaining
40a copy of this software and associated documentation files (the
41"Software"), to deal in the Software without restriction, including
42without limitation the rights to use, copy, modify, merge, publish,
43distribute, sublicense, and/or sell copies of the Software, and to
44permit persons to whom the Software is furnished to do so, subject to
45the following conditions:
46
47The above copyright notice and this permission notice shall be
48included in all copies or substantial portions of the Software.
49
50THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
51EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
52MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
53IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
54CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
55TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
56SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57*/
58
59#define _GNU_SOURCE
60#include <ctype.h>
61#include <string.h>
62
63int strverscmp(const char *l0, const char *r0)
64{
65        const unsigned char *l = (const void *)l0;
66        const unsigned char *r = (const void *)r0;
67        size_t i, dp, j;
68        int z = 1;
69
70        /* Find maximal matching prefix and track its maximal digit
71         * suffix and whether those digits are all zeros. */
72        for (dp=i=0; l[i]==r[i]; i++) {
73                int c = l[i];
74                if (!c) return 0;
75                if (!isdigit(c)) dp=i+1, z=1;
76                else if (c!='0') z=0;
77        }
78
79        if (l[dp]!='0' && r[dp]!='0') {
80                /* If we're not looking at a digit sequence that began
81                 * with a zero, longest digit string is greater. */
82                for (j=i; isdigit(l[j]); j++)
83                        if (!isdigit(r[j])) return 1;
84                if (isdigit(r[j])) return -1;
85        } else if (z && dp<i && (isdigit(l[i]) || isdigit(r[i]))) {
86                /* Otherwise, if common prefix of digit sequence is
87                 * all zeros, digits order less than non-digits. */
88                return (unsigned char)(l[i]-'0') - (unsigned char)(r[i]-'0');
89        }
90
91        return l[i] - r[i];
92}
Note: See TracBrowser for help on using the repository browser.