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 | |
| 48 | // TODO(cpu): remove this method. |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 49 | Version* Version::GetVersionFromString(const std::string& version_str) { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 50 | Version* vers = new Version(version_str); |
| 51 | if (vers->IsValid()) { |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 52 | return vers; |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 53 | } |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 54 | delete vers; |
| 55 | return NULL; |
| 56 | } |
| 57 | |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 58 | // TODO(cpu): remove this method. |
jkummerow@chromium.org | df3e671 | 2010-12-21 17:27:25 +0900 | [diff] [blame] | 59 | Version* Version::Clone() const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 60 | DCHECK(IsValid()); |
| 61 | return new Version(*this); |
jkummerow@chromium.org | df3e671 | 2010-12-21 17:27:25 +0900 | [diff] [blame] | 62 | } |
| 63 | |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 64 | bool Version::Equals(const Version& that) const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 65 | DCHECK(IsValid()); |
| 66 | DCHECK(that.IsValid()); |
| 67 | return (CompareTo(that) == 0); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int Version::CompareTo(const Version& other) const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 71 | DCHECK(IsValid()); |
| 72 | DCHECK(other.IsValid()); |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 73 | size_t count = std::min(components_.size(), other.components_.size()); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 74 | for (size_t i = 0; i < count; ++i) { |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 75 | if (components_[i] > other.components_[i]) |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 76 | return 1; |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 77 | if (components_[i] < other.components_[i]) |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 78 | return -1; |
| 79 | } |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [diff] [blame] | 80 | if (components_.size() > other.components_.size()) { |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 81 | for (size_t i = count; i < components_.size(); ++i) |
| 82 | if (components_[i] > 0) |
| 83 | return 1; |
akalin@chromium.org | 2659ef1 | 2010-03-26 07:19:04 +0900 | [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) |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | const std::string Version::GetString() const { |
cpu@chromium.org | fa761a3 | 2011-06-08 02:21:30 +0900 | [diff] [blame] | 93 | DCHECK(IsValid()); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 94 | std::string version_str; |
robertshield@chromium.org | a9914a3 | 2010-11-11 02:02:19 +0900 | [diff] [blame] | 95 | size_t count = components_.size(); |
| 96 | for (size_t i = 0; i < count - 1; ++i) { |
brettw@chromium.org | cbde69a | 2010-07-31 04:28:44 +0900 | [diff] [blame] | 97 | version_str.append(base::IntToString(components_[i])); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 98 | version_str.append("."); |
| 99 | } |
brettw@chromium.org | cbde69a | 2010-07-31 04:28:44 +0900 | [diff] [blame] | 100 | version_str.append(base::IntToString(components_[count - 1])); |
erikkay@google.com | e05e3c6 | 2009-01-30 04:18:57 +0900 | [diff] [blame] | 101 | return version_str; |
| 102 | } |