Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2865)

Unified Diff: base/version.cc

Issue 7105008: Clean up base/Version (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/version.h ('k') | base/version_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/version.cc
===================================================================
--- base/version.cc (revision 86383)
+++ base/version.cc (working copy)
@@ -10,40 +10,63 @@
#include "base/string_number_conversions.h"
#include "base/string_split.h"
#include "base/string_util.h"
-#include "base/utf_string_conversions.h"
-Version::Version() : is_valid_(false) {}
+Version::Version() {
+}
-Version::~Version() {}
+Version::Version(const std::string& version_str) {
+ std::vector<std::string> numbers;
+ base::SplitString(version_str, '.', &numbers);
+ if (numbers.empty())
+ return;
+ std::vector<uint16> parsed;
+ for (std::vector<std::string>::iterator i = numbers.begin();
+ i != numbers.end(); ++i) {
+ int num;
+ if (!base::StringToInt(*i, &num))
+ return;
+ if (num < 0)
+ return;
+ const uint16 max = 0xFFFF;
+ if (num > max)
+ return;
+ // This throws out things like +3, or 032.
+ if (base::IntToString(num) != *i)
+ return;
+ parsed.push_back(static_cast<uint16>(num));
+ }
+ components_.swap(parsed);
+}
-// static
+bool Version::IsValid() const {
+ return (!components_.empty());
+}
+
+// TODO(cpu): remove this method.
Version* Version::GetVersionFromString(const std::string& version_str) {
- Version* vers = new Version();
- if (vers->InitFromString(version_str)) {
- DCHECK(vers->is_valid_);
+ Version* vers = new Version(version_str);
+ if (vers->IsValid()) {
return vers;
}
delete vers;
return NULL;
}
+// TODO(cpu): remove this method.
Version* Version::Clone() const {
- DCHECK(is_valid_);
- Version* copy = new Version();
- copy->components_ = components_;
- copy->is_valid_ = true;
- return copy;
+ DCHECK(IsValid());
+ return new Version(*this);
}
bool Version::Equals(const Version& that) const {
- DCHECK(is_valid_);
- DCHECK(that.is_valid_);
- return CompareTo(that) == 0;
+ DCHECK(IsValid());
+ DCHECK(that.IsValid());
+ return (CompareTo(that) == 0);
}
int Version::CompareTo(const Version& other) const {
- DCHECK(is_valid_);
- DCHECK(other.is_valid_);
+ DCHECK(IsValid());
+ DCHECK(other.IsValid());
size_t count = std::min(components_.size(), other.components_.size());
for (size_t i = 0; i < count; ++i) {
if (components_[i] > other.components_[i])
@@ -64,7 +87,7 @@
}
const std::string Version::GetString() const {
- DCHECK(is_valid_);
+ DCHECK(IsValid());
std::string version_str;
size_t count = components_.size();
for (size_t i = 0; i < count - 1; ++i) {
@@ -74,29 +97,3 @@
version_str.append(base::IntToString(components_[count - 1]));
return version_str;
}
-
-bool Version::InitFromString(const std::string& version_str) {
- DCHECK(!is_valid_);
- std::vector<std::string> numbers;
- base::SplitString(version_str, '.', &numbers);
- if (numbers.empty())
- return false;
- for (std::vector<std::string>::iterator i = numbers.begin();
- i != numbers.end(); ++i) {
- int num;
- if (!base::StringToInt(*i, &num))
- return false;
- if (num < 0)
- return false;
- const uint16 max = 0xFFFF;
- if (num > max)
- return false;
- // This throws out things like +3, or 032.
- if (base::IntToString(num) != *i)
- return false;
- uint16 component = static_cast<uint16>(num);
- components_.push_back(component);
- }
- is_valid_ = true;
- return true;
-}
« no previous file with comments | « base/version.h ('k') | base/version_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698