Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/version.h" | 5 #include "base/version.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 | 13 |
| 14 namespace { | |
| 15 | |
| 16 // This function parses the |numbers| vector representing the different numbers | |
| 17 // inside the version string and constructs a vector of valid integers. It stops | |
| 18 // when it reaches an invalid item (including the wildcard character). |parsed| | |
| 19 // is the resulting integer vector. | |
| 20 bool ParseVersionNumbers(const std::vector<std::string>& numbers, | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Please document the return value.
Mathieu
2012/06/19 20:01:35
Done.
| |
| 21 std::vector<uint16>& parsed) { | |
| 22 for (std::vector<std::string>::const_iterator i = numbers.begin(); | |
| 23 i != numbers.end(); ++i) { | |
| 24 int num; | |
| 25 if (!base::StringToInt(*i, &num)) | |
| 26 return false; | |
| 27 if (num < 0) | |
| 28 return false; | |
| 29 const uint16 max = 0xFFFF; | |
| 30 if (num > max) | |
| 31 return false; | |
| 32 // This throws out things like +3, or 032. | |
| 33 if (base::IntToString(num) != *i) | |
| 34 return false; | |
| 35 parsed.push_back(static_cast<uint16>(num)); | |
| 36 } | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 14 Version::Version() { | 42 Version::Version() { |
| 15 } | 43 } |
| 16 | 44 |
| 17 Version::~Version() { | 45 Version::~Version() { |
| 18 } | 46 } |
| 19 | 47 |
| 20 Version::Version(const std::string& version_str) { | 48 Version::Version(const std::string& version_str) { |
| 21 std::vector<std::string> numbers; | 49 std::vector<std::string> numbers; |
| 22 base::SplitString(version_str, '.', &numbers); | 50 base::SplitString(version_str, '.', &numbers); |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
How about folding the SplitString() code into |Par
Mathieu
2012/06/19 20:01:35
Done.
| |
| 23 if (numbers.empty()) | 51 if (numbers.empty()) |
| 24 return; | 52 return; |
| 25 std::vector<uint16> parsed; | 53 std::vector<uint16> parsed; |
| 26 for (std::vector<std::string>::iterator i = numbers.begin(); | 54 if (!ParseVersionNumbers(numbers, parsed)) |
| 27 i != numbers.end(); ++i) { | 55 return; |
| 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); | 56 components_.swap(parsed); |
| 42 } | 57 } |
| 43 | 58 |
| 44 bool Version::IsValid() const { | 59 bool Version::IsValid() const { |
| 45 return (!components_.empty()); | 60 return (!components_.empty()); |
| 46 } | 61 } |
| 47 | 62 |
| 63 // static | |
| 64 bool Version::IsValidWildcard(const std::string& version_str) { | |
| 65 if (version_str.empty()) | |
| 66 return false; | |
| 67 | |
| 68 if (version_str.length() == 1 || | |
| 69 version_str.compare(version_str.length() - 2, 2, ".*") != 0) { | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Use EndsWith() from base/string_util.h.
Mathieu
2012/06/19 20:01:35
Done.
| |
| 70 // Either a lone character or a full version number. | |
| 71 Version version(version_str); | |
| 72 return version.IsValid(); | |
| 73 } else { | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Nit: Don't need an else if the previous block is a
Mathieu
2012/06/19 20:01:35
Done.
| |
| 74 // Version strings length 2 or more, ending with ".*". | |
| 75 Version version(version_str.substr(0, version_str.size() - 2)); | |
| 76 return version.IsValid(); | |
| 77 } | |
| 78 } | |
| 79 | |
| 48 bool Version::IsOlderThan(const std::string& version_str) const { | 80 bool Version::IsOlderThan(const std::string& version_str) const { |
| 49 Version proposed_ver(version_str); | 81 Version proposed_ver(version_str); |
| 50 if (!proposed_ver.IsValid()) | 82 if (!proposed_ver.IsValid()) |
| 51 return false; | 83 return false; |
| 52 return (CompareTo(proposed_ver) < 0); | 84 return (CompareTo(proposed_ver) < 0); |
| 53 } | 85 } |
| 54 | 86 |
| 87 int Version::CompareToWildcard(const std::string& version_str) const { | |
| 88 DCHECK(IsValid()); | |
| 89 DCHECK(Version::IsValidWildcard(version_str)); | |
| 90 | |
| 91 // Default behavior if the string doesn't end with a wildcard. | |
| 92 if (version_str.compare(version_str.length() - 2, 2, ".*") != 0) { | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Use EndsWith() from base/string_util.h
Mathieu
2012/06/19 20:01:35
Done.
| |
| 93 Version version(version_str); | |
| 94 DCHECK(version.IsValid()); | |
| 95 return CompareTo(version); | |
| 96 } | |
| 97 | |
| 98 // Removing the * and returning immediately if the argument version is less | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Nit: Use a different verb tense, e.g.:
// Remove
Mathieu
2012/06/19 20:01:35
Done.
| |
| 99 // or equal (the * won't affect here). | |
| 100 Version other_version(version_str.substr(0, version_str.size() - 2)); | |
| 101 int comparison = CompareTo(other_version); | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Is there a way to avoid having to do this (i.e. pa
Mathieu
2012/06/19 20:01:35
Done.
| |
| 102 if (comparison == -1 || comparison == 0) | |
| 103 return comparison; | |
| 104 | |
| 105 // Trying to catch the equality case 1.2 vs 1.*. In all other cases, it truly | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Nit: Reword comment. "Trying" doesn't sound very c
Mathieu
2012/06/19 20:01:35
Done.
| |
| 106 // is a "greater than" case. | |
| 107 std::vector<std::string> other_numbers; | |
| 108 base::SplitString(version_str, '.', &other_numbers); | |
| 109 std::vector<uint16> parsed; | |
| 110 ParseVersionNumbers(other_numbers, parsed); // Will not parse the *. | |
|
Alexei Svitkine (slow)
2012/06/19 18:30:27
Assign the result to a local "bool success" variab
Mathieu
2012/06/19 20:01:35
Done.
| |
| 111 | |
| 112 DCHECK_GT(parsed.size(), 0UL); | |
| 113 for (size_t i = 0; i < parsed.size(); ++i) { | |
| 114 if (components_[i] != parsed[i]) | |
| 115 return 1; | |
| 116 } | |
| 117 return 0; | |
| 118 } | |
| 119 | |
| 55 // TODO(cpu): remove this method. | 120 // TODO(cpu): remove this method. |
| 56 Version* Version::GetVersionFromString(const std::string& version_str) { | 121 Version* Version::GetVersionFromString(const std::string& version_str) { |
| 57 Version* vers = new Version(version_str); | 122 Version* vers = new Version(version_str); |
| 58 if (vers->IsValid()) { | 123 if (vers->IsValid()) { |
| 59 return vers; | 124 return vers; |
| 60 } | 125 } |
| 61 delete vers; | 126 delete vers; |
| 62 return NULL; | 127 return NULL; |
| 63 } | 128 } |
| 64 | 129 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 DCHECK(IsValid()); | 165 DCHECK(IsValid()); |
| 101 std::string version_str; | 166 std::string version_str; |
| 102 size_t count = components_.size(); | 167 size_t count = components_.size(); |
| 103 for (size_t i = 0; i < count - 1; ++i) { | 168 for (size_t i = 0; i < count - 1; ++i) { |
| 104 version_str.append(base::IntToString(components_[i])); | 169 version_str.append(base::IntToString(components_[i])); |
| 105 version_str.append("."); | 170 version_str.append("."); |
| 106 } | 171 } |
| 107 version_str.append(base::IntToString(components_[count - 1])); | 172 version_str.append(base::IntToString(components_[count - 1])); |
| 108 return version_str; | 173 return version_str; |
| 109 } | 174 } |
| OLD | NEW |