| OLD | NEW |
| 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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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 // To reduce the risk of such problems, use of these functions over the | 30 // To reduce the risk of such problems, use of these functions over the |
| 31 // //base versions. | 31 // //base versions. |
| 32 | 32 |
| 33 class GURL; | 33 class GURL; |
| 34 | 34 |
| 35 namespace net { | 35 namespace net { |
| 36 | 36 |
| 37 // Parses a string representing a decimal number to an |int|. Returns true on | 37 // ParseNonNegativeInteger() parses a string representing a decimal number. |
| 38 // success, and fills |*output| with the result. Note that |*output| is not | 38 // Returns true on success, and fills |*output| with the result: |
| 39 // modified on failure. | |
| 40 // | 39 // |
| 41 // Recognized inputs take the form: | 40 // ParseNonNegativeInteger() recognizes inputs of the form: |
| 42 // 1*DIGIT | 41 // 1*DIGIT |
| 43 // | 42 // |
| 44 // Where DIGIT is an ASCII number in the range '0' - '9' | 43 // Where DIGIT is an ASCII number in the range '0' - '9' |
| 45 // | 44 // |
| 46 // Note that: | 45 // Note that: |
| 47 // * Parsing is locale independent | 46 // * Parsing is locale independent |
| 47 // * |*output| is not modified on failure |
| 48 // * Leading zeros are allowed (numbers needn't be in minimal encoding) | 48 // * Leading zeros are allowed (numbers needn't be in minimal encoding) |
| 49 // * Inputs that would overflow the output are rejected. | 49 // * Inputs that would overflow the output type are rejected |
| 50 // * Only accepts integers | 50 // * ParseNonNegativeInteger() has overloads for a variety of output |
| 51 // types. Regardless of the output type's signedness, the output is NEVER |
| 52 // negative on success. |
| 51 // | 53 // |
| 52 // Examples of recognized inputs are: | 54 // Examples of accepted inputs are: |
| 53 // "13" | 55 // "13" |
| 54 // "0" | 56 // "0" |
| 55 // "00013" | 57 // "00013" |
| 56 // | 58 // |
| 57 // Examples of rejected inputs are: | 59 // Examples of rejected inputs are: |
| 58 // " 13" | 60 // " 13" |
| 59 // "-13" | 61 // "-13" |
| 60 // "+13" | 62 // "+13" |
| 61 // "0x15" | 63 // "0x15" |
| 62 // "13.3" | 64 // "13.3" |
| 63 NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input, | 65 // "999999999999999999999999999999999999999999999" |
| 64 int* output) WARN_UNUSED_RESULT; | 66 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input, |
| 67 int32_t* output) WARN_UNUSED_RESULT; |
| 68 |
| 69 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input, |
| 70 uint32_t* output) WARN_UNUSED_RESULT; |
| 71 |
| 72 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input, |
| 73 int64_t* output) WARN_UNUSED_RESULT; |
| 74 |
| 75 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input, |
| 76 uint64_t* output) WARN_UNUSED_RESULT; |
| 77 |
| 78 // ParseSignedInteger() parses a string representing a decimal number. |
| 79 // Returns true on success, and fills |*output| with the result: |
| 80 // |
| 81 // ParseSignedInteger() recognizes inputs of the form: |
| 82 // ("" | "-") 1*DIGIT |
| 83 // |
| 84 // Where DIGIT is an ASCII number in the range '0' - '9' |
| 85 // |
| 86 // Note that: |
| 87 // * Parsing is locale independent |
| 88 // * |*output| is not modified on failure |
| 89 // * Leading zeros are allowed (numbers needn't be in minimal encoding) |
| 90 // * Zero can be expressed as a negative or positive quantity. |
| 91 // * Inputs that would overflow/underflow the output type are rejected |
| 92 // |
| 93 // Examples of accepted inputs are: |
| 94 // "13" |
| 95 // "-13" |
| 96 // "0" |
| 97 // "-0" |
| 98 // "00013" |
| 99 // |
| 100 // Examples of rejected inputs are: |
| 101 // " 13" |
| 102 // "+13" |
| 103 // "0x15" |
| 104 // "13.3" |
| 105 // "999999999999999999999999999999999999999999999" |
| 106 NET_EXPORT bool ParseSignedInteger(const base::StringPiece& input, |
| 107 int64_t* output) WARN_UNUSED_RESULT; |
| 108 |
| 109 NET_EXPORT bool ParseSignedInteger(const base::StringPiece& input, |
| 110 int32_t* output) WARN_UNUSED_RESULT; |
| 65 | 111 |
| 66 } // namespace net | 112 } // namespace net |
| 67 | 113 |
| 68 #endif // NET_BASE_PARSE_NUMBER_H_ | 114 #endif // NET_BASE_PARSE_NUMBER_H_ |
| OLD | NEW |