[email protected] | 1513bf8 | 2011-06-07 17:43:20 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 19b8d82 | 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] | 19b8d82 | 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] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 14 | Version::Version() { |
15 | } | ||||
[email protected] | 9989c9b | 2011-01-07 20:23:43 | [diff] [blame] | 16 | |
[email protected] | 1513bf8 | 2011-06-07 17:43:20 | [diff] [blame^] | 17 | Version::~Version() { |
18 | } | ||||
19 | |||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 20 | Version::Version(const std::string& version_str) { |
21 | std::vector<std::string> numbers; | ||||
22 | base::SplitString(version_str, '.', &numbers); | ||||
23 | if (numbers.empty()) | ||||
24 | return; | ||||
25 | std::vector<uint16> parsed; | ||||
26 | for (std::vector<std::string>::iterator i = numbers.begin(); | ||||
27 | i != numbers.end(); ++i) { | ||||
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); | ||||
42 | } | ||||
[email protected] | 9989c9b | 2011-01-07 20:23:43 | [diff] [blame] | 43 | |
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 44 | bool Version::IsValid() const { |
45 | return (!components_.empty()); | ||||
46 | } | ||||
47 | |||||
48 | // TODO(cpu): remove this method. | ||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 49 | Version* Version::GetVersionFromString(const std::string& version_str) { |
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 50 | Version* vers = new Version(version_str); |
51 | if (vers->IsValid()) { | ||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 52 | return vers; |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 53 | } |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 54 | delete vers; |
55 | return NULL; | ||||
56 | } | ||||
57 | |||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 58 | // TODO(cpu): remove this method. |
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 59 | Version* Version::Clone() const { |
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 60 | DCHECK(IsValid()); |
61 | return new Version(*this); | ||||
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 62 | } |
63 | |||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 64 | bool Version::Equals(const Version& that) const { |
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 65 | DCHECK(IsValid()); |
66 | DCHECK(that.IsValid()); | ||||
67 | return (CompareTo(that) == 0); | ||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 68 | } |
69 | |||||
70 | int Version::CompareTo(const Version& other) const { | ||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 71 | DCHECK(IsValid()); |
72 | DCHECK(other.IsValid()); | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 73 | size_t count = std::min(components_.size(), other.components_.size()); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 74 | for (size_t i = 0; i < count; ++i) { |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 75 | if (components_[i] > other.components_[i]) |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 76 | return 1; |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 77 | if (components_[i] < other.components_[i]) |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 78 | return -1; |
79 | } | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 80 | if (components_.size() > other.components_.size()) { |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 81 | for (size_t i = count; i < components_.size(); ++i) |
82 | if (components_[i] > 0) | ||||
83 | return 1; | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 84 | } else if (components_.size() < other.components_.size()) { |
85 | for (size_t i = count; i < other.components_.size(); ++i) | ||||
86 | if (other.components_[i] > 0) | ||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 87 | return -1; |
88 | } | ||||
89 | return 0; | ||||
90 | } | ||||
91 | |||||
92 | const std::string Version::GetString() const { | ||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 93 | DCHECK(IsValid()); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 94 | std::string version_str; |
[email protected] | 6dc910c | 2010-11-10 17:02:19 | [diff] [blame] | 95 | size_t count = components_.size(); |
96 | for (size_t i = 0; i < count - 1; ++i) { | ||||
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 97 | version_str.append(base::IntToString(components_[i])); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 98 | version_str.append("."); |
99 | } | ||||
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 100 | version_str.append(base::IntToString(components_[count - 1])); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 101 | return version_str; |
102 | } |