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.
5 #include "base/version.h"
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"
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);
32 for (auto it = numbers.begin(); it != numbers.end(); ++it) {
33 if (StartsWith(*it, "+", CompareCase::SENSITIVE))
37 if (!StringToUint(*it, &num))
40 // This throws out leading zeros for the first item only.
41 if (it == numbers.begin() && UintToString(num) != *it)
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);
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])
61 if (components1[i] < components2[i])
64 if (components1.size() > components2.size()) {
65 for (size_t i = count; i < components1.size(); ++i) {
66 if (components1[i] > 0)
69 } else if (components1.size() < components2.size()) {
70 for (size_t i = count; i < components2.size(); ++i) {
71 if (components2[i] > 0)
80 Version::Version() = default;
82 Version::Version(const Version& other) = default;
84 Version::~Version() = default;
86 Version::Version(const std::string& version_str) {
87 std::vector<uint32_t> parsed;
88 if (!ParseVersionNumbers(version_str, &parsed))
91 components_.swap(parsed);
94 Version::Version(std::vector<uint32_t> components)
95 : components_(std::move(components)) {}
97 bool Version::IsValid() const {
98 return (!components_.empty());
102 bool Version::IsValidWildcardString(const std::string& wildcard_string) {
103 std::string version_string = wildcard_string;
104 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE))
105 version_string.resize(version_string.size() - 2);
107 Version version(version_string);
108 return version.IsValid();
111 int Version::CompareToWildcardString(const std::string& wildcard_string) const {
113 DCHECK(Version::IsValidWildcardString(wildcard_string));
115 // Default behavior if the string doesn't end with a wildcard.
116 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
117 Version version(wildcard_string);
118 DCHECK(version.IsValid());
119 return CompareTo(version);
122 std::vector<uint32_t> parsed;
123 const bool success = ParseVersionNumbers(
124 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
126 const int comparison = CompareVersionComponents(components_, parsed);
127 // If the version is smaller than the wildcard version's |parsed| vector,
128 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the
129 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to
130 // 1.2.2.* is 0 regardless of the wildcard). Under this logic,
131 // 1.2.0.0.0.0 compared to 1.2.* is 0.
132 if (comparison == -1 || comparison == 0)
135 // Catch the case where the digits of |parsed| are found in |components_|,
136 // which means that the two are equal since |parsed| has a trailing "*".
137 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since
138 // components is greater (e.g. 3.2.3 vs 1.*).
139 DCHECK_GT(parsed.size(), 0UL);
140 const size_t min_num_comp = std::min(components_.size(), parsed.size());
141 for (size_t i = 0; i < min_num_comp; ++i) {
142 if (components_[i] != parsed[i])
148 int Version::CompareTo(const Version& other) const {
150 DCHECK(other.IsValid());
151 return CompareVersionComponents(components_, other.components_);
154 const std::string Version::GetString() const {
156 std::string version_str;
157 size_t count = components_.size();
158 for (size_t i = 0; i < count - 1; ++i) {
159 version_str.append(UintToString(components_[i]));
160 version_str.append(".");
162 version_str.append(UintToString(components_[count - 1]));
166 bool operator==(const Version& v1, const Version& v2) {
167 return v1.CompareTo(v2) == 0;
170 bool operator!=(const Version& v1, const Version& v2) {
174 bool operator<(const Version& v1, const Version& v2) {
175 return v1.CompareTo(v2) < 0;
178 bool operator<=(const Version& v1, const Version& v2) {
179 return v1.CompareTo(v2) <= 0;
182 bool operator>(const Version& v1, const Version& v2) {
183 return v1.CompareTo(v2) > 0;
186 bool operator>=(const Version& v1, const Version& v2) {
187 return v1.CompareTo(v2) >= 0;
190 std::ostream& operator<<(std::ostream& stream, const Version& v) {
191 return stream << v.GetString();