Joshua
open source statistical hierarchical phrase-based machine translation system
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/kenlm/util/double-conversion/double-conversion.h
00001 // Copyright 2012 the V8 project authors. All rights reserved.
00002 // Redistribution and use in source and binary forms, with or without
00003 // modification, are permitted provided that the following conditions are
00004 // met:
00005 //
00006 //     * Redistributions of source code must retain the above copyright
00007 //       notice, this list of conditions and the following disclaimer.
00008 //     * Redistributions in binary form must reproduce the above
00009 //       copyright notice, this list of conditions and the following
00010 //       disclaimer in the documentation and/or other materials provided
00011 //       with the distribution.
00012 //     * Neither the name of Google Inc. nor the names of its
00013 //       contributors may be used to endorse or promote products derived
00014 //       from this software without specific prior written permission.
00015 //
00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 
00028 #ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
00029 #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
00030 
00031 #include "utils.h"
00032 
00033 namespace double_conversion {
00034 
00035 class DoubleToStringConverter {
00036  public:
00037   // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
00038   // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
00039   // function returns false.
00040   static const int kMaxFixedDigitsBeforePoint = 60;
00041   static const int kMaxFixedDigitsAfterPoint = 60;
00042 
00043   // When calling ToExponential with a requested_digits
00044   // parameter > kMaxExponentialDigits then the function returns false.
00045   static const int kMaxExponentialDigits = 120;
00046 
00047   // When calling ToPrecision with a requested_digits
00048   // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
00049   // then the function returns false.
00050   static const int kMinPrecisionDigits = 1;
00051   static const int kMaxPrecisionDigits = 120;
00052 
00053   enum Flags {
00054     NO_FLAGS = 0,
00055     EMIT_POSITIVE_EXPONENT_SIGN = 1,
00056     EMIT_TRAILING_DECIMAL_POINT = 2,
00057     EMIT_TRAILING_ZERO_AFTER_POINT = 4,
00058     UNIQUE_ZERO = 8
00059   };
00060 
00061   // Flags should be a bit-or combination of the possible Flags-enum.
00062   //  - NO_FLAGS: no special flags.
00063   //  - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
00064   //    form, emits a '+' for positive exponents. Example: 1.2e+2.
00065   //  - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
00066   //    converted into decimal format then a trailing decimal point is appended.
00067   //    Example: 2345.0 is converted to "2345.".
00068   //  - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
00069   //    emits a trailing '0'-character. This flag requires the
00070   //    EXMIT_TRAILING_DECIMAL_POINT flag.
00071   //    Example: 2345.0 is converted to "2345.0".
00072   //  - UNIQUE_ZERO: "-0.0" is converted to "0.0".
00073   //
00074   // Infinity symbol and nan_symbol provide the string representation for these
00075   // special values. If the string is NULL and the special value is encountered
00076   // then the conversion functions return false.
00077   //
00078   // The exponent_character is used in exponential representations. It is
00079   // usually 'e' or 'E'.
00080   //
00081   // When converting to the shortest representation the converter will
00082   // represent input numbers in decimal format if they are in the interval
00083   // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
00084   //    (lower boundary included, greater boundary excluded).
00085   // Example: with decimal_in_shortest_low = -6 and
00086   //               decimal_in_shortest_high = 21:
00087   //   ToShortest(0.000001)  -> "0.000001"
00088   //   ToShortest(0.0000001) -> "1e-7"
00089   //   ToShortest(111111111111111111111.0)  -> "111111111111111110000"
00090   //   ToShortest(100000000000000000000.0)  -> "100000000000000000000"
00091   //   ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
00092   //
00093   // When converting to precision mode the converter may add
00094   // max_leading_padding_zeroes before returning the number in exponential
00095   // format.
00096   // Example with max_leading_padding_zeroes_in_precision_mode = 6.
00097   //   ToPrecision(0.0000012345, 2) -> "0.0000012"
00098   //   ToPrecision(0.00000012345, 2) -> "1.2e-7"
00099   // Similarily the converter may add up to
00100   // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
00101   // returning an exponential representation. A zero added by the
00102   // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
00103   // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
00104   //   ToPrecision(230.0, 2) -> "230"
00105   //   ToPrecision(230.0, 2) -> "230."  with EMIT_TRAILING_DECIMAL_POINT.
00106   //   ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
00107   DoubleToStringConverter(int flags,
00108                           const char* infinity_symbol,
00109                           const char* nan_symbol,
00110                           char exponent_character,
00111                           int decimal_in_shortest_low,
00112                           int decimal_in_shortest_high,
00113                           int max_leading_padding_zeroes_in_precision_mode,
00114                           int max_trailing_padding_zeroes_in_precision_mode)
00115       : flags_(flags),
00116         infinity_symbol_(infinity_symbol),
00117         nan_symbol_(nan_symbol),
00118         exponent_character_(exponent_character),
00119         decimal_in_shortest_low_(decimal_in_shortest_low),
00120         decimal_in_shortest_high_(decimal_in_shortest_high),
00121         max_leading_padding_zeroes_in_precision_mode_(
00122             max_leading_padding_zeroes_in_precision_mode),
00123         max_trailing_padding_zeroes_in_precision_mode_(
00124             max_trailing_padding_zeroes_in_precision_mode) {
00125     // When 'trailing zero after the point' is set, then 'trailing point'
00126     // must be set too.
00127     ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
00128         !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
00129   }
00130 
00131   // Returns a converter following the EcmaScript specification.
00132   static const DoubleToStringConverter& EcmaScriptConverter();
00133 
00134   // Computes the shortest string of digits that correctly represent the input
00135   // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
00136   // (see constructor) it then either returns a decimal representation, or an
00137   // exponential representation.
00138   // Example with decimal_in_shortest_low = -6,
00139   //              decimal_in_shortest_high = 21,
00140   //              EMIT_POSITIVE_EXPONENT_SIGN activated, and
00141   //              EMIT_TRAILING_DECIMAL_POINT deactived:
00142   //   ToShortest(0.000001)  -> "0.000001"
00143   //   ToShortest(0.0000001) -> "1e-7"
00144   //   ToShortest(111111111111111111111.0)  -> "111111111111111110000"
00145   //   ToShortest(100000000000000000000.0)  -> "100000000000000000000"
00146   //   ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
00147   //
00148   // Note: the conversion may round the output if the returned string
00149   // is accurate enough to uniquely identify the input-number.
00150   // For example the most precise representation of the double 9e59 equals
00151   // "899999999999999918767229449717619953810131273674690656206848", but
00152   // the converter will return the shorter (but still correct) "9e59".
00153   //
00154   // Returns true if the conversion succeeds. The conversion always succeeds
00155   // except when the input value is special and no infinity_symbol or
00156   // nan_symbol has been given to the constructor.
00157   bool ToShortest(double value, StringBuilder* result_builder) const {
00158     return ToShortestIeeeNumber(value, result_builder, SHORTEST);
00159   }
00160 
00161   // Same as ToShortest, but for single-precision floats.
00162   bool ToShortestSingle(float value, StringBuilder* result_builder) const {
00163     return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
00164   }
00165 
00166 
00167   // Computes a decimal representation with a fixed number of digits after the
00168   // decimal point. The last emitted digit is rounded.
00169   //
00170   // Examples:
00171   //   ToFixed(3.12, 1) -> "3.1"
00172   //   ToFixed(3.1415, 3) -> "3.142"
00173   //   ToFixed(1234.56789, 4) -> "1234.5679"
00174   //   ToFixed(1.23, 5) -> "1.23000"
00175   //   ToFixed(0.1, 4) -> "0.1000"
00176   //   ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
00177   //   ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
00178   //   ToFixed(0.1, 17) -> "0.10000000000000001"
00179   //
00180   // If requested_digits equals 0, then the tail of the result depends on
00181   // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
00182   // Examples, for requested_digits == 0,
00183   //   let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
00184   //    - false and false: then 123.45 -> 123
00185   //                             0.678 -> 1
00186   //    - true and false: then 123.45 -> 123.
00187   //                            0.678 -> 1.
00188   //    - true and true: then 123.45 -> 123.0
00189   //                           0.678 -> 1.0
00190   //
00191   // Returns true if the conversion succeeds. The conversion always succeeds
00192   // except for the following cases:
00193   //   - the input value is special and no infinity_symbol or nan_symbol has
00194   //     been provided to the constructor,
00195   //   - 'value' > 10^kMaxFixedDigitsBeforePoint, or
00196   //   - 'requested_digits' > kMaxFixedDigitsAfterPoint.
00197   // The last two conditions imply that the result will never contain more than
00198   // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
00199   // (one additional character for the sign, and one for the decimal point).
00200   bool ToFixed(double value,
00201                int requested_digits,
00202                StringBuilder* result_builder) const;
00203 
00204   // Computes a representation in exponential format with requested_digits
00205   // after the decimal point. The last emitted digit is rounded.
00206   // If requested_digits equals -1, then the shortest exponential representation
00207   // is computed.
00208   //
00209   // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
00210   //               exponent_character set to 'e'.
00211   //   ToExponential(3.12, 1) -> "3.1e0"
00212   //   ToExponential(5.0, 3) -> "5.000e0"
00213   //   ToExponential(0.001, 2) -> "1.00e-3"
00214   //   ToExponential(3.1415, -1) -> "3.1415e0"
00215   //   ToExponential(3.1415, 4) -> "3.1415e0"
00216   //   ToExponential(3.1415, 3) -> "3.142e0"
00217   //   ToExponential(123456789000000, 3) -> "1.235e14"
00218   //   ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
00219   //   ToExponential(1000000000000000019884624838656.0, 32) ->
00220   //                     "1.00000000000000001988462483865600e30"
00221   //   ToExponential(1234, 0) -> "1e3"
00222   //
00223   // Returns true if the conversion succeeds. The conversion always succeeds
00224   // except for the following cases:
00225   //   - the input value is special and no infinity_symbol or nan_symbol has
00226   //     been provided to the constructor,
00227   //   - 'requested_digits' > kMaxExponentialDigits.
00228   // The last condition implies that the result will never contain more than
00229   // kMaxExponentialDigits + 8 characters (the sign, the digit before the
00230   // decimal point, the decimal point, the exponent character, the
00231   // exponent's sign, and at most 3 exponent digits).
00232   bool ToExponential(double value,
00233                      int requested_digits,
00234                      StringBuilder* result_builder) const;
00235 
00236   // Computes 'precision' leading digits of the given 'value' and returns them
00237   // either in exponential or decimal format, depending on
00238   // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
00239   // constructor).
00240   // The last computed digit is rounded.
00241   //
00242   // Example with max_leading_padding_zeroes_in_precision_mode = 6.
00243   //   ToPrecision(0.0000012345, 2) -> "0.0000012"
00244   //   ToPrecision(0.00000012345, 2) -> "1.2e-7"
00245   // Similarily the converter may add up to
00246   // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
00247   // returning an exponential representation. A zero added by the
00248   // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
00249   // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
00250   //   ToPrecision(230.0, 2) -> "230"
00251   //   ToPrecision(230.0, 2) -> "230."  with EMIT_TRAILING_DECIMAL_POINT.
00252   //   ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
00253   // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
00254   //    EMIT_TRAILING_ZERO_AFTER_POINT:
00255   //   ToPrecision(123450.0, 6) -> "123450"
00256   //   ToPrecision(123450.0, 5) -> "123450"
00257   //   ToPrecision(123450.0, 4) -> "123500"
00258   //   ToPrecision(123450.0, 3) -> "123000"
00259   //   ToPrecision(123450.0, 2) -> "1.2e5"
00260   //
00261   // Returns true if the conversion succeeds. The conversion always succeeds
00262   // except for the following cases:
00263   //   - the input value is special and no infinity_symbol or nan_symbol has
00264   //     been provided to the constructor,
00265   //   - precision < kMinPericisionDigits
00266   //   - precision > kMaxPrecisionDigits
00267   // The last condition implies that the result will never contain more than
00268   // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
00269   // exponent character, the exponent's sign, and at most 3 exponent digits).
00270   bool ToPrecision(double value,
00271                    int precision,
00272                    StringBuilder* result_builder) const;
00273 
00274   enum DtoaMode {
00275     // Produce the shortest correct representation.
00276     // For example the output of 0.299999999999999988897 is (the less accurate
00277     // but correct) 0.3.
00278     SHORTEST,
00279     // Same as SHORTEST, but for single-precision floats.
00280     SHORTEST_SINGLE,
00281     // Produce a fixed number of digits after the decimal point.
00282     // For instance fixed(0.1, 4) becomes 0.1000
00283     // If the input number is big, the output will be big.
00284     FIXED,
00285     // Fixed number of digits (independent of the decimal point).
00286     PRECISION
00287   };
00288 
00289   // The maximal number of digits that are needed to emit a double in base 10.
00290   // A higher precision can be achieved by using more digits, but the shortest
00291   // accurate representation of any double will never use more digits than
00292   // kBase10MaximalLength.
00293   // Note that DoubleToAscii null-terminates its input. So the given buffer
00294   // should be at least kBase10MaximalLength + 1 characters long.
00295   static const int kBase10MaximalLength = 17;
00296 
00297   // Converts the given double 'v' to ascii. 'v' must not be NaN, +Infinity, or
00298   // -Infinity. In SHORTEST_SINGLE-mode this restriction also applies to 'v'
00299   // after it has been casted to a single-precision float. That is, in this
00300   // mode static_cast<float>(v) must not be NaN, +Infinity or -Infinity.
00301   //
00302   // The result should be interpreted as buffer * 10^(point-length).
00303   //
00304   // The output depends on the given mode:
00305   //  - SHORTEST: produce the least amount of digits for which the internal
00306   //   identity requirement is still satisfied. If the digits are printed
00307   //   (together with the correct exponent) then reading this number will give
00308   //   'v' again. The buffer will choose the representation that is closest to
00309   //   'v'. If there are two at the same distance, than the one farther away
00310   //   from 0 is chosen (halfway cases - ending with 5 - are rounded up).
00311   //   In this mode the 'requested_digits' parameter is ignored.
00312   //  - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
00313   //  - FIXED: produces digits necessary to print a given number with
00314   //   'requested_digits' digits after the decimal point. The produced digits
00315   //   might be too short in which case the caller has to fill the remainder
00316   //   with '0's.
00317   //   Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
00318   //   Halfway cases are rounded towards +/-Infinity (away from 0). The call
00319   //   toFixed(0.15, 2) thus returns buffer="2", point=0.
00320   //   The returned buffer may contain digits that would be truncated from the
00321   //   shortest representation of the input.
00322   //  - PRECISION: produces 'requested_digits' where the first digit is not '0'.
00323   //   Even though the length of produced digits usually equals
00324   //   'requested_digits', the function is allowed to return fewer digits, in
00325   //   which case the caller has to fill the missing digits with '0's.
00326   //   Halfway cases are again rounded away from 0.
00327   // DoubleToAscii expects the given buffer to be big enough to hold all
00328   // digits and a terminating null-character. In SHORTEST-mode it expects a
00329   // buffer of at least kBase10MaximalLength + 1. In all other modes the
00330   // requested_digits parameter and the padding-zeroes limit the size of the
00331   // output. Don't forget the decimal point, the exponent character and the
00332   // terminating null-character when computing the maximal output size.
00333   // The given length is only used in debug mode to ensure the buffer is big
00334   // enough.
00335   static void DoubleToAscii(double v,
00336                             DtoaMode mode,
00337                             int requested_digits,
00338                             char* buffer,
00339                             int buffer_length,
00340                             bool* sign,
00341                             int* length,
00342                             int* point);
00343 
00344  private:
00345   // Implementation for ToShortest and ToShortestSingle.
00346   bool ToShortestIeeeNumber(double value,
00347                             StringBuilder* result_builder,
00348                             DtoaMode mode) const;
00349 
00350   // If the value is a special value (NaN or Infinity) constructs the
00351   // corresponding string using the configured infinity/nan-symbol.
00352   // If either of them is NULL or the value is not special then the
00353   // function returns false.
00354   bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
00355   // Constructs an exponential representation (i.e. 1.234e56).
00356   // The given exponent assumes a decimal point after the first decimal digit.
00357   void CreateExponentialRepresentation(const char* decimal_digits,
00358                                        int length,
00359                                        int exponent,
00360                                        StringBuilder* result_builder) const;
00361   // Creates a decimal representation (i.e 1234.5678).
00362   void CreateDecimalRepresentation(const char* decimal_digits,
00363                                    int length,
00364                                    int decimal_point,
00365                                    int digits_after_point,
00366                                    StringBuilder* result_builder) const;
00367 
00368   const int flags_;
00369   const char* const infinity_symbol_;
00370   const char* const nan_symbol_;
00371   const char exponent_character_;
00372   const int decimal_in_shortest_low_;
00373   const int decimal_in_shortest_high_;
00374   const int max_leading_padding_zeroes_in_precision_mode_;
00375   const int max_trailing_padding_zeroes_in_precision_mode_;
00376 
00377   DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
00378 };
00379 
00380 
00381 class StringToDoubleConverter {
00382  public:
00383   // Enumeration for allowing octals and ignoring junk when converting
00384   // strings to numbers.
00385   enum Flags {
00386     NO_FLAGS = 0,
00387     ALLOW_HEX = 1,
00388     ALLOW_OCTALS = 2,
00389     ALLOW_TRAILING_JUNK = 4,
00390     ALLOW_LEADING_SPACES = 8,
00391     ALLOW_TRAILING_SPACES = 16,
00392     ALLOW_SPACES_AFTER_SIGN = 32
00393   };
00394 
00395   // Flags should be a bit-or combination of the possible Flags-enum.
00396   //  - NO_FLAGS: no special flags.
00397   //  - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
00398   //      Ex: StringToDouble("0x1234") -> 4660.0
00399   //          In StringToDouble("0x1234.56") the characters ".56" are trailing
00400   //          junk. The result of the call is hence dependent on
00401   //          the ALLOW_TRAILING_JUNK flag and/or the junk value.
00402   //      With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
00403   //      the string will not be parsed as "0" followed by junk.
00404   //
00405   //  - ALLOW_OCTALS: recognizes the prefix "0" for octals:
00406   //      If a sequence of octal digits starts with '0', then the number is
00407   //      read as octal integer. Octal numbers may only be integers.
00408   //      Ex: StringToDouble("01234") -> 668.0
00409   //          StringToDouble("012349") -> 12349.0  // Not a sequence of octal
00410   //                                               // digits.
00411   //          In StringToDouble("01234.56") the characters ".56" are trailing
00412   //          junk. The result of the call is hence dependent on
00413   //          the ALLOW_TRAILING_JUNK flag and/or the junk value.
00414   //          In StringToDouble("01234e56") the characters "e56" are trailing
00415   //          junk, too.
00416   //  - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
00417   //      a double literal.
00418   //  - ALLOW_LEADING_SPACES: skip over leading spaces.
00419   //  - ALLOW_TRAILING_SPACES: ignore trailing spaces.
00420   //  - ALLOW_SPACES_AFTER_SIGN: ignore spaces after the sign.
00421   //       Ex: StringToDouble("-   123.2") -> -123.2.
00422   //           StringToDouble("+   123.2") -> 123.2
00423   //
00424   // empty_string_value is returned when an empty string is given as input.
00425   // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
00426   // containing only spaces is converted to the 'empty_string_value', too.
00427   //
00428   // junk_string_value is returned when
00429   //  a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
00430   //     part of a double-literal) is found.
00431   //  b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
00432   //     double literal.
00433   //
00434   // infinity_symbol and nan_symbol are strings that are used to detect
00435   // inputs that represent infinity and NaN. They can be null, in which case
00436   // they are ignored.
00437   // The conversion routine first reads any possible signs. Then it compares the
00438   // following character of the input-string with the first character of
00439   // the infinity, and nan-symbol. If either matches, the function assumes, that
00440   // a match has been found, and expects the following input characters to match
00441   // the remaining characters of the special-value symbol.
00442   // This means that the following restrictions apply to special-value symbols:
00443   //  - they must not start with signs ('+', or '-'),
00444   //  - they must not have the same first character.
00445   //  - they must not start with digits.
00446   //
00447   // Examples:
00448   //  flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
00449   //  empty_string_value = 0.0,
00450   //  junk_string_value = NaN,
00451   //  infinity_symbol = "infinity",
00452   //  nan_symbol = "nan":
00453   //    StringToDouble("0x1234") -> 4660.0.
00454   //    StringToDouble("0x1234K") -> 4660.0.
00455   //    StringToDouble("") -> 0.0  // empty_string_value.
00456   //    StringToDouble(" ") -> NaN  // junk_string_value.
00457   //    StringToDouble(" 1") -> NaN  // junk_string_value.
00458   //    StringToDouble("0x") -> NaN  // junk_string_value.
00459   //    StringToDouble("-123.45") -> -123.45.
00460   //    StringToDouble("--123.45") -> NaN  // junk_string_value.
00461   //    StringToDouble("123e45") -> 123e45.
00462   //    StringToDouble("123E45") -> 123e45.
00463   //    StringToDouble("123e+45") -> 123e45.
00464   //    StringToDouble("123E-45") -> 123e-45.
00465   //    StringToDouble("123e") -> 123.0  // trailing junk ignored.
00466   //    StringToDouble("123e-") -> 123.0  // trailing junk ignored.
00467   //    StringToDouble("+NaN") -> NaN  // NaN string literal.
00468   //    StringToDouble("-infinity") -> -inf.  // infinity literal.
00469   //    StringToDouble("Infinity") -> NaN  // junk_string_value.
00470   //
00471   //  flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
00472   //  empty_string_value = 0.0,
00473   //  junk_string_value = NaN,
00474   //  infinity_symbol = NULL,
00475   //  nan_symbol = NULL:
00476   //    StringToDouble("0x1234") -> NaN  // junk_string_value.
00477   //    StringToDouble("01234") -> 668.0.
00478   //    StringToDouble("") -> 0.0  // empty_string_value.
00479   //    StringToDouble(" ") -> 0.0  // empty_string_value.
00480   //    StringToDouble(" 1") -> 1.0
00481   //    StringToDouble("0x") -> NaN  // junk_string_value.
00482   //    StringToDouble("0123e45") -> NaN  // junk_string_value.
00483   //    StringToDouble("01239E45") -> 1239e45.
00484   //    StringToDouble("-infinity") -> NaN  // junk_string_value.
00485   //    StringToDouble("NaN") -> NaN  // junk_string_value.
00486   StringToDoubleConverter(int flags,
00487                           double empty_string_value,
00488                           double junk_string_value,
00489                           const char* infinity_symbol,
00490                           const char* nan_symbol)
00491       : flags_(flags),
00492         empty_string_value_(empty_string_value),
00493         junk_string_value_(junk_string_value),
00494         infinity_symbol_(infinity_symbol),
00495         nan_symbol_(nan_symbol) {
00496   }
00497 
00498   // Performs the conversion.
00499   // The output parameter 'processed_characters_count' is set to the number
00500   // of characters that have been processed to read the number.
00501   // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
00502   // in the 'processed_characters_count'. Trailing junk is never included.
00503   double StringToDouble(const char* buffer,
00504                         int length,
00505                         int* processed_characters_count) const {
00506     return StringToIeee(buffer, length, processed_characters_count, true);
00507   }
00508 
00509   // Same as StringToDouble but reads a float.
00510   // Note that this is not equivalent to static_cast<float>(StringToDouble(...))
00511   // due to potential double-rounding.
00512   float StringToFloat(const char* buffer,
00513                       int length,
00514                       int* processed_characters_count) const {
00515     return static_cast<float>(StringToIeee(buffer, length,
00516                                            processed_characters_count, false));
00517   }
00518 
00519  private:
00520   const int flags_;
00521   const double empty_string_value_;
00522   const double junk_string_value_;
00523   const char* const infinity_symbol_;
00524   const char* const nan_symbol_;
00525 
00526   double StringToIeee(const char* buffer,
00527                       int length,
00528                       int* processed_characters_count,
00529                       bool read_as_double) const;
00530 
00531   DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
00532 };
00533 
00534 }  // namespace double_conversion
00535 
00536 #endif  // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_