mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 5 | #include "base/version.h" |
| 6 | |
jkummerow@chromium.org | df3e671 | 2010-12-21 17:27:25 +0900 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 9 | #include "base/logging.h" |
brettw@chromium.org | abcde5c | 2013-02-07 11:57:22 +0900 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
tfarina@chromium.org | 63aaf3f | 2013-02-08 08:01:39 +0900 | [diff] [blame] | 11 | #include "base/strings/string_split.h" |
avi@chromium.org | 68a745c | 2013-06-11 05:11:14 +0900 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 13 | |
xhwang@chromium.org | 993e506 | 2013-04-22 16:35:50 +0900 | [diff] [blame] | 14 | namespace base { |
| 15 | |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // Parses the |numbers| vector representing the different numbers |
| 19 | // inside the version string and constructs a vector of valid integers. It stops |
| 20 | // when it reaches an invalid item (including the wildcard character). |parsed| |
| 21 | // is the resulting integer vector. Function returns true if all numbers were |
| 22 | // parsed successfully, false otherwise. |
| 23 | bool ParseVersionNumbers(const std::string& version_str, |
| 24 | std::vector<uint16>* parsed) { |
| 25 | std::vector<std::string> numbers; |
xhwang@chromium.org | 993e506 | 2013-04-22 16:35:50 +0900 | [diff] [blame] | 26 | SplitString(version_str, '.', &numbers); |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 27 | if (numbers.empty()) |
| 28 | return false; |
| 29 | |
| 30 | for (std::vector<std::string>::const_iterator it = numbers.begin(); |
| 31 | it != numbers.end(); ++it) { |
| 32 | int num; |
xhwang@chromium.org | 993e506 | 2013-04-22 16:35:50 +0900 | [diff] [blame] | 33 | if (!StringToInt(*it, &num)) |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 34 | return false; |
| 35 | |
| 36 | if (num < 0) |
| 37 | return false; |
| 38 | |
| 39 | const uint16 max = 0xFFFF; |
| 40 | if (num > max) |
| 41 | return false; |
| 42 | |
| 43 | // This throws out things like +3, or 032. |
xhwang@chromium.org | 993e506 | 2013-04-22 16:35:50 +0900 | [diff] [blame] | 44 | if (IntToString(num) != *it) |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 45 | return false; |
| 46 | |
| 47 | parsed->push_back(static_cast<uint16>(num)); |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | // Compares version components in |components1| with components in |
waffles@chromium.org | be67211 | 2013-06-21 14:00:30 +0900 | [diff] [blame] | 53 | // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to, |
| 54 | // or greater than |components2|, respectively. |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 55 | int CompareVersionComponents(const std::vector<uint16>& components1, |
| 56 | const std::vector<uint16>& components2) { |
| 57 | const size_t count = std::min(components1.size(), components2.size()); |
| 58 | for (size_t i = 0; i < count; ++i) { |
| 59 | if (components1[i] > components2[i]) |
| 60 | return 1; |
| 61 | if (components1[i] < components2[i]) |
| 62 | return -1; |
| 63 | } |
| 64 | if (components1.size() > components2.size()) { |
| 65 | for (size_t i = count; i < components1.size(); ++i) { |
| 66 | if (components1[i] > 0) |
| 67 | return 1; |
| 68 | } |
| 69 | } else if (components1.size() < components2.size()) { |
| 70 | for (size_t i = count; i < components2.size(); ++i) { |
| 71 | if (components2[i] > 0) |
| 72 | return -1; |
| 73 | } |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | } // namespace |
| 79 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 80 | Version::Version() { |
| 81 | } |
erg@google.com | 67a2543 | 2011-01-08 05:23:43 +0900 | [diff] [blame] | 82 | |
cpu@chromium.org | 331e27b | 2011-06-08 02:43:20 +0900 | [diff] [blame] | 83 | Version::~Version() { |
| 84 | } |
| 85 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 86 | Version::Version(const std::string& version_str) { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 87 | std::vector<uint16> parsed; |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 88 | if (!ParseVersionNumbers(version_str, &parsed)) |
| 89 | return; |
| 90 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 91 | components_.swap(parsed); |
| 92 | } |
erg@google.com | 67a2543 | 2011-01-08 05:23:43 +0900 | [diff] [blame] | 93 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 94 | bool Version::IsValid() const { |
| 95 | return (!components_.empty()); |
| 96 | } |
| 97 | |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 98 | // static |
| 99 | bool Version::IsValidWildcardString(const std::string& wildcard_string) { |
| 100 | std::string version_string = wildcard_string; |
| 101 | if (EndsWith(wildcard_string.c_str(), ".*", false)) |
| 102 | version_string = wildcard_string.substr(0, wildcard_string.size() - 2); |
| 103 | |
| 104 | Version version(version_string); |
| 105 | return version.IsValid(); |
| 106 | } |
| 107 | |
cpu@chromium.org | 9008a61 | 2011-08-02 02:45:08 +0900 | [diff] [blame] | 108 | bool Version::IsOlderThan(const std::string& version_str) const { |
| 109 | Version proposed_ver(version_str); |
| 110 | if (!proposed_ver.IsValid()) |
| 111 | return false; |
| 112 | return (CompareTo(proposed_ver) < 0); |
| 113 | } |
| 114 | |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 115 | int Version::CompareToWildcardString(const std::string& wildcard_string) const { |
| 116 | DCHECK(IsValid()); |
| 117 | DCHECK(Version::IsValidWildcardString(wildcard_string)); |
| 118 | |
| 119 | // Default behavior if the string doesn't end with a wildcard. |
| 120 | if (!EndsWith(wildcard_string.c_str(), ".*", false)) { |
| 121 | Version version(wildcard_string); |
| 122 | DCHECK(version.IsValid()); |
| 123 | return CompareTo(version); |
| 124 | } |
| 125 | |
| 126 | std::vector<uint16> parsed; |
| 127 | const bool success = ParseVersionNumbers( |
| 128 | wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); |
| 129 | DCHECK(success); |
| 130 | const int comparison = CompareVersionComponents(components_, parsed); |
| 131 | // If the version is smaller than the wildcard version's |parsed| vector, |
| 132 | // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the |
| 133 | // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to |
| 134 | // 1.2.2.* is 0 regardless of the wildcard). Under this logic, |
| 135 | // 1.2.0.0.0.0 compared to 1.2.* is 0. |
| 136 | if (comparison == -1 || comparison == 0) |
| 137 | return comparison; |
| 138 | |
| 139 | // Catch the case where the digits of |parsed| are found in |components_|, |
| 140 | // which means that the two are equal since |parsed| has a trailing "*". |
| 141 | // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since |
| 142 | // components is greater (e.g. 3.2.3 vs 1.*). |
| 143 | DCHECK_GT(parsed.size(), 0UL); |
| 144 | const size_t min_num_comp = std::min(components_.size(), parsed.size()); |
| 145 | for (size_t i = 0; i < min_num_comp; ++i) { |
| 146 | if (components_[i] != parsed[i]) |
| 147 | return 1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 152 | bool Version::Equals(const Version& that) const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 153 | DCHECK(IsValid()); |
| 154 | DCHECK(that.IsValid()); |
| 155 | return (CompareTo(that) == 0); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int Version::CompareTo(const Version& other) const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 159 | DCHECK(IsValid()); |
| 160 | DCHECK(other.IsValid()); |
mathp@google.com | 1088cfd | 2012-07-05 01:22:48 +0900 | [diff] [blame] | 161 | return CompareVersionComponents(components_, other.components_); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | const std::string Version::GetString() const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 165 | DCHECK(IsValid()); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 166 | std::string version_str; |
robertshield@chromium.org | a9914a3 | 2010-11-11 02:02:19 +0900 | [diff] [blame] | 167 | size_t count = components_.size(); |
| 168 | for (size_t i = 0; i < count - 1; ++i) { |
xhwang@chromium.org | 993e506 | 2013-04-22 16:35:50 +0900 | [diff] [blame] | 169 | version_str.append(IntToString(components_[i])); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 170 | version_str.append("."); |
| 171 | } |
xhwang@chromium.org | 993e506 | 2013-04-22 16:35:50 +0900 | [diff] [blame] | 172 | version_str.append(IntToString(components_[count - 1])); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 173 | return version_str; |
| 174 | } |
xhwang@chromium.org | 993e506 | 2013-04-22 16:35:50 +0900 | [diff] [blame] | 175 | |
| 176 | } // namespace base |