version.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/version.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include <ostream>
  8. #include "base/check_op.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/string_split.h"
  11. #include "base/strings/string_util.h"
  12. namespace base {
  13. namespace {
  14. // Parses the |numbers| vector representing the different numbers
  15. // inside the version string and constructs a vector of valid integers. It stops
  16. // when it reaches an invalid item (including the wildcard character). |parsed|
  17. // is the resulting integer vector. Function returns true if all numbers were
  18. // parsed successfully, false otherwise.
  19. bool ParseVersionNumbers(StringPiece version_str,
  20. std::vector<uint32_t>* parsed) {
  21. std::vector<StringPiece> numbers =
  22. SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL);
  23. if (numbers.empty())
  24. return false;
  25. for (auto it = numbers.begin(); it != numbers.end(); ++it) {
  26. if (StartsWith(*it, "+", CompareCase::SENSITIVE))
  27. return false;
  28. unsigned int num;
  29. if (!StringToUint(*it, &num))
  30. return false;
  31. // This throws out leading zeros for the first item only.
  32. if (it == numbers.begin() && NumberToString(num) != *it)
  33. return false;
  34. // StringToUint returns unsigned int but Version fields are uint32_t.
  35. static_assert(sizeof (uint32_t) == sizeof (unsigned int),
  36. "uint32_t must be same as unsigned int");
  37. parsed->push_back(num);
  38. }
  39. return true;
  40. }
  41. // Compares version components in |components1| with components in
  42. // |components2|. Returns -1, 0 or 1 if |components1| is less than, equal to,
  43. // or greater than |components2|, respectively.
  44. int CompareVersionComponents(const std::vector<uint32_t>& components1,
  45. const std::vector<uint32_t>& components2) {
  46. const size_t count = std::min(components1.size(), components2.size());
  47. for (size_t i = 0; i < count; ++i) {
  48. if (components1[i] > components2[i])
  49. return 1;
  50. if (components1[i] < components2[i])
  51. return -1;
  52. }
  53. if (components1.size() > components2.size()) {
  54. for (size_t i = count; i < components1.size(); ++i) {
  55. if (components1[i] > 0)
  56. return 1;
  57. }
  58. } else if (components1.size() < components2.size()) {
  59. for (size_t i = count; i < components2.size(); ++i) {
  60. if (components2[i] > 0)
  61. return -1;
  62. }
  63. }
  64. return 0;
  65. }
  66. } // namespace
  67. Version::Version() = default;
  68. Version::Version(const Version& other) = default;
  69. Version::~Version() = default;
  70. Version::Version(StringPiece version_str) {
  71. std::vector<uint32_t> parsed;
  72. if (!ParseVersionNumbers(version_str, &parsed))
  73. return;
  74. components_.swap(parsed);
  75. }
  76. Version::Version(std::vector<uint32_t> components)
  77. : components_(std::move(components)) {}
  78. bool Version::IsValid() const {
  79. return (!components_.empty());
  80. }
  81. // static
  82. bool Version::IsValidWildcardString(StringPiece wildcard_string) {
  83. StringPiece version_string = wildcard_string;
  84. if (EndsWith(version_string, ".*", CompareCase::SENSITIVE))
  85. version_string = version_string.substr(0, version_string.size() - 2);
  86. Version version(version_string);
  87. return version.IsValid();
  88. }
  89. int Version::CompareToWildcardString(StringPiece wildcard_string) const {
  90. DCHECK(IsValid());
  91. DCHECK(Version::IsValidWildcardString(wildcard_string));
  92. // Default behavior if the string doesn't end with a wildcard.
  93. if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
  94. Version version(wildcard_string);
  95. DCHECK(version.IsValid());
  96. return CompareTo(version);
  97. }
  98. std::vector<uint32_t> parsed;
  99. const bool success = ParseVersionNumbers(
  100. wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
  101. DCHECK(success);
  102. const int comparison = CompareVersionComponents(components_, parsed);
  103. // If the version is smaller than the wildcard version's |parsed| vector,
  104. // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the
  105. // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to
  106. // 1.2.2.* is 0 regardless of the wildcard). Under this logic,
  107. // 1.2.0.0.0.0 compared to 1.2.* is 0.
  108. if (comparison == -1 || comparison == 0)
  109. return comparison;
  110. // Catch the case where the digits of |parsed| are found in |components_|,
  111. // which means that the two are equal since |parsed| has a trailing "*".
  112. // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since
  113. // components is greater (e.g. 3.2.3 vs 1.*).
  114. DCHECK_GT(parsed.size(), 0UL);
  115. const size_t min_num_comp = std::min(components_.size(), parsed.size());
  116. for (size_t i = 0; i < min_num_comp; ++i) {
  117. if (components_[i] != parsed[i])
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. int Version::CompareTo(const Version& other) const {
  123. DCHECK(IsValid());
  124. DCHECK(other.IsValid());
  125. return CompareVersionComponents(components_, other.components_);
  126. }
  127. std::string Version::GetString() const {
  128. if (!IsValid())
  129. return "invalid";
  130. std::string version_str;
  131. size_t count = components_.size();
  132. for (size_t i = 0; i < count - 1; ++i) {
  133. version_str.append(NumberToString(components_[i]));
  134. version_str.append(".");
  135. }
  136. version_str.append(NumberToString(components_[count - 1]));
  137. return version_str;
  138. }
  139. bool operator==(const Version& v1, const Version& v2) {
  140. return v1.CompareTo(v2) == 0;
  141. }
  142. bool operator!=(const Version& v1, const Version& v2) {
  143. return !(v1 == v2);
  144. }
  145. bool operator<(const Version& v1, const Version& v2) {
  146. return v1.CompareTo(v2) < 0;
  147. }
  148. bool operator<=(const Version& v1, const Version& v2) {
  149. return v1.CompareTo(v2) <= 0;
  150. }
  151. bool operator>(const Version& v1, const Version& v2) {
  152. return v1.CompareTo(v2) > 0;
  153. }
  154. bool operator>=(const Version& v1, const Version& v2) {
  155. return v1.CompareTo(v2) >= 0;
  156. }
  157. std::ostream& operator<<(std::ostream& stream, const Version& v) {
  158. return stream << v.GetString();
  159. }
  160. } // namespace base