1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/double-conversion/double-conversion.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,538 @@ 1.4 +// Copyright 2012 the V8 project authors. All rights reserved. 1.5 +// Redistribution and use in source and binary forms, with or without 1.6 +// modification, are permitted provided that the following conditions are 1.7 +// met: 1.8 +// 1.9 +// * Redistributions of source code must retain the above copyright 1.10 +// notice, this list of conditions and the following disclaimer. 1.11 +// * Redistributions in binary form must reproduce the above 1.12 +// copyright notice, this list of conditions and the following 1.13 +// disclaimer in the documentation and/or other materials provided 1.14 +// with the distribution. 1.15 +// * Neither the name of Google Inc. nor the names of its 1.16 +// contributors may be used to endorse or promote products derived 1.17 +// from this software without specific prior written permission. 1.18 +// 1.19 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.20 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.21 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.22 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.23 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.24 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.25 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.26 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.27 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.29 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + 1.31 +#ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 1.32 +#define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 1.33 + 1.34 +#include "mozilla/Types.h" 1.35 +#include "utils.h" 1.36 + 1.37 +namespace double_conversion { 1.38 + 1.39 +class DoubleToStringConverter { 1.40 + public: 1.41 + // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint 1.42 + // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the 1.43 + // function returns false. 1.44 + static const int kMaxFixedDigitsBeforePoint = 60; 1.45 + static const int kMaxFixedDigitsAfterPoint = 60; 1.46 + 1.47 + // When calling ToExponential with a requested_digits 1.48 + // parameter > kMaxExponentialDigits then the function returns false. 1.49 + static const int kMaxExponentialDigits = 120; 1.50 + 1.51 + // When calling ToPrecision with a requested_digits 1.52 + // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits 1.53 + // then the function returns false. 1.54 + static const int kMinPrecisionDigits = 1; 1.55 + static const int kMaxPrecisionDigits = 120; 1.56 + 1.57 + enum Flags { 1.58 + NO_FLAGS = 0, 1.59 + EMIT_POSITIVE_EXPONENT_SIGN = 1, 1.60 + EMIT_TRAILING_DECIMAL_POINT = 2, 1.61 + EMIT_TRAILING_ZERO_AFTER_POINT = 4, 1.62 + UNIQUE_ZERO = 8 1.63 + }; 1.64 + 1.65 + // Flags should be a bit-or combination of the possible Flags-enum. 1.66 + // - NO_FLAGS: no special flags. 1.67 + // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent 1.68 + // form, emits a '+' for positive exponents. Example: 1.2e+2. 1.69 + // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is 1.70 + // converted into decimal format then a trailing decimal point is appended. 1.71 + // Example: 2345.0 is converted to "2345.". 1.72 + // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point 1.73 + // emits a trailing '0'-character. This flag requires the 1.74 + // EXMIT_TRAILING_DECIMAL_POINT flag. 1.75 + // Example: 2345.0 is converted to "2345.0". 1.76 + // - UNIQUE_ZERO: "-0.0" is converted to "0.0". 1.77 + // 1.78 + // Infinity symbol and nan_symbol provide the string representation for these 1.79 + // special values. If the string is NULL and the special value is encountered 1.80 + // then the conversion functions return false. 1.81 + // 1.82 + // The exponent_character is used in exponential representations. It is 1.83 + // usually 'e' or 'E'. 1.84 + // 1.85 + // When converting to the shortest representation the converter will 1.86 + // represent input numbers in decimal format if they are in the interval 1.87 + // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ 1.88 + // (lower boundary included, greater boundary excluded). 1.89 + // Example: with decimal_in_shortest_low = -6 and 1.90 + // decimal_in_shortest_high = 21: 1.91 + // ToShortest(0.000001) -> "0.000001" 1.92 + // ToShortest(0.0000001) -> "1e-7" 1.93 + // ToShortest(111111111111111111111.0) -> "111111111111111110000" 1.94 + // ToShortest(100000000000000000000.0) -> "100000000000000000000" 1.95 + // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 1.96 + // 1.97 + // When converting to precision mode the converter may add 1.98 + // max_leading_padding_zeroes before returning the number in exponential 1.99 + // format. 1.100 + // Example with max_leading_padding_zeroes_in_precision_mode = 6. 1.101 + // ToPrecision(0.0000012345, 2) -> "0.0000012" 1.102 + // ToPrecision(0.00000012345, 2) -> "1.2e-7" 1.103 + // Similarily the converter may add up to 1.104 + // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid 1.105 + // returning an exponential representation. A zero added by the 1.106 + // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 1.107 + // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 1.108 + // ToPrecision(230.0, 2) -> "230" 1.109 + // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 1.110 + // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. 1.111 + DoubleToStringConverter(int flags, 1.112 + const char* infinity_symbol, 1.113 + const char* nan_symbol, 1.114 + char exponent_character, 1.115 + int decimal_in_shortest_low, 1.116 + int decimal_in_shortest_high, 1.117 + int max_leading_padding_zeroes_in_precision_mode, 1.118 + int max_trailing_padding_zeroes_in_precision_mode) 1.119 + : flags_(flags), 1.120 + infinity_symbol_(infinity_symbol), 1.121 + nan_symbol_(nan_symbol), 1.122 + exponent_character_(exponent_character), 1.123 + decimal_in_shortest_low_(decimal_in_shortest_low), 1.124 + decimal_in_shortest_high_(decimal_in_shortest_high), 1.125 + max_leading_padding_zeroes_in_precision_mode_( 1.126 + max_leading_padding_zeroes_in_precision_mode), 1.127 + max_trailing_padding_zeroes_in_precision_mode_( 1.128 + max_trailing_padding_zeroes_in_precision_mode) { 1.129 + // When 'trailing zero after the point' is set, then 'trailing point' 1.130 + // must be set too. 1.131 + ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || 1.132 + !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); 1.133 + } 1.134 + 1.135 + // Returns a converter following the EcmaScript specification. 1.136 + static MFBT_API const DoubleToStringConverter& EcmaScriptConverter(); 1.137 + 1.138 + // Computes the shortest string of digits that correctly represent the input 1.139 + // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high 1.140 + // (see constructor) it then either returns a decimal representation, or an 1.141 + // exponential representation. 1.142 + // Example with decimal_in_shortest_low = -6, 1.143 + // decimal_in_shortest_high = 21, 1.144 + // EMIT_POSITIVE_EXPONENT_SIGN activated, and 1.145 + // EMIT_TRAILING_DECIMAL_POINT deactived: 1.146 + // ToShortest(0.000001) -> "0.000001" 1.147 + // ToShortest(0.0000001) -> "1e-7" 1.148 + // ToShortest(111111111111111111111.0) -> "111111111111111110000" 1.149 + // ToShortest(100000000000000000000.0) -> "100000000000000000000" 1.150 + // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 1.151 + // 1.152 + // Note: the conversion may round the output if the returned string 1.153 + // is accurate enough to uniquely identify the input-number. 1.154 + // For example the most precise representation of the double 9e59 equals 1.155 + // "899999999999999918767229449717619953810131273674690656206848", but 1.156 + // the converter will return the shorter (but still correct) "9e59". 1.157 + // 1.158 + // Returns true if the conversion succeeds. The conversion always succeeds 1.159 + // except when the input value is special and no infinity_symbol or 1.160 + // nan_symbol has been given to the constructor. 1.161 + bool ToShortest(double value, StringBuilder* result_builder) const { 1.162 + return ToShortestIeeeNumber(value, result_builder, SHORTEST); 1.163 + } 1.164 + 1.165 + // Same as ToShortest, but for single-precision floats. 1.166 + bool ToShortestSingle(float value, StringBuilder* result_builder) const { 1.167 + return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); 1.168 + } 1.169 + 1.170 + 1.171 + // Computes a decimal representation with a fixed number of digits after the 1.172 + // decimal point. The last emitted digit is rounded. 1.173 + // 1.174 + // Examples: 1.175 + // ToFixed(3.12, 1) -> "3.1" 1.176 + // ToFixed(3.1415, 3) -> "3.142" 1.177 + // ToFixed(1234.56789, 4) -> "1234.5679" 1.178 + // ToFixed(1.23, 5) -> "1.23000" 1.179 + // ToFixed(0.1, 4) -> "0.1000" 1.180 + // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" 1.181 + // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" 1.182 + // ToFixed(0.1, 17) -> "0.10000000000000001" 1.183 + // 1.184 + // If requested_digits equals 0, then the tail of the result depends on 1.185 + // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. 1.186 + // Examples, for requested_digits == 0, 1.187 + // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be 1.188 + // - false and false: then 123.45 -> 123 1.189 + // 0.678 -> 1 1.190 + // - true and false: then 123.45 -> 123. 1.191 + // 0.678 -> 1. 1.192 + // - true and true: then 123.45 -> 123.0 1.193 + // 0.678 -> 1.0 1.194 + // 1.195 + // Returns true if the conversion succeeds. The conversion always succeeds 1.196 + // except for the following cases: 1.197 + // - the input value is special and no infinity_symbol or nan_symbol has 1.198 + // been provided to the constructor, 1.199 + // - 'value' > 10^kMaxFixedDigitsBeforePoint, or 1.200 + // - 'requested_digits' > kMaxFixedDigitsAfterPoint. 1.201 + // The last two conditions imply that the result will never contain more than 1.202 + // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters 1.203 + // (one additional character for the sign, and one for the decimal point). 1.204 + MFBT_API bool ToFixed(double value, 1.205 + int requested_digits, 1.206 + StringBuilder* result_builder) const; 1.207 + 1.208 + // Computes a representation in exponential format with requested_digits 1.209 + // after the decimal point. The last emitted digit is rounded. 1.210 + // If requested_digits equals -1, then the shortest exponential representation 1.211 + // is computed. 1.212 + // 1.213 + // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and 1.214 + // exponent_character set to 'e'. 1.215 + // ToExponential(3.12, 1) -> "3.1e0" 1.216 + // ToExponential(5.0, 3) -> "5.000e0" 1.217 + // ToExponential(0.001, 2) -> "1.00e-3" 1.218 + // ToExponential(3.1415, -1) -> "3.1415e0" 1.219 + // ToExponential(3.1415, 4) -> "3.1415e0" 1.220 + // ToExponential(3.1415, 3) -> "3.142e0" 1.221 + // ToExponential(123456789000000, 3) -> "1.235e14" 1.222 + // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" 1.223 + // ToExponential(1000000000000000019884624838656.0, 32) -> 1.224 + // "1.00000000000000001988462483865600e30" 1.225 + // ToExponential(1234, 0) -> "1e3" 1.226 + // 1.227 + // Returns true if the conversion succeeds. The conversion always succeeds 1.228 + // except for the following cases: 1.229 + // - the input value is special and no infinity_symbol or nan_symbol has 1.230 + // been provided to the constructor, 1.231 + // - 'requested_digits' > kMaxExponentialDigits. 1.232 + // The last condition implies that the result will never contain more than 1.233 + // kMaxExponentialDigits + 8 characters (the sign, the digit before the 1.234 + // decimal point, the decimal point, the exponent character, the 1.235 + // exponent's sign, and at most 3 exponent digits). 1.236 + MFBT_API bool ToExponential(double value, 1.237 + int requested_digits, 1.238 + StringBuilder* result_builder) const; 1.239 + 1.240 + // Computes 'precision' leading digits of the given 'value' and returns them 1.241 + // either in exponential or decimal format, depending on 1.242 + // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the 1.243 + // constructor). 1.244 + // The last computed digit is rounded. 1.245 + // 1.246 + // Example with max_leading_padding_zeroes_in_precision_mode = 6. 1.247 + // ToPrecision(0.0000012345, 2) -> "0.0000012" 1.248 + // ToPrecision(0.00000012345, 2) -> "1.2e-7" 1.249 + // Similarily the converter may add up to 1.250 + // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid 1.251 + // returning an exponential representation. A zero added by the 1.252 + // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 1.253 + // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 1.254 + // ToPrecision(230.0, 2) -> "230" 1.255 + // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 1.256 + // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. 1.257 + // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no 1.258 + // EMIT_TRAILING_ZERO_AFTER_POINT: 1.259 + // ToPrecision(123450.0, 6) -> "123450" 1.260 + // ToPrecision(123450.0, 5) -> "123450" 1.261 + // ToPrecision(123450.0, 4) -> "123500" 1.262 + // ToPrecision(123450.0, 3) -> "123000" 1.263 + // ToPrecision(123450.0, 2) -> "1.2e5" 1.264 + // 1.265 + // Returns true if the conversion succeeds. The conversion always succeeds 1.266 + // except for the following cases: 1.267 + // - the input value is special and no infinity_symbol or nan_symbol has 1.268 + // been provided to the constructor, 1.269 + // - precision < kMinPericisionDigits 1.270 + // - precision > kMaxPrecisionDigits 1.271 + // The last condition implies that the result will never contain more than 1.272 + // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the 1.273 + // exponent character, the exponent's sign, and at most 3 exponent digits). 1.274 + MFBT_API bool ToPrecision(double value, 1.275 + int precision, 1.276 + bool* used_exponential_notation, 1.277 + StringBuilder* result_builder) const; 1.278 + 1.279 + enum DtoaMode { 1.280 + // Produce the shortest correct representation. 1.281 + // For example the output of 0.299999999999999988897 is (the less accurate 1.282 + // but correct) 0.3. 1.283 + SHORTEST, 1.284 + // Same as SHORTEST, but for single-precision floats. 1.285 + SHORTEST_SINGLE, 1.286 + // Produce a fixed number of digits after the decimal point. 1.287 + // For instance fixed(0.1, 4) becomes 0.1000 1.288 + // If the input number is big, the output will be big. 1.289 + FIXED, 1.290 + // Fixed number of digits (independent of the decimal point). 1.291 + PRECISION 1.292 + }; 1.293 + 1.294 + // The maximal number of digits that are needed to emit a double in base 10. 1.295 + // A higher precision can be achieved by using more digits, but the shortest 1.296 + // accurate representation of any double will never use more digits than 1.297 + // kBase10MaximalLength. 1.298 + // Note that DoubleToAscii null-terminates its input. So the given buffer 1.299 + // should be at least kBase10MaximalLength + 1 characters long. 1.300 + static const MFBT_DATA int kBase10MaximalLength = 17; 1.301 + 1.302 + // Converts the given double 'v' to ascii. 'v' must not be NaN, +Infinity, or 1.303 + // -Infinity. In SHORTEST_SINGLE-mode this restriction also applies to 'v' 1.304 + // after it has been casted to a single-precision float. That is, in this 1.305 + // mode static_cast<float>(v) must not be NaN, +Infinity or -Infinity. 1.306 + // 1.307 + // The result should be interpreted as buffer * 10^(point-length). 1.308 + // 1.309 + // The output depends on the given mode: 1.310 + // - SHORTEST: produce the least amount of digits for which the internal 1.311 + // identity requirement is still satisfied. If the digits are printed 1.312 + // (together with the correct exponent) then reading this number will give 1.313 + // 'v' again. The buffer will choose the representation that is closest to 1.314 + // 'v'. If there are two at the same distance, than the one farther away 1.315 + // from 0 is chosen (halfway cases - ending with 5 - are rounded up). 1.316 + // In this mode the 'requested_digits' parameter is ignored. 1.317 + // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. 1.318 + // - FIXED: produces digits necessary to print a given number with 1.319 + // 'requested_digits' digits after the decimal point. The produced digits 1.320 + // might be too short in which case the caller has to fill the remainder 1.321 + // with '0's. 1.322 + // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. 1.323 + // Halfway cases are rounded towards +/-Infinity (away from 0). The call 1.324 + // toFixed(0.15, 2) thus returns buffer="2", point=0. 1.325 + // The returned buffer may contain digits that would be truncated from the 1.326 + // shortest representation of the input. 1.327 + // - PRECISION: produces 'requested_digits' where the first digit is not '0'. 1.328 + // Even though the length of produced digits usually equals 1.329 + // 'requested_digits', the function is allowed to return fewer digits, in 1.330 + // which case the caller has to fill the missing digits with '0's. 1.331 + // Halfway cases are again rounded away from 0. 1.332 + // DoubleToAscii expects the given buffer to be big enough to hold all 1.333 + // digits and a terminating null-character. In SHORTEST-mode it expects a 1.334 + // buffer of at least kBase10MaximalLength + 1. In all other modes the 1.335 + // requested_digits parameter and the padding-zeroes limit the size of the 1.336 + // output. Don't forget the decimal point, the exponent character and the 1.337 + // terminating null-character when computing the maximal output size. 1.338 + // The given length is only used in debug mode to ensure the buffer is big 1.339 + // enough. 1.340 + static MFBT_API void DoubleToAscii(double v, 1.341 + DtoaMode mode, 1.342 + int requested_digits, 1.343 + char* buffer, 1.344 + int buffer_length, 1.345 + bool* sign, 1.346 + int* length, 1.347 + int* point); 1.348 + 1.349 + private: 1.350 + // Implementation for ToShortest and ToShortestSingle. 1.351 + MFBT_API bool ToShortestIeeeNumber(double value, 1.352 + StringBuilder* result_builder, 1.353 + DtoaMode mode) const; 1.354 + 1.355 + // If the value is a special value (NaN or Infinity) constructs the 1.356 + // corresponding string using the configured infinity/nan-symbol. 1.357 + // If either of them is NULL or the value is not special then the 1.358 + // function returns false. 1.359 + MFBT_API bool HandleSpecialValues(double value, StringBuilder* result_builder) const; 1.360 + // Constructs an exponential representation (i.e. 1.234e56). 1.361 + // The given exponent assumes a decimal point after the first decimal digit. 1.362 + MFBT_API void CreateExponentialRepresentation(const char* decimal_digits, 1.363 + int length, 1.364 + int exponent, 1.365 + StringBuilder* result_builder) const; 1.366 + // Creates a decimal representation (i.e 1234.5678). 1.367 + MFBT_API void CreateDecimalRepresentation(const char* decimal_digits, 1.368 + int length, 1.369 + int decimal_point, 1.370 + int digits_after_point, 1.371 + StringBuilder* result_builder) const; 1.372 + 1.373 + const int flags_; 1.374 + const char* const infinity_symbol_; 1.375 + const char* const nan_symbol_; 1.376 + const char exponent_character_; 1.377 + const int decimal_in_shortest_low_; 1.378 + const int decimal_in_shortest_high_; 1.379 + const int max_leading_padding_zeroes_in_precision_mode_; 1.380 + const int max_trailing_padding_zeroes_in_precision_mode_; 1.381 + 1.382 + DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); 1.383 +}; 1.384 + 1.385 + 1.386 +class StringToDoubleConverter { 1.387 + public: 1.388 + // Enumeration for allowing octals and ignoring junk when converting 1.389 + // strings to numbers. 1.390 + enum Flags { 1.391 + NO_FLAGS = 0, 1.392 + ALLOW_HEX = 1, 1.393 + ALLOW_OCTALS = 2, 1.394 + ALLOW_TRAILING_JUNK = 4, 1.395 + ALLOW_LEADING_SPACES = 8, 1.396 + ALLOW_TRAILING_SPACES = 16, 1.397 + ALLOW_SPACES_AFTER_SIGN = 32 1.398 + }; 1.399 + 1.400 + // Flags should be a bit-or combination of the possible Flags-enum. 1.401 + // - NO_FLAGS: no special flags. 1.402 + // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers. 1.403 + // Ex: StringToDouble("0x1234") -> 4660.0 1.404 + // In StringToDouble("0x1234.56") the characters ".56" are trailing 1.405 + // junk. The result of the call is hence dependent on 1.406 + // the ALLOW_TRAILING_JUNK flag and/or the junk value. 1.407 + // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK, 1.408 + // the string will not be parsed as "0" followed by junk. 1.409 + // 1.410 + // - ALLOW_OCTALS: recognizes the prefix "0" for octals: 1.411 + // If a sequence of octal digits starts with '0', then the number is 1.412 + // read as octal integer. Octal numbers may only be integers. 1.413 + // Ex: StringToDouble("01234") -> 668.0 1.414 + // StringToDouble("012349") -> 12349.0 // Not a sequence of octal 1.415 + // // digits. 1.416 + // In StringToDouble("01234.56") the characters ".56" are trailing 1.417 + // junk. The result of the call is hence dependent on 1.418 + // the ALLOW_TRAILING_JUNK flag and/or the junk value. 1.419 + // In StringToDouble("01234e56") the characters "e56" are trailing 1.420 + // junk, too. 1.421 + // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of 1.422 + // a double literal. 1.423 + // - ALLOW_LEADING_SPACES: skip over leading spaces. 1.424 + // - ALLOW_TRAILING_SPACES: ignore trailing spaces. 1.425 + // - ALLOW_SPACES_AFTER_SIGN: ignore spaces after the sign. 1.426 + // Ex: StringToDouble("- 123.2") -> -123.2. 1.427 + // StringToDouble("+ 123.2") -> 123.2 1.428 + // 1.429 + // empty_string_value is returned when an empty string is given as input. 1.430 + // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string 1.431 + // containing only spaces is converted to the 'empty_string_value', too. 1.432 + // 1.433 + // junk_string_value is returned when 1.434 + // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not 1.435 + // part of a double-literal) is found. 1.436 + // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a 1.437 + // double literal. 1.438 + // 1.439 + // infinity_symbol and nan_symbol are strings that are used to detect 1.440 + // inputs that represent infinity and NaN. They can be null, in which case 1.441 + // they are ignored. 1.442 + // The conversion routine first reads any possible signs. Then it compares the 1.443 + // following character of the input-string with the first character of 1.444 + // the infinity, and nan-symbol. If either matches, the function assumes, that 1.445 + // a match has been found, and expects the following input characters to match 1.446 + // the remaining characters of the special-value symbol. 1.447 + // This means that the following restrictions apply to special-value symbols: 1.448 + // - they must not start with signs ('+', or '-'), 1.449 + // - they must not have the same first character. 1.450 + // - they must not start with digits. 1.451 + // 1.452 + // Examples: 1.453 + // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK, 1.454 + // empty_string_value = 0.0, 1.455 + // junk_string_value = NaN, 1.456 + // infinity_symbol = "infinity", 1.457 + // nan_symbol = "nan": 1.458 + // StringToDouble("0x1234") -> 4660.0. 1.459 + // StringToDouble("0x1234K") -> 4660.0. 1.460 + // StringToDouble("") -> 0.0 // empty_string_value. 1.461 + // StringToDouble(" ") -> NaN // junk_string_value. 1.462 + // StringToDouble(" 1") -> NaN // junk_string_value. 1.463 + // StringToDouble("0x") -> NaN // junk_string_value. 1.464 + // StringToDouble("-123.45") -> -123.45. 1.465 + // StringToDouble("--123.45") -> NaN // junk_string_value. 1.466 + // StringToDouble("123e45") -> 123e45. 1.467 + // StringToDouble("123E45") -> 123e45. 1.468 + // StringToDouble("123e+45") -> 123e45. 1.469 + // StringToDouble("123E-45") -> 123e-45. 1.470 + // StringToDouble("123e") -> 123.0 // trailing junk ignored. 1.471 + // StringToDouble("123e-") -> 123.0 // trailing junk ignored. 1.472 + // StringToDouble("+NaN") -> NaN // NaN string literal. 1.473 + // StringToDouble("-infinity") -> -inf. // infinity literal. 1.474 + // StringToDouble("Infinity") -> NaN // junk_string_value. 1.475 + // 1.476 + // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES, 1.477 + // empty_string_value = 0.0, 1.478 + // junk_string_value = NaN, 1.479 + // infinity_symbol = NULL, 1.480 + // nan_symbol = NULL: 1.481 + // StringToDouble("0x1234") -> NaN // junk_string_value. 1.482 + // StringToDouble("01234") -> 668.0. 1.483 + // StringToDouble("") -> 0.0 // empty_string_value. 1.484 + // StringToDouble(" ") -> 0.0 // empty_string_value. 1.485 + // StringToDouble(" 1") -> 1.0 1.486 + // StringToDouble("0x") -> NaN // junk_string_value. 1.487 + // StringToDouble("0123e45") -> NaN // junk_string_value. 1.488 + // StringToDouble("01239E45") -> 1239e45. 1.489 + // StringToDouble("-infinity") -> NaN // junk_string_value. 1.490 + // StringToDouble("NaN") -> NaN // junk_string_value. 1.491 + StringToDoubleConverter(int flags, 1.492 + double empty_string_value, 1.493 + double junk_string_value, 1.494 + const char* infinity_symbol, 1.495 + const char* nan_symbol) 1.496 + : flags_(flags), 1.497 + empty_string_value_(empty_string_value), 1.498 + junk_string_value_(junk_string_value), 1.499 + infinity_symbol_(infinity_symbol), 1.500 + nan_symbol_(nan_symbol) { 1.501 + } 1.502 + 1.503 + // Performs the conversion. 1.504 + // The output parameter 'processed_characters_count' is set to the number 1.505 + // of characters that have been processed to read the number. 1.506 + // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included 1.507 + // in the 'processed_characters_count'. Trailing junk is never included. 1.508 + double StringToDouble(const char* buffer, 1.509 + int length, 1.510 + int* processed_characters_count) const { 1.511 + return StringToIeee(buffer, length, processed_characters_count, true); 1.512 + } 1.513 + 1.514 + // Same as StringToDouble but reads a float. 1.515 + // Note that this is not equivalent to static_cast<float>(StringToDouble(...)) 1.516 + // due to potential double-rounding. 1.517 + float StringToFloat(const char* buffer, 1.518 + int length, 1.519 + int* processed_characters_count) const { 1.520 + return static_cast<float>(StringToIeee(buffer, length, 1.521 + processed_characters_count, false)); 1.522 + } 1.523 + 1.524 + private: 1.525 + const int flags_; 1.526 + const double empty_string_value_; 1.527 + const double junk_string_value_; 1.528 + const char* const infinity_symbol_; 1.529 + const char* const nan_symbol_; 1.530 + 1.531 + double StringToIeee(const char* buffer, 1.532 + int length, 1.533 + int* processed_characters_count, 1.534 + bool read_as_double) const; 1.535 + 1.536 + DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter); 1.537 +}; 1.538 + 1.539 +} // namespace double_conversion 1.540 + 1.541 +#endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_