[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [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 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 5 | #include "base/version.h" |
| 6 | |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | 5ae0b763e | 2013-02-07 23:01:39 | [diff] [blame] | 13 | #include "base/strings/string_split.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 15 | |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 16 | namespace base { |
| 17 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // Parses the |numbers| vector representing the different numbers |
| 21 | // inside the version string and constructs a vector of valid integers. It stops |
| 22 | // when it reaches an invalid item (including the wildcard character). |parsed| |
| 23 | // is the resulting integer vector. Function returns true if all numbers were |
| 24 | // parsed successfully, false otherwise. |
| 25 | bool ParseVersionNumbers(const std::string& version_str, |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 26 | std::vector<uint32_t>* parsed) { |
brettw | 89365dc | 2015-06-16 05:52:47 | [diff] [blame] | 27 | std::vector<StringPiece> numbers = |
| 28 | SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL); |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 29 | if (numbers.empty()) |
| 30 | return false; |
| 31 | |
brettw | 89365dc | 2015-06-16 05:52:47 | [diff] [blame] | 32 | for (auto it = numbers.begin(); it != numbers.end(); ++it) { |
| 33 | if (StartsWith(*it, "+", CompareCase::SENSITIVE)) |
wfh | f151249 | 2015-02-22 05:41:39 | [diff] [blame] | 34 | return false; |
brettw | 89365dc | 2015-06-16 05:52:47 | [diff] [blame] | 35 | |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 36 | unsigned int num; |
| 37 | if (!StringToUint(*it, &num)) |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 38 | return false; |
| 39 | |
wfh | f151249 | 2015-02-22 05:41:39 | [diff] [blame] | 40 | // This throws out leading zeros for the first item only. |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 41 | if (it == numbers.begin() && UintToString(num) != *it) |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 42 | return false; |
| 43 | |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 44 | // StringToUint returns unsigned int but Version fields are uint32_t. |
| 45 | static_assert(sizeof (uint32_t) == sizeof (unsigned int), |
| 46 | "uint32_t must be same as unsigned int"); |
| 47 | parsed->push_back(num); |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | // Compares version components in |components1| with components in |
[email protected] | 8c71390f | 2013-06-21 05:00:30 | [diff] [blame] | 53 | // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to, |
| 54 | // or greater than |components2|, respectively. |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 55 | int CompareVersionComponents(const std::vector<uint32_t>& components1, |
| 56 | const std::vector<uint32_t>& components2) { |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 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 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 80 | Version::Version() { |
| 81 | } |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 82 | |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 83 | Version::Version(const Version& other) = default; |
| 84 | |
[email protected] | 1513bf8 | 2011-06-07 17:43:20 | [diff] [blame] | 85 | Version::~Version() { |
| 86 | } |
| 87 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 88 | Version::Version(const std::string& version_str) { |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 89 | std::vector<uint32_t> parsed; |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 90 | if (!ParseVersionNumbers(version_str, &parsed)) |
| 91 | return; |
| 92 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 93 | components_.swap(parsed); |
| 94 | } |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 95 | |
staraz | 8fb3808 | 2016-07-25 18:48:21 | [diff] [blame] | 96 | Version::Version(std::vector<uint32_t> components) : components_(components) {} |
| 97 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 98 | bool Version::IsValid() const { |
| 99 | return (!components_.empty()); |
| 100 | } |
| 101 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 102 | // static |
| 103 | bool Version::IsValidWildcardString(const std::string& wildcard_string) { |
| 104 | std::string version_string = wildcard_string; |
brettw | 89365dc | 2015-06-16 05:52:47 | [diff] [blame] | 105 | if (EndsWith(version_string, ".*", CompareCase::SENSITIVE)) |
| 106 | version_string.resize(version_string.size() - 2); |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 107 | |
| 108 | Version version(version_string); |
| 109 | return version.IsValid(); |
| 110 | } |
| 111 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 112 | int Version::CompareToWildcardString(const std::string& wildcard_string) const { |
| 113 | DCHECK(IsValid()); |
| 114 | DCHECK(Version::IsValidWildcardString(wildcard_string)); |
| 115 | |
| 116 | // Default behavior if the string doesn't end with a wildcard. |
brettw | 89365dc | 2015-06-16 05:52:47 | [diff] [blame] | 117 | if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) { |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 118 | Version version(wildcard_string); |
| 119 | DCHECK(version.IsValid()); |
| 120 | return CompareTo(version); |
| 121 | } |
| 122 | |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 123 | std::vector<uint32_t> parsed; |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 124 | const bool success = ParseVersionNumbers( |
| 125 | wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); |
| 126 | DCHECK(success); |
| 127 | const int comparison = CompareVersionComponents(components_, parsed); |
| 128 | // If the version is smaller than the wildcard version's |parsed| vector, |
| 129 | // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the |
| 130 | // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to |
| 131 | // 1.2.2.* is 0 regardless of the wildcard). Under this logic, |
| 132 | // 1.2.0.0.0.0 compared to 1.2.* is 0. |
| 133 | if (comparison == -1 || comparison == 0) |
| 134 | return comparison; |
| 135 | |
| 136 | // Catch the case where the digits of |parsed| are found in |components_|, |
| 137 | // which means that the two are equal since |parsed| has a trailing "*". |
| 138 | // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since |
| 139 | // components is greater (e.g. 3.2.3 vs 1.*). |
| 140 | DCHECK_GT(parsed.size(), 0UL); |
| 141 | const size_t min_num_comp = std::min(components_.size(), parsed.size()); |
| 142 | for (size_t i = 0; i < min_num_comp; ++i) { |
| 143 | if (components_[i] != parsed[i]) |
| 144 | return 1; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 149 | int Version::CompareTo(const Version& other) const { |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 150 | DCHECK(IsValid()); |
| 151 | DCHECK(other.IsValid()); |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 152 | return CompareVersionComponents(components_, other.components_); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | const std::string Version::GetString() const { |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 156 | DCHECK(IsValid()); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 157 | std::string version_str; |
[email protected] | 6dc910c | 2010-11-10 17:02:19 | [diff] [blame] | 158 | size_t count = components_.size(); |
| 159 | for (size_t i = 0; i < count - 1; ++i) { |
ricea | 0dd5057 | 2015-07-27 19:08:46 | [diff] [blame] | 160 | version_str.append(UintToString(components_[i])); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 161 | version_str.append("."); |
| 162 | } |
ricea | 0dd5057 | 2015-07-27 19:08:46 | [diff] [blame] | 163 | version_str.append(UintToString(components_[count - 1])); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 164 | return version_str; |
| 165 | } |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 166 | |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 167 | bool operator==(const Version& v1, const Version& v2) { |
| 168 | return v1.CompareTo(v2) == 0; |
| 169 | } |
| 170 | |
| 171 | bool operator!=(const Version& v1, const Version& v2) { |
| 172 | return !(v1 == v2); |
| 173 | } |
| 174 | |
| 175 | bool operator<(const Version& v1, const Version& v2) { |
| 176 | return v1.CompareTo(v2) < 0; |
| 177 | } |
| 178 | |
| 179 | bool operator<=(const Version& v1, const Version& v2) { |
| 180 | return v1.CompareTo(v2) <= 0; |
| 181 | } |
| 182 | |
| 183 | bool operator>(const Version& v1, const Version& v2) { |
| 184 | return v1.CompareTo(v2) > 0; |
| 185 | } |
| 186 | |
| 187 | bool operator>=(const Version& v1, const Version& v2) { |
| 188 | return v1.CompareTo(v2) >= 0; |
| 189 | } |
| 190 | |
| 191 | std::ostream& operator<<(std::ostream& stream, const Version& v) { |
| 192 | return stream << v.GetString(); |
| 193 | } |
| 194 | |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 195 | } // namespace base |