[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 | |||||
[email protected] | 30c157c | 2011-08-01 17:45:08 | [diff] [blame^] | 48 | bool Version::IsOlderThan(const std::string& version_str) const { |
49 | Version proposed_ver(version_str); | ||||
50 | if (!proposed_ver.IsValid()) | ||||
51 | return false; | ||||
52 | return (CompareTo(proposed_ver) < 0); | ||||
53 | } | ||||
54 | |||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 55 | // TODO(cpu): remove this method. |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 56 | Version* Version::GetVersionFromString(const std::string& version_str) { |
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 57 | Version* vers = new Version(version_str); |
58 | if (vers->IsValid()) { | ||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 59 | return vers; |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 60 | } |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 61 | delete vers; |
62 | return NULL; | ||||
63 | } | ||||
64 | |||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 65 | // TODO(cpu): remove this method. |
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 66 | Version* Version::Clone() const { |
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 67 | DCHECK(IsValid()); |
68 | return new Version(*this); | ||||
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 69 | } |
70 | |||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 71 | bool Version::Equals(const Version& that) const { |
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 72 | DCHECK(IsValid()); |
73 | DCHECK(that.IsValid()); | ||||
74 | return (CompareTo(that) == 0); | ||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 75 | } |
76 | |||||
77 | int Version::CompareTo(const Version& other) const { | ||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 78 | DCHECK(IsValid()); |
79 | DCHECK(other.IsValid()); | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 80 | size_t count = std::min(components_.size(), other.components_.size()); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 81 | for (size_t i = 0; i < count; ++i) { |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 82 | if (components_[i] > other.components_[i]) |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 83 | return 1; |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 84 | if (components_[i] < other.components_[i]) |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 85 | return -1; |
86 | } | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 87 | if (components_.size() > other.components_.size()) { |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 88 | for (size_t i = count; i < components_.size(); ++i) |
89 | if (components_[i] > 0) | ||||
90 | return 1; | ||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 91 | } else if (components_.size() < other.components_.size()) { |
92 | for (size_t i = count; i < other.components_.size(); ++i) | ||||
93 | if (other.components_[i] > 0) | ||||
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 94 | return -1; |
95 | } | ||||
96 | return 0; | ||||
97 | } | ||||
98 | |||||
99 | const std::string Version::GetString() const { | ||||
[email protected] | 7600247 | 2011-06-07 17:21:30 | [diff] [blame] | 100 | DCHECK(IsValid()); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 101 | std::string version_str; |
[email protected] | 6dc910c | 2010-11-10 17:02:19 | [diff] [blame] | 102 | size_t count = components_.size(); |
103 | for (size_t i = 0; i < count - 1; ++i) { | ||||
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 104 | version_str.append(base::IntToString(components_[i])); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 105 | version_str.append("."); |
106 | } | ||||
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 107 | version_str.append(base::IntToString(components_[count - 1])); |
[email protected] | 19b8d82 | 2009-01-29 19:18:57 | [diff] [blame] | 108 | return version_str; |
109 | } |