1 /* Compare file names containing version numbers.
3 Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
4 Copyright (C) 2001 Anthony Towns <aj@azure.humbug.org.au>
5 Copyright (C) 2008-2024 Free Software Foundation, Inc.
7 This file is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation, either version 3 of the
10 License, or (at your option) any later version.
12 This file is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
23 /* This file uses _GL_ATTRIBUTE_PURE. */
24 #if !_GL_CONFIG_H_INCLUDED
25 #error "Please include config.h first."
35 /* Compare strings A and B as file names containing version numbers,
36 and return an integer that is negative, zero, or positive depending
37 on whether A compares less than, equal to, or greater than B.
39 Use the following version sort algorithm:
41 1. Compare the strings' maximal-length non-digit prefixes lexically.
42 If there is a difference return that difference.
43 Otherwise discard the prefixes and continue with the next step.
45 2. Compare the strings' maximal-length digit prefixes, using
46 numeric comparison of the numbers represented by each prefix.
47 (Treat an empty prefix as zero; this can happen only at string end.)
48 If there is a difference, return that difference.
49 Otherwise discard the prefixes and continue with the next step.
51 3. If both strings are empty, return 0. Otherwise continue with step 1.
53 In version sort, lexical comparison is left to right, byte by byte,
54 using the byte's numeric value (0-255), except that:
56 1. ASCII letters sort before other bytes.
57 2. A tilde sorts before anything, even an empty string.
59 In addition to the version sort rules, the following strings have
60 special priority and sort before all other strings (listed in order):
65 4. Strings starting with "." sort before other strings.
67 Before comparing two strings where both begin with non-".",
68 or where both begin with "." but neither is "." or "..",
69 suffixes matching the C-locale extended regular expression
70 (\.[A-Za-z~][A-Za-z0-9~]*)*$ are removed and the strings compared
71 without them, using version sort without special priority;
72 if they do not compare equal, this comparison result is used and
73 the suffixes are effectively ignored. Otherwise, the entire
74 strings are compared using version sort. When removing a suffix
75 from a nonempty string, remove the maximal-length suffix such that
76 the remaining string is nonempty.
78 This function is intended to be a replacement for strverscmp. */
79 int filevercmp (char const *a
, char const *b
) _GL_ATTRIBUTE_PURE
;
81 /* Like filevercmp, except compare the byte arrays A (of length ALEN)
82 and B (of length BLEN) so that A and B can contain '\0', which
83 sorts just before '\1'. But if ALEN is -1 treat A as a string
84 terminated by '\0', and similarly for BLEN. */
85 int filenvercmp (char const *a
, ptrdiff_t alen
, char const *b
, ptrdiff_t blen
)
93 #endif /* FILEVERCMP_H */