blob: cdd15a00db7d46d292fd79ea5b6ed80d5a9a642a [file] [log] [blame]
cpu@chromium.org331e27b2011-06-08 02:43:20 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
erikkay@google.come05e3c62009-01-30 04:18:57 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
erikkay@google.come05e3c62009-01-30 04:18:57 +09005#include "base/version.h"
6
jkummerow@chromium.orgdf3e6712010-12-21 17:27:25 +09007#include <algorithm>
8
akalin@chromium.org2659ef12010-03-26 07:19:04 +09009#include "base/logging.h"
brettw@chromium.orgcbde69a2010-07-31 04:28:44 +090010#include "base/string_number_conversions.h"
tfarina@chromium.orge995a8b2010-09-24 13:52:11 +090011#include "base/string_split.h"
akalin@chromium.org2659ef12010-03-26 07:19:04 +090012#include "base/string_util.h"
13
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090014Version::Version() {
15}
erg@google.com67a25432011-01-08 05:23:43 +090016
cpu@chromium.org331e27b2011-06-08 02:43:20 +090017Version::~Version() {
18}
19
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090020Version::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}
erg@google.com67a25432011-01-08 05:23:43 +090043
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090044bool Version::IsValid() const {
45 return (!components_.empty());
46}
47
48// TODO(cpu): remove this method.
erikkay@google.come05e3c62009-01-30 04:18:57 +090049Version* Version::GetVersionFromString(const std::string& version_str) {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090050 Version* vers = new Version(version_str);
51 if (vers->IsValid()) {
erikkay@google.come05e3c62009-01-30 04:18:57 +090052 return vers;
akalin@chromium.org2659ef12010-03-26 07:19:04 +090053 }
erikkay@google.come05e3c62009-01-30 04:18:57 +090054 delete vers;
55 return NULL;
56}
57
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090058// TODO(cpu): remove this method.
jkummerow@chromium.orgdf3e6712010-12-21 17:27:25 +090059Version* Version::Clone() const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090060 DCHECK(IsValid());
61 return new Version(*this);
jkummerow@chromium.orgdf3e6712010-12-21 17:27:25 +090062}
63
erikkay@google.come05e3c62009-01-30 04:18:57 +090064bool Version::Equals(const Version& that) const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090065 DCHECK(IsValid());
66 DCHECK(that.IsValid());
67 return (CompareTo(that) == 0);
erikkay@google.come05e3c62009-01-30 04:18:57 +090068}
69
70int Version::CompareTo(const Version& other) const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090071 DCHECK(IsValid());
72 DCHECK(other.IsValid());
akalin@chromium.org2659ef12010-03-26 07:19:04 +090073 size_t count = std::min(components_.size(), other.components_.size());
erikkay@google.come05e3c62009-01-30 04:18:57 +090074 for (size_t i = 0; i < count; ++i) {
akalin@chromium.org2659ef12010-03-26 07:19:04 +090075 if (components_[i] > other.components_[i])
erikkay@google.come05e3c62009-01-30 04:18:57 +090076 return 1;
akalin@chromium.org2659ef12010-03-26 07:19:04 +090077 if (components_[i] < other.components_[i])
erikkay@google.come05e3c62009-01-30 04:18:57 +090078 return -1;
79 }
akalin@chromium.org2659ef12010-03-26 07:19:04 +090080 if (components_.size() > other.components_.size()) {
erikkay@google.come05e3c62009-01-30 04:18:57 +090081 for (size_t i = count; i < components_.size(); ++i)
82 if (components_[i] > 0)
83 return 1;
akalin@chromium.org2659ef12010-03-26 07:19:04 +090084 } else if (components_.size() < other.components_.size()) {
85 for (size_t i = count; i < other.components_.size(); ++i)
86 if (other.components_[i] > 0)
erikkay@google.come05e3c62009-01-30 04:18:57 +090087 return -1;
88 }
89 return 0;
90}
91
92const std::string Version::GetString() const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090093 DCHECK(IsValid());
erikkay@google.come05e3c62009-01-30 04:18:57 +090094 std::string version_str;
robertshield@chromium.orga9914a32010-11-11 02:02:19 +090095 size_t count = components_.size();
96 for (size_t i = 0; i < count - 1; ++i) {
brettw@chromium.orgcbde69a2010-07-31 04:28:44 +090097 version_str.append(base::IntToString(components_[i]));
erikkay@google.come05e3c62009-01-30 04:18:57 +090098 version_str.append(".");
99 }
brettw@chromium.orgcbde69a2010-07-31 04:28:44 +0900100 version_str.append(base::IntToString(components_[count - 1]));
erikkay@google.come05e3c62009-01-30 04:18:57 +0900101 return version_str;
102}