Skip to content
Snippets Groups Projects

[lib/LinuxVersion] multiple fixes

Merged Florian Fischer requested to merge aj46ezos/emper:fix-linux-version-comparision into master
6 files
+ 109
22
Compare changes
  • Side-by-side
  • Inline
Files
6
+ 14
8
@@ -33,19 +33,22 @@ static auto checked_strtol(const std::string& s) -> long {
@@ -33,19 +33,22 @@ static auto checked_strtol(const std::string& s) -> long {
namespace emper::lib {
namespace emper::lib {
LinuxVersion LinuxVersion::linuxVersion;
// Returns 1 if s is smaller, -1 if this is smaller, 0 if equal
// Returns 1 if s is smaller, -1 if this is smaller, 0 if equal
auto LinuxVersion::compare(const std::string& s) -> int {
auto LinuxVersion::compare(const std::string& s) -> int {
 
size_t dot_pos, dot_pos2;
size_t last_dot_pos = 0, last_dot_pos2 = 0;
size_t last_dot_pos = 0, last_dot_pos2 = 0;
 
bool was_last = false;
for (;;) {
for (;;) {
size_t dot_pos = this->version.find('.', last_dot_pos);
dot_pos = this->version.find('.', last_dot_pos);
// We run out of parts to compare the versions must be equal
if (dot_pos == std::string::npos) {
if (dot_pos == std::string::npos) return 0;
was_last = true;
dot_pos = this->version.size();
size_t dot_pos2 = s.find('.', last_dot_pos2);
dot_pos2 = s.size();
assert(dot_pos2 != std::string::npos);
} else {
 
dot_pos2 = s.find('.', last_dot_pos2);
 
assert(dot_pos2 != std::string::npos);
 
}
long n1 = checked_strtol(this->version.substr(last_dot_pos, dot_pos - last_dot_pos));
long n1 = checked_strtol(this->version.substr(last_dot_pos, dot_pos - last_dot_pos));
long n2 = checked_strtol(s.substr(last_dot_pos2, dot_pos2 - last_dot_pos2));
long n2 = checked_strtol(s.substr(last_dot_pos2, dot_pos2 - last_dot_pos2));
@@ -54,6 +57,9 @@ auto LinuxVersion::compare(const std::string& s) -> int {
@@ -54,6 +57,9 @@ auto LinuxVersion::compare(const std::string& s) -> int {
if (n1 < n2) return -1;
if (n1 < n2) return -1;
 
// We ran out of parts to compare the versions must be equal
 
if (was_last) return 0;
 
last_dot_pos = dot_pos + 1;
last_dot_pos = dot_pos + 1;
last_dot_pos2 = dot_pos2 + 1;
last_dot_pos2 = dot_pos2 + 1;
}
}
Loading