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

Side by Side Diff: base/version.cc

Issue 1575523002: Comparison and streaming operators for base::Version (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes mistake in previous patch set. Created 4 years, 11 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
« 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) 2012 The Chromium Authors. All rights reserved. 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 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
(...skipping 15 matching lines...) Expand all
26 std::vector<uint32_t>* parsed) { 26 std::vector<uint32_t>* parsed) {
27 std::vector<StringPiece> numbers = 27 std::vector<StringPiece> numbers =
28 SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL); 28 SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL);
29 if (numbers.empty()) 29 if (numbers.empty())
30 return false; 30 return false;
31 31
32 for (auto it = numbers.begin(); it != numbers.end(); ++it) { 32 for (auto it = numbers.begin(); it != numbers.end(); ++it) {
33 if (StartsWith(*it, "+", CompareCase::SENSITIVE)) 33 if (StartsWith(*it, "+", CompareCase::SENSITIVE))
34 return false; 34 return false;
35 35
36 // TODO(brettw) when we have a StringPiece version of StringToUint, delete
37 // this string conversion.
38 unsigned int num; 36 unsigned int num;
39 if (!StringToUint(*it, &num)) 37 if (!StringToUint(*it, &num))
40 return false; 38 return false;
41 39
42 // This throws out leading zeros for the first item only. 40 // This throws out leading zeros for the first item only.
43 if (it == numbers.begin() && UintToString(num) != *it) 41 if (it == numbers.begin() && UintToString(num) != *it)
44 return false; 42 return false;
45 43
46 // StringToUint returns unsigned int but Version fields are uint32_t. 44 // StringToUint returns unsigned int but Version fields are uint32_t.
47 static_assert(sizeof (uint32_t) == sizeof (unsigned int), 45 static_assert(sizeof (uint32_t) == sizeof (unsigned int),
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 // static 98 // static
101 bool Version::IsValidWildcardString(const std::string& wildcard_string) { 99 bool Version::IsValidWildcardString(const std::string& wildcard_string) {
102 std::string version_string = wildcard_string; 100 std::string version_string = wildcard_string;
103 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE)) 101 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE))
104 version_string.resize(version_string.size() - 2); 102 version_string.resize(version_string.size() - 2);
105 103
106 Version version(version_string); 104 Version version(version_string);
107 return version.IsValid(); 105 return version.IsValid();
108 } 106 }
109 107
110 bool 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
117 int Version::CompareToWildcardString(const std::string& wildcard_string) const { 108 int Version::CompareToWildcardString(const std::string& wildcard_string) const {
118 DCHECK(IsValid()); 109 DCHECK(IsValid());
119 DCHECK(Version::IsValidWildcardString(wildcard_string)); 110 DCHECK(Version::IsValidWildcardString(wildcard_string));
120 111
121 // Default behavior if the string doesn't end with a wildcard. 112 // Default behavior if the string doesn't end with a wildcard.
122 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) { 113 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
123 Version version(wildcard_string); 114 Version version(wildcard_string);
124 DCHECK(version.IsValid()); 115 DCHECK(version.IsValid());
125 return CompareTo(version); 116 return CompareTo(version);
126 } 117 }
(...skipping 17 matching lines...) Expand all
144 // components is greater (e.g. 3.2.3 vs 1.*). 135 // components is greater (e.g. 3.2.3 vs 1.*).
145 DCHECK_GT(parsed.size(), 0UL); 136 DCHECK_GT(parsed.size(), 0UL);
146 const size_t min_num_comp = std::min(components_.size(), parsed.size()); 137 const size_t min_num_comp = std::min(components_.size(), parsed.size());
147 for (size_t i = 0; i < min_num_comp; ++i) { 138 for (size_t i = 0; i < min_num_comp; ++i) {
148 if (components_[i] != parsed[i]) 139 if (components_[i] != parsed[i])
149 return 1; 140 return 1;
150 } 141 }
151 return 0; 142 return 0;
152 } 143 }
153 144
154 bool Version::Equals(const Version& that) const {
155 DCHECK(IsValid());
156 DCHECK(that.IsValid());
157 return (CompareTo(that) == 0);
158 }
159
160 int Version::CompareTo(const Version& other) const { 145 int Version::CompareTo(const Version& other) const {
161 DCHECK(IsValid()); 146 DCHECK(IsValid());
162 DCHECK(other.IsValid()); 147 DCHECK(other.IsValid());
163 return CompareVersionComponents(components_, other.components_); 148 return CompareVersionComponents(components_, other.components_);
164 } 149 }
165 150
166 const std::string Version::GetString() const { 151 const std::string Version::GetString() const {
167 DCHECK(IsValid()); 152 DCHECK(IsValid());
168 std::string version_str; 153 std::string version_str;
169 size_t count = components_.size(); 154 size_t count = components_.size();
170 for (size_t i = 0; i < count - 1; ++i) { 155 for (size_t i = 0; i < count - 1; ++i) {
171 version_str.append(UintToString(components_[i])); 156 version_str.append(UintToString(components_[i]));
172 version_str.append("."); 157 version_str.append(".");
173 } 158 }
174 version_str.append(UintToString(components_[count - 1])); 159 version_str.append(UintToString(components_[count - 1]));
175 return version_str; 160 return version_str;
176 } 161 }
177 162
163 bool operator==(const Version& v1, const Version& v2) {
164 return v1.CompareTo(v2) == 0;
165 }
166
167 bool operator!=(const Version& v1, const Version& v2) {
168 return !(v1 == v2);
169 }
170
171 bool operator<(const Version& v1, const Version& v2) {
172 return v1.CompareTo(v2) < 0;
173 }
174
175 bool operator<=(const Version& v1, const Version& v2) {
176 return v1.CompareTo(v2) <= 0;
177 }
178
179 bool operator>(const Version& v1, const Version& v2) {
180 return v1.CompareTo(v2) > 0;
181 }
182
183 bool operator>=(const Version& v1, const Version& v2) {
184 return v1.CompareTo(v2) >= 0;
185 }
186
187 std::ostream& operator<<(std::ostream& stream, const Version& v) {
188 return stream << v.GetString();
189 }
190
178 } // namespace base 191 } // namespace base
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