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

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

Issue 1828103002: Extend net/base/parse_number.h for parsing of negative numbers, and determining if there was overflo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy_num
Patch Set: improve comments 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 #ifndef NET_BASE_PARSE_NUMBER_H_ 5 #ifndef NET_BASE_PARSE_NUMBER_H_
6 #define NET_BASE_PARSE_NUMBER_H_ 6 #define NET_BASE_PARSE_NUMBER_H_
7 7
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/strings/string_piece.h" 9 #include "base/strings/string_piece.h"
10 #include "net/base/net_export.h" 10 #include "net/base/net_export.h"
11 11
12 // This file contains utility functions for parsing numbers, in the context of 12 // This file contains utility functions for parsing numbers, in the context of
13 // network protocols. 13 // network protocols.
14 // 14 //
15 // Q: Doesn't //base already provide these in string_number_conversions.h, with 15 // Q: Doesn't //base already provide these in string_number_conversions.h, with
16 // functions like base::StringToInt()? 16 // functions like base::StringToInt()?
17 // 17 //
18 // A: Yes, and those functions are used under the hood by these 18 // A: Yes, and those functions are used under the hood by these
19 // implementations. 19 // implementations.
20 // 20 //
21 // However using the number parsing functions from //base directly in network 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. 22 // code can lead to subtle bugs, as the //base versions are more permissive.
23 // For instance "+99" is successfully parsed by base::StringToInt(). 23 // For instance "+99" is successfully parsed by base::StringToInt().
24 // 24 //
25 // However in the majority of places in //net, a leading plus on a number 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 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 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. 28 // issue applies when parsing a content-length header.
29 // 29 //
30 // Another difficulty with using base::StringToInt() is distinguishing
31 // overflow/underflow from parsing failures.
32 //
30 // To reduce the risk of such problems, use of these functions over the 33 // To reduce the risk of such problems, use of these functions over the
31 // //base versions. 34 // //base versions.
32 35
33 class GURL; 36 class GURL;
34 37
35 namespace net { 38 namespace net {
36 39
37 // Parses a string representing a decimal number to an |int|. Returns true on 40 // The specific reason why ParseIntegerBase10 failed.
38 // success, and fills |*output| with the result. Note that |*output| is not 41 enum class ParseIntegerError {
39 // modified on failure. 42 // The parsed number couldn't fit into the provided output type because it was
43 // too large.
44 FAILED_OVERFLOW,
45
46 // The parsed number couldn't fit into the provided output type because it was
47 // too small (negative).
48 FAILED_UNDERFLOW,
49
50 // The number failed to be parsed because it wasn't a valid decimal number.
51 // This includes the case where the number was negative but the sign-policy
52 // requested a non-negative number.
53 FAILED_PARSE,
54 };
55
56 // ParseIntegerBase10() parses a string representing a decimal number.
57 // Returns true on success, and fills |*output| with the result. If
58 // |optional_error| was non-null, then it is filled with the reason for the
59 // failure.
40 // 60 //
41 // Recognized inputs take the form: 61 // When allow_negative==true, accepted inputs are of the form:
62 //
42 // 1*DIGIT 63 // 1*DIGIT
43 // 64 //
44 // Where DIGIT is an ASCII number in the range '0' - '9' 65 // Whereas when allow_negative==false, accepted inputs may be optionally
mmenke 2016/03/25 15:31:49 I think you've switched the true and false cases.
eroman 2016/03/25 17:08:09 Oops, indeed
eroman 2016/03/25 19:04:29 Done - this is no longer applicable.
66 // preceded with a negative:
45 // 67 //
46 // Note that: 68 // ("" | "-") 1*DIGIT
69 //
70 // DIGIT is a base-10 digit in the ASCII range '0' - '9'
71 //
72 // (By our definition, "-0" is only allowed when allow_negative==false)
73 //
74 // This function is appropriate for use in places that parse HTTP header
75 // style numbers which are defined as 1*DIGIT.
76 //
77 // Note that:
47 // * Parsing is locale independent 78 // * Parsing is locale independent
48 // * Leading zeros are allowed (numbers needn't be in minimal encoding) 79 // * |*output| is NEVER modified on failure
49 // * Inputs that would overflow the output are rejected. 80 // * |optional_error| can be nullptr
50 // * Only accepts integers 81 // * Leading zeros ARE allowed on any number
51 // 82 // * Zero can be expressed as either a positive or negative quantity,
52 // Examples of recognized inputs are: 83 // TODO(eroman): however "-0" is rejected when the output type is unsigned
53 // "13" 84 // due to an implementation reason
mmenke 2016/03/25 15:31:49 Is this really a TODO? If we don't accept negativ
eroman 2016/03/25 19:04:29 Done: No longer applicable, due to signed/unsigned
54 // "0" 85 NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
55 // "00013" 86 bool allow_negative,
56 // 87 int32_t* output,
57 // Examples of rejected inputs are: 88 ParseIntegerError* optional_error)
58 // " 13" 89 WARN_UNUSED_RESULT;
59 // "-13" 90
60 // "+13" 91 NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
61 // "0x15" 92 bool allow_negative,
62 // "13.3" 93 uint32_t* output,
63 NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input, 94 ParseIntegerError* optional_error)
64 int* output) WARN_UNUSED_RESULT; 95 WARN_UNUSED_RESULT;
96
97 NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
98 bool allow_negative,
99 int64_t* output,
100 ParseIntegerError* optional_error)
101 WARN_UNUSED_RESULT;
102
103 NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
104 bool allow_negative,
mmenke 2016/03/25 15:31:49 Suggest making this an enum - makes for clearer, i
eroman 2016/03/25 19:04:29 Done. I named the enum "ParseInteger" to make the
105 uint64_t* output,
mmenke 2016/03/25 15:31:49 allow_negative + uint seems weird. I'd suggest re
eroman 2016/03/25 17:08:09 Agreed, that was weird, and also agreed it looks c
mmenke 2016/03/25 17:36:32 I'm fine with the default argument values. I almo
eroman 2016/03/25 19:04:29 Ack
106 ParseIntegerError* optional_error)
107 WARN_UNUSED_RESULT;
65 108
66 } // namespace net 109 } // namespace net
67 110
68 #endif // NET_BASE_PARSE_NUMBER_H_ 111 #endif // NET_BASE_PARSE_NUMBER_H_
OLDNEW
« no previous file with comments | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | net/base/parse_number.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698