![]() |
|
|||
File indexing completed on 2025-08-28 08:27:15
0001 // Copyright 2012 the V8 project authors. All rights reserved. 0002 // Redistribution and use in source and binary forms, with or without 0003 // modification, are permitted provided that the following conditions are 0004 // met: 0005 // 0006 // * Redistributions of source code must retain the above copyright 0007 // notice, this list of conditions and the following disclaimer. 0008 // * Redistributions in binary form must reproduce the above 0009 // copyright notice, this list of conditions and the following 0010 // disclaimer in the documentation and/or other materials provided 0011 // with the distribution. 0012 // * Neither the name of Google Inc. nor the names of its 0013 // contributors may be used to endorse or promote products derived 0014 // from this software without specific prior written permission. 0015 // 0016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0027 0028 #ifndef DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_ 0029 #define DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_ 0030 0031 #include "utils.h" 0032 0033 namespace arrow_vendored { 0034 namespace double_conversion { 0035 0036 class StringToDoubleConverter { 0037 public: 0038 // Enumeration for allowing octals and ignoring junk when converting 0039 // strings to numbers. 0040 enum Flags { 0041 NO_FLAGS = 0, 0042 ALLOW_HEX = 1, 0043 ALLOW_OCTALS = 2, 0044 ALLOW_TRAILING_JUNK = 4, 0045 ALLOW_LEADING_SPACES = 8, 0046 ALLOW_TRAILING_SPACES = 16, 0047 ALLOW_SPACES_AFTER_SIGN = 32, 0048 ALLOW_CASE_INSENSITIVITY = 64, 0049 ALLOW_CASE_INSENSIBILITY = 64, // Deprecated 0050 ALLOW_HEX_FLOATS = 128, 0051 }; 0052 0053 static const uc16 kNoSeparator = '\0'; 0054 0055 // Flags should be a bit-or combination of the possible Flags-enum. 0056 // - NO_FLAGS: no special flags. 0057 // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers. 0058 // Ex: StringToDouble("0x1234") -> 4660.0 0059 // In StringToDouble("0x1234.56") the characters ".56" are trailing 0060 // junk. The result of the call is hence dependent on 0061 // the ALLOW_TRAILING_JUNK flag and/or the junk value. 0062 // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK, 0063 // the string will not be parsed as "0" followed by junk. 0064 // 0065 // - ALLOW_OCTALS: recognizes the prefix "0" for octals: 0066 // If a sequence of octal digits starts with '0', then the number is 0067 // read as octal integer. Octal numbers may only be integers. 0068 // Ex: StringToDouble("01234") -> 668.0 0069 // StringToDouble("012349") -> 12349.0 // Not a sequence of octal 0070 // // digits. 0071 // In StringToDouble("01234.56") the characters ".56" are trailing 0072 // junk. The result of the call is hence dependent on 0073 // the ALLOW_TRAILING_JUNK flag and/or the junk value. 0074 // In StringToDouble("01234e56") the characters "e56" are trailing 0075 // junk, too. 0076 // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of 0077 // a double literal. 0078 // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces, 0079 // new-lines, and tabs. 0080 // - ALLOW_TRAILING_SPACES: ignore trailing whitespace. 0081 // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign. 0082 // Ex: StringToDouble("- 123.2") -> -123.2. 0083 // StringToDouble("+ 123.2") -> 123.2 0084 // - ALLOW_CASE_INSENSITIVITY: ignore case of characters for special values: 0085 // infinity and nan. 0086 // - ALLOW_HEX_FLOATS: allows hexadecimal float literals. 0087 // This *must* start with "0x" and separate the exponent with "p". 0088 // Examples: 0x1.2p3 == 9.0 0089 // 0x10.1p0 == 16.0625 0090 // ALLOW_HEX and ALLOW_HEX_FLOATS are indented. 0091 // 0092 // empty_string_value is returned when an empty string is given as input. 0093 // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string 0094 // containing only spaces is converted to the 'empty_string_value', too. 0095 // 0096 // junk_string_value is returned when 0097 // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not 0098 // part of a double-literal) is found. 0099 // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a 0100 // double literal. 0101 // 0102 // infinity_symbol and nan_symbol are strings that are used to detect 0103 // inputs that represent infinity and NaN. They can be null, in which case 0104 // they are ignored. 0105 // The conversion routine first reads any possible signs. Then it compares the 0106 // following character of the input-string with the first character of 0107 // the infinity, and nan-symbol. If either matches, the function assumes, that 0108 // a match has been found, and expects the following input characters to match 0109 // the remaining characters of the special-value symbol. 0110 // This means that the following restrictions apply to special-value symbols: 0111 // - they must not start with signs ('+', or '-'), 0112 // - they must not have the same first character. 0113 // - they must not start with digits. 0114 // 0115 // If the separator character is not kNoSeparator, then that specific 0116 // character is ignored when in between two valid digits of the significant. 0117 // It is not allowed to appear in the exponent. 0118 // It is not allowed to lead or trail the number. 0119 // It is not allowed to appear twice next to each other. 0120 // 0121 // Examples: 0122 // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK, 0123 // empty_string_value = 0.0, 0124 // junk_string_value = NaN, 0125 // infinity_symbol = "infinity", 0126 // nan_symbol = "nan": 0127 // StringToDouble("0x1234") -> 4660.0. 0128 // StringToDouble("0x1234K") -> 4660.0. 0129 // StringToDouble("") -> 0.0 // empty_string_value. 0130 // StringToDouble(" ") -> NaN // junk_string_value. 0131 // StringToDouble(" 1") -> NaN // junk_string_value. 0132 // StringToDouble("0x") -> NaN // junk_string_value. 0133 // StringToDouble("-123.45") -> -123.45. 0134 // StringToDouble("--123.45") -> NaN // junk_string_value. 0135 // StringToDouble("123e45") -> 123e45. 0136 // StringToDouble("123E45") -> 123e45. 0137 // StringToDouble("123e+45") -> 123e45. 0138 // StringToDouble("123E-45") -> 123e-45. 0139 // StringToDouble("123e") -> 123.0 // trailing junk ignored. 0140 // StringToDouble("123e-") -> 123.0 // trailing junk ignored. 0141 // StringToDouble("+NaN") -> NaN // NaN string literal. 0142 // StringToDouble("-infinity") -> -inf. // infinity literal. 0143 // StringToDouble("Infinity") -> NaN // junk_string_value. 0144 // 0145 // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES, 0146 // empty_string_value = 0.0, 0147 // junk_string_value = NaN, 0148 // infinity_symbol = NULL, 0149 // nan_symbol = NULL: 0150 // StringToDouble("0x1234") -> NaN // junk_string_value. 0151 // StringToDouble("01234") -> 668.0. 0152 // StringToDouble("") -> 0.0 // empty_string_value. 0153 // StringToDouble(" ") -> 0.0 // empty_string_value. 0154 // StringToDouble(" 1") -> 1.0 0155 // StringToDouble("0x") -> NaN // junk_string_value. 0156 // StringToDouble("0123e45") -> NaN // junk_string_value. 0157 // StringToDouble("01239E45") -> 1239e45. 0158 // StringToDouble("-infinity") -> NaN // junk_string_value. 0159 // StringToDouble("NaN") -> NaN // junk_string_value. 0160 // 0161 // flags = NO_FLAGS, 0162 // separator = ' ': 0163 // StringToDouble("1 2 3 4") -> 1234.0 0164 // StringToDouble("1 2") -> NaN // junk_string_value 0165 // StringToDouble("1 000 000.0") -> 1000000.0 0166 // StringToDouble("1.000 000") -> 1.0 0167 // StringToDouble("1.0e1 000") -> NaN // junk_string_value 0168 StringToDoubleConverter(int flags, 0169 double empty_string_value, 0170 double junk_string_value, 0171 const char* infinity_symbol, 0172 const char* nan_symbol, 0173 uc16 separator = kNoSeparator) 0174 : flags_(flags), 0175 empty_string_value_(empty_string_value), 0176 junk_string_value_(junk_string_value), 0177 infinity_symbol_(infinity_symbol), 0178 nan_symbol_(nan_symbol), 0179 separator_(separator) { 0180 } 0181 0182 // Performs the conversion. 0183 // The output parameter 'processed_characters_count' is set to the number 0184 // of characters that have been processed to read the number. 0185 // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included 0186 // in the 'processed_characters_count'. Trailing junk is never included. 0187 double StringToDouble(const char* buffer, 0188 int length, 0189 int* processed_characters_count) const; 0190 0191 // Same as StringToDouble above but for 16 bit characters. 0192 double StringToDouble(const uc16* buffer, 0193 int length, 0194 int* processed_characters_count) const; 0195 0196 // Same as StringToDouble but reads a float. 0197 // Note that this is not equivalent to static_cast<float>(StringToDouble(...)) 0198 // due to potential double-rounding. 0199 float StringToFloat(const char* buffer, 0200 int length, 0201 int* processed_characters_count) const; 0202 0203 // Same as StringToFloat above but for 16 bit characters. 0204 float StringToFloat(const uc16* buffer, 0205 int length, 0206 int* processed_characters_count) const; 0207 0208 // Same as StringToDouble for T = double, and StringToFloat for T = float. 0209 template <typename T> 0210 T StringTo(const char* buffer, 0211 int length, 0212 int* processed_characters_count) const; 0213 0214 // Same as StringTo above but for 16 bit characters. 0215 template <typename T> 0216 T StringTo(const uc16* buffer, 0217 int length, 0218 int* processed_characters_count) const; 0219 0220 private: 0221 const int flags_; 0222 const double empty_string_value_; 0223 const double junk_string_value_; 0224 const char* const infinity_symbol_; 0225 const char* const nan_symbol_; 0226 const uc16 separator_; 0227 0228 template <class Iterator> 0229 double StringToIeee(Iterator start_pointer, 0230 int length, 0231 bool read_as_double, 0232 int* processed_characters_count) const; 0233 0234 DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter); 0235 }; 0236 0237 } // namespace double_conversion 0238 } // namespace arrow_vendored 0239 0240 #endif // DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |