OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <vector> | 5 #include "base/version.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "base/string_util.h" | 8 #include "base/string_util.h" |
8 #include "base/version.h" | |
9 | 9 |
10 // static | 10 // static |
11 Version* Version::GetVersionFromString(const std::wstring& version_str) { | 11 Version* Version::GetVersionFromString(const std::wstring& version_str) { |
12 if (!IsStringASCII(version_str)) | 12 if (!IsStringASCII(version_str)) |
13 return NULL; | 13 return NULL; |
14 return GetVersionFromString(WideToASCII(version_str)); | 14 return GetVersionFromString(WideToASCII(version_str)); |
15 } | 15 } |
16 | 16 |
17 // static | 17 // static |
18 Version* Version::GetVersionFromString(const std::string& version_str) { | 18 Version* Version::GetVersionFromString(const std::string& version_str) { |
19 Version* vers = new Version(); | 19 Version* vers = new Version(); |
20 if (vers->InitFromString(version_str)) | 20 if (vers->InitFromString(version_str)) { |
| 21 DCHECK(vers->is_valid_); |
21 return vers; | 22 return vers; |
| 23 } |
22 delete vers; | 24 delete vers; |
23 return NULL; | 25 return NULL; |
24 } | 26 } |
25 | 27 |
| 28 Version::Version() : is_valid_(false) {} |
| 29 |
26 bool Version::Equals(const Version& that) const { | 30 bool Version::Equals(const Version& that) const { |
| 31 DCHECK(is_valid_); |
| 32 DCHECK(that.is_valid_); |
27 return CompareTo(that) == 0; | 33 return CompareTo(that) == 0; |
28 } | 34 } |
29 | 35 |
30 int Version::CompareTo(const Version& other) const { | 36 int Version::CompareTo(const Version& other) const { |
31 std::vector<uint16> other_components = other.components(); | 37 DCHECK(is_valid_); |
32 size_t count = std::min(components_.size(), other_components.size()); | 38 DCHECK(other.is_valid_); |
| 39 size_t count = std::min(components_.size(), other.components_.size()); |
33 for (size_t i = 0; i < count; ++i) { | 40 for (size_t i = 0; i < count; ++i) { |
34 if (components_[i] > other_components[i]) | 41 if (components_[i] > other.components_[i]) |
35 return 1; | 42 return 1; |
36 if (components_[i] < other_components[i]) | 43 if (components_[i] < other.components_[i]) |
37 return -1; | 44 return -1; |
38 } | 45 } |
39 if (components_.size() > other_components.size()) { | 46 if (components_.size() > other.components_.size()) { |
40 for (size_t i = count; i < components_.size(); ++i) | 47 for (size_t i = count; i < components_.size(); ++i) |
41 if (components_[i] > 0) | 48 if (components_[i] > 0) |
42 return 1; | 49 return 1; |
43 } else if (components_.size() < other_components.size()) { | 50 } else if (components_.size() < other.components_.size()) { |
44 for (size_t i = count; i < other_components.size(); ++i) | 51 for (size_t i = count; i < other.components_.size(); ++i) |
45 if (other_components[i] > 0) | 52 if (other.components_[i] > 0) |
46 return -1; | 53 return -1; |
47 } | 54 } |
48 return 0; | 55 return 0; |
49 } | 56 } |
50 | 57 |
51 const std::string Version::GetString() const { | 58 const std::string Version::GetString() const { |
| 59 DCHECK(is_valid_); |
52 std::string version_str; | 60 std::string version_str; |
53 int count = components_.size(); | 61 int count = components_.size(); |
54 for (int i = 0; i < count - 1; ++i) { | 62 for (int i = 0; i < count - 1; ++i) { |
55 version_str.append(IntToString(components_[i])); | 63 version_str.append(IntToString(components_[i])); |
56 version_str.append("."); | 64 version_str.append("."); |
57 } | 65 } |
58 version_str.append(IntToString(components_[count - 1])); | 66 version_str.append(IntToString(components_[count - 1])); |
59 return version_str; | 67 return version_str; |
60 } | 68 } |
61 | 69 |
62 bool Version::InitFromString(const std::string& version_str) { | 70 bool Version::InitFromString(const std::string& version_str) { |
| 71 DCHECK(!is_valid_); |
63 std::vector<std::string> numbers; | 72 std::vector<std::string> numbers; |
64 SplitString(version_str, '.', &numbers); | 73 SplitString(version_str, '.', &numbers); |
| 74 if (numbers.empty()) |
| 75 return false; |
65 for (std::vector<std::string>::iterator i = numbers.begin(); | 76 for (std::vector<std::string>::iterator i = numbers.begin(); |
66 i != numbers.end(); ++i) { | 77 i != numbers.end(); ++i) { |
67 int num; | 78 int num; |
68 if (!StringToInt(*i, &num)) | 79 if (!StringToInt(*i, &num)) |
69 return false; | 80 return false; |
70 if (num < 0) | 81 if (num < 0) |
71 return false; | 82 return false; |
72 const uint16 max = 0xFFFF; | 83 const uint16 max = 0xFFFF; |
73 if (num > max) | 84 if (num > max) |
74 return false; | 85 return false; |
75 // This throws out things like +3, or 032. | 86 // This throws out things like +3, or 032. |
76 if (IntToString(num) != *i) | 87 if (IntToString(num) != *i) |
77 return false; | 88 return false; |
78 uint16 component = static_cast<uint16>(num); | 89 uint16 component = static_cast<uint16>(num); |
79 components_.push_back(component); | 90 components_.push_back(component); |
80 } | 91 } |
| 92 is_valid_ = true; |
81 return true; | 93 return true; |
82 } | 94 } |
OLD | NEW |