blob: 228dcb8aef20e4a33d9dd03269ceaa5195ba1a32 [file] [log] [blame]
[email protected]810b2502012-07-04 16:22:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]19b8d822009-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]19b8d822009-01-29 19:18:575#include "base/version.h"
6
[email protected]c014f2b2013-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]5ae0b762013-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]810b2502012-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,
wfhbf68f4d2015-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]810b2502012-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
36 // TODO(brettw) when we have a StringPiece version of StringToUint, delete
37 // this string conversion.
wfhbf68f4d2015-03-10 01:32:5938 unsigned int num;
39 if (!StringToUint(*it, &num))
[email protected]810b2502012-07-04 16:22:4840 return false;
41
wfhf1512492015-02-22 05:41:3942 // This throws out leading zeros for the first item only.
wfhbf68f4d2015-03-10 01:32:5943 if (it == numbers.begin() && UintToString(num) != *it)
[email protected]810b2502012-07-04 16:22:4844 return false;
45
wfhbf68f4d2015-03-10 01:32:5946 // StringToUint returns unsigned int but Version fields are uint32_t.
47 static_assert(sizeof (uint32_t) == sizeof (unsigned int),
48 "uint32_t must be same as unsigned int");
49 parsed->push_back(num);
[email protected]810b2502012-07-04 16:22:4850 }
51 return true;
52}
53
54// Compares version components in |components1| with components in
[email protected]8c713902013-06-21 05:00:3055// |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
56// or greater than |components2|, respectively.
wfhbf68f4d2015-03-10 01:32:5957int CompareVersionComponents(const std::vector<uint32_t>& components1,
58 const std::vector<uint32_t>& components2) {
[email protected]810b2502012-07-04 16:22:4859 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
[email protected]76002472011-06-07 17:21:3082Version::Version() {
83}
[email protected]9989c9b2011-01-07 20:23:4384
[email protected]1513bf82011-06-07 17:43:2085Version::~Version() {
86}
87
[email protected]76002472011-06-07 17:21:3088Version::Version(const std::string& version_str) {
wfhbf68f4d2015-03-10 01:32:5989 std::vector<uint32_t> parsed;
[email protected]810b2502012-07-04 16:22:4890 if (!ParseVersionNumbers(version_str, &parsed))
91 return;
92
[email protected]76002472011-06-07 17:21:3093 components_.swap(parsed);
94}
[email protected]9989c9b2011-01-07 20:23:4395
[email protected]76002472011-06-07 17:21:3096bool Version::IsValid() const {
97 return (!components_.empty());
98}
99
[email protected]810b2502012-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]810b2502012-07-04 16:22:48105
106 Version version(version_string);
107 return version.IsValid();
108}
109
[email protected]30c157c2011-08-01 17:45:08110bool 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
[email protected]810b2502012-07-04 16:22:48117int 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.
brettw89365dc2015-06-16 05:52:47122 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
[email protected]810b2502012-07-04 16:22:48123 Version version(wildcard_string);
124 DCHECK(version.IsValid());
125 return CompareTo(version);
126 }
127
wfhbf68f4d2015-03-10 01:32:59128 std::vector<uint32_t> parsed;
[email protected]810b2502012-07-04 16:22:48129 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
[email protected]19b8d822009-01-29 19:18:57154bool Version::Equals(const Version& that) const {
[email protected]76002472011-06-07 17:21:30155 DCHECK(IsValid());
156 DCHECK(that.IsValid());
157 return (CompareTo(that) == 0);
[email protected]19b8d822009-01-29 19:18:57158}
159
160int Version::CompareTo(const Version& other) const {
[email protected]76002472011-06-07 17:21:30161 DCHECK(IsValid());
162 DCHECK(other.IsValid());
[email protected]810b2502012-07-04 16:22:48163 return CompareVersionComponents(components_, other.components_);
[email protected]19b8d822009-01-29 19:18:57164}
165
166const std::string Version::GetString() const {
[email protected]76002472011-06-07 17:21:30167 DCHECK(IsValid());
[email protected]19b8d822009-01-29 19:18:57168 std::string version_str;
[email protected]6dc910c2010-11-10 17:02:19169 size_t count = components_.size();
170 for (size_t i = 0; i < count - 1; ++i) {
[email protected]1f04ef42013-04-22 07:35:50171 version_str.append(IntToString(components_[i]));
[email protected]19b8d822009-01-29 19:18:57172 version_str.append(".");
173 }
[email protected]1f04ef42013-04-22 07:35:50174 version_str.append(IntToString(components_[count - 1]));
[email protected]19b8d822009-01-29 19:18:57175 return version_str;
176}
[email protected]1f04ef42013-04-22 07:35:50177
178} // namespace base