blob: 1f9bd204e0c6898ee1ce421a279f8323f81957d2 [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
[email protected]30c157c2011-08-01 17:45:0848bool 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
[email protected]76002472011-06-07 17:21:3055// TODO(cpu): remove this method.
[email protected]19b8d822009-01-29 19:18:5756Version* Version::GetVersionFromString(const std::string& version_str) {
[email protected]76002472011-06-07 17:21:3057 Version* vers = new Version(version_str);
58 if (vers->IsValid()) {
[email protected]19b8d822009-01-29 19:18:5759 return vers;
[email protected]26931bc2010-03-25 22:19:0460 }
[email protected]19b8d822009-01-29 19:18:5761 delete vers;
62 return NULL;
63}
64
[email protected]76002472011-06-07 17:21:3065// TODO(cpu): remove this method.
[email protected]b566c112010-12-21 08:27:2566Version* Version::Clone() const {
[email protected]76002472011-06-07 17:21:3067 DCHECK(IsValid());
68 return new Version(*this);
[email protected]b566c112010-12-21 08:27:2569}
70
[email protected]19b8d822009-01-29 19:18:5771bool Version::Equals(const Version& that) const {
[email protected]76002472011-06-07 17:21:3072 DCHECK(IsValid());
73 DCHECK(that.IsValid());
74 return (CompareTo(that) == 0);
[email protected]19b8d822009-01-29 19:18:5775}
76
77int Version::CompareTo(const Version& other) const {
[email protected]76002472011-06-07 17:21:3078 DCHECK(IsValid());
79 DCHECK(other.IsValid());
[email protected]26931bc2010-03-25 22:19:0480 size_t count = std::min(components_.size(), other.components_.size());
[email protected]19b8d822009-01-29 19:18:5781 for (size_t i = 0; i < count; ++i) {
[email protected]26931bc2010-03-25 22:19:0482 if (components_[i] > other.components_[i])
[email protected]19b8d822009-01-29 19:18:5783 return 1;
[email protected]26931bc2010-03-25 22:19:0484 if (components_[i] < other.components_[i])
[email protected]19b8d822009-01-29 19:18:5785 return -1;
86 }
[email protected]26931bc2010-03-25 22:19:0487 if (components_.size() > other.components_.size()) {
[email protected]19b8d822009-01-29 19:18:5788 for (size_t i = count; i < components_.size(); ++i)
89 if (components_[i] > 0)
90 return 1;
[email protected]26931bc2010-03-25 22:19:0491 } else if (components_.size() < other.components_.size()) {
92 for (size_t i = count; i < other.components_.size(); ++i)
93 if (other.components_[i] > 0)
[email protected]19b8d822009-01-29 19:18:5794 return -1;
95 }
96 return 0;
97}
98
99const std::string Version::GetString() const {
[email protected]76002472011-06-07 17:21:30100 DCHECK(IsValid());
[email protected]19b8d822009-01-29 19:18:57101 std::string version_str;
[email protected]6dc910c2010-11-10 17:02:19102 size_t count = components_.size();
103 for (size_t i = 0; i < count - 1; ++i) {
[email protected]528c56d2010-07-30 19:28:44104 version_str.append(base::IntToString(components_[i]));
[email protected]19b8d822009-01-29 19:18:57105 version_str.append(".");
106 }
[email protected]528c56d2010-07-30 19:28:44107 version_str.append(base::IntToString(components_[count - 1]));
[email protected]19b8d822009-01-29 19:18:57108 return version_str;
109}