blob: 74c570ce1731d1dc62be654c58692c890a06fc83 [file] [log] [blame]
[email protected]4e5ae20f2010-09-24 04:52:111// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]19b8d82f2009-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]19b8d82f2009-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]760024782011-06-07 17:21:3014Version::Version() {
15}
[email protected]9989c9bb2011-01-07 20:23:4316
[email protected]760024782011-06-07 17:21:3017Version::Version(const std::string& version_str) {
18 std::vector<std::string> numbers;
19 base::SplitString(version_str, '.', &numbers);
20 if (numbers.empty())
21 return;
22 std::vector<uint16> parsed;
23 for (std::vector<std::string>::iterator i = numbers.begin();
24 i != numbers.end(); ++i) {
25 int num;
26 if (!base::StringToInt(*i, &num))
27 return;
28 if (num < 0)
29 return;
30 const uint16 max = 0xFFFF;
31 if (num > max)
32 return;
33 // This throws out things like +3, or 032.
34 if (base::IntToString(num) != *i)
35 return;
36 parsed.push_back(static_cast<uint16>(num));
37 }
38 components_.swap(parsed);
39}
[email protected]9989c9bb2011-01-07 20:23:4340
[email protected]760024782011-06-07 17:21:3041bool Version::IsValid() const {
42 return (!components_.empty());
43}
44
45// TODO(cpu): remove this method.
[email protected]19b8d82f2009-01-29 19:18:5746Version* Version::GetVersionFromString(const std::string& version_str) {
[email protected]760024782011-06-07 17:21:3047 Version* vers = new Version(version_str);
48 if (vers->IsValid()) {
[email protected]19b8d82f2009-01-29 19:18:5749 return vers;
[email protected]26931bc2010-03-25 22:19:0450 }
[email protected]19b8d82f2009-01-29 19:18:5751 delete vers;
52 return NULL;
53}
54
[email protected]760024782011-06-07 17:21:3055// TODO(cpu): remove this method.
[email protected]b566c112010-12-21 08:27:2556Version* Version::Clone() const {
[email protected]760024782011-06-07 17:21:3057 DCHECK(IsValid());
58 return new Version(*this);
[email protected]b566c112010-12-21 08:27:2559}
60
[email protected]19b8d82f2009-01-29 19:18:5761bool Version::Equals(const Version& that) const {
[email protected]760024782011-06-07 17:21:3062 DCHECK(IsValid());
63 DCHECK(that.IsValid());
64 return (CompareTo(that) == 0);
[email protected]19b8d82f2009-01-29 19:18:5765}
66
67int Version::CompareTo(const Version& other) const {
[email protected]760024782011-06-07 17:21:3068 DCHECK(IsValid());
69 DCHECK(other.IsValid());
[email protected]26931bc2010-03-25 22:19:0470 size_t count = std::min(components_.size(), other.components_.size());
[email protected]19b8d82f2009-01-29 19:18:5771 for (size_t i = 0; i < count; ++i) {
[email protected]26931bc2010-03-25 22:19:0472 if (components_[i] > other.components_[i])
[email protected]19b8d82f2009-01-29 19:18:5773 return 1;
[email protected]26931bc2010-03-25 22:19:0474 if (components_[i] < other.components_[i])
[email protected]19b8d82f2009-01-29 19:18:5775 return -1;
76 }
[email protected]26931bc2010-03-25 22:19:0477 if (components_.size() > other.components_.size()) {
[email protected]19b8d82f2009-01-29 19:18:5778 for (size_t i = count; i < components_.size(); ++i)
79 if (components_[i] > 0)
80 return 1;
[email protected]26931bc2010-03-25 22:19:0481 } else if (components_.size() < other.components_.size()) {
82 for (size_t i = count; i < other.components_.size(); ++i)
83 if (other.components_[i] > 0)
[email protected]19b8d82f2009-01-29 19:18:5784 return -1;
85 }
86 return 0;
87}
88
89const std::string Version::GetString() const {
[email protected]760024782011-06-07 17:21:3090 DCHECK(IsValid());
[email protected]19b8d82f2009-01-29 19:18:5791 std::string version_str;
[email protected]6dc910c2010-11-10 17:02:1992 size_t count = components_.size();
93 for (size_t i = 0; i < count - 1; ++i) {
[email protected]528c56d2010-07-30 19:28:4494 version_str.append(base::IntToString(components_[i]));
[email protected]19b8d82f2009-01-29 19:18:5795 version_str.append(".");
96 }
[email protected]528c56d2010-07-30 19:28:4497 version_str.append(base::IntToString(components_[count - 1]));
[email protected]19b8d82f2009-01-29 19:18:5798 return version_str;
99}