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<uint32_t>* parsed) { | 26 std::vector<uint32_t>* parsed) { |
27 std::vector<std::string> numbers; | 27 std::vector<StringPiece> numbers = |
28 SplitString(version_str, '.', &numbers); | 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 (std::vector<std::string>::const_iterator it = numbers.begin(); | 32 for (auto it = numbers.begin(); it != numbers.end(); ++it) { |
33 it != numbers.end(); ++it) { | 33 if (StartsWith(*it, "+", CompareCase::SENSITIVE)) |
34 if (StartsWithASCII(*it, "+", false)) | |
35 return false; | 34 return false; |
| 35 |
| 36 // TODO(brettw) when we have a StringPiece version of StringToUint, delete |
| 37 // this string conversion. |
36 unsigned int num; | 38 unsigned int num; |
37 if (!StringToUint(*it, &num)) | 39 if (!StringToUint(*it, &num)) |
38 return false; | 40 return false; |
39 | 41 |
40 // This throws out leading zeros for the first item only. | 42 // This throws out leading zeros for the first item only. |
41 if (it == numbers.begin() && UintToString(num) != *it) | 43 if (it == numbers.begin() && UintToString(num) != *it) |
42 return false; | 44 return false; |
43 | 45 |
44 // StringToUint returns unsigned int but Version fields are uint32_t. | 46 // StringToUint returns unsigned int but Version fields are uint32_t. |
45 static_assert(sizeof (uint32_t) == sizeof (unsigned int), | 47 static_assert(sizeof (uint32_t) == sizeof (unsigned int), |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 components_.swap(parsed); | 93 components_.swap(parsed); |
92 } | 94 } |
93 | 95 |
94 bool Version::IsValid() const { | 96 bool Version::IsValid() const { |
95 return (!components_.empty()); | 97 return (!components_.empty()); |
96 } | 98 } |
97 | 99 |
98 // static | 100 // static |
99 bool Version::IsValidWildcardString(const std::string& wildcard_string) { | 101 bool Version::IsValidWildcardString(const std::string& wildcard_string) { |
100 std::string version_string = wildcard_string; | 102 std::string version_string = wildcard_string; |
101 if (EndsWith(wildcard_string.c_str(), ".*", false)) | 103 if (EndsWith(version_string, ".*", CompareCase::SENSITIVE)) |
102 version_string = wildcard_string.substr(0, wildcard_string.size() - 2); | 104 version_string.resize(version_string.size() - 2); |
103 | 105 |
104 Version version(version_string); | 106 Version version(version_string); |
105 return version.IsValid(); | 107 return version.IsValid(); |
106 } | 108 } |
107 | 109 |
108 bool Version::IsOlderThan(const std::string& version_str) const { | 110 bool Version::IsOlderThan(const std::string& version_str) const { |
109 Version proposed_ver(version_str); | 111 Version proposed_ver(version_str); |
110 if (!proposed_ver.IsValid()) | 112 if (!proposed_ver.IsValid()) |
111 return false; | 113 return false; |
112 return (CompareTo(proposed_ver) < 0); | 114 return (CompareTo(proposed_ver) < 0); |
113 } | 115 } |
114 | 116 |
115 int Version::CompareToWildcardString(const std::string& wildcard_string) const { | 117 int Version::CompareToWildcardString(const std::string& wildcard_string) const { |
116 DCHECK(IsValid()); | 118 DCHECK(IsValid()); |
117 DCHECK(Version::IsValidWildcardString(wildcard_string)); | 119 DCHECK(Version::IsValidWildcardString(wildcard_string)); |
118 | 120 |
119 // Default behavior if the string doesn't end with a wildcard. | 121 // Default behavior if the string doesn't end with a wildcard. |
120 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { | 122 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) { |
121 Version version(wildcard_string); | 123 Version version(wildcard_string); |
122 DCHECK(version.IsValid()); | 124 DCHECK(version.IsValid()); |
123 return CompareTo(version); | 125 return CompareTo(version); |
124 } | 126 } |
125 | 127 |
126 std::vector<uint32_t> parsed; | 128 std::vector<uint32_t> parsed; |
127 const bool success = ParseVersionNumbers( | 129 const bool success = ParseVersionNumbers( |
128 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); | 130 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); |
129 DCHECK(success); | 131 DCHECK(success); |
130 const int comparison = CompareVersionComponents(components_, parsed); | 132 const int comparison = CompareVersionComponents(components_, parsed); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 size_t count = components_.size(); | 169 size_t count = components_.size(); |
168 for (size_t i = 0; i < count - 1; ++i) { | 170 for (size_t i = 0; i < count - 1; ++i) { |
169 version_str.append(IntToString(components_[i])); | 171 version_str.append(IntToString(components_[i])); |
170 version_str.append("."); | 172 version_str.append("."); |
171 } | 173 } |
172 version_str.append(IntToString(components_[count - 1])); | 174 version_str.append(IntToString(components_[count - 1])); |
173 return version_str; | 175 return version_str; |
174 } | 176 } |
175 | 177 |
176 } // namespace base | 178 } // namespace base |
OLD | NEW |