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 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // Parses the |numbers| vector representing the different numbers | 20 // Parses the |numbers| vector representing the different numbers |
| 21 // inside the version string and constructs a vector of valid integers. It stops | 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| | 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 | 23 // is the resulting integer vector. Function returns true if all numbers were |
| 24 // parsed successfully, false otherwise. | 24 // parsed successfully, false otherwise. |
| 25 bool ParseVersionNumbers(const std::string& version_str, | 25 bool ParseVersionNumbers(const std::string& version_str, |
| 26 std::vector<uint16>* parsed) { | 26 std::vector<uint32>* parsed) { |
| 27 std::vector<std::string> numbers; | 27 std::vector<std::string> numbers; |
| 28 SplitString(version_str, '.', &numbers); | 28 SplitString(version_str, '.', &numbers); |
| 29 if (numbers.empty()) | 29 if (numbers.empty()) |
| 30 return false; | 30 return false; |
| 31 | 31 |
| 32 for (std::vector<std::string>::const_iterator it = numbers.begin(); | 32 for (std::vector<std::string>::const_iterator it = numbers.begin(); |
| 33 it != numbers.end(); ++it) { | 33 it != numbers.end(); ++it) { |
| 34 if (StartsWithASCII(*it, "+", false)) | 34 if (StartsWithASCII(*it, "+", false)) |
| 35 return false; | 35 return false; |
| 36 int num; | 36 int64 num; |
| 37 if (!StringToInt(*it, &num)) | 37 if (!StringToInt64(*it, &num)) |
|
grt (UTC plus 2)
2015/03/09 13:19:49
why not StringToUint? you could static_assert that
Nico
2015/03/09 14:21:57
Is anything wrong with this? It makes it much easi
Will Harris
2015/03/09 22:52:17
Done.
Will Harris
2015/03/09 22:52:17
grt's way makes the code far simpler - since Strin
| |
| 38 return false; | 38 return false; |
| 39 | 39 |
| 40 if (num < 0) | 40 if (num < 0) |
| 41 return false; | 41 return false; |
| 42 | 42 |
| 43 const uint16 max = 0xFFFF; | 43 const int64 max = 0xFFFFFFFF; |
| 44 if (num > max) | 44 if (num > max) |
| 45 return false; | 45 return false; |
| 46 | 46 |
| 47 // This throws out leading zeros for the first item only. | 47 // This throws out leading zeros for the first item only. |
| 48 if (it == numbers.begin() && IntToString(num) != *it) | 48 if (it == numbers.begin() && Int64ToString(num) != *it) |
| 49 return false; | 49 return false; |
| 50 | 50 |
| 51 parsed->push_back(static_cast<uint16>(num)); | 51 parsed->push_back(static_cast<uint32>(num)); |
| 52 } | 52 } |
| 53 return true; | 53 return true; |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Compares version components in |components1| with components in | 56 // Compares version components in |components1| with components in |
| 57 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to, | 57 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to, |
| 58 // or greater than |components2|, respectively. | 58 // or greater than |components2|, respectively. |
| 59 int CompareVersionComponents(const std::vector<uint16>& components1, | 59 int CompareVersionComponents(const std::vector<uint32>& components1, |
| 60 const std::vector<uint16>& components2) { | 60 const std::vector<uint32>& components2) { |
| 61 const size_t count = std::min(components1.size(), components2.size()); | 61 const size_t count = std::min(components1.size(), components2.size()); |
| 62 for (size_t i = 0; i < count; ++i) { | 62 for (size_t i = 0; i < count; ++i) { |
| 63 if (components1[i] > components2[i]) | 63 if (components1[i] > components2[i]) |
| 64 return 1; | 64 return 1; |
| 65 if (components1[i] < components2[i]) | 65 if (components1[i] < components2[i]) |
| 66 return -1; | 66 return -1; |
| 67 } | 67 } |
| 68 if (components1.size() > components2.size()) { | 68 if (components1.size() > components2.size()) { |
| 69 for (size_t i = count; i < components1.size(); ++i) { | 69 for (size_t i = count; i < components1.size(); ++i) { |
| 70 if (components1[i] > 0) | 70 if (components1[i] > 0) |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 81 | 81 |
| 82 } // namespace | 82 } // namespace |
| 83 | 83 |
| 84 Version::Version() { | 84 Version::Version() { |
| 85 } | 85 } |
| 86 | 86 |
| 87 Version::~Version() { | 87 Version::~Version() { |
| 88 } | 88 } |
| 89 | 89 |
| 90 Version::Version(const std::string& version_str) { | 90 Version::Version(const std::string& version_str) { |
| 91 std::vector<uint16> parsed; | 91 std::vector<uint32> parsed; |
| 92 if (!ParseVersionNumbers(version_str, &parsed)) | 92 if (!ParseVersionNumbers(version_str, &parsed)) |
| 93 return; | 93 return; |
| 94 | 94 |
| 95 components_.swap(parsed); | 95 components_.swap(parsed); |
| 96 } | 96 } |
| 97 | 97 |
| 98 bool Version::IsValid() const { | 98 bool Version::IsValid() const { |
| 99 return (!components_.empty()); | 99 return (!components_.empty()); |
| 100 } | 100 } |
| 101 | 101 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 120 DCHECK(IsValid()); | 120 DCHECK(IsValid()); |
| 121 DCHECK(Version::IsValidWildcardString(wildcard_string)); | 121 DCHECK(Version::IsValidWildcardString(wildcard_string)); |
| 122 | 122 |
| 123 // Default behavior if the string doesn't end with a wildcard. | 123 // Default behavior if the string doesn't end with a wildcard. |
| 124 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { | 124 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { |
| 125 Version version(wildcard_string); | 125 Version version(wildcard_string); |
| 126 DCHECK(version.IsValid()); | 126 DCHECK(version.IsValid()); |
| 127 return CompareTo(version); | 127 return CompareTo(version); |
| 128 } | 128 } |
| 129 | 129 |
| 130 std::vector<uint16> parsed; | 130 std::vector<uint32> parsed; |
| 131 const bool success = ParseVersionNumbers( | 131 const bool success = ParseVersionNumbers( |
| 132 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); | 132 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); |
| 133 DCHECK(success); | 133 DCHECK(success); |
| 134 const int comparison = CompareVersionComponents(components_, parsed); | 134 const int comparison = CompareVersionComponents(components_, parsed); |
| 135 // If the version is smaller than the wildcard version's |parsed| vector, | 135 // If the version is smaller than the wildcard version's |parsed| vector, |
| 136 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the | 136 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the |
| 137 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to | 137 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to |
| 138 // 1.2.2.* is 0 regardless of the wildcard). Under this logic, | 138 // 1.2.2.* is 0 regardless of the wildcard). Under this logic, |
| 139 // 1.2.0.0.0.0 compared to 1.2.* is 0. | 139 // 1.2.0.0.0.0 compared to 1.2.* is 0. |
| 140 if (comparison == -1 || comparison == 0) | 140 if (comparison == -1 || comparison == 0) |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 171 size_t count = components_.size(); | 171 size_t count = components_.size(); |
| 172 for (size_t i = 0; i < count - 1; ++i) { | 172 for (size_t i = 0; i < count - 1; ++i) { |
| 173 version_str.append(IntToString(components_[i])); | 173 version_str.append(IntToString(components_[i])); |
| 174 version_str.append("."); | 174 version_str.append("."); |
| 175 } | 175 } |
| 176 version_str.append(IntToString(components_[count - 1])); | 176 version_str.append(IntToString(components_[count - 1])); |
| 177 return version_str; | 177 return version_str; |
| 178 } | 178 } |
| 179 | 179 |
| 180 } // namespace base | 180 } // namespace base |
| OLD | NEW |