double-conversion.h 26 KB

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