| 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 #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_ |
| OLD | NEW |