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