blob: 7e897b238aaa15121b7d98f89cd34768bfc3c4dd [file] [log] [blame]
[email protected]810b25082012-07-04 16:22:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]19b8d82f2009-01-29 19:18:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]19b8d82f2009-01-29 19:18:575#include "base/version.h"
6
[email protected]c014f2b32013-09-03 23:29:127#include <stddef.h>
8
[email protected]b566c112010-12-21 08:27:259#include <algorithm>
10
[email protected]26931bc2010-03-25 22:19:0411#include "base/logging.h"
[email protected]dfa049e2013-02-07 02:57:2212#include "base/strings/string_number_conversions.h"
[email protected]5ae0b763e2013-02-07 23:01:3913#include "base/strings/string_split.h"
[email protected]c851cfd2013-06-10 20:11:1414#include "base/strings/string_util.h"
[email protected]26931bc2010-03-25 22:19:0415
[email protected]1f04ef42013-04-22 07:35:5016namespace base {
17
[email protected]810b25082012-07-04 16:22:4818namespace {
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.
25bool ParseVersionNumbers(const std::string& version_str,
wfhbf68f4d5b2015-03-10 01:32:5926 std::vector<uint32_t>* parsed) {
brettw89365dc2015-06-16 05:52:4727 std::vector<StringPiece> numbers =
28 SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL);
[email protected]810b25082012-07-04 16:22:4829 if (numbers.empty())
30 return false;
31
brettw89365dc2015-06-16 05:52:4732 for (auto it = numbers.begin(); it != numbers.end(); ++it) {
33 if (StartsWith(*it, "+", CompareCase::SENSITIVE))
wfhf1512492015-02-22 05:41:3934 return false;
brettw89365dc2015-06-16 05:52:4735
wfhbf68f4d5b2015-03-10 01:32:5936 unsigned int num;
37 if (!StringToUint(*it, &num))
[email protected]810b25082012-07-04 16:22:4838 return false;
39
wfhf1512492015-02-22 05:41:3940 // This throws out leading zeros for the first item only.
wfhbf68f4d5b2015-03-10 01:32:5941 if (it == numbers.begin() && UintToString(num) != *it)
[email protected]810b25082012-07-04 16:22:4842 return false;
43
wfhbf68f4d5b2015-03-10 01:32:5944 // 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]810b25082012-07-04 16:22:4848 }
49 return true;
50}
51
52// Compares version components in |components1| with components in
[email protected]8c71390f2013-06-21 05:00:3053// |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
54// or greater than |components2|, respectively.
wfhbf68f4d5b2015-03-10 01:32:5955int CompareVersionComponents(const std::vector<uint32_t>& components1,
56 const std::vector<uint32_t>& components2) {
[email protected]810b25082012-07-04 16:22:4857 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
Chris Watkinsbb7211c2017-11-29 07:16:3880Version::Version() = default;
[email protected]9989c9bb2011-01-07 20:23:4381
vmpstre65942b2016-02-25 00:50:3182Version::Version(const Version& other) = default;
83
Chris Watkinsbb7211c2017-11-29 07:16:3884Version::~Version() = default;
[email protected]1513bf82011-06-07 17:43:2085
[email protected]760024782011-06-07 17:21:3086Version::Version(const std::string& version_str) {
wfhbf68f4d5b2015-03-10 01:32:5987 std::vector<uint32_t> parsed;
[email protected]810b25082012-07-04 16:22:4888 if (!ParseVersionNumbers(version_str, &parsed))
89 return;
90
[email protected]760024782011-06-07 17:21:3091 components_.swap(parsed);
92}
[email protected]9989c9bb2011-01-07 20:23:4393
staraz8fb38082016-07-25 18:48:2194Version::Version(std::vector<uint32_t> components) : components_(components) {}
95
[email protected]760024782011-06-07 17:21:3096bool Version::IsValid() const {
97 return (!components_.empty());
98}
99
[email protected]810b25082012-07-04 16:22:48100// static
101bool Version::IsValidWildcardString(const std::string& wildcard_string) {
102 std::string version_string = wildcard_string;
brettw89365dc2015-06-16 05:52:47103 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE))
104 version_string.resize(version_string.size() - 2);
[email protected]810b25082012-07-04 16:22:48105
106 Version version(version_string);
107 return version.IsValid();
108}
109
[email protected]810b25082012-07-04 16:22:48110int 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.
brettw89365dc2015-06-16 05:52:47115 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
[email protected]810b25082012-07-04 16:22:48116 Version version(wildcard_string);
117 DCHECK(version.IsValid());
118 return CompareTo(version);
119 }
120
wfhbf68f4d5b2015-03-10 01:32:59121 std::vector<uint32_t> parsed;
[email protected]810b25082012-07-04 16:22:48122 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
[email protected]19b8d82f2009-01-29 19:18:57147int Version::CompareTo(const Version& other) const {
[email protected]760024782011-06-07 17:21:30148 DCHECK(IsValid());
149 DCHECK(other.IsValid());
[email protected]810b25082012-07-04 16:22:48150 return CompareVersionComponents(components_, other.components_);
[email protected]19b8d82f2009-01-29 19:18:57151}
152
153const std::string Version::GetString() const {
[email protected]760024782011-06-07 17:21:30154 DCHECK(IsValid());
[email protected]19b8d82f2009-01-29 19:18:57155 std::string version_str;
[email protected]6dc910c2010-11-10 17:02:19156 size_t count = components_.size();
157 for (size_t i = 0; i < count - 1; ++i) {
ricea0dd50572015-07-27 19:08:46158 version_str.append(UintToString(components_[i]));
[email protected]19b8d82f2009-01-29 19:18:57159 version_str.append(".");
160 }
ricea0dd50572015-07-27 19:08:46161 version_str.append(UintToString(components_[count - 1]));
[email protected]19b8d82f2009-01-29 19:18:57162 return version_str;
163}
[email protected]1f04ef42013-04-22 07:35:50164
robpercivaldcd8b102016-01-25 19:39:00165bool operator==(const Version& v1, const Version& v2) {
166 return v1.CompareTo(v2) == 0;
167}
168
169bool operator!=(const Version& v1, const Version& v2) {
170 return !(v1 == v2);
171}
172
173bool operator<(const Version& v1, const Version& v2) {
174 return v1.CompareTo(v2) < 0;
175}
176
177bool operator<=(const Version& v1, const Version& v2) {
178 return v1.CompareTo(v2) <= 0;
179}
180
181bool operator>(const Version& v1, const Version& v2) {
182 return v1.CompareTo(v2) > 0;
183}
184
185bool operator>=(const Version& v1, const Version& v2) {
186 return v1.CompareTo(v2) >= 0;
187}
188
189std::ostream& operator<<(std::ostream& stream, const Version& v) {
190 return stream << v.GetString();
191}
192
[email protected]1f04ef42013-04-22 07:35:50193} // namespace base