Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // This function parses the |numbers| vector representing the different numbers | |
|
Alexei Svitkine (slow)
2012/06/26 18:22:03
Nit: Remove "This function" - just start the sente
Mathieu
2012/06/27 15:40:06
Done.
| |
| 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) { | |
|
Ilya Sherman
2012/06/26 20:53:04
nit: This should be passed as a pointer, rather th
Mathieu
2012/06/27 15:40:06
Done.
| |
| 23 std::vector<std::string> numbers; | |
| 24 base::SplitString(version_str, '.', &numbers); | |
| 25 if (numbers.empty()) | |
| 26 return false; | |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: It might be helpful to add a blank l
Mathieu
2012/06/27 15:40:06
Done.
| |
| 27 for (std::vector<std::string>::const_iterator i = numbers.begin(); | |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: I generally prefer to use "it" rathe
Mathieu
2012/06/27 20:25:56
Done.
| |
| 28 i != numbers.end(); ++i) { | |
| 29 int num; | |
| 30 if (!base::StringToInt(*i, &num)) | |
| 31 return false; | |
| 32 if (num < 0) | |
| 33 return false; | |
| 34 const uint16 max = 0xFFFF; | |
| 35 if (num > max) | |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: I assume that this does the right th
| |
| 36 return false; | |
| 37 // This throws out things like +3, or 032. | |
| 38 if (base::IntToString(num) != *i) | |
| 39 return false; | |
| 40 parsed.push_back(static_cast<uint16>(num)); | |
| 41 } | |
| 42 return true; | |
| 43 } | |
| 44 | |
| 45 // Compares version components in |comp1| with components in |comp2|. Returns | |
| 46 // -1, 0 or 1 if |comp1| is greater than, equal to, or less than |comp2|, | |
| 47 // respectively. | |
| 48 int CompareVersionComponents(const std::vector<uint16>& comp1, | |
|
Ilya Sherman
2012/06/26 20:53:04
nit: Prefer to avoid abbreviations, so name this "
Mathieu
2012/06/27 15:40:06
Done.
| |
| 49 const std::vector<uint16>& comp2) { | |
| 50 const size_t count = std::min(comp1.size(), comp2.size()); | |
| 51 for (size_t i = 0; i < count; ++i) { | |
| 52 if (comp1[i] > comp2[i]) | |
| 53 return 1; | |
| 54 if (comp1[i] < comp2[i]) | |
| 55 return -1; | |
| 56 } | |
|
Ilya Sherman
2012/06/26 20:53:04
nit: Perhaps the logic below this line should rema
| |
| 57 if (comp1.size() > comp2.size()) { | |
| 58 for (size_t i = count; i < comp1.size(); ++i) | |
| 59 if (comp1[i] > 0) | |
| 60 return 1; | |
| 61 } else if (comp1.size() < comp2.size()) { | |
| 62 for (size_t i = count; i < comp2.size(); ++i) | |
| 63 if (comp2[i] > 0) | |
| 64 return -1; | |
| 65 } | |
| 66 return 0; | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 14 Version::Version() { | 71 Version::Version() { |
| 15 } | 72 } |
| 16 | 73 |
| 17 Version::~Version() { | 74 Version::~Version() { |
| 18 } | 75 } |
| 19 | 76 |
| 20 Version::Version(const std::string& version_str) { | 77 Version::Version(const std::string& version_str) { |
| 21 std::vector<std::string> numbers; | 78 std::vector<uint16> parsed; |
| 22 base::SplitString(version_str, '.', &numbers); | 79 if (!ParseVersionNumbers(version_str, parsed)) |
| 23 if (numbers.empty()) | |
| 24 return; | 80 return; |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: Add a blank line after this return s
Mathieu
2012/06/27 15:40:06
Done.
| |
| 25 std::vector<uint16> parsed; | |
| 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); | 81 components_.swap(parsed); |
| 42 } | 82 } |
| 43 | 83 |
| 44 bool Version::IsValid() const { | 84 bool Version::IsValid() const { |
| 45 return (!components_.empty()); | 85 return (!components_.empty()); |
| 46 } | 86 } |
| 47 | 87 |
| 88 // static | |
| 89 bool Version::IsValidWildcardString(const std::string& wildcard_string) { | |
| 90 if (wildcard_string.empty()) | |
|
Alexei Svitkine (slow)
2012/06/26 18:22:03
Is this needed anymore or will this be covered by
Mathieu
2012/06/27 15:40:06
Done.
| |
| 91 return false; | |
| 92 | |
| 93 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { | |
| 94 // Either a lone character or a full version number. | |
| 95 Version version(wildcard_string); | |
| 96 return version.IsValid(); | |
|
Ilya Sherman
2012/06/26 20:53:04
Optional nit: These two lines are essentially repe
Mathieu
2012/06/27 15:40:06
Done.
| |
| 97 } | |
| 98 // Version strings length 2 or more, ending with ".*". | |
| 99 Version version(wildcard_string.substr(0, wildcard_string.size() - 2)); | |
| 100 return version.IsValid(); | |
| 101 } | |
| 102 | |
| 48 bool Version::IsOlderThan(const std::string& version_str) const { | 103 bool Version::IsOlderThan(const std::string& version_str) const { |
| 49 Version proposed_ver(version_str); | 104 Version proposed_ver(version_str); |
| 50 if (!proposed_ver.IsValid()) | 105 if (!proposed_ver.IsValid()) |
| 51 return false; | 106 return false; |
| 52 return (CompareTo(proposed_ver) < 0); | 107 return (CompareTo(proposed_ver) < 0); |
| 53 } | 108 } |
| 54 | 109 |
| 110 int Version::CompareToWildcardString(const std::string& wildcard_string) const { | |
| 111 DCHECK(IsValid()); | |
| 112 DCHECK(Version::IsValidWildcardString(wildcard_string)); | |
| 113 | |
| 114 // Default behavior if the string doesn't end with a wildcard. | |
| 115 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { | |
| 116 Version version(wildcard_string); | |
| 117 DCHECK(version.IsValid()); | |
| 118 return CompareTo(version); | |
| 119 } | |
| 120 | |
| 121 std::vector<uint16> parsed; | |
| 122 const bool success = ParseVersionNumbers( | |
| 123 wildcard_string.substr(0, wildcard_string.length() - 2), parsed); | |
| 124 DCHECK(success); | |
| 125 const int comparison = CompareVersionComponents(components_, parsed); | |
| 126 // If the version is smaller than the wildcard version's |parsed| vector, | |
| 127 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the | |
| 128 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to | |
| 129 // 1.2.2.* is 0 regardless of the wildcard). | |
|
Ilya Sherman
2012/06/26 20:53:04
nit: I think |comparison| will also be 0 when comp
Mathieu
2012/06/27 15:40:06
Done.
| |
| 130 if (comparison == -1 || comparison == 0) | |
| 131 return comparison; | |
| 132 | |
| 133 // Catch the case where the digits of |parsed| are found in |components_|, | |
| 134 // which means that the two are equal since |parsed| has a trailing "*". | |
| 135 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since | |
| 136 // components is greater (e.g. 3.2.3 vs 1.*). | |
| 137 DCHECK_GT(parsed.size(), 0UL); | |
| 138 const size_t min_num_comp = std::min(components_.size(), parsed.size()); | |
| 139 for (size_t i = 0; i < min_num_comp; ++i) { | |
| 140 if (components_[i] != parsed[i]) | |
| 141 return 1; | |
| 142 } | |
| 143 return 0; | |
| 144 } | |
| 145 | |
| 55 // TODO(cpu): remove this method. | 146 // TODO(cpu): remove this method. |
| 56 Version* Version::GetVersionFromString(const std::string& version_str) { | 147 Version* Version::GetVersionFromString(const std::string& version_str) { |
| 57 Version* vers = new Version(version_str); | 148 Version* vers = new Version(version_str); |
| 58 if (vers->IsValid()) { | 149 if (vers->IsValid()) { |
| 59 return vers; | 150 return vers; |
| 60 } | 151 } |
| 61 delete vers; | 152 delete vers; |
| 62 return NULL; | 153 return NULL; |
| 63 } | 154 } |
| 64 | 155 |
| 65 // TODO(cpu): remove this method. | 156 // TODO(cpu): remove this method. |
| 66 Version* Version::Clone() const { | 157 Version* Version::Clone() const { |
| 67 DCHECK(IsValid()); | 158 DCHECK(IsValid()); |
| 68 return new Version(*this); | 159 return new Version(*this); |
| 69 } | 160 } |
| 70 | 161 |
| 71 bool Version::Equals(const Version& that) const { | 162 bool Version::Equals(const Version& that) const { |
| 72 DCHECK(IsValid()); | 163 DCHECK(IsValid()); |
| 73 DCHECK(that.IsValid()); | 164 DCHECK(that.IsValid()); |
| 74 return (CompareTo(that) == 0); | 165 return (CompareTo(that) == 0); |
| 75 } | 166 } |
| 76 | 167 |
| 77 int Version::CompareTo(const Version& other) const { | 168 int Version::CompareTo(const Version& other) const { |
| 78 DCHECK(IsValid()); | 169 DCHECK(IsValid()); |
| 79 DCHECK(other.IsValid()); | 170 DCHECK(other.IsValid()); |
| 80 size_t count = std::min(components_.size(), other.components_.size()); | 171 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 } | 172 } |
| 98 | 173 |
| 99 const std::string Version::GetString() const { | 174 const std::string Version::GetString() const { |
| 100 DCHECK(IsValid()); | 175 DCHECK(IsValid()); |
| 101 std::string version_str; | 176 std::string version_str; |
| 102 size_t count = components_.size(); | 177 size_t count = components_.size(); |
| 103 for (size_t i = 0; i < count - 1; ++i) { | 178 for (size_t i = 0; i < count - 1; ++i) { |
| 104 version_str.append(base::IntToString(components_[i])); | 179 version_str.append(base::IntToString(components_[i])); |
| 105 version_str.append("."); | 180 version_str.append("."); |
| 106 } | 181 } |
| 107 version_str.append(base::IntToString(components_[count - 1])); | 182 version_str.append(base::IntToString(components_[count - 1])); |
| 108 return version_str; | 183 return version_str; |
| 109 } | 184 } |
| OLD | NEW |