michael@0: // Copyright 2012 the V8 project authors. All rights reserved. michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following michael@0: // disclaimer in the documentation and/or other materials provided michael@0: // with the distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived michael@0: // from this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ michael@0: #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ michael@0: michael@0: #include "mozilla/Types.h" michael@0: #include "utils.h" michael@0: michael@0: namespace double_conversion { michael@0: michael@0: class DoubleToStringConverter { michael@0: public: michael@0: // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint michael@0: // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the michael@0: // function returns false. michael@0: static const int kMaxFixedDigitsBeforePoint = 60; michael@0: static const int kMaxFixedDigitsAfterPoint = 60; michael@0: michael@0: // When calling ToExponential with a requested_digits michael@0: // parameter > kMaxExponentialDigits then the function returns false. michael@0: static const int kMaxExponentialDigits = 120; michael@0: michael@0: // When calling ToPrecision with a requested_digits michael@0: // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits michael@0: // then the function returns false. michael@0: static const int kMinPrecisionDigits = 1; michael@0: static const int kMaxPrecisionDigits = 120; michael@0: michael@0: enum Flags { michael@0: NO_FLAGS = 0, michael@0: EMIT_POSITIVE_EXPONENT_SIGN = 1, michael@0: EMIT_TRAILING_DECIMAL_POINT = 2, michael@0: EMIT_TRAILING_ZERO_AFTER_POINT = 4, michael@0: UNIQUE_ZERO = 8 michael@0: }; michael@0: michael@0: // Flags should be a bit-or combination of the possible Flags-enum. michael@0: // - NO_FLAGS: no special flags. michael@0: // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent michael@0: // form, emits a '+' for positive exponents. Example: 1.2e+2. michael@0: // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is michael@0: // converted into decimal format then a trailing decimal point is appended. michael@0: // Example: 2345.0 is converted to "2345.". michael@0: // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point michael@0: // emits a trailing '0'-character. This flag requires the michael@0: // EXMIT_TRAILING_DECIMAL_POINT flag. michael@0: // Example: 2345.0 is converted to "2345.0". michael@0: // - UNIQUE_ZERO: "-0.0" is converted to "0.0". michael@0: // michael@0: // Infinity symbol and nan_symbol provide the string representation for these michael@0: // special values. If the string is NULL and the special value is encountered michael@0: // then the conversion functions return false. michael@0: // michael@0: // The exponent_character is used in exponential representations. It is michael@0: // usually 'e' or 'E'. michael@0: // michael@0: // When converting to the shortest representation the converter will michael@0: // represent input numbers in decimal format if they are in the interval michael@0: // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ michael@0: // (lower boundary included, greater boundary excluded). michael@0: // Example: with decimal_in_shortest_low = -6 and michael@0: // decimal_in_shortest_high = 21: michael@0: // ToShortest(0.000001) -> "0.000001" michael@0: // ToShortest(0.0000001) -> "1e-7" michael@0: // ToShortest(111111111111111111111.0) -> "111111111111111110000" michael@0: // ToShortest(100000000000000000000.0) -> "100000000000000000000" michael@0: // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" michael@0: // michael@0: // When converting to precision mode the converter may add michael@0: // max_leading_padding_zeroes before returning the number in exponential michael@0: // format. michael@0: // Example with max_leading_padding_zeroes_in_precision_mode = 6. michael@0: // ToPrecision(0.0000012345, 2) -> "0.0000012" michael@0: // ToPrecision(0.00000012345, 2) -> "1.2e-7" michael@0: // Similarily the converter may add up to michael@0: // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid michael@0: // returning an exponential representation. A zero added by the michael@0: // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. michael@0: // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: michael@0: // ToPrecision(230.0, 2) -> "230" michael@0: // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. michael@0: // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. michael@0: DoubleToStringConverter(int flags, michael@0: const char* infinity_symbol, michael@0: const char* nan_symbol, michael@0: char exponent_character, michael@0: int decimal_in_shortest_low, michael@0: int decimal_in_shortest_high, michael@0: int max_leading_padding_zeroes_in_precision_mode, michael@0: int max_trailing_padding_zeroes_in_precision_mode) michael@0: : flags_(flags), michael@0: infinity_symbol_(infinity_symbol), michael@0: nan_symbol_(nan_symbol), michael@0: exponent_character_(exponent_character), michael@0: decimal_in_shortest_low_(decimal_in_shortest_low), michael@0: decimal_in_shortest_high_(decimal_in_shortest_high), michael@0: max_leading_padding_zeroes_in_precision_mode_( michael@0: max_leading_padding_zeroes_in_precision_mode), michael@0: max_trailing_padding_zeroes_in_precision_mode_( michael@0: max_trailing_padding_zeroes_in_precision_mode) { michael@0: // When 'trailing zero after the point' is set, then 'trailing point' michael@0: // must be set too. michael@0: ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || michael@0: !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); michael@0: } michael@0: michael@0: // Returns a converter following the EcmaScript specification. michael@0: static MFBT_API const DoubleToStringConverter& EcmaScriptConverter(); michael@0: michael@0: // Computes the shortest string of digits that correctly represent the input michael@0: // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high michael@0: // (see constructor) it then either returns a decimal representation, or an michael@0: // exponential representation. michael@0: // Example with decimal_in_shortest_low = -6, michael@0: // decimal_in_shortest_high = 21, michael@0: // EMIT_POSITIVE_EXPONENT_SIGN activated, and michael@0: // EMIT_TRAILING_DECIMAL_POINT deactived: michael@0: // ToShortest(0.000001) -> "0.000001" michael@0: // ToShortest(0.0000001) -> "1e-7" michael@0: // ToShortest(111111111111111111111.0) -> "111111111111111110000" michael@0: // ToShortest(100000000000000000000.0) -> "100000000000000000000" michael@0: // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" michael@0: // michael@0: // Note: the conversion may round the output if the returned string michael@0: // is accurate enough to uniquely identify the input-number. michael@0: // For example the most precise representation of the double 9e59 equals michael@0: // "899999999999999918767229449717619953810131273674690656206848", but michael@0: // the converter will return the shorter (but still correct) "9e59". michael@0: // michael@0: // Returns true if the conversion succeeds. The conversion always succeeds michael@0: // except when the input value is special and no infinity_symbol or michael@0: // nan_symbol has been given to the constructor. michael@0: bool ToShortest(double value, StringBuilder* result_builder) const { michael@0: return ToShortestIeeeNumber(value, result_builder, SHORTEST); michael@0: } michael@0: michael@0: // Same as ToShortest, but for single-precision floats. michael@0: bool ToShortestSingle(float value, StringBuilder* result_builder) const { michael@0: return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); michael@0: } michael@0: michael@0: michael@0: // Computes a decimal representation with a fixed number of digits after the michael@0: // decimal point. The last emitted digit is rounded. michael@0: // michael@0: // Examples: michael@0: // ToFixed(3.12, 1) -> "3.1" michael@0: // ToFixed(3.1415, 3) -> "3.142" michael@0: // ToFixed(1234.56789, 4) -> "1234.5679" michael@0: // ToFixed(1.23, 5) -> "1.23000" michael@0: // ToFixed(0.1, 4) -> "0.1000" michael@0: // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" michael@0: // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" michael@0: // ToFixed(0.1, 17) -> "0.10000000000000001" michael@0: // michael@0: // If requested_digits equals 0, then the tail of the result depends on michael@0: // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. michael@0: // Examples, for requested_digits == 0, michael@0: // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be michael@0: // - false and false: then 123.45 -> 123 michael@0: // 0.678 -> 1 michael@0: // - true and false: then 123.45 -> 123. michael@0: // 0.678 -> 1. michael@0: // - true and true: then 123.45 -> 123.0 michael@0: // 0.678 -> 1.0 michael@0: // michael@0: // Returns true if the conversion succeeds. The conversion always succeeds michael@0: // except for the following cases: michael@0: // - the input value is special and no infinity_symbol or nan_symbol has michael@0: // been provided to the constructor, michael@0: // - 'value' > 10^kMaxFixedDigitsBeforePoint, or michael@0: // - 'requested_digits' > kMaxFixedDigitsAfterPoint. michael@0: // The last two conditions imply that the result will never contain more than michael@0: // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters michael@0: // (one additional character for the sign, and one for the decimal point). michael@0: MFBT_API bool ToFixed(double value, michael@0: int requested_digits, michael@0: StringBuilder* result_builder) const; michael@0: michael@0: // Computes a representation in exponential format with requested_digits michael@0: // after the decimal point. The last emitted digit is rounded. michael@0: // If requested_digits equals -1, then the shortest exponential representation michael@0: // is computed. michael@0: // michael@0: // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and michael@0: // exponent_character set to 'e'. michael@0: // ToExponential(3.12, 1) -> "3.1e0" michael@0: // ToExponential(5.0, 3) -> "5.000e0" michael@0: // ToExponential(0.001, 2) -> "1.00e-3" michael@0: // ToExponential(3.1415, -1) -> "3.1415e0" michael@0: // ToExponential(3.1415, 4) -> "3.1415e0" michael@0: // ToExponential(3.1415, 3) -> "3.142e0" michael@0: // ToExponential(123456789000000, 3) -> "1.235e14" michael@0: // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" michael@0: // ToExponential(1000000000000000019884624838656.0, 32) -> michael@0: // "1.00000000000000001988462483865600e30" michael@0: // ToExponential(1234, 0) -> "1e3" michael@0: // michael@0: // Returns true if the conversion succeeds. The conversion always succeeds michael@0: // except for the following cases: michael@0: // - the input value is special and no infinity_symbol or nan_symbol has michael@0: // been provided to the constructor, michael@0: // - 'requested_digits' > kMaxExponentialDigits. michael@0: // The last condition implies that the result will never contain more than michael@0: // kMaxExponentialDigits + 8 characters (the sign, the digit before the michael@0: // decimal point, the decimal point, the exponent character, the michael@0: // exponent's sign, and at most 3 exponent digits). michael@0: MFBT_API bool ToExponential(double value, michael@0: int requested_digits, michael@0: StringBuilder* result_builder) const; michael@0: michael@0: // Computes 'precision' leading digits of the given 'value' and returns them michael@0: // either in exponential or decimal format, depending on michael@0: // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the michael@0: // constructor). michael@0: // The last computed digit is rounded. michael@0: // michael@0: // Example with max_leading_padding_zeroes_in_precision_mode = 6. michael@0: // ToPrecision(0.0000012345, 2) -> "0.0000012" michael@0: // ToPrecision(0.00000012345, 2) -> "1.2e-7" michael@0: // Similarily the converter may add up to michael@0: // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid michael@0: // returning an exponential representation. A zero added by the michael@0: // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. michael@0: // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: michael@0: // ToPrecision(230.0, 2) -> "230" michael@0: // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. michael@0: // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. michael@0: // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no michael@0: // EMIT_TRAILING_ZERO_AFTER_POINT: michael@0: // ToPrecision(123450.0, 6) -> "123450" michael@0: // ToPrecision(123450.0, 5) -> "123450" michael@0: // ToPrecision(123450.0, 4) -> "123500" michael@0: // ToPrecision(123450.0, 3) -> "123000" michael@0: // ToPrecision(123450.0, 2) -> "1.2e5" michael@0: // michael@0: // Returns true if the conversion succeeds. The conversion always succeeds michael@0: // except for the following cases: michael@0: // - the input value is special and no infinity_symbol or nan_symbol has michael@0: // been provided to the constructor, michael@0: // - precision < kMinPericisionDigits michael@0: // - precision > kMaxPrecisionDigits michael@0: // The last condition implies that the result will never contain more than michael@0: // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the michael@0: // exponent character, the exponent's sign, and at most 3 exponent digits). michael@0: MFBT_API bool ToPrecision(double value, michael@0: int precision, michael@0: bool* used_exponential_notation, michael@0: StringBuilder* result_builder) const; michael@0: michael@0: enum DtoaMode { michael@0: // Produce the shortest correct representation. michael@0: // For example the output of 0.299999999999999988897 is (the less accurate michael@0: // but correct) 0.3. michael@0: SHORTEST, michael@0: // Same as SHORTEST, but for single-precision floats. michael@0: SHORTEST_SINGLE, michael@0: // Produce a fixed number of digits after the decimal point. michael@0: // For instance fixed(0.1, 4) becomes 0.1000 michael@0: // If the input number is big, the output will be big. michael@0: FIXED, michael@0: // Fixed number of digits (independent of the decimal point). michael@0: PRECISION michael@0: }; michael@0: michael@0: // The maximal number of digits that are needed to emit a double in base 10. michael@0: // A higher precision can be achieved by using more digits, but the shortest michael@0: // accurate representation of any double will never use more digits than michael@0: // kBase10MaximalLength. michael@0: // Note that DoubleToAscii null-terminates its input. So the given buffer michael@0: // should be at least kBase10MaximalLength + 1 characters long. michael@0: static const MFBT_DATA int kBase10MaximalLength = 17; michael@0: michael@0: // Converts the given double 'v' to ascii. 'v' must not be NaN, +Infinity, or michael@0: // -Infinity. In SHORTEST_SINGLE-mode this restriction also applies to 'v' michael@0: // after it has been casted to a single-precision float. That is, in this michael@0: // mode static_cast(v) must not be NaN, +Infinity or -Infinity. michael@0: // michael@0: // The result should be interpreted as buffer * 10^(point-length). michael@0: // michael@0: // The output depends on the given mode: michael@0: // - SHORTEST: produce the least amount of digits for which the internal michael@0: // identity requirement is still satisfied. If the digits are printed michael@0: // (together with the correct exponent) then reading this number will give michael@0: // 'v' again. The buffer will choose the representation that is closest to michael@0: // 'v'. If there are two at the same distance, than the one farther away michael@0: // from 0 is chosen (halfway cases - ending with 5 - are rounded up). michael@0: // In this mode the 'requested_digits' parameter is ignored. michael@0: // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. michael@0: // - FIXED: produces digits necessary to print a given number with michael@0: // 'requested_digits' digits after the decimal point. The produced digits michael@0: // might be too short in which case the caller has to fill the remainder michael@0: // with '0's. michael@0: // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. michael@0: // Halfway cases are rounded towards +/-Infinity (away from 0). The call michael@0: // toFixed(0.15, 2) thus returns buffer="2", point=0. michael@0: // The returned buffer may contain digits that would be truncated from the michael@0: // shortest representation of the input. michael@0: // - PRECISION: produces 'requested_digits' where the first digit is not '0'. michael@0: // Even though the length of produced digits usually equals michael@0: // 'requested_digits', the function is allowed to return fewer digits, in michael@0: // which case the caller has to fill the missing digits with '0's. michael@0: // Halfway cases are again rounded away from 0. michael@0: // DoubleToAscii expects the given buffer to be big enough to hold all michael@0: // digits and a terminating null-character. In SHORTEST-mode it expects a michael@0: // buffer of at least kBase10MaximalLength + 1. In all other modes the michael@0: // requested_digits parameter and the padding-zeroes limit the size of the michael@0: // output. Don't forget the decimal point, the exponent character and the michael@0: // terminating null-character when computing the maximal output size. michael@0: // The given length is only used in debug mode to ensure the buffer is big michael@0: // enough. michael@0: static MFBT_API void DoubleToAscii(double v, michael@0: DtoaMode mode, michael@0: int requested_digits, michael@0: char* buffer, michael@0: int buffer_length, michael@0: bool* sign, michael@0: int* length, michael@0: int* point); michael@0: michael@0: private: michael@0: // Implementation for ToShortest and ToShortestSingle. michael@0: MFBT_API bool ToShortestIeeeNumber(double value, michael@0: StringBuilder* result_builder, michael@0: DtoaMode mode) const; michael@0: michael@0: // If the value is a special value (NaN or Infinity) constructs the michael@0: // corresponding string using the configured infinity/nan-symbol. michael@0: // If either of them is NULL or the value is not special then the michael@0: // function returns false. michael@0: MFBT_API bool HandleSpecialValues(double value, StringBuilder* result_builder) const; michael@0: // Constructs an exponential representation (i.e. 1.234e56). michael@0: // The given exponent assumes a decimal point after the first decimal digit. michael@0: MFBT_API void CreateExponentialRepresentation(const char* decimal_digits, michael@0: int length, michael@0: int exponent, michael@0: StringBuilder* result_builder) const; michael@0: // Creates a decimal representation (i.e 1234.5678). michael@0: MFBT_API void CreateDecimalRepresentation(const char* decimal_digits, michael@0: int length, michael@0: int decimal_point, michael@0: int digits_after_point, michael@0: StringBuilder* result_builder) const; michael@0: michael@0: const int flags_; michael@0: const char* const infinity_symbol_; michael@0: const char* const nan_symbol_; michael@0: const char exponent_character_; michael@0: const int decimal_in_shortest_low_; michael@0: const int decimal_in_shortest_high_; michael@0: const int max_leading_padding_zeroes_in_precision_mode_; michael@0: const int max_trailing_padding_zeroes_in_precision_mode_; michael@0: michael@0: DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); michael@0: }; michael@0: michael@0: michael@0: class StringToDoubleConverter { michael@0: public: michael@0: // Enumeration for allowing octals and ignoring junk when converting michael@0: // strings to numbers. michael@0: enum Flags { michael@0: NO_FLAGS = 0, michael@0: ALLOW_HEX = 1, michael@0: ALLOW_OCTALS = 2, michael@0: ALLOW_TRAILING_JUNK = 4, michael@0: ALLOW_LEADING_SPACES = 8, michael@0: ALLOW_TRAILING_SPACES = 16, michael@0: ALLOW_SPACES_AFTER_SIGN = 32 michael@0: }; michael@0: michael@0: // Flags should be a bit-or combination of the possible Flags-enum. michael@0: // - NO_FLAGS: no special flags. michael@0: // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers. michael@0: // Ex: StringToDouble("0x1234") -> 4660.0 michael@0: // In StringToDouble("0x1234.56") the characters ".56" are trailing michael@0: // junk. The result of the call is hence dependent on michael@0: // the ALLOW_TRAILING_JUNK flag and/or the junk value. michael@0: // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK, michael@0: // the string will not be parsed as "0" followed by junk. michael@0: // michael@0: // - ALLOW_OCTALS: recognizes the prefix "0" for octals: michael@0: // If a sequence of octal digits starts with '0', then the number is michael@0: // read as octal integer. Octal numbers may only be integers. michael@0: // Ex: StringToDouble("01234") -> 668.0 michael@0: // StringToDouble("012349") -> 12349.0 // Not a sequence of octal michael@0: // // digits. michael@0: // In StringToDouble("01234.56") the characters ".56" are trailing michael@0: // junk. The result of the call is hence dependent on michael@0: // the ALLOW_TRAILING_JUNK flag and/or the junk value. michael@0: // In StringToDouble("01234e56") the characters "e56" are trailing michael@0: // junk, too. michael@0: // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of michael@0: // a double literal. michael@0: // - ALLOW_LEADING_SPACES: skip over leading spaces. michael@0: // - ALLOW_TRAILING_SPACES: ignore trailing spaces. michael@0: // - ALLOW_SPACES_AFTER_SIGN: ignore spaces after the sign. michael@0: // Ex: StringToDouble("- 123.2") -> -123.2. michael@0: // StringToDouble("+ 123.2") -> 123.2 michael@0: // michael@0: // empty_string_value is returned when an empty string is given as input. michael@0: // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string michael@0: // containing only spaces is converted to the 'empty_string_value', too. michael@0: // michael@0: // junk_string_value is returned when michael@0: // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not michael@0: // part of a double-literal) is found. michael@0: // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a michael@0: // double literal. michael@0: // michael@0: // infinity_symbol and nan_symbol are strings that are used to detect michael@0: // inputs that represent infinity and NaN. They can be null, in which case michael@0: // they are ignored. michael@0: // The conversion routine first reads any possible signs. Then it compares the michael@0: // following character of the input-string with the first character of michael@0: // the infinity, and nan-symbol. If either matches, the function assumes, that michael@0: // a match has been found, and expects the following input characters to match michael@0: // the remaining characters of the special-value symbol. michael@0: // This means that the following restrictions apply to special-value symbols: michael@0: // - they must not start with signs ('+', or '-'), michael@0: // - they must not have the same first character. michael@0: // - they must not start with digits. michael@0: // michael@0: // Examples: michael@0: // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK, michael@0: // empty_string_value = 0.0, michael@0: // junk_string_value = NaN, michael@0: // infinity_symbol = "infinity", michael@0: // nan_symbol = "nan": michael@0: // StringToDouble("0x1234") -> 4660.0. michael@0: // StringToDouble("0x1234K") -> 4660.0. michael@0: // StringToDouble("") -> 0.0 // empty_string_value. michael@0: // StringToDouble(" ") -> NaN // junk_string_value. michael@0: // StringToDouble(" 1") -> NaN // junk_string_value. michael@0: // StringToDouble("0x") -> NaN // junk_string_value. michael@0: // StringToDouble("-123.45") -> -123.45. michael@0: // StringToDouble("--123.45") -> NaN // junk_string_value. michael@0: // StringToDouble("123e45") -> 123e45. michael@0: // StringToDouble("123E45") -> 123e45. michael@0: // StringToDouble("123e+45") -> 123e45. michael@0: // StringToDouble("123E-45") -> 123e-45. michael@0: // StringToDouble("123e") -> 123.0 // trailing junk ignored. michael@0: // StringToDouble("123e-") -> 123.0 // trailing junk ignored. michael@0: // StringToDouble("+NaN") -> NaN // NaN string literal. michael@0: // StringToDouble("-infinity") -> -inf. // infinity literal. michael@0: // StringToDouble("Infinity") -> NaN // junk_string_value. michael@0: // michael@0: // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES, michael@0: // empty_string_value = 0.0, michael@0: // junk_string_value = NaN, michael@0: // infinity_symbol = NULL, michael@0: // nan_symbol = NULL: michael@0: // StringToDouble("0x1234") -> NaN // junk_string_value. michael@0: // StringToDouble("01234") -> 668.0. michael@0: // StringToDouble("") -> 0.0 // empty_string_value. michael@0: // StringToDouble(" ") -> 0.0 // empty_string_value. michael@0: // StringToDouble(" 1") -> 1.0 michael@0: // StringToDouble("0x") -> NaN // junk_string_value. michael@0: // StringToDouble("0123e45") -> NaN // junk_string_value. michael@0: // StringToDouble("01239E45") -> 1239e45. michael@0: // StringToDouble("-infinity") -> NaN // junk_string_value. michael@0: // StringToDouble("NaN") -> NaN // junk_string_value. michael@0: StringToDoubleConverter(int flags, michael@0: double empty_string_value, michael@0: double junk_string_value, michael@0: const char* infinity_symbol, michael@0: const char* nan_symbol) michael@0: : flags_(flags), michael@0: empty_string_value_(empty_string_value), michael@0: junk_string_value_(junk_string_value), michael@0: infinity_symbol_(infinity_symbol), michael@0: nan_symbol_(nan_symbol) { michael@0: } michael@0: michael@0: // Performs the conversion. michael@0: // The output parameter 'processed_characters_count' is set to the number michael@0: // of characters that have been processed to read the number. michael@0: // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included michael@0: // in the 'processed_characters_count'. Trailing junk is never included. michael@0: double StringToDouble(const char* buffer, michael@0: int length, michael@0: int* processed_characters_count) const { michael@0: return StringToIeee(buffer, length, processed_characters_count, true); michael@0: } michael@0: michael@0: // Same as StringToDouble but reads a float. michael@0: // Note that this is not equivalent to static_cast(StringToDouble(...)) michael@0: // due to potential double-rounding. michael@0: float StringToFloat(const char* buffer, michael@0: int length, michael@0: int* processed_characters_count) const { michael@0: return static_cast(StringToIeee(buffer, length, michael@0: processed_characters_count, false)); michael@0: } michael@0: michael@0: private: michael@0: const int flags_; michael@0: const double empty_string_value_; michael@0: const double junk_string_value_; michael@0: const char* const infinity_symbol_; michael@0: const char* const nan_symbol_; michael@0: michael@0: double StringToIeee(const char* buffer, michael@0: int length, michael@0: int* processed_characters_count, michael@0: bool read_as_double) const; michael@0: michael@0: DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter); michael@0: }; michael@0: michael@0: } // namespace double_conversion michael@0: michael@0: #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_