Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: base/version.cc

Issue 1182453004: Write new Starts/EndsWith and convert FilePath functions to StringPiece. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@string_util
Patch Set: Fix save_package using Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 15
16 namespace base { 16 namespace base {
17 17
18 namespace { 18 namespace {
19 19
20 // Parses the |numbers| vector representing the different numbers 20 // Parses the |numbers| vector representing the different numbers
21 // inside the version string and constructs a vector of valid integers. It stops 21 // inside the version string and constructs a vector of valid integers. It stops
22 // when it reaches an invalid item (including the wildcard character). |parsed| 22 // when it reaches an invalid item (including the wildcard character). |parsed|
23 // is the resulting integer vector. Function returns true if all numbers were 23 // is the resulting integer vector. Function returns true if all numbers were
24 // parsed successfully, false otherwise. 24 // parsed successfully, false otherwise.
25 bool ParseVersionNumbers(const std::string& version_str, 25 bool ParseVersionNumbers(const std::string& version_str,
26 std::vector<uint32_t>* parsed) { 26 std::vector<uint32_t>* parsed) {
27 std::vector<std::string> numbers; 27 std::vector<StringPiece> numbers =
28 SplitString(version_str, '.', &numbers); 28 SplitStringPiece(version_str, ".", KEEP_WHITESPACE, SPLIT_WANT_ALL);
29 if (numbers.empty()) 29 if (numbers.empty())
30 return false; 30 return false;
31 31
32 for (std::vector<std::string>::const_iterator it = numbers.begin(); 32 for (auto it = numbers.begin(); it != numbers.end(); ++it) {
33 it != numbers.end(); ++it) { 33 if (StartsWith(*it, "+", CompareCase::SENSITIVE))
34 if (StartsWithASCII(*it, "+", false))
35 return false; 34 return false;
35
36 // TODO(brettw) when we have a StringPiece version of StringToUint, delete
37 // this string conversion.
36 unsigned int num; 38 unsigned int num;
37 if (!StringToUint(*it, &num)) 39 if (!StringToUint(*it, &num))
38 return false; 40 return false;
39 41
40 // This throws out leading zeros for the first item only. 42 // This throws out leading zeros for the first item only.
41 if (it == numbers.begin() && UintToString(num) != *it) 43 if (it == numbers.begin() && UintToString(num) != *it)
42 return false; 44 return false;
43 45
44 // StringToUint returns unsigned int but Version fields are uint32_t. 46 // StringToUint returns unsigned int but Version fields are uint32_t.
45 static_assert(sizeof (uint32_t) == sizeof (unsigned int), 47 static_assert(sizeof (uint32_t) == sizeof (unsigned int),
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 92
91 components_.swap(parsed); 93 components_.swap(parsed);
92 } 94 }
93 95
94 bool Version::IsValid() const { 96 bool Version::IsValid() const {
95 return (!components_.empty()); 97 return (!components_.empty());
96 } 98 }
97 99
98 // static 100 // static
99 bool Version::IsValidWildcardString(const std::string& wildcard_string) { 101 bool Version::IsValidWildcardString(const std::string& wildcard_string) {
100 std::string version_string = wildcard_string; 102 std::string version_string;
101 if (EndsWith(wildcard_string.c_str(), ".*", false)) 103 if (EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE))
102 version_string = wildcard_string.substr(0, wildcard_string.size() - 2); 104 version_string = wildcard_string.substr(0, wildcard_string.size() - 2);
Peter Kasting 2015/06/15 20:41:40 Nit: Hmm... I think it might be more efficient to
brettw 2015/06/15 23:00:48 You're right. I'm going to do resize(version_strin
Peter Kasting 2015/06/16 00:08:18 OK... I actually find it a bit less clear semantic
105 else
106 version_string = wildcard_string;
103 107
104 Version version(version_string); 108 Version version(version_string);
105 return version.IsValid(); 109 return version.IsValid();
106 } 110 }
107 111
108 bool Version::IsOlderThan(const std::string& version_str) const { 112 bool Version::IsOlderThan(const std::string& version_str) const {
109 Version proposed_ver(version_str); 113 Version proposed_ver(version_str);
110 if (!proposed_ver.IsValid()) 114 if (!proposed_ver.IsValid())
111 return false; 115 return false;
112 return (CompareTo(proposed_ver) < 0); 116 return (CompareTo(proposed_ver) < 0);
113 } 117 }
114 118
115 int Version::CompareToWildcardString(const std::string& wildcard_string) const { 119 int Version::CompareToWildcardString(const std::string& wildcard_string) const {
116 DCHECK(IsValid()); 120 DCHECK(IsValid());
117 DCHECK(Version::IsValidWildcardString(wildcard_string)); 121 DCHECK(Version::IsValidWildcardString(wildcard_string));
118 122
119 // Default behavior if the string doesn't end with a wildcard. 123 // Default behavior if the string doesn't end with a wildcard.
120 if (!EndsWith(wildcard_string.c_str(), ".*", false)) { 124 if (!EndsWith(wildcard_string, ".*", CompareCase::SENSITIVE)) {
121 Version version(wildcard_string); 125 Version version(wildcard_string);
122 DCHECK(version.IsValid()); 126 DCHECK(version.IsValid());
123 return CompareTo(version); 127 return CompareTo(version);
124 } 128 }
125 129
126 std::vector<uint32_t> parsed; 130 std::vector<uint32_t> parsed;
127 const bool success = ParseVersionNumbers( 131 const bool success = ParseVersionNumbers(
128 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed); 132 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
129 DCHECK(success); 133 DCHECK(success);
130 const int comparison = CompareVersionComponents(components_, parsed); 134 const int comparison = CompareVersionComponents(components_, parsed);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 size_t count = components_.size(); 171 size_t count = components_.size();
168 for (size_t i = 0; i < count - 1; ++i) { 172 for (size_t i = 0; i < count - 1; ++i) {
169 version_str.append(IntToString(components_[i])); 173 version_str.append(IntToString(components_[i]));
170 version_str.append("."); 174 version_str.append(".");
171 } 175 }
172 version_str.append(IntToString(components_[count - 1])); 176 version_str.append(IntToString(components_[count - 1]));
173 return version_str; 177 return version_str;
174 } 178 }
175 179
176 } // namespace base 180 } // namespace base
OLDNEW
« base/strings/string_util.cc ('K') | « base/sys_info.cc ('k') | base/win/win_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698