compare_versions: limit how many parts are checked.

This can be used to only check the first one or two parts of a version,
to ensure only the major, or major+minor parts match.
This commit is contained in:
James Turner
2017-01-05 10:52:48 +00:00
parent 7b0faed03a
commit fd34cc30b8
3 changed files with 13 additions and 2 deletions

View File

@@ -372,12 +372,16 @@ namespace simgear {
return result;
}
int compare_versions(const string& v1, const string& v2)
int compare_versions(const string& v1, const string& v2, int maxComponents)
{
vector<string> v1parts(split(v1, "."));
vector<string> v2parts(split(v2, "."));
int lastPart = std::min(v1parts.size(), v2parts.size());
if (maxComponents > 0) {
lastPart = std::min(lastPart, maxComponents);
}
for (int part=0; part < lastPart; ++part) {
int part1 = to_int(v1parts[part]);
int part2 = to_int(v2parts[part]);

View File

@@ -174,8 +174,10 @@ namespace simgear {
* any number of terms are supported.
* @return 0 if versions match, -ve number if v1 is lower, +ve if v1
* is greater
* @param maxComponents is the maximum number of components to look at.
* This can be used to ignore (say) the patch level by setting it to 2
*/
int compare_versions(const std::string& v1, const std::string& v2);
int compare_versions(const std::string& v1, const std::string& v2, int maxComponents = 0);
/**
* Convert a string to upper case.

View File

@@ -155,6 +155,11 @@ void test_compare_versions()
// Since we compare numerically, leading zeros shouldn't matter
SG_CHECK_EQUAL(strutils::compare_versions("0.06.7", "0.6.07"), 0);
SG_CHECK_EQUAL(strutils::compare_versions("10.6.7", "10.6.8", 2), 0);
SG_CHECK_GT(strutils::compare_versions("10.7.7", "10.6.8", 2), 0);
SG_CHECK_EQUAL(strutils::compare_versions("10.8.7", "10.6.8", 1), 0);
}
void test_md5_hex()