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