blob: 1f9bd204e0c6898ee1ce421a279f8323f81957d2 [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
cpu@chromium.org9008a612011-08-02 02:45:08 +090048bool 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
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090055// TODO(cpu): remove this method.
erikkay@google.come05e3c62009-01-30 04:18:57 +090056Version* Version::GetVersionFromString(const std::string& version_str) {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090057 Version* vers = new Version(version_str);
58 if (vers->IsValid()) {
erikkay@google.come05e3c62009-01-30 04:18:57 +090059 return vers;
akalin@chromium.org2659ef12010-03-26 07:19:04 +090060 }
erikkay@google.come05e3c62009-01-30 04:18:57 +090061 delete vers;
62 return NULL;
63}
64
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090065// TODO(cpu): remove this method.
jkummerow@chromium.orgdf3e6712010-12-21 17:27:25 +090066Version* Version::Clone() const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090067 DCHECK(IsValid());
68 return new Version(*this);
jkummerow@chromium.orgdf3e6712010-12-21 17:27:25 +090069}
70
erikkay@google.come05e3c62009-01-30 04:18:57 +090071bool Version::Equals(const Version& that) const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090072 DCHECK(IsValid());
73 DCHECK(that.IsValid());
74 return (CompareTo(that) == 0);
erikkay@google.come05e3c62009-01-30 04:18:57 +090075}
76
77int Version::CompareTo(const Version& other) const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090078 DCHECK(IsValid());
79 DCHECK(other.IsValid());
akalin@chromium.org2659ef12010-03-26 07:19:04 +090080 size_t count = std::min(components_.size(), other.components_.size());
erikkay@google.come05e3c62009-01-30 04:18:57 +090081 for (size_t i = 0; i < count; ++i) {
akalin@chromium.org2659ef12010-03-26 07:19:04 +090082 if (components_[i] > other.components_[i])
erikkay@google.come05e3c62009-01-30 04:18:57 +090083 return 1;
akalin@chromium.org2659ef12010-03-26 07:19:04 +090084 if (components_[i] < other.components_[i])
erikkay@google.come05e3c62009-01-30 04:18:57 +090085 return -1;
86 }
akalin@chromium.org2659ef12010-03-26 07:19:04 +090087 if (components_.size() > other.components_.size()) {
erikkay@google.come05e3c62009-01-30 04:18:57 +090088 for (size_t i = count; i < components_.size(); ++i)
89 if (components_[i] > 0)
90 return 1;
akalin@chromium.org2659ef12010-03-26 07:19:04 +090091 } else if (components_.size() < other.components_.size()) {
92 for (size_t i = count; i < other.components_.size(); ++i)
93 if (other.components_[i] > 0)
erikkay@google.come05e3c62009-01-30 04:18:57 +090094 return -1;
95 }
96 return 0;
97}
98
99const std::string Version::GetString() const {
cpu@chromium.orgfa761a32011-06-08 02:21:30 +0900100 DCHECK(IsValid());
erikkay@google.come05e3c62009-01-30 04:18:57 +0900101 std::string version_str;
robertshield@chromium.orga9914a32010-11-11 02:02:19 +0900102 size_t count = components_.size();
103 for (size_t i = 0; i < count - 1; ++i) {
brettw@chromium.orgcbde69a2010-07-31 04:28:44 +0900104 version_str.append(base::IntToString(components_[i]));
erikkay@google.come05e3c62009-01-30 04:18:57 +0900105 version_str.append(".");
106 }
brettw@chromium.orgcbde69a2010-07-31 04:28:44 +0900107 version_str.append(base::IntToString(components_[count - 1]));
erikkay@google.come05e3c62009-01-30 04:18:57 +0900108 return version_str;
109}