string-to-double.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_STRING_TO_DOUBLE_H_
  28. #define DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
  29. #include "utils.h"
  30. namespace double_conversion {
  31. class StringToDoubleConverter {
  32. public:
  33. // Enumeration for allowing octals and ignoring junk when converting
  34. // strings to numbers.
  35. enum Flags {
  36. NO_FLAGS = 0,
  37. ALLOW_HEX = 1,
  38. ALLOW_OCTALS = 2,
  39. ALLOW_TRAILING_JUNK = 4,
  40. ALLOW_LEADING_SPACES = 8,
  41. ALLOW_TRAILING_SPACES = 16,
  42. ALLOW_SPACES_AFTER_SIGN = 32,
  43. ALLOW_CASE_INSENSITIVITY = 64,
  44. ALLOW_CASE_INSENSIBILITY = 64, // Deprecated
  45. ALLOW_HEX_FLOATS = 128,
  46. };
  47. static const uc16 kNoSeparator = '\0';
  48. // Flags should be a bit-or combination of the possible Flags-enum.
  49. // - NO_FLAGS: no special flags.
  50. // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
  51. // Ex: StringToDouble("0x1234") -> 4660.0
  52. // In StringToDouble("0x1234.56") the characters ".56" are trailing
  53. // junk. The result of the call is hence dependent on
  54. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  55. // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
  56. // the string will not be parsed as "0" followed by junk.
  57. //
  58. // - ALLOW_OCTALS: recognizes the prefix "0" for octals:
  59. // If a sequence of octal digits starts with '0', then the number is
  60. // read as octal integer. Octal numbers may only be integers.
  61. // Ex: StringToDouble("01234") -> 668.0
  62. // StringToDouble("012349") -> 12349.0 // Not a sequence of octal
  63. // // digits.
  64. // In StringToDouble("01234.56") the characters ".56" are trailing
  65. // junk. The result of the call is hence dependent on
  66. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  67. // In StringToDouble("01234e56") the characters "e56" are trailing
  68. // junk, too.
  69. // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
  70. // a double literal.
  71. // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
  72. // new-lines, and tabs.
  73. // - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
  74. // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
  75. // Ex: StringToDouble("- 123.2") -> -123.2.
  76. // StringToDouble("+ 123.2") -> 123.2
  77. // - ALLOW_CASE_INSENSITIVITY: ignore case of characters for special values:
  78. // infinity and nan.
  79. // - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
  80. // This *must* start with "0x" and separate the exponent with "p".
  81. // Examples: 0x1.2p3 == 9.0
  82. // 0x10.1p0 == 16.0625
  83. // ALLOW_HEX and ALLOW_HEX_FLOATS are indendent.
  84. //
  85. // empty_string_value is returned when an empty string is given as input.
  86. // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
  87. // containing only spaces is converted to the 'empty_string_value', too.
  88. //
  89. // junk_string_value is returned when
  90. // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
  91. // part of a double-literal) is found.
  92. // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
  93. // double literal.
  94. //
  95. // infinity_symbol and nan_symbol are strings that are used to detect
  96. // inputs that represent infinity and NaN. They can be null, in which case
  97. // they are ignored.
  98. // The conversion routine first reads any possible signs. Then it compares the
  99. // following character of the input-string with the first character of
  100. // the infinity, and nan-symbol. If either matches, the function assumes, that
  101. // a match has been found, and expects the following input characters to match
  102. // the remaining characters of the special-value symbol.
  103. // This means that the following restrictions apply to special-value symbols:
  104. // - they must not start with signs ('+', or '-'),
  105. // - they must not have the same first character.
  106. // - they must not start with digits.
  107. //
  108. // If the separator character is not kNoSeparator, then that specific
  109. // character is ignored when in between two valid digits of the significant.
  110. // It is not allowed to appear in the exponent.
  111. // It is not allowed to lead or trail the number.
  112. // It is not allowed to appear twice next to each other.
  113. //
  114. // Examples:
  115. // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
  116. // empty_string_value = 0.0,
  117. // junk_string_value = NaN,
  118. // infinity_symbol = "infinity",
  119. // nan_symbol = "nan":
  120. // StringToDouble("0x1234") -> 4660.0.
  121. // StringToDouble("0x1234K") -> 4660.0.
  122. // StringToDouble("") -> 0.0 // empty_string_value.
  123. // StringToDouble(" ") -> NaN // junk_string_value.
  124. // StringToDouble(" 1") -> NaN // junk_string_value.
  125. // StringToDouble("0x") -> NaN // junk_string_value.
  126. // StringToDouble("-123.45") -> -123.45.
  127. // StringToDouble("--123.45") -> NaN // junk_string_value.
  128. // StringToDouble("123e45") -> 123e45.
  129. // StringToDouble("123E45") -> 123e45.
  130. // StringToDouble("123e+45") -> 123e45.
  131. // StringToDouble("123E-45") -> 123e-45.
  132. // StringToDouble("123e") -> 123.0 // trailing junk ignored.
  133. // StringToDouble("123e-") -> 123.0 // trailing junk ignored.
  134. // StringToDouble("+NaN") -> NaN // NaN string literal.
  135. // StringToDouble("-infinity") -> -inf. // infinity literal.
  136. // StringToDouble("Infinity") -> NaN // junk_string_value.
  137. //
  138. // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
  139. // empty_string_value = 0.0,
  140. // junk_string_value = NaN,
  141. // infinity_symbol = NULL,
  142. // nan_symbol = NULL:
  143. // StringToDouble("0x1234") -> NaN // junk_string_value.
  144. // StringToDouble("01234") -> 668.0.
  145. // StringToDouble("") -> 0.0 // empty_string_value.
  146. // StringToDouble(" ") -> 0.0 // empty_string_value.
  147. // StringToDouble(" 1") -> 1.0
  148. // StringToDouble("0x") -> NaN // junk_string_value.
  149. // StringToDouble("0123e45") -> NaN // junk_string_value.
  150. // StringToDouble("01239E45") -> 1239e45.
  151. // StringToDouble("-infinity") -> NaN // junk_string_value.
  152. // StringToDouble("NaN") -> NaN // junk_string_value.
  153. //
  154. // flags = NO_FLAGS,
  155. // separator = ' ':
  156. // StringToDouble("1 2 3 4") -> 1234.0
  157. // StringToDouble("1 2") -> NaN // junk_string_value
  158. // StringToDouble("1 000 000.0") -> 1000000.0
  159. // StringToDouble("1.000 000") -> 1.0
  160. // StringToDouble("1.0e1 000") -> NaN // junk_string_value
  161. StringToDoubleConverter(int flags,
  162. double empty_string_value,
  163. double junk_string_value,
  164. const char* infinity_symbol,
  165. const char* nan_symbol,
  166. uc16 separator = kNoSeparator)
  167. : flags_(flags),
  168. empty_string_value_(empty_string_value),
  169. junk_string_value_(junk_string_value),
  170. infinity_symbol_(infinity_symbol),
  171. nan_symbol_(nan_symbol),
  172. separator_(separator) {
  173. }
  174. // Performs the conversion.
  175. // The output parameter 'processed_characters_count' is set to the number
  176. // of characters that have been processed to read the number.
  177. // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
  178. // in the 'processed_characters_count'. Trailing junk is never included.
  179. double StringToDouble(const char* buffer,
  180. int length,
  181. int* processed_characters_count) const;
  182. // Same as StringToDouble above but for 16 bit characters.
  183. double StringToDouble(const uc16* buffer,
  184. int length,
  185. int* processed_characters_count) const;
  186. // Same as StringToDouble but reads a float.
  187. // Note that this is not equivalent to static_cast<float>(StringToDouble(...))
  188. // due to potential double-rounding.
  189. float StringToFloat(const char* buffer,
  190. int length,
  191. int* processed_characters_count) const;
  192. // Same as StringToFloat above but for 16 bit characters.
  193. float StringToFloat(const uc16* buffer,
  194. int length,
  195. int* processed_characters_count) const;
  196. private:
  197. const int flags_;
  198. const double empty_string_value_;
  199. const double junk_string_value_;
  200. const char* const infinity_symbol_;
  201. const char* const nan_symbol_;
  202. const uc16 separator_;
  203. template <class Iterator>
  204. double StringToIeee(Iterator start_pointer,
  205. int length,
  206. bool read_as_double,
  207. int* processed_characters_count) const;
  208. DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
  209. };
  210. } // namespace double_conversion
  211. #endif // DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_