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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
13 | 13 |
| 14 namespace base { |
| 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 // Parses the |numbers| vector representing the different numbers | 18 // Parses the |numbers| vector representing the different numbers |
17 // inside the version string and constructs a vector of valid integers. It stops | 19 // 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| | 20 // when it reaches an invalid item (including the wildcard character). |parsed| |
19 // is the resulting integer vector. Function returns true if all numbers were | 21 // is the resulting integer vector. Function returns true if all numbers were |
20 // parsed successfully, false otherwise. | 22 // parsed successfully, false otherwise. |
21 bool ParseVersionNumbers(const std::string& version_str, | 23 bool ParseVersionNumbers(const std::string& version_str, |
22 std::vector<uint16>* parsed) { | 24 std::vector<uint16>* parsed) { |
23 std::vector<std::string> numbers; | 25 std::vector<std::string> numbers; |
24 base::SplitString(version_str, '.', &numbers); | 26 SplitString(version_str, '.', &numbers); |
25 if (numbers.empty()) | 27 if (numbers.empty()) |
26 return false; | 28 return false; |
27 | 29 |
28 for (std::vector<std::string>::const_iterator it = numbers.begin(); | 30 for (std::vector<std::string>::const_iterator it = numbers.begin(); |
29 it != numbers.end(); ++it) { | 31 it != numbers.end(); ++it) { |
30 int num; | 32 int num; |
31 if (!base::StringToInt(*it, &num)) | 33 if (!StringToInt(*it, &num)) |
32 return false; | 34 return false; |
33 | 35 |
34 if (num < 0) | 36 if (num < 0) |
35 return false; | 37 return false; |
36 | 38 |
37 const uint16 max = 0xFFFF; | 39 const uint16 max = 0xFFFF; |
38 if (num > max) | 40 if (num > max) |
39 return false; | 41 return false; |
40 | 42 |
41 // This throws out things like +3, or 032. | 43 // This throws out things like +3, or 032. |
42 if (base::IntToString(num) != *it) | 44 if (IntToString(num) != *it) |
43 return false; | 45 return false; |
44 | 46 |
45 parsed->push_back(static_cast<uint16>(num)); | 47 parsed->push_back(static_cast<uint16>(num)); |
46 } | 48 } |
47 return true; | 49 return true; |
48 } | 50 } |
49 | 51 |
50 // Compares version components in |components1| with components in | 52 // Compares version components in |components1| with components in |
51 // |components2|. Returns -1, 0 or 1 if |components1| is greater than, equal to, | 53 // |components2|. Returns -1, 0 or 1 if |components1| is greater than, equal to, |
52 // or less than |components2|, respectively. | 54 // or less than |components2|, respectively. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 DCHECK(IsValid()); | 159 DCHECK(IsValid()); |
158 DCHECK(other.IsValid()); | 160 DCHECK(other.IsValid()); |
159 return CompareVersionComponents(components_, other.components_); | 161 return CompareVersionComponents(components_, other.components_); |
160 } | 162 } |
161 | 163 |
162 const std::string Version::GetString() const { | 164 const std::string Version::GetString() const { |
163 DCHECK(IsValid()); | 165 DCHECK(IsValid()); |
164 std::string version_str; | 166 std::string version_str; |
165 size_t count = components_.size(); | 167 size_t count = components_.size(); |
166 for (size_t i = 0; i < count - 1; ++i) { | 168 for (size_t i = 0; i < count - 1; ++i) { |
167 version_str.append(base::IntToString(components_[i])); | 169 version_str.append(IntToString(components_[i])); |
168 version_str.append("."); | 170 version_str.append("."); |
169 } | 171 } |
170 version_str.append(base::IntToString(components_[count - 1])); | 172 version_str.append(IntToString(components_[count - 1])); |
171 return version_str; | 173 return version_str; |
172 } | 174 } |
| 175 |
| 176 } // namespace base |
OLD | NEW |