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

Side by Side Diff: net/base/ip_address_number.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
« no previous file with comments | « net/base/ip_address_number.h ('k') | net/base/ip_pattern.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "net/base/ip_address_number.h" 5 #include "net/base/ip_address_number.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_piece.h" 9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_split.h" 10 #include "base/strings/string_split.h"
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 } 149 }
150 150
151 // Otherwise, try IPv4. 151 // Otherwise, try IPv4.
152 ip_number->resize(4); // 32 bits. 152 ip_number->resize(4); // 32 bits.
153 int num_components; 153 int num_components;
154 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( 154 url::CanonHostInfo::Family family = url::IPv4AddressToNumber(
155 hostname.data(), host_comp, &(*ip_number)[0], &num_components); 155 hostname.data(), host_comp, &(*ip_number)[0], &num_components);
156 return family == url::CanonHostInfo::IPV4; 156 return family == url::CanonHostInfo::IPV4;
157 } 157 }
158 158
159 bool ParseIPLiteralToNumber(const std::string& ip_literal, 159 bool ParseIPLiteralToNumber(const base::StringPiece& ip_literal,
160 IPAddressNumber* ip_number) { 160 IPAddressNumber* ip_number) {
161 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains 161 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains
162 // a colon however, it must be an IPv6 address. 162 // a colon however, it must be an IPv6 address.
163 if (ip_literal.find(':') != std::string::npos) { 163 if (ip_literal.find(':') != base::StringPiece::npos) {
164 // GURL expects IPv6 hostnames to be surrounded with brackets. 164 // GURL expects IPv6 hostnames to be surrounded with brackets.
165 std::string host_brackets = "[" + ip_literal + "]"; 165 std::string host_brackets = "[";
166 ip_literal.AppendToString(&host_brackets);
167 host_brackets.push_back(']');
166 url::Component host_comp(0, host_brackets.size()); 168 url::Component host_comp(0, host_brackets.size());
167 169
168 // Try parsing the hostname as an IPv6 literal. 170 // Try parsing the hostname as an IPv6 literal.
169 ip_number->resize(16); // 128 bits. 171 ip_number->resize(16); // 128 bits.
170 return url::IPv6AddressToNumber(host_brackets.data(), host_comp, 172 return url::IPv6AddressToNumber(host_brackets.data(), host_comp,
171 &(*ip_number)[0]); 173 &(*ip_number)[0]);
172 } 174 }
173 175
174 // Otherwise the string is an IPv4 address. 176 // Otherwise the string is an IPv4 address.
175 ip_number->resize(4); // 32 bits. 177 ip_number->resize(4); // 32 bits.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 address.end()); 217 address.end());
216 } 218 }
217 219
218 bool ParseCIDRBlock(const std::string& cidr_literal, 220 bool ParseCIDRBlock(const std::string& cidr_literal,
219 IPAddressNumber* ip_number, 221 IPAddressNumber* ip_number,
220 size_t* prefix_length_in_bits) { 222 size_t* prefix_length_in_bits) {
221 // We expect CIDR notation to match one of these two templates: 223 // We expect CIDR notation to match one of these two templates:
222 // <IPv4-literal> "/" <number of bits> 224 // <IPv4-literal> "/" <number of bits>
223 // <IPv6-literal> "/" <number of bits> 225 // <IPv6-literal> "/" <number of bits>
224 226
225 std::vector<std::string> parts; 227 std::vector<base::StringPiece> parts = base::SplitStringPiece(
226 base::SplitString(cidr_literal, '/', &parts); 228 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
227 if (parts.size() != 2) 229 if (parts.size() != 2)
228 return false; 230 return false;
229 231
230 // Parse the IP address. 232 // Parse the IP address.
231 if (!ParseIPLiteralToNumber(parts[0], ip_number)) 233 if (!ParseIPLiteralToNumber(parts[0], ip_number))
232 return false; 234 return false;
233 235
234 // Parse the prefix length. 236 // Parse the prefix length.
235 int number_of_bits = -1; 237 int number_of_bits = -1;
236 if (!base::StringToInt(parts[1], &number_of_bits)) 238 if (!base::StringToInt(parts[1], &number_of_bits))
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 } 288 }
287 return a1.size() * CHAR_BIT; 289 return a1.size() * CHAR_BIT;
288 } 290 }
289 291
290 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 292 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
291 IPAddressNumber all_ones(mask.size(), 0xFF); 293 IPAddressNumber all_ones(mask.size(), 0xFF);
292 return CommonPrefixLength(mask, all_ones); 294 return CommonPrefixLength(mask, all_ones);
293 } 295 }
294 296
295 } // namespace net 297 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ip_address_number.h ('k') | net/base/ip_pattern.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698