]>
cygwin.com Git - cygwin-apps/setup.git/blob - csu_util/version_compare.cc
2 * Copyright (c) 2004 Max Bowsher
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by Max Bowsher
15 #include "version_compare.h"
19 static inline bool isdigit(char c
) { return (c
>= '0' && c
<= '9'); }
21 /* Sort two version numbers, comparing equivalently seperated strings of
24 * Returns a positive number if (a > b)
25 * Returns a negative number if (a < b)
26 * Returns zero if (a == b)
28 * Inspired but not equivalent to rpmvercmp().
30 int version_compare (string a
, string b
)
34 size_t apos1
, apos2
= 0, bpos1
, bpos2
= 0;
35 size_t alen
= a
.length(), blen
= b
.length();
39 while (apos2
< alen
&& bpos2
< blen
)
44 if (isdigit(a
[apos2
]))
46 while (apos2
< alen
&& isdigit(a
[apos2
])) apos2
++;
47 while (bpos2
< blen
&& isdigit(b
[bpos2
])) bpos2
++;
52 while (apos2
< alen
&& !isdigit(a
[apos2
])) apos2
++;
53 while (bpos2
< blen
&& !isdigit(b
[bpos2
])) bpos2
++;
57 /* if (apos1 == apos2) { a logical impossibility has happened; } */
59 /* isdigit(a[0]) != isdigit(b[0])
60 * arbitrarily sort the non-digit first */
61 if (bpos1
== bpos2
) return (isnum
? 1 : -1);
65 /* skip numeric leading zeros */
66 while (apos1
< alen
&& a
[apos1
] == '0') apos1
++;
67 while (bpos1
< blen
&& b
[bpos1
] == '0') bpos1
++;
69 /* if one number has more digits, it is greater */
70 if (apos2
-apos1
> bpos2
-bpos1
) return 1;
71 if (apos2
-apos1
< bpos2
-bpos1
) return -1;
74 /* do an ordinary lexicographic string comparison */
75 cval
= a
.compare(apos1
, apos2
-apos1
, b
, bpos1
, bpos2
-bpos1
);
76 if (cval
) return (cval
< 1 ? -1 : 1);
79 /* ran out of characters in one string, without finding a difference */
81 /* maybe they were the same version, but with different leading zeros */
82 if (apos2
== alen
&& bpos2
== blen
) return 0;
84 /* the version with a suffix remaining is greater */
85 return (apos2
< alen
? 1 : -1);
88 #ifdef TESTING_VERSION_COMPARE
100 static version_pair test_data
[] =
102 { "1.0.0", "2.0.0" },
106 { "2.456", "2.1000" },
107 { "2.1000", "3.111" },
113 int main(int argc
, char* argv
[])
115 version_pair
*i
= test_data
;
119 cout
<< setw(10) << i
->a
<< ", " << setw(10) << i
->b
<< " : "
120 << version_compare(i
->a
, i
->b
) << ", " << version_compare(i
->b
, i
->a
)
This page took 0.040875 seconds and 5 git commands to generate.