blob: cdd15a00db7d46d292fd79ea5b6ed80d5a9a642a [file] [log] [blame]
[email protected]1513bf82011-06-07 17:43:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]19b8d822009-01-29 19:18:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]19b8d822009-01-29 19:18:575#include "base/version.h"
6
[email protected]b566c112010-12-21 08:27:257#include <algorithm>
8
[email protected]26931bc2010-03-25 22:19:049#include "base/logging.h"
[email protected]528c56d2010-07-30 19:28:4410#include "base/string_number_conversions.h"
[email protected]4e5ae20f2010-09-24 04:52:1111#include "base/string_split.h"
[email protected]26931bc2010-03-25 22:19:0412#include "base/string_util.h"
13
[email protected]76002472011-06-07 17:21:3014Version::Version() {
15}
[email protected]9989c9b2011-01-07 20:23:4316
[email protected]1513bf82011-06-07 17:43:2017Version::~Version() {
18}
19
[email protected]76002472011-06-07 17:21:3020Version::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]9989c9b2011-01-07 20:23:4343
[email protected]76002472011-06-07 17:21:3044bool Version::IsValid() const {
45 return (!components_.empty());
46}
47
48// TODO(cpu): remove this method.
[email protected]19b8d822009-01-29 19:18:5749Version* Version::GetVersionFromString(const std::string& version_str) {
[email protected]76002472011-06-07 17:21:3050 Version* vers = new Version(version_str);
51 if (vers->IsValid()) {
[email protected]19b8d822009-01-29 19:18:5752 return vers;
[email protected]26931bc2010-03-25 22:19:0453 }
[email protected]19b8d822009-01-29 19:18:5754 delete vers;
55 return NULL;
56}
57
[email protected]76002472011-06-07 17:21:3058// TODO(cpu): remove this method.
[email protected]b566c112010-12-21 08:27:2559Version* Version::Clone() const {
[email protected]76002472011-06-07 17:21:3060 DCHECK(IsValid());
61 return new Version(*this);
[email protected]b566c112010-12-21 08:27:2562}
63
[email protected]19b8d822009-01-29 19:18:5764bool Version::Equals(const Version& that) const {
[email protected]76002472011-06-07 17:21:3065 DCHECK(IsValid());
66 DCHECK(that.IsValid());
67 return (CompareTo(that) == 0);
[email protected]19b8d822009-01-29 19:18:5768}
69
70int Version::CompareTo(const Version& other) const {
[email protected]76002472011-06-07 17:21:3071 DCHECK(IsValid());
72 DCHECK(other.IsValid());
[email protected]26931bc2010-03-25 22:19:0473 size_t count = std::min(components_.size(), other.components_.size());
[email protected]19b8d822009-01-29 19:18:5774 for (size_t i = 0; i < count; ++i) {
[email protected]26931bc2010-03-25 22:19:0475 if (components_[i] > other.components_[i])
[email protected]19b8d822009-01-29 19:18:5776 return 1;
[email protected]26931bc2010-03-25 22:19:0477 if (components_[i] < other.components_[i])
[email protected]19b8d822009-01-29 19:18:5778 return -1;
79 }
[email protected]26931bc2010-03-25 22:19:0480 if (components_.size() > other.components_.size()) {
[email protected]19b8d822009-01-29 19:18:5781 for (size_t i = count; i < components_.size(); ++i)
82 if (components_[i] > 0)
83 return 1;
[email protected]26931bc2010-03-25 22:19:0484 } else if (components_.size() < other.components_.size()) {
85 for (size_t i = count; i < other.components_.size(); ++i)
86 if (other.components_[i] > 0)
[email protected]19b8d822009-01-29 19:18:5787 return -1;
88 }
89 return 0;
90}
91
92const std::string Version::GetString() const {
[email protected]76002472011-06-07 17:21:3093 DCHECK(IsValid());
[email protected]19b8d822009-01-29 19:18:5794 std::string version_str;
[email protected]6dc910c2010-11-10 17:02:1995 size_t count = components_.size();
96 for (size_t i = 0; i < count - 1; ++i) {
[email protected]528c56d2010-07-30 19:28:4497 version_str.append(base::IntToString(components_[i]));
[email protected]19b8d822009-01-29 19:18:5798 version_str.append(".");
99 }
[email protected]528c56d2010-07-30 19:28:44100 version_str.append(base::IntToString(components_[count - 1]));
[email protected]19b8d822009-01-29 19:18:57101 return version_str;
102}