Include inttypes.h unconditionally in format_macros.h - VS2103 now supplies this...
[chromium-blink-merge.git] / base / version.cc
blobede8a4586eb9bb8685b05a00939772cd7eaaefdb
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"
7 #include <stddef.h>
9 #include <algorithm>
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
16 namespace base {
18 namespace {
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.
25 bool ParseVersionNumbers(const std::string& version_str,
26 std::vector<uint32_t>* parsed) {
27 std::vector<std::string> numbers;
28 SplitString(version_str, '.', &numbers);
29 if (numbers.empty())
30 return false;
32 for (std::vector<std::string>::const_iterator it = numbers.begin();
33 it != numbers.end(); ++it) {
34 if (StartsWithASCII(*it, "+", false))
35 return false;
36 unsigned int num;
37 if (!StringToUint(*it, &num))
38 return false;
40 // This throws out leading zeros for the first item only.
41 if (it == numbers.begin() && UintToString(num) != *it)
42 return false;
44 // 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);
49 return true;
52 // Compares version components in |components1| with components in
53 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
54 // or greater than |components2|, respectively.
55 int CompareVersionComponents(const std::vector<uint32_t>& components1,
56 const std::vector<uint32_t>& components2) {
57 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;
64 if (components1.size() > components2.size()) {
65 for (size_t i = count; i < components1.size(); ++i) {
66 if (components1[i] > 0)
67 return 1;
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;
75 return 0;
78 } // namespace
80 Version::Version() {
83 Version::~Version() {
86 Version::Version(const std::string& version_str) {
87 std::vector<uint32_t> parsed;
88 if (!ParseVersionNumbers(version_str, &parsed))
89 return;
91 components_.swap(parsed);
94 bool Version::IsValid() const {
95 return (!components_.empty());
98 // static
99 bool Version::IsValidWildcardString(const std::string& wildcard_string) {
100 std::string version_string = wildcard_string;
101 if (EndsWith(wildcard_string.c_str(), ".*", false))
102 version_string = wildcard_string.substr(0, wildcard_string.size() - 2);
104 Version version(version_string);
105 return version.IsValid();
108 bool Version::IsOlderThan(const std::string& version_str) const {
109 Version proposed_ver(version_str);
110 if (!proposed_ver.IsValid())
111 return false;
112 return (CompareTo(proposed_ver) < 0);
115 int Version::CompareToWildcardString(const std::string& wildcard_string) const {
116 DCHECK(IsValid());
117 DCHECK(Version::IsValidWildcardString(wildcard_string));
119 // Default behavior if the string doesn't end with a wildcard.
120 if (!EndsWith(wildcard_string.c_str(), ".*", false)) {
121 Version version(wildcard_string);
122 DCHECK(version.IsValid());
123 return CompareTo(version);
126 std::vector<uint32_t> parsed;
127 const bool success = ParseVersionNumbers(
128 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
129 DCHECK(success);
130 const int comparison = CompareVersionComponents(components_, parsed);
131 // If the version is smaller than the wildcard version's |parsed| vector,
132 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the
133 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to
134 // 1.2.2.* is 0 regardless of the wildcard). Under this logic,
135 // 1.2.0.0.0.0 compared to 1.2.* is 0.
136 if (comparison == -1 || comparison == 0)
137 return comparison;
139 // Catch the case where the digits of |parsed| are found in |components_|,
140 // which means that the two are equal since |parsed| has a trailing "*".
141 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since
142 // components is greater (e.g. 3.2.3 vs 1.*).
143 DCHECK_GT(parsed.size(), 0UL);
144 const size_t min_num_comp = std::min(components_.size(), parsed.size());
145 for (size_t i = 0; i < min_num_comp; ++i) {
146 if (components_[i] != parsed[i])
147 return 1;
149 return 0;
152 bool Version::Equals(const Version& that) const {
153 DCHECK(IsValid());
154 DCHECK(that.IsValid());
155 return (CompareTo(that) == 0);
158 int Version::CompareTo(const Version& other) const {
159 DCHECK(IsValid());
160 DCHECK(other.IsValid());
161 return CompareVersionComponents(components_, other.components_);
164 const std::string Version::GetString() const {
165 DCHECK(IsValid());
166 std::string version_str;
167 size_t count = components_.size();
168 for (size_t i = 0; i < count - 1; ++i) {
169 version_str.append(IntToString(components_[i]));
170 version_str.append(".");
172 version_str.append(IntToString(components_[count - 1]));
173 return version_str;
176 } // namespace base