Index: base/version.cc |
=================================================================== |
--- base/version.cc (revision 142705) |
+++ base/version.cc (working copy) |
@@ -11,6 +11,28 @@ |
#include "base/string_split.h" |
#include "base/string_util.h" |
+namespace { |
Alexei Svitkine (slow)
2012/06/18 22:03:44
Please add an empty line between the start of the
Mathieu
2012/06/19 14:30:20
Done.
|
+bool ParseVersionNumbers(const std::vector<std::string>& numbers, |
+ std::vector<uint16>& parsed) { |
Alexei Svitkine (slow)
2012/06/18 22:03:44
Align second parameter.
Mathieu
2012/06/19 14:30:20
Done.
|
+ for (std::vector<std::string>::const_iterator i = numbers.begin(); |
+ i != numbers.end(); ++i) { |
+ int num; |
+ if (!base::StringToInt(*i, &num)) |
+ return false; |
+ if (num < 0) |
+ return false; |
+ const uint16 max = 0xFFFF; |
+ if (num > max) |
+ return false; |
+ // This throws out things like +3, or 032. |
+ if (base::IntToString(num) != *i) |
+ return false; |
+ parsed.push_back(static_cast<uint16>(num)); |
+ } |
+ return true; |
+} |
+} |
Alexei Svitkine (slow)
2012/06/18 22:03:44
The namespace should end with a comment, like so:
Mathieu
2012/06/19 14:30:20
Done.
|
+ |
Version::Version() { |
} |
@@ -23,21 +45,8 @@ |
if (numbers.empty()) |
return; |
std::vector<uint16> parsed; |
- for (std::vector<std::string>::iterator i = numbers.begin(); |
- i != numbers.end(); ++i) { |
- int num; |
- if (!base::StringToInt(*i, &num)) |
- return; |
- if (num < 0) |
- return; |
- const uint16 max = 0xFFFF; |
- if (num > max) |
- return; |
- // This throws out things like +3, or 032. |
- if (base::IntToString(num) != *i) |
- return; |
- parsed.push_back(static_cast<uint16>(num)); |
- } |
+ if (!ParseVersionNumbers(numbers, parsed)) |
+ return; |
components_.swap(parsed); |
} |
@@ -45,6 +54,18 @@ |
return (!components_.empty()); |
} |
+bool Version::IsValidWildcard(const std::string& version_str) { |
Alexei Svitkine (slow)
2012/06/18 22:03:44
For static methods, the convention is to put a com
Mathieu
2012/06/19 14:30:20
Done.
|
+ if (version_str.at(version_str.length() - 1) != '*') { |
+ Version version(version_str); |
Alexei Svitkine (slow)
2012/06/18 22:03:44
Indent this better.
Mathieu
2012/06/19 14:30:20
Done.
|
+ return version.IsValid(); |
+ } else { |
+ std::string no_wc_version_str = version_str.substr(0, |
+ version_str.size() - 2); |
+ Version version(no_wc_version_str); |
+ return version.IsValid(); |
+ } |
+} |
+ |
bool Version::IsOlderThan(const std::string& version_str) const { |
Version proposed_ver(version_str); |
if (!proposed_ver.IsValid()) |
@@ -52,6 +73,39 @@ |
return (CompareTo(proposed_ver) < 0); |
} |
+int Version::CompareToWildcard(const std::string& version_str) const { |
+ DCHECK(IsValid()); |
+ DCHECK(Version::IsValidWildcard(version_str)); |
+ |
+ // Default behavior if the string doesn't end with a wildcard. |
+ if (version_str.at(version_str.length() - 1) != '*') { |
+ Version version(version_str); |
+ DCHECK(version.IsValid()); |
+ return CompareTo(version); |
+ } |
+ |
+ // Removing the * and returning immediately if the argument version is less |
+ // or equal (the * won't affect here). |
+ Version other_version(version_str.substr(0, version_str.size() - 2)); |
+ int comparison = CompareTo(other_version); |
+ if (comparison == -1 || comparison == 0) |
+ return comparison; |
+ |
+ // Trying to catch the equality case 1.2 vs 1.*. In all other cases, it truly |
+ // is a "greater than" case. |
+ std::vector<std::string> other_numbers; |
+ base::SplitString(version_str, '.', &other_numbers); |
+ std::vector<uint16> parsed; |
+ ParseVersionNumbers(other_numbers, parsed); // Will not parse the *. |
+ |
+ DCHECK_GT(parsed.size(), 0); |
+ for (size_t i = 0; i < parsed.size(); ++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); |