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
3 // found in the LICENSE file.
5 #include "base/version.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
20 // Parses the |numbers| vector representing the different numbers
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|
23 // is the resulting integer vector. Function returns true if all numbers were
24 // parsed successfully, false otherwise.
25 bool ParseVersionNumbers(const std::string
& version_str
,
26 std::vector
<uint16
>* parsed
) {
27 std::vector
<std::string
> numbers
;
28 SplitString(version_str
, '.', &numbers
);
32 for (std::vector
<std::string
>::const_iterator it
= numbers
.begin();
33 it
!= numbers
.end(); ++it
) {
34 if (StartsWithASCII(*it
, "+", false))
37 if (!StringToInt(*it
, &num
))
43 const uint16 max
= 0xFFFF;
47 // This throws out leading zeros for the first item only.
48 if (it
== numbers
.begin() && IntToString(num
) != *it
)
51 parsed
->push_back(static_cast<uint16
>(num
));
56 // Compares version components in |components1| with components in
57 // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
58 // or greater than |components2|, respectively.
59 int CompareVersionComponents(const std::vector
<uint16
>& components1
,
60 const std::vector
<uint16
>& components2
) {
61 const size_t count
= std::min(components1
.size(), components2
.size());
62 for (size_t i
= 0; i
< count
; ++i
) {
63 if (components1
[i
] > components2
[i
])
65 if (components1
[i
] < components2
[i
])
68 if (components1
.size() > components2
.size()) {
69 for (size_t i
= count
; i
< components1
.size(); ++i
) {
70 if (components1
[i
] > 0)
73 } else if (components1
.size() < components2
.size()) {
74 for (size_t i
= count
; i
< components2
.size(); ++i
) {
75 if (components2
[i
] > 0)
90 Version::Version(const std::string
& version_str
) {
91 std::vector
<uint16
> parsed
;
92 if (!ParseVersionNumbers(version_str
, &parsed
))
95 components_
.swap(parsed
);
98 bool Version::IsValid() const {
99 return (!components_
.empty());
103 bool Version::IsValidWildcardString(const std::string
& wildcard_string
) {
104 std::string version_string
= wildcard_string
;
105 if (EndsWith(wildcard_string
.c_str(), ".*", false))
106 version_string
= wildcard_string
.substr(0, wildcard_string
.size() - 2);
108 Version
version(version_string
);
109 return version
.IsValid();
112 bool Version::IsOlderThan(const std::string
& version_str
) const {
113 Version
proposed_ver(version_str
);
114 if (!proposed_ver
.IsValid())
116 return (CompareTo(proposed_ver
) < 0);
119 int Version::CompareToWildcardString(const std::string
& wildcard_string
) const {
121 DCHECK(Version::IsValidWildcardString(wildcard_string
));
123 // Default behavior if the string doesn't end with a wildcard.
124 if (!EndsWith(wildcard_string
.c_str(), ".*", false)) {
125 Version
version(wildcard_string
);
126 DCHECK(version
.IsValid());
127 return CompareTo(version
);
130 std::vector
<uint16
> parsed
;
131 const bool success
= ParseVersionNumbers(
132 wildcard_string
.substr(0, wildcard_string
.length() - 2), &parsed
);
134 const int comparison
= CompareVersionComponents(components_
, parsed
);
135 // 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
137 // 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,
139 // 1.2.0.0.0.0 compared to 1.2.* is 0.
140 if (comparison
== -1 || comparison
== 0)
143 // Catch the case where the digits of |parsed| are found in |components_|,
144 // which means that the two are equal since |parsed| has a trailing "*".
145 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since
146 // components is greater (e.g. 3.2.3 vs 1.*).
147 DCHECK_GT(parsed
.size(), 0UL);
148 const size_t min_num_comp
= std::min(components_
.size(), parsed
.size());
149 for (size_t i
= 0; i
< min_num_comp
; ++i
) {
150 if (components_
[i
] != parsed
[i
])
156 bool Version::Equals(const Version
& that
) const {
158 DCHECK(that
.IsValid());
159 return (CompareTo(that
) == 0);
162 int Version::CompareTo(const Version
& other
) const {
164 DCHECK(other
.IsValid());
165 return CompareVersionComponents(components_
, other
.components_
);
168 const std::string
Version::GetString() const {
170 std::string version_str
;
171 size_t count
= components_
.size();
172 for (size_t i
= 0; i
< count
- 1; ++i
) {
173 version_str
.append(IntToString(components_
[i
]));
174 version_str
.append(".");
176 version_str
.append(IntToString(components_
[count
- 1]));