You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

543 lines
26 KiB

  1. // Copyright 2012 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
  28. #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
  29. #include "double-conversion/utils.h"
  30. namespace double_conversion {
  31. class DoubleToStringConverter {
  32. public:
  33. // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
  34. // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
  35. // function returns false.
  36. static const int kMaxFixedDigitsBeforePoint = 60;
  37. static const int kMaxFixedDigitsAfterPoint = 60;
  38. // When calling ToExponential with a requested_digits
  39. // parameter > kMaxExponentialDigits then the function returns false.
  40. static const int kMaxExponentialDigits = 120;
  41. // When calling ToPrecision with a requested_digits
  42. // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
  43. // then the function returns false.
  44. static const int kMinPrecisionDigits = 1;
  45. static const int kMaxPrecisionDigits = 120;
  46. enum Flags {
  47. NO_FLAGS = 0,
  48. EMIT_POSITIVE_EXPONENT_SIGN = 1,
  49. EMIT_TRAILING_DECIMAL_POINT = 2,
  50. EMIT_TRAILING_ZERO_AFTER_POINT = 4,
  51. UNIQUE_ZERO = 8
  52. };
  53. // Flags should be a bit-or combination of the possible Flags-enum.
  54. // - NO_FLAGS: no special flags.
  55. // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
  56. // form, emits a '+' for positive exponents. Example: 1.2e+2.
  57. // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
  58. // converted into decimal format then a trailing decimal point is appended.
  59. // Example: 2345.0 is converted to "2345.".
  60. // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
  61. // emits a trailing '0'-character. This flag requires the
  62. // EXMIT_TRAILING_DECIMAL_POINT flag.
  63. // Example: 2345.0 is converted to "2345.0".
  64. // - UNIQUE_ZERO: "-0.0" is converted to "0.0".
  65. //
  66. // Infinity symbol and nan_symbol provide the string representation for these
  67. // special values. If the string is NULL and the special value is encountered
  68. // then the conversion functions return false.
  69. //
  70. // The exponent_character is used in exponential representations. It is
  71. // usually 'e' or 'E'.
  72. //
  73. // When converting to the shortest representation the converter will
  74. // represent input numbers in decimal format if they are in the interval
  75. // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
  76. // (lower boundary included, greater boundary excluded).
  77. // Example: with decimal_in_shortest_low = -6 and
  78. // decimal_in_shortest_high = 21:
  79. // ToShortest(0.000001) -> "0.000001"
  80. // ToShortest(0.0000001) -> "1e-7"
  81. // ToShortest(111111111111111111111.0) -> "111111111111111110000"
  82. // ToShortest(100000000000000000000.0) -> "100000000000000000000"
  83. // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
  84. //
  85. // When converting to precision mode the converter may add
  86. // max_leading_padding_zeroes before returning the number in exponential
  87. // format.
  88. // Example with max_leading_padding_zeroes_in_precision_mode = 6.
  89. // ToPrecision(0.0000012345, 2) -> "0.0000012"
  90. // ToPrecision(0.00000012345, 2) -> "1.2e-7"
  91. // Similarily the converter may add up to
  92. // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
  93. // returning an exponential representation. A zero added by the
  94. // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
  95. // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
  96. // ToPrecision(230.0, 2) -> "230"
  97. // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
  98. // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
  99. DoubleToStringConverter(int flags,
  100. const char* infinity_symbol,
  101. const char* nan_symbol,
  102. char exponent_character,
  103. int decimal_in_shortest_low,
  104. int decimal_in_shortest_high,
  105. int max_leading_padding_zeroes_in_precision_mode,
  106. int max_trailing_padding_zeroes_in_precision_mode)
  107. : flags_(flags),
  108. infinity_symbol_(infinity_symbol),
  109. nan_symbol_(nan_symbol),
  110. exponent_character_(exponent_character),
  111. decimal_in_shortest_low_(decimal_in_shortest_low),
  112. decimal_in_shortest_high_(decimal_in_shortest_high),
  113. max_leading_padding_zeroes_in_precision_mode_(
  114. max_leading_padding_zeroes_in_precision_mode),
  115. max_trailing_padding_zeroes_in_precision_mode_(
  116. max_trailing_padding_zeroes_in_precision_mode) {
  117. // When 'trailing zero after the point' is set, then 'trailing point'
  118. // must be set too.
  119. ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
  120. !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
  121. }
  122. // Returns a converter following the EcmaScript specification.
  123. static const DoubleToStringConverter& EcmaScriptConverter();
  124. // Computes the shortest string of digits that correctly represent the input
  125. // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
  126. // (see constructor) it then either returns a decimal representation, or an
  127. // exponential representation.
  128. // Example with decimal_in_shortest_low = -6,
  129. // decimal_in_shortest_high = 21,
  130. // EMIT_POSITIVE_EXPONENT_SIGN activated, and
  131. // EMIT_TRAILING_DECIMAL_POINT deactived:
  132. // ToShortest(0.000001) -> "0.000001"
  133. // ToShortest(0.0000001) -> "1e-7"
  134. // ToShortest(111111111111111111111.0) -> "111111111111111110000"
  135. // ToShortest(100000000000000000000.0) -> "100000000000000000000"
  136. // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
  137. //
  138. // Note: the conversion may round the output if the returned string
  139. // is accurate enough to uniquely identify the input-number.
  140. // For example the most precise representation of the double 9e59 equals
  141. // "899999999999999918767229449717619953810131273674690656206848", but
  142. // the converter will return the shorter (but still correct) "9e59".
  143. //
  144. // Returns true if the conversion succeeds. The conversion always succeeds
  145. // except when the input value is special and no infinity_symbol or
  146. // nan_symbol has been given to the constructor.
  147. bool ToShortest(double value, StringBuilder* result_builder) const {
  148. return ToShortestIeeeNumber(value, result_builder, SHORTEST);
  149. }
  150. // Same as ToShortest, but for single-precision floats.
  151. bool ToShortestSingle(float value, StringBuilder* result_builder) const {
  152. return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
  153. }
  154. // Computes a decimal representation with a fixed number of digits after the
  155. // decimal point. The last emitted digit is rounded.
  156. //
  157. // Examples:
  158. // ToFixed(3.12, 1) -> "3.1"
  159. // ToFixed(3.1415, 3) -> "3.142"
  160. // ToFixed(1234.56789, 4) -> "1234.5679"
  161. // ToFixed(1.23, 5) -> "1.23000"
  162. // ToFixed(0.1, 4) -> "0.1000"
  163. // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
  164. // ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
  165. // ToFixed(0.1, 17) -> "0.10000000000000001"
  166. //
  167. // If requested_digits equals 0, then the tail of the result depends on
  168. // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
  169. // Examples, for requested_digits == 0,
  170. // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
  171. // - false and false: then 123.45 -> 123
  172. // 0.678 -> 1
  173. // - true and false: then 123.45 -> 123.
  174. // 0.678 -> 1.
  175. // - true and true: then 123.45 -> 123.0
  176. // 0.678 -> 1.0
  177. //
  178. // Returns true if the conversion succeeds. The conversion always succeeds
  179. // except for the following cases:
  180. // - the input value is special and no infinity_symbol or nan_symbol has
  181. // been provided to the constructor,
  182. // - 'value' > 10^kMaxFixedDigitsBeforePoint, or
  183. // - 'requested_digits' > kMaxFixedDigitsAfterPoint.
  184. // The last two conditions imply that the result will never contain more than
  185. // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
  186. // (one additional character for the sign, and one for the decimal point).
  187. bool ToFixed(double value,
  188. int requested_digits,
  189. StringBuilder* result_builder) const;
  190. // Computes a representation in exponential format with requested_digits
  191. // after the decimal point. The last emitted digit is rounded.
  192. // If requested_digits equals -1, then the shortest exponential representation
  193. // is computed.
  194. //
  195. // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
  196. // exponent_character set to 'e'.
  197. // ToExponential(3.12, 1) -> "3.1e0"
  198. // ToExponential(5.0, 3) -> "5.000e0"
  199. // ToExponential(0.001, 2) -> "1.00e-3"
  200. // ToExponential(3.1415, -1) -> "3.1415e0"
  201. // ToExponential(3.1415, 4) -> "3.1415e0"
  202. // ToExponential(3.1415, 3) -> "3.142e0"
  203. // ToExponential(123456789000000, 3) -> "1.235e14"
  204. // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
  205. // ToExponential(1000000000000000019884624838656.0, 32) ->
  206. // "1.00000000000000001988462483865600e30"
  207. // ToExponential(1234, 0) -> "1e3"
  208. //
  209. // Returns true if the conversion succeeds. The conversion always succeeds
  210. // except for the following cases:
  211. // - the input value is special and no infinity_symbol or nan_symbol has
  212. // been provided to the constructor,
  213. // - 'requested_digits' > kMaxExponentialDigits.
  214. // The last condition implies that the result will never contain more than
  215. // kMaxExponentialDigits + 8 characters (the sign, the digit before the
  216. // decimal point, the decimal point, the exponent character, the
  217. // exponent's sign, and at most 3 exponent digits).
  218. bool ToExponential(double value,
  219. int requested_digits,
  220. StringBuilder* result_builder) const;
  221. // Computes 'precision' leading digits of the given 'value' and returns them
  222. // either in exponential or decimal format, depending on
  223. // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
  224. // constructor).
  225. // The last computed digit is rounded.
  226. //
  227. // Example with max_leading_padding_zeroes_in_precision_mode = 6.
  228. // ToPrecision(0.0000012345, 2) -> "0.0000012"
  229. // ToPrecision(0.00000012345, 2) -> "1.2e-7"
  230. // Similarily the converter may add up to
  231. // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
  232. // returning an exponential representation. A zero added by the
  233. // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
  234. // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
  235. // ToPrecision(230.0, 2) -> "230"
  236. // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
  237. // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
  238. // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
  239. // EMIT_TRAILING_ZERO_AFTER_POINT:
  240. // ToPrecision(123450.0, 6) -> "123450"
  241. // ToPrecision(123450.0, 5) -> "123450"
  242. // ToPrecision(123450.0, 4) -> "123500"
  243. // ToPrecision(123450.0, 3) -> "123000"
  244. // ToPrecision(123450.0, 2) -> "1.2e5"
  245. //
  246. // Returns true if the conversion succeeds. The conversion always succeeds
  247. // except for the following cases:
  248. // - the input value is special and no infinity_symbol or nan_symbol has
  249. // been provided to the constructor,
  250. // - precision < kMinPericisionDigits
  251. // - precision > kMaxPrecisionDigits
  252. // The last condition implies that the result will never contain more than
  253. // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
  254. // exponent character, the exponent's sign, and at most 3 exponent digits).
  255. bool ToPrecision(double value,
  256. int precision,
  257. StringBuilder* result_builder) const;
  258. enum DtoaMode {
  259. // Produce the shortest correct representation.
  260. // For example the output of 0.299999999999999988897 is (the less accurate
  261. // but correct) 0.3.
  262. SHORTEST,
  263. // Same as SHORTEST, but for single-precision floats.
  264. SHORTEST_SINGLE,
  265. // Produce a fixed number of digits after the decimal point.
  266. // For instance fixed(0.1, 4) becomes 0.1000
  267. // If the input number is big, the output will be big.
  268. FIXED,
  269. // Fixed number of digits (independent of the decimal point).
  270. PRECISION
  271. };
  272. // The maximal number of digits that are needed to emit a double in base 10.
  273. // A higher precision can be achieved by using more digits, but the shortest
  274. // accurate representation of any double will never use more digits than
  275. // kBase10MaximalLength.
  276. // Note that DoubleToAscii null-terminates its input. So the given buffer
  277. // should be at least kBase10MaximalLength + 1 characters long.
  278. static const int kBase10MaximalLength = 17;
  279. // Converts the given double 'v' to ascii. 'v' must not be NaN, +Infinity, or
  280. // -Infinity. In SHORTEST_SINGLE-mode this restriction also applies to 'v'
  281. // after it has been casted to a single-precision float. That is, in this
  282. // mode static_cast<float>(v) must not be NaN, +Infinity or -Infinity.
  283. //
  284. // The result should be interpreted as buffer * 10^(point-length).
  285. //
  286. // The output depends on the given mode:
  287. // - SHORTEST: produce the least amount of digits for which the internal
  288. // identity requirement is still satisfied. If the digits are printed
  289. // (together with the correct exponent) then reading this number will give
  290. // 'v' again. The buffer will choose the representation that is closest to
  291. // 'v'. If there are two at the same distance, than the one farther away
  292. // from 0 is chosen (halfway cases - ending with 5 - are rounded up).
  293. // In this mode the 'requested_digits' parameter is ignored.
  294. // - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
  295. // - FIXED: produces digits necessary to print a given number with
  296. // 'requested_digits' digits after the decimal point. The produced digits
  297. // might be too short in which case the caller has to fill the remainder
  298. // with '0's.
  299. // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
  300. // Halfway cases are rounded towards +/-Infinity (away from 0). The call
  301. // toFixed(0.15, 2) thus returns buffer="2", point=0.
  302. // The returned buffer may contain digits that would be truncated from the
  303. // shortest representation of the input.
  304. // - PRECISION: produces 'requested_digits' where the first digit is not '0'.
  305. // Even though the length of produced digits usually equals
  306. // 'requested_digits', the function is allowed to return fewer digits, in
  307. // which case the caller has to fill the missing digits with '0's.
  308. // Halfway cases are again rounded away from 0.
  309. // DoubleToAscii expects the given buffer to be big enough to hold all
  310. // digits and a terminating null-character. In SHORTEST-mode it expects a
  311. // buffer of at least kBase10MaximalLength + 1. In all other modes the
  312. // requested_digits parameter and the padding-zeroes limit the size of the
  313. // output. Don't forget the decimal point, the exponent character and the
  314. // terminating null-character when computing the maximal output size.
  315. // The given length is only used in debug mode to ensure the buffer is big
  316. // enough.
  317. static void DoubleToAscii(double v,
  318. DtoaMode mode,
  319. int requested_digits,
  320. char* buffer,
  321. int buffer_length,
  322. bool* sign,
  323. int* length,
  324. int* point);
  325. private:
  326. // Implementation for ToShortest and ToShortestSingle.
  327. bool ToShortestIeeeNumber(double value,
  328. StringBuilder* result_builder,
  329. DtoaMode mode) const;
  330. // If the value is a special value (NaN or Infinity) constructs the
  331. // corresponding string using the configured infinity/nan-symbol.
  332. // If either of them is NULL or the value is not special then the
  333. // function returns false.
  334. bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
  335. // Constructs an exponential representation (i.e. 1.234e56).
  336. // The given exponent assumes a decimal point after the first decimal digit.
  337. void CreateExponentialRepresentation(const char* decimal_digits,
  338. int length,
  339. int exponent,
  340. StringBuilder* result_builder) const;
  341. // Creates a decimal representation (i.e 1234.5678).
  342. void CreateDecimalRepresentation(const char* decimal_digits,
  343. int length,
  344. int decimal_point,
  345. int digits_after_point,
  346. StringBuilder* result_builder) const;
  347. const int flags_;
  348. const char* const infinity_symbol_;
  349. const char* const nan_symbol_;
  350. const char exponent_character_;
  351. const int decimal_in_shortest_low_;
  352. const int decimal_in_shortest_high_;
  353. const int max_leading_padding_zeroes_in_precision_mode_;
  354. const int max_trailing_padding_zeroes_in_precision_mode_;
  355. DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
  356. };
  357. class StringToDoubleConverter {
  358. public:
  359. // Enumeration for allowing octals and ignoring junk when converting
  360. // strings to numbers.
  361. enum Flags {
  362. NO_FLAGS = 0,
  363. ALLOW_HEX = 1,
  364. ALLOW_OCTALS = 2,
  365. ALLOW_TRAILING_JUNK = 4,
  366. ALLOW_LEADING_SPACES = 8,
  367. ALLOW_TRAILING_SPACES = 16,
  368. ALLOW_SPACES_AFTER_SIGN = 32
  369. };
  370. // Flags should be a bit-or combination of the possible Flags-enum.
  371. // - NO_FLAGS: no special flags.
  372. // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
  373. // Ex: StringToDouble("0x1234") -> 4660.0
  374. // In StringToDouble("0x1234.56") the characters ".56" are trailing
  375. // junk. The result of the call is hence dependent on
  376. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  377. // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
  378. // the string will not be parsed as "0" followed by junk.
  379. //
  380. // - ALLOW_OCTALS: recognizes the prefix "0" for octals:
  381. // If a sequence of octal digits starts with '0', then the number is
  382. // read as octal integer. Octal numbers may only be integers.
  383. // Ex: StringToDouble("01234") -> 668.0
  384. // StringToDouble("012349") -> 12349.0 // Not a sequence of octal
  385. // // digits.
  386. // In StringToDouble("01234.56") the characters ".56" are trailing
  387. // junk. The result of the call is hence dependent on
  388. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  389. // In StringToDouble("01234e56") the characters "e56" are trailing
  390. // junk, too.
  391. // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
  392. // a double literal.
  393. // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
  394. // new-lines, and tabs.
  395. // - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
  396. // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
  397. // Ex: StringToDouble("- 123.2") -> -123.2.
  398. // StringToDouble("+ 123.2") -> 123.2
  399. //
  400. // empty_string_value is returned when an empty string is given as input.
  401. // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
  402. // containing only spaces is converted to the 'empty_string_value', too.
  403. //
  404. // junk_string_value is returned when
  405. // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
  406. // part of a double-literal) is found.
  407. // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
  408. // double literal.
  409. //
  410. // infinity_symbol and nan_symbol are strings that are used to detect
  411. // inputs that represent infinity and NaN. They can be null, in which case
  412. // they are ignored.
  413. // The conversion routine first reads any possible signs. Then it compares the
  414. // following character of the input-string with the first character of
  415. // the infinity, and nan-symbol. If either matches, the function assumes, that
  416. // a match has been found, and expects the following input characters to match
  417. // the remaining characters of the special-value symbol.
  418. // This means that the following restrictions apply to special-value symbols:
  419. // - they must not start with signs ('+', or '-'),
  420. // - they must not have the same first character.
  421. // - they must not start with digits.
  422. //
  423. // Examples:
  424. // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
  425. // empty_string_value = 0.0,
  426. // junk_string_value = NaN,
  427. // infinity_symbol = "infinity",
  428. // nan_symbol = "nan":
  429. // StringToDouble("0x1234") -> 4660.0.
  430. // StringToDouble("0x1234K") -> 4660.0.
  431. // StringToDouble("") -> 0.0 // empty_string_value.
  432. // StringToDouble(" ") -> NaN // junk_string_value.
  433. // StringToDouble(" 1") -> NaN // junk_string_value.
  434. // StringToDouble("0x") -> NaN // junk_string_value.
  435. // StringToDouble("-123.45") -> -123.45.
  436. // StringToDouble("--123.45") -> NaN // junk_string_value.
  437. // StringToDouble("123e45") -> 123e45.
  438. // StringToDouble("123E45") -> 123e45.
  439. // StringToDouble("123e+45") -> 123e45.
  440. // StringToDouble("123E-45") -> 123e-45.
  441. // StringToDouble("123e") -> 123.0 // trailing junk ignored.
  442. // StringToDouble("123e-") -> 123.0 // trailing junk ignored.
  443. // StringToDouble("+NaN") -> NaN // NaN string literal.
  444. // StringToDouble("-infinity") -> -inf. // infinity literal.
  445. // StringToDouble("Infinity") -> NaN // junk_string_value.
  446. //
  447. // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
  448. // empty_string_value = 0.0,
  449. // junk_string_value = NaN,
  450. // infinity_symbol = NULL,
  451. // nan_symbol = NULL:
  452. // StringToDouble("0x1234") -> NaN // junk_string_value.
  453. // StringToDouble("01234") -> 668.0.
  454. // StringToDouble("") -> 0.0 // empty_string_value.
  455. // StringToDouble(" ") -> 0.0 // empty_string_value.
  456. // StringToDouble(" 1") -> 1.0
  457. // StringToDouble("0x") -> NaN // junk_string_value.
  458. // StringToDouble("0123e45") -> NaN // junk_string_value.
  459. // StringToDouble("01239E45") -> 1239e45.
  460. // StringToDouble("-infinity") -> NaN // junk_string_value.
  461. // StringToDouble("NaN") -> NaN // junk_string_value.
  462. StringToDoubleConverter(int flags,
  463. double empty_string_value,
  464. double junk_string_value,
  465. const char* infinity_symbol,
  466. const char* nan_symbol)
  467. : flags_(flags),
  468. empty_string_value_(empty_string_value),
  469. junk_string_value_(junk_string_value),
  470. infinity_symbol_(infinity_symbol),
  471. nan_symbol_(nan_symbol) {
  472. }
  473. // Performs the conversion.
  474. // The output parameter 'processed_characters_count' is set to the number
  475. // of characters that have been processed to read the number.
  476. // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
  477. // in the 'processed_characters_count'. Trailing junk is never included.
  478. double StringToDouble(const char* buffer,
  479. int length,
  480. int* processed_characters_count) const;
  481. // Same as StringToDouble above but for 16 bit characters.
  482. double StringToDouble(const uc16* buffer,
  483. int length,
  484. int* processed_characters_count) const;
  485. // Same as StringToDouble but reads a float.
  486. // Note that this is not equivalent to static_cast<float>(StringToDouble(...))
  487. // due to potential double-rounding.
  488. float StringToFloat(const char* buffer,
  489. int length,
  490. int* processed_characters_count) const;
  491. // Same as StringToFloat above but for 16 bit characters.
  492. float StringToFloat(const uc16* buffer,
  493. int length,
  494. int* processed_characters_count) const;
  495. private:
  496. const int flags_;
  497. const double empty_string_value_;
  498. const double junk_string_value_;
  499. const char* const infinity_symbol_;
  500. const char* const nan_symbol_;
  501. template <class Iterator>
  502. double StringToIeee(Iterator start_pointer,
  503. int length,
  504. bool read_as_double,
  505. int* processed_characters_count) const;
  506. DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
  507. };
  508. } // namespace double_conversion
  509. #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_