[email protected] | 4e5ae20f | 2010-09-24 04:52:11 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 5 | #include "base/version.h" |
6 | |||||
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 7 | #include <algorithm> |
8 | |||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 10 | #include "base/string_number_conversions.h" |
[email protected] | 4e5ae20f | 2010-09-24 04:52:11 | [diff] [blame] | 11 | #include "base/string_split.h" |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 12 | #include "base/string_util.h" |
13 | |||||
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 14 | Version::Version() { |
15 | } | ||||
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 16 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 17 | Version::Version(const std::string& version_str) { |
18 | std::vector<std::string> numbers; | ||||
19 | base::SplitString(version_str, '.', &numbers); | ||||
20 | if (numbers.empty()) | ||||
21 | return; | ||||
22 | std::vector<uint16> parsed; | ||||
23 | for (std::vector<std::string>::iterator i = numbers.begin(); | ||||
24 | i != numbers.end(); ++i) { | ||||
25 | int num; | ||||
26 | if (!base::StringToInt(*i, &num)) | ||||
27 | return; | ||||
28 | if (num < 0) | ||||
29 | return; | ||||
30 | const uint16 max = 0xFFFF; | ||||
31 | if (num > max) | ||||
32 | return; | ||||
33 | // This throws out things like +3, or 032. | ||||
34 | if (base::IntToString(num) != *i) | ||||
35 | return; | ||||
36 | parsed.push_back(static_cast<uint16>(num)); | ||||
37 | } | ||||
38 | components_.swap(parsed); | ||||
39 | } | ||||
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 40 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 41 | bool Version::IsValid() const { |
42 | return (!components_.empty()); | ||||
43 | } | ||||
44 | |||||
45 | // TODO(cpu): remove this method. | ||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 46 | Version* Version::GetVersionFromString(const std::string& version_str) { |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 47 | Version* vers = new Version(version_str); |
48 | if (vers->IsValid()) { | ||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 49 | return vers; |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 50 | } |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 51 | delete vers; |
52 | return NULL; | ||||
53 | } | ||||
54 | |||||
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 55 | // TODO(cpu): remove this method. |
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 56 | Version* Version::Clone() const { |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 57 | DCHECK(IsValid()); |
58 | return new Version(*this); | ||||
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 59 | } |
60 | |||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 61 | bool Version::Equals(const Version& that) const { |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 62 | DCHECK(IsValid()); |
63 | DCHECK(that.IsValid()); | ||||
64 | return (CompareTo(that) == 0); | ||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 65 | } |
66 | |||||
67 | int Version::CompareTo(const Version& other) const { | ||||
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 68 | DCHECK(IsValid()); |
69 | DCHECK(other.IsValid()); | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 70 | size_t count = std::min(components_.size(), other.components_.size()); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 71 | for (size_t i = 0; i < count; ++i) { |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 72 | if (components_[i] > other.components_[i]) |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 73 | return 1; |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 74 | if (components_[i] < other.components_[i]) |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 75 | return -1; |
76 | } | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 77 | if (components_.size() > other.components_.size()) { |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 78 | for (size_t i = count; i < components_.size(); ++i) |
79 | if (components_[i] > 0) | ||||
80 | return 1; | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 81 | } else if (components_.size() < other.components_.size()) { |
82 | for (size_t i = count; i < other.components_.size(); ++i) | ||||
83 | if (other.components_[i] > 0) | ||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 84 | return -1; |
85 | } | ||||
86 | return 0; | ||||
87 | } | ||||
88 | |||||
89 | const std::string Version::GetString() const { | ||||
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame^] | 90 | DCHECK(IsValid()); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 91 | std::string version_str; |
[email protected] | 6dc910c | 2010-11-10 17:02:19 | [diff] [blame] | 92 | size_t count = components_.size(); |
93 | for (size_t i = 0; i < count - 1; ++i) { | ||||
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 94 | version_str.append(base::IntToString(components_[i])); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 95 | version_str.append("."); |
96 | } | ||||
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 97 | version_str.append(base::IntToString(components_[count - 1])); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 98 | return version_str; |
99 | } |