This source file includes following definitions.
- ParseVersionNumbers
- CompareVersionComponents
- IsValid
- IsValidWildcardString
- IsOlderThan
- CompareToWildcardString
- Equals
- CompareTo
- GetString
#include "base/version.h"
#include <stddef.h>
#include <algorithm>
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
namespace base {
namespace {
bool ParseVersionNumbers(const std::string& version_str,
std::vector<uint16>* parsed) {
std::vector<std::string> numbers;
SplitString(version_str, '.', &numbers);
if (numbers.empty())
return false;
for (std::vector<std::string>::const_iterator it = numbers.begin();
it != numbers.end(); ++it) {
int num;
if (!StringToInt(*it, &num))
return false;
if (num < 0)
return false;
const uint16 max = 0xFFFF;
if (num > max)
return false;
if (IntToString(num) != *it)
return false;
parsed->push_back(static_cast<uint16>(num));
}
return true;
}
int CompareVersionComponents(const std::vector<uint16>& components1,
const std::vector<uint16>& components2) {
const size_t count = std::min(components1.size(), components2.size());
for (size_t i = 0; i < count; ++i) {
if (components1[i] > components2[i])
return 1;
if (components1[i] < components2[i])
return -1;
}
if (components1.size() > components2.size()) {
for (size_t i = count; i < components1.size(); ++i) {
if (components1[i] > 0)
return 1;
}
} else if (components1.size() < components2.size()) {
for (size_t i = count; i < components2.size(); ++i) {
if (components2[i] > 0)
return -1;
}
}
return 0;
}
}
Version::Version() {
}
Version::~Version() {
}
Version::Version(const std::string& version_str) {
std::vector<uint16> parsed;
if (!ParseVersionNumbers(version_str, &parsed))
return;
components_.swap(parsed);
}
bool Version::IsValid() const {
return (!components_.empty());
}
bool Version::IsValidWildcardString(const std::string& wildcard_string) {
std::string version_string = wildcard_string;
if (EndsWith(wildcard_string.c_str(), ".*", false))
version_string = wildcard_string.substr(0, wildcard_string.size() - 2);
Version version(version_string);
return version.IsValid();
}
bool Version::IsOlderThan(const std::string& version_str) const {
Version proposed_ver(version_str);
if (!proposed_ver.IsValid())
return false;
return (CompareTo(proposed_ver) < 0);
}
int Version::CompareToWildcardString(const std::string& wildcard_string) const {
DCHECK(IsValid());
DCHECK(Version::IsValidWildcardString(wildcard_string));
if (!EndsWith(wildcard_string.c_str(), ".*", false)) {
Version version(wildcard_string);
DCHECK(version.IsValid());
return CompareTo(version);
}
std::vector<uint16> parsed;
const bool success = ParseVersionNumbers(
wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
DCHECK(success);
const int comparison = CompareVersionComponents(components_, parsed);
if (comparison == -1 || comparison == 0)
return comparison;
DCHECK_GT(parsed.size(), 0UL);
const size_t min_num_comp = std::min(components_.size(), parsed.size());
for (size_t i = 0; i < min_num_comp; ++i) {
if (components_[i] != parsed[i])
return 1;
}
return 0;
}
bool Version::Equals(const Version& that) const {
DCHECK(IsValid());
DCHECK(that.IsValid());
return (CompareTo(that) == 0);
}
int Version::CompareTo(const Version& other) const {
DCHECK(IsValid());
DCHECK(other.IsValid());
return CompareVersionComponents(components_, other.components_);
}
const std::string Version::GetString() const {
DCHECK(IsValid());
std::string version_str;
size_t count = components_.size();
for (size_t i = 0; i < count - 1; ++i) {
version_str.append(IntToString(components_[i]));
version_str.append(".");
}
version_str.append(IntToString(components_[count - 1]));
return version_str;
}
}