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

Side by Side Diff: net/base/ip_pattern.cc

Issue 1215933004: New new versions of Starts/EndsWith and SplitString in net (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@starts_with
Patch Set: Created 5 years, 5 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "net/base/ip_pattern.h" 5 #include "net/base/ip_pattern.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 ++pattern_it; 85 ++pattern_it;
86 } 86 }
87 return true; 87 return true;
88 } 88 }
89 89
90 bool IPPattern::ParsePattern(const std::string& ip_pattern) { 90 bool IPPattern::ParsePattern(const std::string& ip_pattern) {
91 DCHECK(ip_mask_.empty()); 91 DCHECK(ip_mask_.empty());
92 if (ip_pattern.find(':') != std::string::npos) { 92 if (ip_pattern.find(':') != std::string::npos) {
93 is_ipv4_ = false; 93 is_ipv4_ = false;
94 } 94 }
95 Strings components; 95
96 base::SplitString(ip_pattern, is_ipv4_ ? '.' : ':', &components); 96 std::vector<base::StringPiece> components =
97 base::SplitStringPiece(ip_pattern, is_ipv4_ ? "." : ":",
98 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
97 if (components.size() != (is_ipv4_ ? 4u : 8u)) { 99 if (components.size() != (is_ipv4_ ? 4u : 8u)) {
98 DVLOG(1) << "Invalid component count: " << ip_pattern; 100 DVLOG(1) << "Invalid component count: " << ip_pattern;
99 return false; 101 return false;
100 } 102 }
101 for (Strings::iterator component_it = components.begin(); 103 for (base::StringPiece component : components) {
Ryan Sleevi 2015/07/06 08:52:01 EXTREME PEDANTRY: Wouldn't for (const auto& compo
brettw 2015/07/06 16:52:34 This particular loop is weird because it actually
102 component_it != components.end(); ++component_it) { 104 if (component.empty()) {
103 if (component_it->empty()) {
104 DVLOG(1) << "Empty component: " << ip_pattern; 105 DVLOG(1) << "Empty component: " << ip_pattern;
105 return false; 106 return false;
106 } 107 }
107 if (*component_it == "*") { 108 if (component == "*") {
108 // Let standard code handle this below. 109 // Let standard code handle this below.
109 *component_it = is_ipv4_ ? "[0-255]" : "[0-FFFF]"; 110 component = is_ipv4_ ? "[0-255]" : "[0-FFFF]";
110 } else if ((*component_it)[0] != '[') { 111 } else if (component[0] != '[') {
111 // This value will just have a specific integer to match. 112 // This value will just have a specific integer to match.
112 uint32_t value; 113 uint32_t value;
113 if (!ValueTextToInt(*component_it, &value)) 114 if (!ValueTextToInt(component, &value))
114 return false; 115 return false;
115 ip_mask_.push_back(true); 116 ip_mask_.push_back(true);
116 component_values_.push_back(value); 117 component_values_.push_back(value);
117 continue; 118 continue;
118 } 119 }
119 if ((*component_it)[component_it->size() - 1] != ']') { 120 if (component[component.size() - 1] != ']') {
120 DVLOG(1) << "Missing close bracket: " << ip_pattern; 121 DVLOG(1) << "Missing close bracket: " << ip_pattern;
121 return false; 122 return false;
122 } 123 }
123 // Now we know the size() is at least 2. 124 // Now we know the size() is at least 2.
124 if (component_it->size() == 2) { 125 if (component.size() == 2) {
125 DVLOG(1) << "Empty bracket: " << ip_pattern; 126 DVLOG(1) << "Empty bracket: " << ip_pattern;
126 return false; 127 return false;
127 } 128 }
128 // We'll need a pattern to match this bracketed component. 129 // We'll need a pattern to match this bracketed component.
129 scoped_ptr<ComponentPattern> component_pattern(new ComponentPattern); 130 scoped_ptr<ComponentPattern> component_pattern(new ComponentPattern);
130 // Trim leading and trailing bracket before calling for parsing. 131 // Trim leading and trailing bracket before calling for parsing.
131 if (!ParseComponentPattern(base::StringPiece(component_it->data() + 1, 132 if (!ParseComponentPattern(component.substr(1, component.size() - 2),
132 component_it->size() - 2), component_pattern.get())) { 133 component_pattern.get())) {
133 return false; 134 return false;
134 } 135 }
135 ip_mask_.push_back(false); 136 ip_mask_.push_back(false);
136 component_patterns_.push_back(component_pattern.release()); 137 component_patterns_.push_back(component_pattern.release());
137 } 138 }
138 return true; 139 return true;
139 } 140 }
140 141
141 bool IPPattern::ParseComponentPattern(const base::StringPiece& text, 142 bool IPPattern::ParseComponentPattern(const base::StringPiece& text,
142 ComponentPattern* pattern) const { 143 ComponentPattern* pattern) const {
143 // We're given a comma separated set of ranges, some of which may be simple 144 // We're given a comma separated set of ranges, some of which may be simple
144 // constants. 145 // constants.
145 Strings ranges; 146 for (const std::string& range : base::SplitString(
146 base::SplitString(text.as_string(), ',', &ranges); 147 text, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
147 for (Strings::iterator range_it = ranges.begin(); 148 base::StringTokenizer range_pair(range, "-");
148 range_it != ranges.end(); ++range_it) {
149 base::StringTokenizer range_pair(*range_it, "-");
150 uint32_t min = 0; 149 uint32_t min = 0;
151 range_pair.GetNext(); 150 range_pair.GetNext();
152 if (!ValueTextToInt(range_pair.token(), &min)) 151 if (!ValueTextToInt(range_pair.token_piece(), &min))
153 return false; 152 return false;
154 uint32_t max = min; // Sometimes we have no distinct max. 153 uint32_t max = min; // Sometimes we have no distinct max.
155 if (range_pair.GetNext()) { 154 if (range_pair.GetNext()) {
156 if (!ValueTextToInt(range_pair.token(), &max)) 155 if (!ValueTextToInt(range_pair.token_piece(), &max))
157 return false; 156 return false;
158 } 157 }
159 if (range_pair.GetNext()) { 158 if (range_pair.GetNext()) {
160 // Too many "-" in this range specifier. 159 // Too many "-" in this range specifier.
161 DVLOG(1) << "Too many hyphens in range: "; 160 DVLOG(1) << "Too many hyphens in range: ";
162 return false; 161 return false;
163 } 162 }
164 pattern->AppendRange(min, max); 163 pattern->AppendRange(min, max);
165 } 164 }
166 return true; 165 return true;
(...skipping 12 matching lines...) Expand all
179 return false; 178 return false;
180 } 179 }
181 if (!is_ipv4_ && *output > 0xFFFFu) { 180 if (!is_ipv4_ && *output > 0xFFFFu) {
182 DVLOG(1) << "IPv6 component greater than 0xFFFF"; 181 DVLOG(1) << "IPv6 component greater than 0xFFFF";
183 return false; 182 return false;
184 } 183 }
185 return ok; 184 return ok;
186 } 185 }
187 186
188 } // namespace net 187 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698