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

Side by Side Diff: net/base/parse_number.h

Issue 1829873002: Add base/parse_number.h to generalize number parsing done in //net. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more tests Created 4 years, 9 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.cc ('k') | net/base/parse_number.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #ifndef NET_BASE_PARSE_NUMBER_H_
6 #define NET_BASE_PARSE_NUMBER_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/strings/string_piece.h"
10 #include "net/base/net_export.h"
11
12 // This file contains utility functions for parsing numbers, in the context of
13 // network protocols.
14 //
15 // Q: Doesn't //base already provide these in string_number_conversions.h, with
16 // functions like base::StringToInt()?
17 //
18 // A: Yes, and those functions are used under the hood by these
19 // implementations.
20 //
21 // However using the number parsing functions from //base directly in network
22 // code can lead to subtle bugs, as the //base versions are more permissive.
23 // For instance "+99" is successfully parsed by base::StringToInt().
24 //
25 // However in the majority of places in //net, a leading plus on a number
26 // should be considered invalid. For instance when parsing a host:port pair
27 // you wouldn't want to recognize "foo:+99" as having a port of 99. The same
28 // issue applies when parsing a content-length header.
29 //
30 // To reduce the risk of such problems, use of these functions over the
31 // //base versions.
32
33 class GURL;
34
35 namespace net {
36
37 // Parses a string representing a decimal number to an |int|. Returns true on
38 // success, and fills |*output| with the result. Note that |*output| is not
39 // modified on failure.
40 //
41 // Recognized inputs take the form:
42 // 1*DIGIT
43 //
44 // Where DIGIT is an ASCII number in the range '0' - '9'
45 //
46 // Note that:
47 // * Parsing is locale independent
48 // * Leading zeros are allowed (numbers needn't be in minimal encoding)
49 // * Inputs that would overflow the output are rejected.
50 // * Only accepts integers
51 //
52 // Examples of recognized inputs are:
53 // "13"
54 // "0"
55 // "00013"
56 //
57 // Examples of rejected inputs are:
58 // " 13"
59 // "-13"
60 // "+13"
61 // "0x15"
62 // "13.3"
63 NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input,
64 int* output) WARN_UNUSED_RESULT;
65
66 } // namespace net
67
68 #endif // NET_BASE_PARSE_NUMBER_H_
OLDNEW
« no previous file with comments | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698