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 { | |
Alexei Svitkine (slow)
2012/06/18 22:03:44
Please add an empty line between the start of the
Mathieu
2012/06/19 14:30:20
Done.
| |
15 bool ParseVersionNumbers(const std::vector<std::string>& numbers, | |
16 std::vector<uint16>& parsed) { | |
Alexei Svitkine (slow)
2012/06/18 22:03:44
Align second parameter.
Mathieu
2012/06/19 14:30:20
Done.
| |
17 for (std::vector<std::string>::const_iterator i = numbers.begin(); | |
18 i != numbers.end(); ++i) { | |
19 int num; | |
20 if (!base::StringToInt(*i, &num)) | |
21 return false; | |
22 if (num < 0) | |
23 return false; | |
24 const uint16 max = 0xFFFF; | |
25 if (num > max) | |
26 return false; | |
27 // This throws out things like +3, or 032. | |
28 if (base::IntToString(num) != *i) | |
29 return false; | |
30 parsed.push_back(static_cast<uint16>(num)); | |
31 } | |
32 return true; | |
33 } | |
34 } | |
Alexei Svitkine (slow)
2012/06/18 22:03:44
The namespace should end with a comment, like so:
Mathieu
2012/06/19 14:30:20
Done.
| |
35 | |
14 Version::Version() { | 36 Version::Version() { |
15 } | 37 } |
16 | 38 |
17 Version::~Version() { | 39 Version::~Version() { |
18 } | 40 } |
19 | 41 |
20 Version::Version(const std::string& version_str) { | 42 Version::Version(const std::string& version_str) { |
21 std::vector<std::string> numbers; | 43 std::vector<std::string> numbers; |
22 base::SplitString(version_str, '.', &numbers); | 44 base::SplitString(version_str, '.', &numbers); |
23 if (numbers.empty()) | 45 if (numbers.empty()) |
24 return; | 46 return; |
25 std::vector<uint16> parsed; | 47 std::vector<uint16> parsed; |
26 for (std::vector<std::string>::iterator i = numbers.begin(); | 48 if (!ParseVersionNumbers(numbers, parsed)) |
27 i != numbers.end(); ++i) { | 49 return; |
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); | 50 components_.swap(parsed); |
42 } | 51 } |
43 | 52 |
44 bool Version::IsValid() const { | 53 bool Version::IsValid() const { |
45 return (!components_.empty()); | 54 return (!components_.empty()); |
46 } | 55 } |
47 | 56 |
57 bool Version::IsValidWildcard(const std::string& version_str) { | |
Alexei Svitkine (slow)
2012/06/18 22:03:44
For static methods, the convention is to put a com
Mathieu
2012/06/19 14:30:20
Done.
| |
58 if (version_str.at(version_str.length() - 1) != '*') { | |
59 Version version(version_str); | |
Alexei Svitkine (slow)
2012/06/18 22:03:44
Indent this better.
Mathieu
2012/06/19 14:30:20
Done.
| |
60 return version.IsValid(); | |
61 } else { | |
62 std::string no_wc_version_str = version_str.substr(0, | |
63 version_str.size() - 2); | |
64 Version version(no_wc_version_str); | |
65 return version.IsValid(); | |
66 } | |
67 } | |
68 | |
48 bool Version::IsOlderThan(const std::string& version_str) const { | 69 bool Version::IsOlderThan(const std::string& version_str) const { |
49 Version proposed_ver(version_str); | 70 Version proposed_ver(version_str); |
50 if (!proposed_ver.IsValid()) | 71 if (!proposed_ver.IsValid()) |
51 return false; | 72 return false; |
52 return (CompareTo(proposed_ver) < 0); | 73 return (CompareTo(proposed_ver) < 0); |
53 } | 74 } |
54 | 75 |
76 int Version::CompareToWildcard(const std::string& version_str) const { | |
77 DCHECK(IsValid()); | |
78 DCHECK(Version::IsValidWildcard(version_str)); | |
79 | |
80 // Default behavior if the string doesn't end with a wildcard. | |
81 if (version_str.at(version_str.length() - 1) != '*') { | |
82 Version version(version_str); | |
83 DCHECK(version.IsValid()); | |
84 return CompareTo(version); | |
85 } | |
86 | |
87 // Removing the * and returning immediately if the argument version is less | |
88 // or equal (the * won't affect here). | |
89 Version other_version(version_str.substr(0, version_str.size() - 2)); | |
90 int comparison = CompareTo(other_version); | |
91 if (comparison == -1 || comparison == 0) | |
92 return comparison; | |
93 | |
94 // Trying to catch the equality case 1.2 vs 1.*. In all other cases, it truly | |
95 // is a "greater than" case. | |
96 std::vector<std::string> other_numbers; | |
97 base::SplitString(version_str, '.', &other_numbers); | |
98 std::vector<uint16> parsed; | |
99 ParseVersionNumbers(other_numbers, parsed); // Will not parse the *. | |
100 | |
101 DCHECK_GT(parsed.size(), 0); | |
102 for (size_t i = 0; i < parsed.size(); ++i) { | |
103 if (components_[i] != parsed[i]) | |
104 return 1; | |
105 } | |
106 return 0; | |
107 } | |
108 | |
55 // TODO(cpu): remove this method. | 109 // TODO(cpu): remove this method. |
56 Version* Version::GetVersionFromString(const std::string& version_str) { | 110 Version* Version::GetVersionFromString(const std::string& version_str) { |
57 Version* vers = new Version(version_str); | 111 Version* vers = new Version(version_str); |
58 if (vers->IsValid()) { | 112 if (vers->IsValid()) { |
59 return vers; | 113 return vers; |
60 } | 114 } |
61 delete vers; | 115 delete vers; |
62 return NULL; | 116 return NULL; |
63 } | 117 } |
64 | 118 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 DCHECK(IsValid()); | 154 DCHECK(IsValid()); |
101 std::string version_str; | 155 std::string version_str; |
102 size_t count = components_.size(); | 156 size_t count = components_.size(); |
103 for (size_t i = 0; i < count - 1; ++i) { | 157 for (size_t i = 0; i < count - 1; ++i) { |
104 version_str.append(base::IntToString(components_[i])); | 158 version_str.append(base::IntToString(components_[i])); |
105 version_str.append("."); | 159 version_str.append("."); |
106 } | 160 } |
107 version_str.append(base::IntToString(components_[count - 1])); | 161 version_str.append(base::IntToString(components_[count - 1])); |
108 return version_str; | 162 return version_str; |
109 } | 163 } |
OLD | NEW |