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_t>* 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 unsigned int num; |
37 if (!StringToInt(*it, &num)) | 37 if (!StringToUint(*it, &num)) |
38 return false; | |
39 | |
40 if (num < 0) | |
41 return false; | |
42 | |
43 const uint16 max = 0xFFFF; | |
44 if (num > max) | |
45 return false; | 38 return false; |
46 | 39 |
47 // This throws out leading zeros for the first item only. | 40 // This throws out leading zeros for the first item only. |
48 if (it == numbers.begin() && IntToString(num) != *it) | 41 if (it == numbers.begin() && UintToString(num) != *it) |
49 return false; | 42 return false; |
50 | 43 |
51 parsed->push_back(static_cast<uint16>(num)); | 44 // StringToUint returns unsigned int but Version fields are uint32_t. |
| 45 static_assert(sizeof (uint32_t) == sizeof (unsigned int), |
| 46 "uint32_t must be same as unsigned int"); |
| 47 parsed->push_back(num); |
52 } | 48 } |
53 return true; | 49 return true; |
54 } | 50 } |
55 | 51 |
56 // Compares version components in |components1| with components in | 52 // Compares version components in |components1| with components in |
57 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to, | 53 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to, |
58 // or greater than |components2|, respectively. | 54 // or greater than |components2|, respectively. |
59 int CompareVersionComponents(const std::vector<uint16>& components1, | 55 int CompareVersionComponents(const std::vector<uint32_t>& components1, |
60 const std::vector<uint16>& components2) { | 56 const std::vector<uint32_t>& components2) { |
61 const size_t count = std::min(components1.size(), components2.size()); | 57 const size_t count = std::min(components1.size(), components2.size()); |
62 for (size_t i = 0; i < count; ++i) { | 58 for (size_t i = 0; i < count; ++i) { |
63 if (components1[i] > components2[i]) | 59 if (components1[i] > components2[i]) |
64 return 1; | 60 return 1; |
65 if (components1[i] < components2[i]) | 61 if (components1[i] < components2[i]) |
66 return -1; | 62 return -1; |
67 } | 63 } |
68 if (components1.size() > components2.size()) { | 64 if (components1.size() > components2.size()) { |
69 for (size_t i = count; i < components1.size(); ++i) { | 65 for (size_t i = count; i < components1.size(); ++i) { |
70 if (components1[i] > 0) | 66 if (components1[i] > 0) |
(...skipping 10 matching lines...) Expand all Loading... |
81 | 77 |
82 } // namespace | 78 } // namespace |
83 | 79 |
84 Version::Version() { | 80 Version::Version() { |
85 } | 81 } |
86 | 82 |
87 Version::~Version() { | 83 Version::~Version() { |
88 } | 84 } |
89 | 85 |
90 Version::Version(const std::string& version_str) { | 86 Version::Version(const std::string& version_str) { |
91 std::vector<uint16> parsed; | 87 std::vector<uint32_t> parsed; |
92 if (!ParseVersionNumbers(version_str, &parsed)) | 88 if (!ParseVersionNumbers(version_str, &parsed)) |
93 return; | 89 return; |
94 | 90 |
95 components_.swap(parsed); | 91 components_.swap(parsed); |
96 } | 92 } |
97 | 93 |
98 bool Version::IsValid() const { | 94 bool Version::IsValid() const { |
99 return (!components_.empty()); | 95 return (!components_.empty()); |
100 } | 96 } |
101 | 97 |
(...skipping 18 matching lines...) Expand all Loading... |
120 DCHECK(IsValid()); | 116 DCHECK(IsValid()); |
121 DCHECK(Version::IsValidWildcardString(wildcard_string)); | 117 DCHECK(Version::IsValidWildcardString(wildcard_string)); |
122 | 118 |
123 // Default behavior if the string doesn't end with a wildcard. | 119 // Default behavior if the string doesn't end with a wildcard. |
124 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { | 120 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { |
125 Version version(wildcard_string); | 121 Version version(wildcard_string); |
126 DCHECK(version.IsValid()); | 122 DCHECK(version.IsValid()); |
127 return CompareTo(version); | 123 return CompareTo(version); |
128 } | 124 } |
129 | 125 |
130 std::vector<uint16> parsed; | 126 std::vector<uint32_t> parsed; |
131 const bool success = ParseVersionNumbers( | 127 const bool success = ParseVersionNumbers( |
132 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); | 128 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); |
133 DCHECK(success); | 129 DCHECK(success); |
134 const int comparison = CompareVersionComponents(components_, parsed); | 130 const int comparison = CompareVersionComponents(components_, parsed); |
135 // If the version is smaller than the wildcard version's |parsed| vector, | 131 // 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 | 132 // 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 | 133 // 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, | 134 // 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. | 135 // 1.2.0.0.0.0 compared to 1.2.* is 0. |
140 if (comparison == -1 || comparison == 0) | 136 if (comparison == -1 || comparison == 0) |
(...skipping 30 matching lines...) Expand all Loading... |
171 size_t count = components_.size(); | 167 size_t count = components_.size(); |
172 for (size_t i = 0; i < count - 1; ++i) { | 168 for (size_t i = 0; i < count - 1; ++i) { |
173 version_str.append(IntToString(components_[i])); | 169 version_str.append(IntToString(components_[i])); |
174 version_str.append("."); | 170 version_str.append("."); |
175 } | 171 } |
176 version_str.append(IntToString(components_[count - 1])); | 172 version_str.append(IntToString(components_[count - 1])); |
177 return version_str; | 173 return version_str; |
178 } | 174 } |
179 | 175 |
180 } // namespace base | 176 } // namespace base |
OLD | NEW |