Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 // This file contains utility functions for parsing numbers, in the context of | |
| 6 // network protocols. | |
| 7 // | |
| 8 // Q: Doesn't //base already provide these in string_number_conversions.h, with | |
| 9 // functions like base::StringToInt()? | |
| 10 // | |
| 11 // A: Yes, and those functions are used under the hood by these | |
| 12 // implementations. | |
| 13 // | |
| 14 // However using the number parsing functions from //base directly in network | |
| 15 // code can lead to subtle bugs, as the //base versions are more permissive. | |
| 16 // For instance "+99" is successfully parsed by base::StringToInt(). | |
| 17 // | |
| 18 // However in the majority of places in //net, a leading plus on a number | |
| 19 // should be considered invalid. For instance when parsing a host:port pair | |
| 20 // you wouldn't want to recognize "foo:+99" as having a port of 99. The same | |
| 21 // issue applies when parsing a content-length header. | |
| 22 // | |
| 23 // To reduce the risk of such problems, use of these functions over the | |
| 24 // //base versions. | |
|
mmenke
2016/03/23 19:32:28
Mind putting these under the includes? I know tha
eroman
2016/03/23 19:50:30
Done, I agree with finding it there hard.
However
mmenke
2016/03/23 20:12:06
I dunno...This doesn't describe the contents of th
| |
| 25 | |
| 26 #ifndef NET_BASE_PARSE_NUMBER_H_ | |
| 27 #define NET_BASE_PARSE_NUMBER_H_ | |
| 28 | |
| 29 #include "base/compiler_specific.h" | |
| 30 #include "base/strings/string_piece.h" | |
|
mmenke
2016/03/23 19:32:28
Forward declare this, and include it in the CC fil
eroman
2016/03/23 19:50:30
StringPiece I believe can't (easily) be forward de
mmenke
2016/03/23 20:12:06
I'm afraid I don't. StringPiece looks so nice and
| |
| 31 #include "net/base/net_export.h" | |
| 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, | |
|
eroman
2016/03/23 19:15:01
This may seem like overkill to have 1 function in
mmenke
2016/03/23 19:32:28
I think this is reasonable.
| |
| 64 int* output) WARN_UNUSED_RESULT; | |
|
eroman
2016/03/23 19:15:01
WARN_UNUSED_RESULT is also a modification over the
| |
| 65 | |
| 66 } // namespace net | |
| 67 | |
| 68 #endif // NET_BASE_PARSE_NUMBER_H_ | |
| OLD | NEW |