Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 16 matching lines...) Expand all Loading... | |
| 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 | 36 // TODO(brettw) when we have a StringPiece version of StringToUint, delete |
| 37 // this string conversion. | 37 // this string conversion. |
|
Rob Percival
2016/01/09 23:54:03
What does the above TODO mean?
brettw
2016/01/19 23:33:32
Look like it's obsolete and we can delete the TODO
| |
| 38 unsigned int num; | 38 unsigned int num; |
| 39 if (!StringToUint(*it, &num)) | 39 if (!StringToUint(*it, &num)) |
| 40 return false; | 40 return false; |
| 41 | 41 |
| 42 // This throws out leading zeros for the first item only. | 42 // This throws out leading zeros for the first item only. |
| 43 if (it == numbers.begin() && UintToString(num) != *it) | 43 if (it == numbers.begin() && UintToString(num) != *it) |
| 44 return false; | 44 return false; |
| 45 | 45 |
| 46 // StringToUint returns unsigned int but Version fields are uint32_t. | 46 // StringToUint returns unsigned int but Version fields are uint32_t. |
| 47 static_assert(sizeof (uint32_t) == sizeof (unsigned int), | 47 static_assert(sizeof (uint32_t) == sizeof (unsigned int), |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 // components is greater (e.g. 3.2.3 vs 1.*). | 144 // components is greater (e.g. 3.2.3 vs 1.*). |
| 145 DCHECK_GT(parsed.size(), 0UL); | 145 DCHECK_GT(parsed.size(), 0UL); |
| 146 const size_t min_num_comp = std::min(components_.size(), parsed.size()); | 146 const size_t min_num_comp = std::min(components_.size(), parsed.size()); |
| 147 for (size_t i = 0; i < min_num_comp; ++i) { | 147 for (size_t i = 0; i < min_num_comp; ++i) { |
| 148 if (components_[i] != parsed[i]) | 148 if (components_[i] != parsed[i]) |
| 149 return 1; | 149 return 1; |
| 150 } | 150 } |
| 151 return 0; | 151 return 0; |
| 152 } | 152 } |
| 153 | 153 |
| 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 { | 154 int Version::CompareTo(const Version& other) const { |
| 161 DCHECK(IsValid()); | 155 DCHECK(IsValid()); |
| 162 DCHECK(other.IsValid()); | 156 DCHECK(other.IsValid()); |
| 163 return CompareVersionComponents(components_, other.components_); | 157 return CompareVersionComponents(components_, other.components_); |
| 164 } | 158 } |
| 165 | 159 |
| 166 const std::string Version::GetString() const { | 160 const std::string Version::GetString() const { |
| 167 DCHECK(IsValid()); | 161 DCHECK(IsValid()); |
| 168 std::string version_str; | 162 std::string version_str; |
| 169 size_t count = components_.size(); | 163 size_t count = components_.size(); |
| 170 for (size_t i = 0; i < count - 1; ++i) { | 164 for (size_t i = 0; i < count - 1; ++i) { |
| 171 version_str.append(UintToString(components_[i])); | 165 version_str.append(UintToString(components_[i])); |
| 172 version_str.append("."); | 166 version_str.append("."); |
| 173 } | 167 } |
| 174 version_str.append(UintToString(components_[count - 1])); | 168 version_str.append(UintToString(components_[count - 1])); |
| 175 return version_str; | 169 return version_str; |
| 176 } | 170 } |
| 177 | 171 |
| 172 bool operator==(const Version& v1, const Version& v2) { | |
| 173 return v1.CompareTo(v2) == 0; | |
| 174 } | |
| 175 | |
| 176 bool operator!=(const Version& v1, const Version& v2) { | |
| 177 return !(v1 == v2); | |
| 178 } | |
| 179 | |
| 180 bool operator<(const Version& v1, const Version& v2) { | |
| 181 return v1.CompareTo(v2) < 0; | |
| 182 } | |
| 183 | |
| 184 bool operator<=(const Version& v1, const Version& v2) { | |
| 185 return v1.CompareTo(v2) <= 0; | |
| 186 } | |
| 187 | |
| 188 bool operator>(const Version& v1, const Version& v2) { | |
| 189 return v1.CompareTo(v2) > 0; | |
| 190 } | |
| 191 | |
| 192 bool operator>=(const Version& v1, const Version& v2) { | |
| 193 return v1.CompareTo(v2) >= 0; | |
| 194 } | |
| 195 | |
| 196 std::ostream& operator<<(std::ostream& stream, const Version& v) { | |
| 197 return stream << v.GetString(); | |
| 198 } | |
| 199 | |
| 178 } // namespace base | 200 } // namespace base |
| OLD | NEW |