cpu@chromium.org | 331e27b | 2011-06-08 02:43:20 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [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 | |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 5 | #include "base/version.h" |
| 6 | |
jkummerow@chromium.org | df3e671 | 2010-12-21 17:27:25 +0900 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 9 | #include "base/logging.h" |
brettw@chromium.org | cbde69a | 2010-07-31 04:28:44 +0900 | [diff] [blame] | 10 | #include "base/string_number_conversions.h" |
tfarina@chromium.org | e995a8b | 2010-09-24 13:52:11 +0900 | [diff] [blame] | 11 | #include "base/string_split.h" |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 12 | #include "base/string_util.h" |
| 13 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 14 | Version::Version() { |
| 15 | } |
erg@google.com | 67a2543 | 2011-01-08 05:23:43 +0900 | [diff] [blame] | 16 | |
cpu@chromium.org | 331e27b | 2011-06-08 02:43:20 +0900 | [diff] [blame] | 17 | Version::~Version() { |
| 18 | } |
| 19 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [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 | } |
erg@google.com | 67a2543 | 2011-01-08 05:23:43 +0900 | [diff] [blame] | 43 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 44 | bool Version::IsValid() const { |
| 45 | return (!components_.empty()); |
| 46 | } |
| 47 | |
cpu@chromium.org | 9008a61 | 2011-08-02 02:45:08 +0900 | [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 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 55 | // TODO(cpu): remove this method. |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 56 | Version* Version::GetVersionFromString(const std::string& version_str) { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 57 | Version* vers = new Version(version_str); |
| 58 | if (vers->IsValid()) { |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 59 | return vers; |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 60 | } |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 61 | delete vers; |
| 62 | return NULL; |
| 63 | } |
| 64 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 65 | // TODO(cpu): remove this method. |
jkummerow@chromium.org | df3e671 | 2010-12-21 17:27:25 +0900 | [diff] [blame] | 66 | Version* Version::Clone() const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 67 | DCHECK(IsValid()); |
| 68 | return new Version(*this); |
jkummerow@chromium.org | df3e671 | 2010-12-21 17:27:25 +0900 | [diff] [blame] | 69 | } |
| 70 | |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 71 | bool Version::Equals(const Version& that) const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 72 | DCHECK(IsValid()); |
| 73 | DCHECK(that.IsValid()); |
| 74 | return (CompareTo(that) == 0); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int Version::CompareTo(const Version& other) const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 78 | DCHECK(IsValid()); |
| 79 | DCHECK(other.IsValid()); |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 80 | size_t count = std::min(components_.size(), other.components_.size()); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 81 | for (size_t i = 0; i < count; ++i) { |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 82 | if (components_[i] > other.components_[i]) |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 83 | return 1; |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 84 | if (components_[i] < other.components_[i]) |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 85 | return -1; |
| 86 | } |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 87 | if (components_.size() > other.components_.size()) { |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 88 | for (size_t i = count; i < components_.size(); ++i) |
| 89 | if (components_[i] > 0) |
| 90 | return 1; |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [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) |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 94 | return -1; |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | const std::string Version::GetString() const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 100 | DCHECK(IsValid()); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 101 | std::string version_str; |
robertshield@chromium.org | a9914a3 | 2010-11-11 02:02:19 +0900 | [diff] [blame] | 102 | size_t count = components_.size(); |
| 103 | for (size_t i = 0; i < count - 1; ++i) { |
brettw@chromium.org | cbde69a | 2010-07-31 04:28:44 +0900 | [diff] [blame] | 104 | version_str.append(base::IntToString(components_[i])); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 105 | version_str.append("."); |
| 106 | } |
brettw@chromium.org | cbde69a | 2010-07-31 04:28:44 +0900 | [diff] [blame] | 107 | version_str.append(base::IntToString(components_[count - 1])); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 108 | return version_str; |
| 109 | } |