Chromium Code Reviews| Index: base/version.cc |
| =================================================================== |
| --- base/version.cc (revision 143914) |
| +++ base/version.cc (working copy) |
| @@ -11,33 +11,73 @@ |
| #include "base/string_split.h" |
| #include "base/string_util.h" |
| -Version::Version() { |
| -} |
| +namespace { |
| -Version::~Version() { |
| -} |
| - |
| -Version::Version(const std::string& version_str) { |
| +// This function parses the |numbers| vector representing the different numbers |
|
Alexei Svitkine (slow)
2012/06/26 18:22:03
Nit: Remove "This function" - just start the sente
Mathieu
2012/06/27 15:40:06
Done.
|
| +// inside the version string and constructs a vector of valid integers. It stops |
| +// when it reaches an invalid item (including the wildcard character). |parsed| |
| +// is the resulting integer vector. Function returns true if all numbers were |
| +// parsed successfully, false otherwise. |
| +bool ParseVersionNumbers(const std::string& version_str, |
| + std::vector<uint16>& parsed) { |
|
Ilya Sherman
2012/06/26 20:53:04
nit: This should be passed as a pointer, rather th
Mathieu
2012/06/27 15:40:06
Done.
|
| std::vector<std::string> numbers; |
| base::SplitString(version_str, '.', &numbers); |
| if (numbers.empty()) |
| - return; |
| - std::vector<uint16> parsed; |
| - for (std::vector<std::string>::iterator i = numbers.begin(); |
| + return false; |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: It might be helpful to add a blank l
Mathieu
2012/06/27 15:40:06
Done.
|
| + for (std::vector<std::string>::const_iterator i = numbers.begin(); |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: I generally prefer to use "it" rathe
Mathieu
2012/06/27 20:25:56
Done.
|
| i != numbers.end(); ++i) { |
| int num; |
| if (!base::StringToInt(*i, &num)) |
| - return; |
| + return false; |
| if (num < 0) |
| - return; |
| + return false; |
| const uint16 max = 0xFFFF; |
| if (num > max) |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: I assume that this does the right th
|
| - return; |
| + return false; |
| // This throws out things like +3, or 032. |
| if (base::IntToString(num) != *i) |
| - return; |
| + return false; |
| parsed.push_back(static_cast<uint16>(num)); |
| } |
| + return true; |
| +} |
| + |
| +// Compares version components in |comp1| with components in |comp2|. Returns |
| +// -1, 0 or 1 if |comp1| is greater than, equal to, or less than |comp2|, |
| +// respectively. |
| +int CompareVersionComponents(const std::vector<uint16>& comp1, |
|
Ilya Sherman
2012/06/26 20:53:04
nit: Prefer to avoid abbreviations, so name this "
Mathieu
2012/06/27 15:40:06
Done.
|
| + const std::vector<uint16>& comp2) { |
| + const size_t count = std::min(comp1.size(), comp2.size()); |
| + for (size_t i = 0; i < count; ++i) { |
| + if (comp1[i] > comp2[i]) |
| + return 1; |
| + if (comp1[i] < comp2[i]) |
| + return -1; |
| + } |
|
Ilya Sherman
2012/06/26 20:53:04
nit: Perhaps the logic below this line should rema
|
| + if (comp1.size() > comp2.size()) { |
| + for (size_t i = count; i < comp1.size(); ++i) |
| + if (comp1[i] > 0) |
| + return 1; |
| + } else if (comp1.size() < comp2.size()) { |
| + for (size_t i = count; i < comp2.size(); ++i) |
| + if (comp2[i] > 0) |
| + return -1; |
| + } |
| + return 0; |
| +} |
| + |
| +} // namespace |
| + |
| +Version::Version() { |
| +} |
| + |
| +Version::~Version() { |
| +} |
| + |
| +Version::Version(const std::string& version_str) { |
| + std::vector<uint16> parsed; |
| + if (!ParseVersionNumbers(version_str, parsed)) |
| + return; |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: Add a blank line after this return s
Mathieu
2012/06/27 15:40:06
Done.
|
| components_.swap(parsed); |
| } |
| @@ -45,6 +85,21 @@ |
| return (!components_.empty()); |
| } |
| +// static |
| +bool Version::IsValidWildcardString(const std::string& wildcard_string) { |
| + if (wildcard_string.empty()) |
|
Alexei Svitkine (slow)
2012/06/26 18:22:03
Is this needed anymore or will this be covered by
Mathieu
2012/06/27 15:40:06
Done.
|
| + return false; |
| + |
| + if (!EndsWith(wildcard_string.c_str(), ".*", false)) { |
| + // Either a lone character or a full version number. |
| + Version version(wildcard_string); |
| + return version.IsValid(); |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: These two lines are essentially repe
Mathieu
2012/06/27 15:40:06
Done.
|
| + } |
| + // Version strings length 2 or more, ending with ".*". |
| + Version version(wildcard_string.substr(0, wildcard_string.size() - 2)); |
| + return version.IsValid(); |
| +} |
| + |
| bool Version::IsOlderThan(const std::string& version_str) const { |
| Version proposed_ver(version_str); |
| if (!proposed_ver.IsValid()) |
| @@ -52,6 +107,42 @@ |
| return (CompareTo(proposed_ver) < 0); |
| } |
| +int Version::CompareToWildcardString(const std::string& wildcard_string) const { |
| + DCHECK(IsValid()); |
| + DCHECK(Version::IsValidWildcardString(wildcard_string)); |
| + |
| + // Default behavior if the string doesn't end with a wildcard. |
| + 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 the version is smaller than the wildcard version's |parsed| vector, |
| + // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the |
| + // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to |
| + // 1.2.2.* is 0 regardless of the wildcard). |
|
Ilya Sherman
2012/06/26 20:53:04
nit: I think |comparison| will also be 0 when comp
Mathieu
2012/06/27 15:40:06
Done.
|
| + if (comparison == -1 || comparison == 0) |
| + return comparison; |
| + |
| + // Catch the case where the digits of |parsed| are found in |components_|, |
| + // which means that the two are equal since |parsed| has a trailing "*". |
| + // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since |
| + // components is greater (e.g. 3.2.3 vs 1.*). |
| + 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; |
| +} |
| + |
| // TODO(cpu): remove this method. |
| Version* Version::GetVersionFromString(const std::string& version_str) { |
| Version* vers = new Version(version_str); |
| @@ -77,23 +168,7 @@ |
| int Version::CompareTo(const Version& other) const { |
| DCHECK(IsValid()); |
| DCHECK(other.IsValid()); |
| - size_t count = std::min(components_.size(), other.components_.size()); |
| - for (size_t i = 0; i < count; ++i) { |
| - if (components_[i] > other.components_[i]) |
| - return 1; |
| - if (components_[i] < other.components_[i]) |
| - return -1; |
| - } |
| - if (components_.size() > other.components_.size()) { |
| - for (size_t i = count; i < components_.size(); ++i) |
| - if (components_[i] > 0) |
| - return 1; |
| - } else if (components_.size() < other.components_.size()) { |
| - for (size_t i = count; i < other.components_.size(); ++i) |
| - if (other.components_[i] > 0) |
| - return -1; |
| - } |
| - return 0; |
| + return CompareVersionComponents(components_, other.components_); |
| } |
| const std::string Version::GetString() const { |