|
||||
File indexing completed on 2025-01-18 09:55:27
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 double_conversion { 0034 0035 class StringToDoubleConverter { 0036 public: 0037 // Enumeration for allowing octals and ignoring junk when converting 0038 // strings to numbers. 0039 enum Flags { 0040 NO_FLAGS = 0, 0041 ALLOW_HEX = 1, 0042 ALLOW_OCTALS = 2, 0043 ALLOW_TRAILING_JUNK = 4, 0044 ALLOW_LEADING_SPACES = 8, 0045 ALLOW_TRAILING_SPACES = 16, 0046 ALLOW_SPACES_AFTER_SIGN = 32, 0047 ALLOW_CASE_INSENSITIVITY = 64, 0048 ALLOW_CASE_INSENSIBILITY = 64, // Deprecated 0049 ALLOW_HEX_FLOATS = 128, 0050 }; 0051 0052 static const uc16 kNoSeparator = '\0'; 0053 0054 // Flags should be a bit-or combination of the possible Flags-enum. 0055 // - NO_FLAGS: no special flags. 0056 // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers. 0057 // Ex: StringToDouble("0x1234") -> 4660.0 0058 // In StringToDouble("0x1234.56") the characters ".56" are trailing 0059 // junk. The result of the call is hence dependent on 0060 // the ALLOW_TRAILING_JUNK flag and/or the junk value. 0061 // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK, 0062 // the string will not be parsed as "0" followed by junk. 0063 // 0064 // - ALLOW_OCTALS: recognizes the prefix "0" for octals: 0065 // If a sequence of octal digits starts with '0', then the number is 0066 // read as octal integer. Octal numbers may only be integers. 0067 // Ex: StringToDouble("01234") -> 668.0 0068 // StringToDouble("012349") -> 12349.0 // Not a sequence of octal 0069 // // digits. 0070 // In StringToDouble("01234.56") the characters ".56" are trailing 0071 // junk. The result of the call is hence dependent on 0072 // the ALLOW_TRAILING_JUNK flag and/or the junk value. 0073 // In StringToDouble("01234e56") the characters "e56" are trailing 0074 // junk, too. 0075 // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of 0076 // a double literal. 0077 // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces, 0078 // new-lines, and tabs. 0079 // - ALLOW_TRAILING_SPACES: ignore trailing whitespace. 0080 // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign. 0081 // Ex: StringToDouble("- 123.2") -> -123.2. 0082 // StringToDouble("+ 123.2") -> 123.2 0083 // - ALLOW_CASE_INSENSITIVITY: ignore case of characters for special values: 0084 // infinity and nan. 0085 // - ALLOW_HEX_FLOATS: allows hexadecimal float literals. 0086 // This *must* start with "0x" and separate the exponent with "p". 0087 // Examples: 0x1.2p3 == 9.0 0088 // 0x10.1p0 == 16.0625 0089 // ALLOW_HEX and ALLOW_HEX_FLOATS are indented. 0090 // 0091 // empty_string_value is returned when an empty string is given as input. 0092 // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string 0093 // containing only spaces is converted to the 'empty_string_value', too. 0094 // 0095 // junk_string_value is returned when 0096 // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not 0097 // part of a double-literal) is found. 0098 // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a 0099 // double literal. 0100 // 0101 // infinity_symbol and nan_symbol are strings that are used to detect 0102 // inputs that represent infinity and NaN. They can be null, in which case 0103 // they are ignored. 0104 // The conversion routine first reads any possible signs. Then it compares the 0105 // following character of the input-string with the first character of 0106 // the infinity, and nan-symbol. If either matches, the function assumes, that 0107 // a match has been found, and expects the following input characters to match 0108 // the remaining characters of the special-value symbol. 0109 // This means that the following restrictions apply to special-value symbols: 0110 // - they must not start with signs ('+', or '-'), 0111 // - they must not have the same first character. 0112 // - they must not start with digits. 0113 // 0114 // If the separator character is not kNoSeparator, then that specific 0115 // character is ignored when in between two valid digits of the significant. 0116 // It is not allowed to appear in the exponent. 0117 // It is not allowed to lead or trail the number. 0118 // It is not allowed to appear twice next to each other. 0119 // 0120 // Examples: 0121 // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK, 0122 // empty_string_value = 0.0, 0123 // junk_string_value = NaN, 0124 // infinity_symbol = "infinity", 0125 // nan_symbol = "nan": 0126 // StringToDouble("0x1234") -> 4660.0. 0127 // StringToDouble("0x1234K") -> 4660.0. 0128 // StringToDouble("") -> 0.0 // empty_string_value. 0129 // StringToDouble(" ") -> NaN // junk_string_value. 0130 // StringToDouble(" 1") -> NaN // junk_string_value. 0131 // StringToDouble("0x") -> NaN // junk_string_value. 0132 // StringToDouble("-123.45") -> -123.45. 0133 // StringToDouble("--123.45") -> NaN // junk_string_value. 0134 // StringToDouble("123e45") -> 123e45. 0135 // StringToDouble("123E45") -> 123e45. 0136 // StringToDouble("123e+45") -> 123e45. 0137 // StringToDouble("123E-45") -> 123e-45. 0138 // StringToDouble("123e") -> 123.0 // trailing junk ignored. 0139 // StringToDouble("123e-") -> 123.0 // trailing junk ignored. 0140 // StringToDouble("+NaN") -> NaN // NaN string literal. 0141 // StringToDouble("-infinity") -> -inf. // infinity literal. 0142 // StringToDouble("Infinity") -> NaN // junk_string_value. 0143 // 0144 // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES, 0145 // empty_string_value = 0.0, 0146 // junk_string_value = NaN, 0147 // infinity_symbol = NULL, 0148 // nan_symbol = NULL: 0149 // StringToDouble("0x1234") -> NaN // junk_string_value. 0150 // StringToDouble("01234") -> 668.0. 0151 // StringToDouble("") -> 0.0 // empty_string_value. 0152 // StringToDouble(" ") -> 0.0 // empty_string_value. 0153 // StringToDouble(" 1") -> 1.0 0154 // StringToDouble("0x") -> NaN // junk_string_value. 0155 // StringToDouble("0123e45") -> NaN // junk_string_value. 0156 // StringToDouble("01239E45") -> 1239e45. 0157 // StringToDouble("-infinity") -> NaN // junk_string_value. 0158 // StringToDouble("NaN") -> NaN // junk_string_value. 0159 // 0160 // flags = NO_FLAGS, 0161 // separator = ' ': 0162 // StringToDouble("1 2 3 4") -> 1234.0 0163 // StringToDouble("1 2") -> NaN // junk_string_value 0164 // StringToDouble("1 000 000.0") -> 1000000.0 0165 // StringToDouble("1.000 000") -> 1.0 0166 // StringToDouble("1.0e1 000") -> NaN // junk_string_value 0167 StringToDoubleConverter(int flags, 0168 double empty_string_value, 0169 double junk_string_value, 0170 const char* infinity_symbol, 0171 const char* nan_symbol, 0172 uc16 separator = kNoSeparator) 0173 : flags_(flags), 0174 empty_string_value_(empty_string_value), 0175 junk_string_value_(junk_string_value), 0176 infinity_symbol_(infinity_symbol), 0177 nan_symbol_(nan_symbol), 0178 separator_(separator) { 0179 } 0180 0181 // Performs the conversion. 0182 // The output parameter 'processed_characters_count' is set to the number 0183 // of characters that have been processed to read the number. 0184 // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included 0185 // in the 'processed_characters_count'. Trailing junk is never included. 0186 double StringToDouble(const char* buffer, 0187 int length, 0188 int* processed_characters_count) const; 0189 0190 // Same as StringToDouble above but for 16 bit characters. 0191 double StringToDouble(const uc16* buffer, 0192 int length, 0193 int* processed_characters_count) const; 0194 0195 // Same as StringToDouble but reads a float. 0196 // Note that this is not equivalent to static_cast<float>(StringToDouble(...)) 0197 // due to potential double-rounding. 0198 float StringToFloat(const char* buffer, 0199 int length, 0200 int* processed_characters_count) const; 0201 0202 // Same as StringToFloat above but for 16 bit characters. 0203 float StringToFloat(const uc16* buffer, 0204 int length, 0205 int* processed_characters_count) const; 0206 0207 // Same as StringToDouble for T = double, and StringToFloat for T = float. 0208 template <typename T> 0209 T StringTo(const char* buffer, 0210 int length, 0211 int* processed_characters_count) const; 0212 0213 // Same as StringTo above but for 16 bit characters. 0214 template <typename T> 0215 T StringTo(const uc16* buffer, 0216 int length, 0217 int* processed_characters_count) const; 0218 0219 private: 0220 const int flags_; 0221 const double empty_string_value_; 0222 const double junk_string_value_; 0223 const char* const infinity_symbol_; 0224 const char* const nan_symbol_; 0225 const uc16 separator_; 0226 0227 template <class Iterator> 0228 double StringToIeee(Iterator start_pointer, 0229 int length, 0230 bool read_as_double, 0231 int* processed_characters_count) const; 0232 0233 DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter); 0234 }; 0235 0236 } // namespace double_conversion 0237 0238 #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 |