Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(741)

Side by Side Diff: base/version.cc

Issue 10576003: VariationsService now supports wildcard in min/max version (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: addressed brettw's comment Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/version.h ('k') | base/version_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/version.h" 5 #include "base/version.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "base/string_split.h" 11 #include "base/string_split.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 13
14 namespace {
15
16 // Parses the |numbers| vector representing the different numbers
17 // inside the version string and constructs a vector of valid integers. It stops
18 // when it reaches an invalid item (including the wildcard character). |parsed|
19 // is the resulting integer vector. Function returns true if all numbers were
20 // parsed successfully, false otherwise.
21 bool ParseVersionNumbers(const std::string& version_str,
22 std::vector<uint16>* parsed) {
23 std::vector<std::string> numbers;
24 base::SplitString(version_str, '.', &numbers);
25 if (numbers.empty())
26 return false;
27
28 for (std::vector<std::string>::const_iterator it = numbers.begin();
29 it != numbers.end(); ++it) {
30 int num;
31 if (!base::StringToInt(*it, &num))
32 return false;
33
34 if (num < 0)
35 return false;
36
37 const uint16 max = 0xFFFF;
38 if (num > max)
39 return false;
40
41 // This throws out things like +3, or 032.
42 if (base::IntToString(num) != *it)
43 return false;
44
45 parsed->push_back(static_cast<uint16>(num));
46 }
47 return true;
48 }
49
50 // Compares version components in |components1| with components in
51 // |components2|. Returns -1, 0 or 1 if |components1| is greater than, equal to,
52 // or less than |components2|, respectively.
53 int CompareVersionComponents(const std::vector<uint16>& components1,
54 const std::vector<uint16>& components2) {
55 const size_t count = std::min(components1.size(), components2.size());
56 for (size_t i = 0; i < count; ++i) {
57 if (components1[i] > components2[i])
58 return 1;
59 if (components1[i] < components2[i])
60 return -1;
61 }
62 if (components1.size() > components2.size()) {
63 for (size_t i = count; i < components1.size(); ++i) {
64 if (components1[i] > 0)
65 return 1;
66 }
67 } else if (components1.size() < components2.size()) {
68 for (size_t i = count; i < components2.size(); ++i) {
69 if (components2[i] > 0)
70 return -1;
71 }
72 }
73 return 0;
74 }
75
76 } // namespace
77
14 Version::Version() { 78 Version::Version() {
15 } 79 }
16 80
17 Version::~Version() { 81 Version::~Version() {
18 } 82 }
19 83
20 Version::Version(const std::string& version_str) { 84 Version::Version(const std::string& version_str) {
21 std::vector<std::string> numbers; 85 std::vector<uint16> parsed;
22 base::SplitString(version_str, '.', &numbers); 86 if (!ParseVersionNumbers(version_str, &parsed))
23 if (numbers.empty())
24 return; 87 return;
25 std::vector<uint16> parsed; 88
26 for (std::vector<std::string>::iterator i = numbers.begin();
27 i != numbers.end(); ++i) {
28 int num;
29 if (!base::StringToInt(*i, &num))
30 return;
31 if (num < 0)
32 return;
33 const uint16 max = 0xFFFF;
34 if (num > max)
35 return;
36 // This throws out things like +3, or 032.
37 if (base::IntToString(num) != *i)
38 return;
39 parsed.push_back(static_cast<uint16>(num));
40 }
41 components_.swap(parsed); 89 components_.swap(parsed);
42 } 90 }
43 91
44 bool Version::IsValid() const { 92 bool Version::IsValid() const {
45 return (!components_.empty()); 93 return (!components_.empty());
46 } 94 }
47 95
96 // static
97 bool Version::IsValidWildcardString(const std::string& wildcard_string) {
98 std::string version_string = wildcard_string;
99 if (EndsWith(wildcard_string.c_str(), ".*", false))
100 version_string = wildcard_string.substr(0, wildcard_string.size() - 2);
101
102 Version version(version_string);
103 return version.IsValid();
104 }
105
48 bool Version::IsOlderThan(const std::string& version_str) const { 106 bool Version::IsOlderThan(const std::string& version_str) const {
49 Version proposed_ver(version_str); 107 Version proposed_ver(version_str);
50 if (!proposed_ver.IsValid()) 108 if (!proposed_ver.IsValid())
51 return false; 109 return false;
52 return (CompareTo(proposed_ver) < 0); 110 return (CompareTo(proposed_ver) < 0);
53 } 111 }
54 112
113 int Version::CompareToWildcardString(const std::string& wildcard_string) const {
114 DCHECK(IsValid());
115 DCHECK(Version::IsValidWildcardString(wildcard_string));
116
117 // Default behavior if the string doesn't end with a wildcard.
118 if (!EndsWith(wildcard_string.c_str(), ".*", false)) {
119 Version version(wildcard_string);
120 DCHECK(version.IsValid());
121 return CompareTo(version);
122 }
123
124 std::vector<uint16> parsed;
125 const bool success = ParseVersionNumbers(
126 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
127 DCHECK(success);
128 const int comparison = CompareVersionComponents(components_, parsed);
129 // If the version is smaller than the wildcard version's |parsed| vector,
130 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the
131 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to
132 // 1.2.2.* is 0 regardless of the wildcard). Under this logic,
133 // 1.2.0.0.0.0 compared to 1.2.* is 0.
134 if (comparison == -1 || comparison == 0)
135 return comparison;
136
137 // Catch the case where the digits of |parsed| are found in |components_|,
138 // which means that the two are equal since |parsed| has a trailing "*".
139 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since
140 // components is greater (e.g. 3.2.3 vs 1.*).
141 DCHECK_GT(parsed.size(), 0UL);
142 const size_t min_num_comp = std::min(components_.size(), parsed.size());
143 for (size_t i = 0; i < min_num_comp; ++i) {
144 if (components_[i] != parsed[i])
145 return 1;
146 }
147 return 0;
148 }
149
55 // TODO(cpu): remove this method. 150 // TODO(cpu): remove this method.
56 Version* Version::GetVersionFromString(const std::string& version_str) { 151 Version* Version::GetVersionFromString(const std::string& version_str) {
57 Version* vers = new Version(version_str); 152 Version* vers = new Version(version_str);
58 if (vers->IsValid()) { 153 if (vers->IsValid()) {
59 return vers; 154 return vers;
60 } 155 }
61 delete vers; 156 delete vers;
62 return NULL; 157 return NULL;
63 } 158 }
64 159
65 // TODO(cpu): remove this method. 160 // TODO(cpu): remove this method.
66 Version* Version::Clone() const { 161 Version* Version::Clone() const {
67 DCHECK(IsValid()); 162 DCHECK(IsValid());
68 return new Version(*this); 163 return new Version(*this);
69 } 164 }
70 165
71 bool Version::Equals(const Version& that) const { 166 bool Version::Equals(const Version& that) const {
72 DCHECK(IsValid()); 167 DCHECK(IsValid());
73 DCHECK(that.IsValid()); 168 DCHECK(that.IsValid());
74 return (CompareTo(that) == 0); 169 return (CompareTo(that) == 0);
75 } 170 }
76 171
77 int Version::CompareTo(const Version& other) const { 172 int Version::CompareTo(const Version& other) const {
78 DCHECK(IsValid()); 173 DCHECK(IsValid());
79 DCHECK(other.IsValid()); 174 DCHECK(other.IsValid());
80 size_t count = std::min(components_.size(), other.components_.size()); 175 return CompareVersionComponents(components_, other.components_);
81 for (size_t i = 0; i < count; ++i) {
82 if (components_[i] > other.components_[i])
83 return 1;
84 if (components_[i] < other.components_[i])
85 return -1;
86 }
87 if (components_.size() > other.components_.size()) {
88 for (size_t i = count; i < components_.size(); ++i)
89 if (components_[i] > 0)
90 return 1;
91 } else if (components_.size() < other.components_.size()) {
92 for (size_t i = count; i < other.components_.size(); ++i)
93 if (other.components_[i] > 0)
94 return -1;
95 }
96 return 0;
97 } 176 }
98 177
99 const std::string Version::GetString() const { 178 const std::string Version::GetString() const {
100 DCHECK(IsValid()); 179 DCHECK(IsValid());
101 std::string version_str; 180 std::string version_str;
102 size_t count = components_.size(); 181 size_t count = components_.size();
103 for (size_t i = 0; i < count - 1; ++i) { 182 for (size_t i = 0; i < count - 1; ++i) {
104 version_str.append(base::IntToString(components_[i])); 183 version_str.append(base::IntToString(components_[i]));
105 version_str.append("."); 184 version_str.append(".");
106 } 185 }
107 version_str.append(base::IntToString(components_[count - 1])); 186 version_str.append(base::IntToString(components_[count - 1]));
108 return version_str; 187 return version_str;
109 } 188 }
OLDNEW
« no previous file with comments | « base/version.h ('k') | base/version_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698