util: allow non numeric version parts

This commit is contained in:
moneromooo-monero 2017-12-07 10:32:48 +00:00
parent 8512a83572
commit 493f7d0974
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 9 additions and 7 deletions

View file

@ -628,13 +628,13 @@ std::string get_nix_version_display_string()
int vercmp(const char *v0, const char *v1)
{
std::vector<std::string> f0, f1;
boost::split(f0, v0, boost::is_any_of("."));
boost::split(f1, v1, boost::is_any_of("."));
while (f0.size() < f1.size())
f0.push_back("0");
while (f1.size() < f0.size())
f1.push_back("0");
for (size_t i = 0; i < f0.size(); ++i) {
boost::split(f0, v0, boost::is_any_of(".-"));
boost::split(f1, v1, boost::is_any_of(".-"));
for (size_t i = 0; i < std::max(f0.size(), f1.size()); ++i) {
if (i >= f0.size())
return -1;
if (i >= f1.size())
return 1;
int f0i = atoi(f0[i].c_str()), f1i = atoi(f1[i].c_str());
int n = f0i - f1i;
if (n)

View file

@ -40,4 +40,6 @@ TEST(vercmp, two_one) { ASSERT_TRUE(tools::vercmp("2", "1") > 0); }
TEST(vercmp, ten_nine) { ASSERT_TRUE(tools::vercmp("10", "9") > 0); }
TEST(vercmp, one_dot_ten_one_dot_nine) { ASSERT_TRUE(tools::vercmp("1.10", "1.9") > 0); }
TEST(vercmp, one_one_dot_nine) { ASSERT_TRUE(tools::vercmp("1", "1.9") < 0); }
TEST(vercmp, to_master) { ASSERT_TRUE(tools::vercmp("1.0", "1.0-master") < 0); }
TEST(vercmp, from_master) { ASSERT_TRUE(tools::vercmp("1.0-master", "1.1") < 0); }