blob: 3a546076101d8a8003fec1ffac355a7f0cb07251 [file] [log] [blame]
mathp@google.com1088cfd2012-07-05 01:22:48 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
erikkay@google.come05e3c62009-01-30 04:18:57 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
erikkay@google.come05e3c62009-01-30 04:18:57 +09005#include "base/version.h"
6
mostynb@opera.comf29dd612013-09-04 08:29:12 +09007#include <stddef.h>
8
jkummerow@chromium.orgdf3e6712010-12-21 17:27:25 +09009#include <algorithm>
10
akalin@chromium.org2659ef12010-03-26 07:19:04 +090011#include "base/logging.h"
brettw@chromium.orgabcde5c2013-02-07 11:57:22 +090012#include "base/strings/string_number_conversions.h"
tfarina@chromium.org63aaf3f2013-02-08 08:01:39 +090013#include "base/strings/string_split.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090014#include "base/strings/string_util.h"
akalin@chromium.org2659ef12010-03-26 07:19:04 +090015
xhwang@chromium.org993e5062013-04-22 16:35:50 +090016namespace base {
17
mathp@google.com1088cfd2012-07-05 01:22:48 +090018namespace {
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,
wfh79716bc2015-03-10 10:32:59 +090026 std::vector<uint32_t>* parsed) {
brettwe1daa972015-06-16 14:52:47 +090027 std::vector<StringPiece> numbers =
28 SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL);
mathp@google.com1088cfd2012-07-05 01:22:48 +090029 if (numbers.empty())
30 return false;
31
brettwe1daa972015-06-16 14:52:47 +090032 for (auto it = numbers.begin(); it != numbers.end(); ++it) {
33 if (StartsWith(*it, "+", CompareCase::SENSITIVE))
wfhb3f3e4d2015-02-22 14:41:39 +090034 return false;
brettwe1daa972015-06-16 14:52:47 +090035
wfh79716bc2015-03-10 10:32:59 +090036 unsigned int num;
37 if (!StringToUint(*it, &num))
mathp@google.com1088cfd2012-07-05 01:22:48 +090038 return false;
39
wfhb3f3e4d2015-02-22 14:41:39 +090040 // This throws out leading zeros for the first item only.
wfh79716bc2015-03-10 10:32:59 +090041 if (it == numbers.begin() && UintToString(num) != *it)
mathp@google.com1088cfd2012-07-05 01:22:48 +090042 return false;
43
wfh79716bc2015-03-10 10:32:59 +090044 // 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);
mathp@google.com1088cfd2012-07-05 01:22:48 +090048 }
49 return true;
50}
51
52// Compares version components in |components1| with components in
waffles@chromium.orgbe672112013-06-21 14:00:30 +090053// |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
54// or greater than |components2|, respectively.
wfh79716bc2015-03-10 10:32:59 +090055int CompareVersionComponents(const std::vector<uint32_t>& components1,
56 const std::vector<uint32_t>& components2) {
mathp@google.com1088cfd2012-07-05 01:22:48 +090057 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 Watkinsd155d9f2017-11-29 16:16:38 +090080Version::Version() = default;
erg@google.com67a25432011-01-08 05:23:43 +090081
vmpstrc8651bf2016-02-25 09:50:31 +090082Version::Version(const Version& other) = default;
83
Chris Watkinsd155d9f2017-11-29 16:16:38 +090084Version::~Version() = default;
cpu@chromium.org331e27b2011-06-08 02:43:20 +090085
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090086Version::Version(const std::string& version_str) {
wfh79716bc2015-03-10 10:32:59 +090087 std::vector<uint32_t> parsed;
mathp@google.com1088cfd2012-07-05 01:22:48 +090088 if (!ParseVersionNumbers(version_str, &parsed))
89 return;
90
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090091 components_.swap(parsed);
92}
erg@google.com67a25432011-01-08 05:23:43 +090093
Daniel Cheng3b069f72018-04-23 19:34:55 +090094Version::Version(std::vector<uint32_t> components)
95 : components_(std::move(components)) {}
staraz810fac52016-07-26 03:48:21 +090096
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090097bool Version::IsValid() const {
98 return (!components_.empty());
99}
100
mathp@google.com1088cfd2012-07-05 01:22:48 +0900101// static
102bool Version::IsValidWildcardString(const std::string& wildcard_string) {
103 std::string version_string = wildcard_string;
brettwe1daa972015-06-16 14:52:47 +0900104 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE))
105 version_string.resize(version_string.size() - 2);
mathp@google.com1088cfd2012-07-05 01:22:48 +0900106
107 Version version(version_string);
108 return version.IsValid();
109}
110
mathp@google.com1088cfd2012-07-05 01:22:48 +0900111int Version::CompareToWildcardString(const std::string& wildcard_string) const {
112 DCHECK(IsValid());
113 DCHECK(Version::IsValidWildcardString(wildcard_string));
114
115 // Default behavior if the string doesn't end with a wildcard.
brettwe1daa972015-06-16 14:52:47 +0900116 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
mathp@google.com1088cfd2012-07-05 01:22:48 +0900117 Version version(wildcard_string);
118 DCHECK(version.IsValid());
119 return CompareTo(version);
120 }
121
wfh79716bc2015-03-10 10:32:59 +0900122 std::vector<uint32_t> parsed;
mathp@google.com1088cfd2012-07-05 01:22:48 +0900123 const bool success = ParseVersionNumbers(
124 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
125 DCHECK(success);
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)
133 return comparison;
134
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])
143 return 1;
144 }
145 return 0;
146}
147
erikkay@google.come05e3c62009-01-30 04:18:57 +0900148int Version::CompareTo(const Version& other) const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +0900149 DCHECK(IsValid());
150 DCHECK(other.IsValid());
mathp@google.com1088cfd2012-07-05 01:22:48 +0900151 return CompareVersionComponents(components_, other.components_);
erikkay@google.come05e3c62009-01-30 04:18:57 +0900152}
153
154const std::string Version::GetString() const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +0900155 DCHECK(IsValid());
erikkay@google.come05e3c62009-01-30 04:18:57 +0900156 std::string version_str;
robertshield@chromium.orga9914a32010-11-11 02:02:19 +0900157 size_t count = components_.size();
158 for (size_t i = 0; i < count - 1; ++i) {
riceacb27fe02015-07-28 04:08:46 +0900159 version_str.append(UintToString(components_[i]));
erikkay@google.come05e3c62009-01-30 04:18:57 +0900160 version_str.append(".");
161 }
riceacb27fe02015-07-28 04:08:46 +0900162 version_str.append(UintToString(components_[count - 1]));
erikkay@google.come05e3c62009-01-30 04:18:57 +0900163 return version_str;
164}
xhwang@chromium.org993e5062013-04-22 16:35:50 +0900165
robpercivalc43ce172016-01-26 04:39:00 +0900166bool operator==(const Version& v1, const Version& v2) {
167 return v1.CompareTo(v2) == 0;
168}
169
170bool operator!=(const Version& v1, const Version& v2) {
171 return !(v1 == v2);
172}
173
174bool operator<(const Version& v1, const Version& v2) {
175 return v1.CompareTo(v2) < 0;
176}
177
178bool operator<=(const Version& v1, const Version& v2) {
179 return v1.CompareTo(v2) <= 0;
180}
181
182bool operator>(const Version& v1, const Version& v2) {
183 return v1.CompareTo(v2) > 0;
184}
185
186bool operator>=(const Version& v1, const Version& v2) {
187 return v1.CompareTo(v2) >= 0;
188}
189
190std::ostream& operator<<(std::ostream& stream, const Version& v) {
191 return stream << v.GetString();
192}
193
xhwang@chromium.org993e5062013-04-22 16:35:50 +0900194} // namespace base