1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/version.h" 6 7 #include <stddef.h> 8 9 #include <algorithm> 10 11 #include "base/logging.h" 12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_split.h" 14 #include "base/strings/string_util.h" 15 16 namespace base { 17 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, 26 std::vector<uint32_t>* parsed) { 27 std::vector<StringPiece> numbers = 28 SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL); 29 if (numbers.empty()) 30 return false; 31 32 for (auto it = numbers.begin(); it != numbers.end(); ++it) { 33 if (StartsWith(*it, "+", CompareCase::SENSITIVE)) 34 return false; 35 36 unsigned int num; 37 if (!StringToUint(*it, &num)) 38 return false; 39 40 // This throws out leading zeros for the first item only. 41 if (it == numbers.begin() && UintToString(num) != *it) 42 return false; 43 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); 48 } 49 return true; 50 } 51 52 // Compares version components in |components1| with components in 53 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to, 54 // or greater than |components2|, respectively. 55 int CompareVersionComponents(const std::vector<uint32_t>& components1, 56 const std::vector<uint32_t>& 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 80 Version::Version() { 81 } 82 83 Version::Version(const Version& other) = default; 84 85 Version::~Version() { 86 } 87 88 Version::Version(const std::string& version_str) { 89 std::vector<uint32_t> parsed; 90 if (!ParseVersionNumbers(version_str, &parsed)) 91 return; 92 93 components_.swap(parsed); 94 } 95 96 bool Version::IsValid() const { 97 return (!components_.empty()); 98 } 99 100 // static 101 bool Version::IsValidWildcardString(const std::string& wildcard_string) { 102 std::string version_string = wildcard_string; 103 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE)) 104 version_string.resize(version_string.size() - 2); 105 106 Version version(version_string); 107 return version.IsValid(); 108 } 109 110 int Version::CompareToWildcardString(const std::string& wildcard_string) const { 111 DCHECK(IsValid()); 112 DCHECK(Version::IsValidWildcardString(wildcard_string)); 113 114 // Default behavior if the string doesn't end with a wildcard. 115 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) { 116 Version version(wildcard_string); 117 DCHECK(version.IsValid()); 118 return CompareTo(version); 119 } 120 121 std::vector<uint32_t> parsed; 122 const bool success = ParseVersionNumbers( 123 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); 124 DCHECK(success); 125 const int comparison = CompareVersionComponents(components_, parsed); 126 // If the version is smaller than the wildcard version's |parsed| vector, 127 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the 128 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to 129 // 1.2.2.* is 0 regardless of the wildcard). Under this logic, 130 // 1.2.0.0.0.0 compared to 1.2.* is 0. 131 if (comparison == -1 || comparison == 0) 132 return comparison; 133 134 // Catch the case where the digits of |parsed| are found in |components_|, 135 // which means that the two are equal since |parsed| has a trailing "*". 136 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since 137 // components is greater (e.g. 3.2.3 vs 1.*). 138 DCHECK_GT(parsed.size(), 0UL); 139 const size_t min_num_comp = std::min(components_.size(), parsed.size()); 140 for (size_t i = 0; i < min_num_comp; ++i) { 141 if (components_[i] != parsed[i]) 142 return 1; 143 } 144 return 0; 145 } 146 147 int Version::CompareTo(const Version& other) const { 148 DCHECK(IsValid()); 149 DCHECK(other.IsValid()); 150 return CompareVersionComponents(components_, other.components_); 151 } 152 153 const std::string Version::GetString() const { 154 DCHECK(IsValid()); 155 std::string version_str; 156 size_t count = components_.size(); 157 for (size_t i = 0; i < count - 1; ++i) { 158 version_str.append(UintToString(components_[i])); 159 version_str.append("."); 160 } 161 version_str.append(UintToString(components_[count - 1])); 162 return version_str; 163 } 164 165 bool operator==(const Version& v1, const Version& v2) { 166 return v1.CompareTo(v2) == 0; 167 } 168 169 bool operator!=(const Version& v1, const Version& v2) { 170 return !(v1 == v2); 171 } 172 173 bool operator<(const Version& v1, const Version& v2) { 174 return v1.CompareTo(v2) < 0; 175 } 176 177 bool operator<=(const Version& v1, const Version& v2) { 178 return v1.CompareTo(v2) <= 0; 179 } 180 181 bool operator>(const Version& v1, const Version& v2) { 182 return v1.CompareTo(v2) > 0; 183 } 184 185 bool operator>=(const Version& v1, const Version& v2) { 186 return v1.CompareTo(v2) >= 0; 187 } 188 189 std::ostream& operator<<(std::ostream& stream, const Version& v) { 190 return stream << v.GetString(); 191 } 192 193 } // namespace base 194