double-conversion.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 "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 digit characters. 'v' must not be NaN,
  280. // +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also
  281. // applies to 'v' after it has been casted to a single-precision float. That
  282. // is, in this mode static_cast<float>(v) must not be NaN, +Infinity or
  283. // -Infinity.
  284. //
  285. // The result should be interpreted as buffer * 10^(point-length).
  286. //
  287. // The digits are written to the buffer in the platform's charset, which is
  288. // often UTF-8 (with ASCII-range digits) but may be another charset, such
  289. // as EBCDIC.
  290. //
  291. // The output depends on the given mode:
  292. // - SHORTEST: produce the least amount of digits for which the internal
  293. // identity requirement is still satisfied. If the digits are printed
  294. // (together with the correct exponent) then reading this number will give
  295. // 'v' again. The buffer will choose the representation that is closest to
  296. // 'v'. If there are two at the same distance, than the one farther away
  297. // from 0 is chosen (halfway cases - ending with 5 - are rounded up).
  298. // In this mode the 'requested_digits' parameter is ignored.
  299. // - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
  300. // - FIXED: produces digits necessary to print a given number with
  301. // 'requested_digits' digits after the decimal point. The produced digits
  302. // might be too short in which case the caller has to fill the remainder
  303. // with '0's.
  304. // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
  305. // Halfway cases are rounded towards +/-Infinity (away from 0). The call
  306. // toFixed(0.15, 2) thus returns buffer="2", point=0.
  307. // The returned buffer may contain digits that would be truncated from the
  308. // shortest representation of the input.
  309. // - PRECISION: produces 'requested_digits' where the first digit is not '0'.
  310. // Even though the length of produced digits usually equals
  311. // 'requested_digits', the function is allowed to return fewer digits, in
  312. // which case the caller has to fill the missing digits with '0's.
  313. // Halfway cases are again rounded away from 0.
  314. // DoubleToAscii expects the given buffer to be big enough to hold all
  315. // digits and a terminating null-character. In SHORTEST-mode it expects a
  316. // buffer of at least kBase10MaximalLength + 1. In all other modes the
  317. // requested_digits parameter and the padding-zeroes limit the size of the
  318. // output. Don't forget the decimal point, the exponent character and the
  319. // terminating null-character when computing the maximal output size.
  320. // The given length is only used in debug mode to ensure the buffer is big
  321. // enough.
  322. static void DoubleToAscii(double v,
  323. DtoaMode mode,
  324. int requested_digits,
  325. char* buffer,
  326. int buffer_length,
  327. bool* sign,
  328. int* length,
  329. int* point);
  330. private:
  331. // Implementation for ToShortest and ToShortestSingle.
  332. bool ToShortestIeeeNumber(double value,
  333. StringBuilder* result_builder,
  334. DtoaMode mode) const;
  335. // If the value is a special value (NaN or Infinity) constructs the
  336. // corresponding string using the configured infinity/nan-symbol.
  337. // If either of them is NULL or the value is not special then the
  338. // function returns false.
  339. bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
  340. // Constructs an exponential representation (i.e. 1.234e56).
  341. // The given exponent assumes a decimal point after the first decimal digit.
  342. void CreateExponentialRepresentation(const char* decimal_digits,
  343. int length,
  344. int exponent,
  345. StringBuilder* result_builder) const;
  346. // Creates a decimal representation (i.e 1234.5678).
  347. void CreateDecimalRepresentation(const char* decimal_digits,
  348. int length,
  349. int decimal_point,
  350. int digits_after_point,
  351. StringBuilder* result_builder) const;
  352. const int flags_;
  353. const char* const infinity_symbol_;
  354. const char* const nan_symbol_;
  355. const char exponent_character_;
  356. const int decimal_in_shortest_low_;
  357. const int decimal_in_shortest_high_;
  358. const int max_leading_padding_zeroes_in_precision_mode_;
  359. const int max_trailing_padding_zeroes_in_precision_mode_;
  360. DC_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
  361. };
  362. class StringToDoubleConverter {
  363. public:
  364. // Enumeration for allowing octals and ignoring junk when converting
  365. // strings to numbers.
  366. enum Flags {
  367. NO_FLAGS = 0,
  368. ALLOW_HEX = 1,
  369. ALLOW_OCTALS = 2,
  370. ALLOW_TRAILING_JUNK = 4,
  371. ALLOW_LEADING_SPACES = 8,
  372. ALLOW_TRAILING_SPACES = 16,
  373. ALLOW_SPACES_AFTER_SIGN = 32,
  374. ALLOW_CASE_INSENSIBILITY = 64,
  375. ALLOW_HEX_FLOATS = 128,
  376. };
  377. static const uc16 kNoSeparator = '\0';
  378. // Flags should be a bit-or combination of the possible Flags-enum.
  379. // - NO_FLAGS: no special flags.
  380. // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
  381. // Ex: StringToDouble("0x1234") -> 4660.0
  382. // In StringToDouble("0x1234.56") the characters ".56" are trailing
  383. // junk. The result of the call is hence dependent on
  384. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  385. // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
  386. // the string will not be parsed as "0" followed by junk.
  387. //
  388. // - ALLOW_OCTALS: recognizes the prefix "0" for octals:
  389. // If a sequence of octal digits starts with '0', then the number is
  390. // read as octal integer. Octal numbers may only be integers.
  391. // Ex: StringToDouble("01234") -> 668.0
  392. // StringToDouble("012349") -> 12349.0 // Not a sequence of octal
  393. // // digits.
  394. // In StringToDouble("01234.56") the characters ".56" are trailing
  395. // junk. The result of the call is hence dependent on
  396. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  397. // In StringToDouble("01234e56") the characters "e56" are trailing
  398. // junk, too.
  399. // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
  400. // a double literal.
  401. // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
  402. // new-lines, and tabs.
  403. // - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
  404. // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
  405. // Ex: StringToDouble("- 123.2") -> -123.2.
  406. // StringToDouble("+ 123.2") -> 123.2
  407. // - ALLOW_CASE_INSENSIBILITY: ignore case of characters for special values:
  408. // infinity and nan.
  409. // - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
  410. // This *must* start with "0x" and separate the exponent with "p".
  411. // Examples: 0x1.2p3 == 9.0
  412. // 0x10.1p0 == 16.0625
  413. // ALLOW_HEX and ALLOW_HEX_FLOATS are indendent.
  414. //
  415. // empty_string_value is returned when an empty string is given as input.
  416. // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
  417. // containing only spaces is converted to the 'empty_string_value', too.
  418. //
  419. // junk_string_value is returned when
  420. // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
  421. // part of a double-literal) is found.
  422. // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
  423. // double literal.
  424. //
  425. // infinity_symbol and nan_symbol are strings that are used to detect
  426. // inputs that represent infinity and NaN. They can be null, in which case
  427. // they are ignored.
  428. // The conversion routine first reads any possible signs. Then it compares the
  429. // following character of the input-string with the first character of
  430. // the infinity, and nan-symbol. If either matches, the function assumes, that
  431. // a match has been found, and expects the following input characters to match
  432. // the remaining characters of the special-value symbol.
  433. // This means that the following restrictions apply to special-value symbols:
  434. // - they must not start with signs ('+', or '-'),
  435. // - they must not have the same first character.
  436. // - they must not start with digits.
  437. //
  438. // If the separator character is not kNoSeparator, then that specific
  439. // character is ignored when in between two valid digits of the significant.
  440. // It is not allowed to appear in the exponent.
  441. // It is not allowed to lead or trail the number.
  442. // It is not allowed to appear twice next to each other.
  443. //
  444. // Examples:
  445. // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
  446. // empty_string_value = 0.0,
  447. // junk_string_value = NaN,
  448. // infinity_symbol = "infinity",
  449. // nan_symbol = "nan":
  450. // StringToDouble("0x1234") -> 4660.0.
  451. // StringToDouble("0x1234K") -> 4660.0.
  452. // StringToDouble("") -> 0.0 // empty_string_value.
  453. // StringToDouble(" ") -> NaN // junk_string_value.
  454. // StringToDouble(" 1") -> NaN // junk_string_value.
  455. // StringToDouble("0x") -> NaN // junk_string_value.
  456. // StringToDouble("-123.45") -> -123.45.
  457. // StringToDouble("--123.45") -> NaN // junk_string_value.
  458. // StringToDouble("123e45") -> 123e45.
  459. // StringToDouble("123E45") -> 123e45.
  460. // StringToDouble("123e+45") -> 123e45.
  461. // StringToDouble("123E-45") -> 123e-45.
  462. // StringToDouble("123e") -> 123.0 // trailing junk ignored.
  463. // StringToDouble("123e-") -> 123.0 // trailing junk ignored.
  464. // StringToDouble("+NaN") -> NaN // NaN string literal.
  465. // StringToDouble("-infinity") -> -inf. // infinity literal.
  466. // StringToDouble("Infinity") -> NaN // junk_string_value.
  467. //
  468. // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
  469. // empty_string_value = 0.0,
  470. // junk_string_value = NaN,
  471. // infinity_symbol = NULL,
  472. // nan_symbol = NULL:
  473. // StringToDouble("0x1234") -> NaN // junk_string_value.
  474. // StringToDouble("01234") -> 668.0.
  475. // StringToDouble("") -> 0.0 // empty_string_value.
  476. // StringToDouble(" ") -> 0.0 // empty_string_value.
  477. // StringToDouble(" 1") -> 1.0
  478. // StringToDouble("0x") -> NaN // junk_string_value.
  479. // StringToDouble("0123e45") -> NaN // junk_string_value.
  480. // StringToDouble("01239E45") -> 1239e45.
  481. // StringToDouble("-infinity") -> NaN // junk_string_value.
  482. // StringToDouble("NaN") -> NaN // junk_string_value.
  483. //
  484. // flags = NO_FLAGS,
  485. // separator = ' ':
  486. // StringToDouble("1 2 3 4") -> 1234.0
  487. // StringToDouble("1 2") -> NaN // junk_string_value
  488. // StringToDouble("1 000 000.0") -> 1000000.0
  489. // StringToDouble("1.000 000") -> 1.0
  490. // StringToDouble("1.0e1 000") -> NaN // junk_string_value
  491. StringToDoubleConverter(int flags,
  492. double empty_string_value,
  493. double junk_string_value,
  494. const char* infinity_symbol,
  495. const char* nan_symbol,
  496. uc16 separator = kNoSeparator)
  497. : flags_(flags),
  498. empty_string_value_(empty_string_value),
  499. junk_string_value_(junk_string_value),
  500. infinity_symbol_(infinity_symbol),
  501. nan_symbol_(nan_symbol),
  502. separator_(separator) {
  503. }
  504. // Performs the conversion.
  505. // The output parameter 'processed_characters_count' is set to the number
  506. // of characters that have been processed to read the number.
  507. // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
  508. // in the 'processed_characters_count'. Trailing junk is never included.
  509. double StringToDouble(const char* buffer,
  510. int length,
  511. int* processed_characters_count) const;
  512. // Same as StringToDouble above but for 16 bit characters.
  513. double StringToDouble(const uc16* buffer,
  514. int length,
  515. int* processed_characters_count) const;
  516. // Same as StringToDouble but reads a float.
  517. // Note that this is not equivalent to static_cast<float>(StringToDouble(...))
  518. // due to potential double-rounding.
  519. float StringToFloat(const char* buffer,
  520. int length,
  521. int* processed_characters_count) const;
  522. // Same as StringToFloat above but for 16 bit characters.
  523. float StringToFloat(const uc16* buffer,
  524. int length,
  525. int* processed_characters_count) const;
  526. private:
  527. const int flags_;
  528. const double empty_string_value_;
  529. const double junk_string_value_;
  530. const char* const infinity_symbol_;
  531. const char* const nan_symbol_;
  532. const uc16 separator_;
  533. template <class Iterator>
  534. double StringToIeee(Iterator start_pointer,
  535. int length,
  536. bool read_as_double,
  537. int* processed_characters_count) const;
  538. DC_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
  539. };
  540. } // namespace double_conversion
  541. #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_