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"
12 #include "base/check_op.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
21 // Parses the |numbers| vector representing the different numbers
22 // inside the version string and constructs a vector of valid integers. It stops
23 // when it reaches an invalid item (including the wildcard character). |parsed|
24 // is the resulting integer vector. Function returns true if all numbers were
25 // parsed successfully, false otherwise.
26 bool ParseVersionNumbers(StringPiece version_str,
27 std::vector<uint32_t>* parsed) {
28 std::vector<StringPiece> numbers =
29 SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL);
33 for (auto it = numbers.begin(); it != numbers.end(); ++it) {
34 if (StartsWith(*it, "+", CompareCase::SENSITIVE))
38 if (!StringToUint(*it, &num))
41 // This throws out leading zeros for the first item only.
42 if (it == numbers.begin() && NumberToString(num) != *it)
45 // StringToUint returns unsigned int but Version fields are uint32_t.
46 static_assert(sizeof (uint32_t) == sizeof (unsigned int),
47 "uint32_t must be same as unsigned int");
48 parsed->push_back(num);
53 // Compares version components in |components1| with components in
54 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
55 // or greater than |components2|, respectively.
56 int CompareVersionComponents(const std::vector<uint32_t>& components1,
57 const std::vector<uint32_t>& components2) {
58 const size_t count = std::min(components1.size(), components2.size());
59 for (size_t i = 0; i < count; ++i) {
60 if (components1[i] > components2[i])
62 if (components1[i] < components2[i])
65 if (components1.size() > components2.size()) {
66 for (size_t i = count; i < components1.size(); ++i) {
67 if (components1[i] > 0)
70 } else if (components1.size() < components2.size()) {
71 for (size_t i = count; i < components2.size(); ++i) {
72 if (components2[i] > 0)
81 Version::Version() = default;
83 Version::Version(const Version& other) = default;
85 Version::~Version() = default;
87 Version::Version(StringPiece version_str) {
88 std::vector<uint32_t> parsed;
89 if (!ParseVersionNumbers(version_str, &parsed))
92 components_.swap(parsed);
95 Version::Version(std::vector<uint32_t> components)
96 : components_(std::move(components)) {}
98 bool Version::IsValid() const {
99 return (!components_.empty());
103 bool Version::IsValidWildcardString(StringPiece wildcard_string) {
104 StringPiece version_string = wildcard_string;
105 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE))
106 version_string = version_string.substr(0, version_string.size() - 2);
108 Version version(version_string);
109 return version.IsValid();
112 int Version::CompareToWildcardString(StringPiece wildcard_string) const {
114 DCHECK(Version::IsValidWildcardString(wildcard_string));
116 // Default behavior if the string doesn't end with a wildcard.
117 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
118 Version version(wildcard_string);
119 DCHECK(version.IsValid());
120 return CompareTo(version);
123 std::vector<uint32_t> parsed;
124 const bool success = ParseVersionNumbers(
125 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
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)
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])
149 int Version::CompareTo(const Version& other) const {
151 DCHECK(other.IsValid());
152 return CompareVersionComponents(components_, other.components_);
155 std::string Version::GetString() const {
157 std::string version_str;
158 size_t count = components_.size();
159 for (size_t i = 0; i < count - 1; ++i) {
160 version_str.append(NumberToString(components_[i]));
161 version_str.append(".");
163 version_str.append(NumberToString(components_[count - 1]));
167 bool operator==(const Version& v1, const Version& v2) {
168 return v1.CompareTo(v2) == 0;
171 bool operator!=(const Version& v1, const Version& v2) {
175 bool operator<(const Version& v1, const Version& v2) {
176 return v1.CompareTo(v2) < 0;
179 bool operator<=(const Version& v1, const Version& v2) {
180 return v1.CompareTo(v2) <= 0;
183 bool operator>(const Version& v1, const Version& v2) {
184 return v1.CompareTo(v2) > 0;
187 bool operator>=(const Version& v1, const Version& v2) {
188 return v1.CompareTo(v2) >= 0;
191 std::ostream& operator<<(std::ostream& stream, const Version& v) {
192 return stream << v.GetString();