1 // Copyright (c) 2010 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"
9 #include "base/logging.h"
10 #include "base/string_number_conversions.h"
11 #include "base/string_split.h"
12 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h"
15 Version::Version() : is_valid_(false) {}
17 Version::~Version() {}
20 Version
* Version::GetVersionFromString(const std::string
& version_str
) {
21 Version
* vers
= new Version();
22 if (vers
->InitFromString(version_str
)) {
23 DCHECK(vers
->is_valid_
);
30 Version
* Version::Clone() const {
32 Version
* copy
= new Version();
33 copy
->components_
= components_
;
34 copy
->is_valid_
= true;
38 bool Version::Equals(const Version
& that
) const {
40 DCHECK(that
.is_valid_
);
41 return CompareTo(that
) == 0;
44 int Version::CompareTo(const Version
& other
) const {
46 DCHECK(other
.is_valid_
);
47 size_t count
= std::min(components_
.size(), other
.components_
.size());
48 for (size_t i
= 0; i
< count
; ++i
) {
49 if (components_
[i
] > other
.components_
[i
])
51 if (components_
[i
] < other
.components_
[i
])
54 if (components_
.size() > other
.components_
.size()) {
55 for (size_t i
= count
; i
< components_
.size(); ++i
)
56 if (components_
[i
] > 0)
58 } else if (components_
.size() < other
.components_
.size()) {
59 for (size_t i
= count
; i
< other
.components_
.size(); ++i
)
60 if (other
.components_
[i
] > 0)
66 const std::string
Version::GetString() const {
68 std::string version_str
;
69 size_t count
= components_
.size();
70 for (size_t i
= 0; i
< count
- 1; ++i
) {
71 version_str
.append(base::IntToString(components_
[i
]));
72 version_str
.append(".");
74 version_str
.append(base::IntToString(components_
[count
- 1]));
78 bool Version::InitFromString(const std::string
& version_str
) {
80 std::vector
<std::string
> numbers
;
81 base::SplitString(version_str
, '.', &numbers
);
84 for (std::vector
<std::string
>::iterator i
= numbers
.begin();
85 i
!= numbers
.end(); ++i
) {
87 if (!base::StringToInt(*i
, &num
))
91 const uint16 max
= 0xFFFF;
94 // This throws out things like +3, or 032.
95 if (base::IntToString(num
) != *i
)
97 uint16 component
= static_cast<uint16
>(num
);
98 components_
.push_back(component
);