blob: 6318b350edcab328feb593a029534ae50b5b84b0 [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,
26 std::vector<uint16>* parsed) {
27 std::vector<std::string> numbers;
xhwang@chromium.org993e5062013-04-22 16:35:50 +090028 SplitString(version_str, '.', &numbers);
mathp@google.com1088cfd2012-07-05 01:22:48 +090029 if (numbers.empty())
30 return false;
31
32 for (std::vector<std::string>::const_iterator it = numbers.begin();
33 it != numbers.end(); ++it) {
34 int num;
xhwang@chromium.org993e5062013-04-22 16:35:50 +090035 if (!StringToInt(*it, &num))
mathp@google.com1088cfd2012-07-05 01:22:48 +090036 return false;
37
38 if (num < 0)
39 return false;
40
41 const uint16 max = 0xFFFF;
42 if (num > max)
43 return false;
44
45 // This throws out things like +3, or 032.
xhwang@chromium.org993e5062013-04-22 16:35:50 +090046 if (IntToString(num) != *it)
mathp@google.com1088cfd2012-07-05 01:22:48 +090047 return false;
48
49 parsed->push_back(static_cast<uint16>(num));
50 }
51 return true;
52}
53
54// Compares version components in |components1| with components in
waffles@chromium.orgbe672112013-06-21 14:00:30 +090055// |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
56// or greater than |components2|, respectively.
mathp@google.com1088cfd2012-07-05 01:22:48 +090057int CompareVersionComponents(const std::vector<uint16>& components1,
58 const std::vector<uint16>& components2) {
59 const size_t count = std::min(components1.size(), components2.size());
60 for (size_t i = 0; i < count; ++i) {
61 if (components1[i] > components2[i])
62 return 1;
63 if (components1[i] < components2[i])
64 return -1;
65 }
66 if (components1.size() > components2.size()) {
67 for (size_t i = count; i < components1.size(); ++i) {
68 if (components1[i] > 0)
69 return 1;
70 }
71 } else if (components1.size() < components2.size()) {
72 for (size_t i = count; i < components2.size(); ++i) {
73 if (components2[i] > 0)
74 return -1;
75 }
76 }
77 return 0;
78}
79
80} // namespace
81
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090082Version::Version() {
83}
erg@google.com67a25432011-01-08 05:23:43 +090084
cpu@chromium.org331e27b2011-06-08 02:43:20 +090085Version::~Version() {
86}
87
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090088Version::Version(const std::string& version_str) {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090089 std::vector<uint16> parsed;
mathp@google.com1088cfd2012-07-05 01:22:48 +090090 if (!ParseVersionNumbers(version_str, &parsed))
91 return;
92
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090093 components_.swap(parsed);
94}
erg@google.com67a25432011-01-08 05:23:43 +090095
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090096bool Version::IsValid() const {
97 return (!components_.empty());
98}
99
mathp@google.com1088cfd2012-07-05 01:22:48 +0900100// static
101bool Version::IsValidWildcardString(const std::string& wildcard_string) {
102 std::string version_string = wildcard_string;
103 if (EndsWith(wildcard_string.c_str(), ".*", false))
104 version_string = wildcard_string.substr(0, wildcard_string.size() - 2);
105
106 Version version(version_string);
107 return version.IsValid();
108}
109
cpu@chromium.org9008a612011-08-02 02:45:08 +0900110bool Version::IsOlderThan(const std::string& version_str) const {
111 Version proposed_ver(version_str);
112 if (!proposed_ver.IsValid())
113 return false;
114 return (CompareTo(proposed_ver) < 0);
115}
116
mathp@google.com1088cfd2012-07-05 01:22:48 +0900117int Version::CompareToWildcardString(const std::string& wildcard_string) const {
118 DCHECK(IsValid());
119 DCHECK(Version::IsValidWildcardString(wildcard_string));
120
121 // Default behavior if the string doesn't end with a wildcard.
122 if (!EndsWith(wildcard_string.c_str(), ".*", false)) {
123 Version version(wildcard_string);
124 DCHECK(version.IsValid());
125 return CompareTo(version);
126 }
127
128 std::vector<uint16> parsed;
129 const bool success = ParseVersionNumbers(
130 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
131 DCHECK(success);
132 const int comparison = CompareVersionComponents(components_, parsed);
133 // If the version is smaller than the wildcard version's |parsed| vector,
134 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the
135 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to
136 // 1.2.2.* is 0 regardless of the wildcard). Under this logic,
137 // 1.2.0.0.0.0 compared to 1.2.* is 0.
138 if (comparison == -1 || comparison == 0)
139 return comparison;
140
141 // Catch the case where the digits of |parsed| are found in |components_|,
142 // which means that the two are equal since |parsed| has a trailing "*".
143 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since
144 // components is greater (e.g. 3.2.3 vs 1.*).
145 DCHECK_GT(parsed.size(), 0UL);
146 const size_t min_num_comp = std::min(components_.size(), parsed.size());
147 for (size_t i = 0; i < min_num_comp; ++i) {
148 if (components_[i] != parsed[i])
149 return 1;
150 }
151 return 0;
152}
153
erikkay@google.come05e3c62009-01-30 04:18:57 +0900154bool Version::Equals(const Version& that) const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +0900155 DCHECK(IsValid());
156 DCHECK(that.IsValid());
157 return (CompareTo(that) == 0);
erikkay@google.come05e3c62009-01-30 04:18:57 +0900158}
159
160int Version::CompareTo(const Version& other) const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +0900161 DCHECK(IsValid());
162 DCHECK(other.IsValid());
mathp@google.com1088cfd2012-07-05 01:22:48 +0900163 return CompareVersionComponents(components_, other.components_);
erikkay@google.come05e3c62009-01-30 04:18:57 +0900164}
165
166const std::string Version::GetString() const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +0900167 DCHECK(IsValid());
erikkay@google.come05e3c62009-01-30 04:18:57 +0900168 std::string version_str;
robertshield@chromium.orga9914a32010-11-11 02:02:19 +0900169 size_t count = components_.size();
170 for (size_t i = 0; i < count - 1; ++i) {
xhwang@chromium.org993e5062013-04-22 16:35:50 +0900171 version_str.append(IntToString(components_[i]));
erikkay@google.come05e3c62009-01-30 04:18:57 +0900172 version_str.append(".");
173 }
xhwang@chromium.org993e5062013-04-22 16:35:50 +0900174 version_str.append(IntToString(components_[count - 1]));
erikkay@google.come05e3c62009-01-30 04:18:57 +0900175 return version_str;
176}
xhwang@chromium.org993e5062013-04-22 16:35:50 +0900177
178} // namespace base