double-conversion-string-to-double.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. //
  4. // From the double-conversion library. Original license:
  5. //
  6. // Copyright 2012 the V8 project authors. All rights reserved.
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
  33. #include "unicode/utypes.h"
  34. #if !UCONFIG_NO_FORMATTING
  35. #ifndef DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
  36. #define DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
  37. // ICU PATCH: Customize header file paths for ICU.
  38. #include "double-conversion-utils.h"
  39. // ICU PATCH: Wrap in ICU namespace
  40. U_NAMESPACE_BEGIN
  41. namespace double_conversion {
  42. class StringToDoubleConverter {
  43. public:
  44. // Enumeration for allowing octals and ignoring junk when converting
  45. // strings to numbers.
  46. enum Flags {
  47. NO_FLAGS = 0,
  48. ALLOW_HEX = 1,
  49. ALLOW_OCTALS = 2,
  50. ALLOW_TRAILING_JUNK = 4,
  51. ALLOW_LEADING_SPACES = 8,
  52. ALLOW_TRAILING_SPACES = 16,
  53. ALLOW_SPACES_AFTER_SIGN = 32,
  54. ALLOW_CASE_INSENSITIVITY = 64,
  55. ALLOW_CASE_INSENSIBILITY = 64, // Deprecated
  56. ALLOW_HEX_FLOATS = 128,
  57. };
  58. static const uc16 kNoSeparator = '\0';
  59. // Flags should be a bit-or combination of the possible Flags-enum.
  60. // - NO_FLAGS: no special flags.
  61. // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
  62. // Ex: StringToDouble("0x1234") -> 4660.0
  63. // In StringToDouble("0x1234.56") the characters ".56" are trailing
  64. // junk. The result of the call is hence dependent on
  65. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  66. // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
  67. // the string will not be parsed as "0" followed by junk.
  68. //
  69. // - ALLOW_OCTALS: recognizes the prefix "0" for octals:
  70. // If a sequence of octal digits starts with '0', then the number is
  71. // read as octal integer. Octal numbers may only be integers.
  72. // Ex: StringToDouble("01234") -> 668.0
  73. // StringToDouble("012349") -> 12349.0 // Not a sequence of octal
  74. // // digits.
  75. // In StringToDouble("01234.56") the characters ".56" are trailing
  76. // junk. The result of the call is hence dependent on
  77. // the ALLOW_TRAILING_JUNK flag and/or the junk value.
  78. // In StringToDouble("01234e56") the characters "e56" are trailing
  79. // junk, too.
  80. // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
  81. // a double literal.
  82. // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
  83. // new-lines, and tabs.
  84. // - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
  85. // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
  86. // Ex: StringToDouble("- 123.2") -> -123.2.
  87. // StringToDouble("+ 123.2") -> 123.2
  88. // - ALLOW_CASE_INSENSITIVITY: ignore case of characters for special values:
  89. // infinity and nan.
  90. // - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
  91. // This *must* start with "0x" and separate the exponent with "p".
  92. // Examples: 0x1.2p3 == 9.0
  93. // 0x10.1p0 == 16.0625
  94. // ALLOW_HEX and ALLOW_HEX_FLOATS are indendent.
  95. //
  96. // empty_string_value is returned when an empty string is given as input.
  97. // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
  98. // containing only spaces is converted to the 'empty_string_value', too.
  99. //
  100. // junk_string_value is returned when
  101. // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
  102. // part of a double-literal) is found.
  103. // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
  104. // double literal.
  105. //
  106. // infinity_symbol and nan_symbol are strings that are used to detect
  107. // inputs that represent infinity and NaN. They can be null, in which case
  108. // they are ignored.
  109. // The conversion routine first reads any possible signs. Then it compares the
  110. // following character of the input-string with the first character of
  111. // the infinity, and nan-symbol. If either matches, the function assumes, that
  112. // a match has been found, and expects the following input characters to match
  113. // the remaining characters of the special-value symbol.
  114. // This means that the following restrictions apply to special-value symbols:
  115. // - they must not start with signs ('+', or '-'),
  116. // - they must not have the same first character.
  117. // - they must not start with digits.
  118. //
  119. // If the separator character is not kNoSeparator, then that specific
  120. // character is ignored when in between two valid digits of the significant.
  121. // It is not allowed to appear in the exponent.
  122. // It is not allowed to lead or trail the number.
  123. // It is not allowed to appear twice next to each other.
  124. //
  125. // Examples:
  126. // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
  127. // empty_string_value = 0.0,
  128. // junk_string_value = NaN,
  129. // infinity_symbol = "infinity",
  130. // nan_symbol = "nan":
  131. // StringToDouble("0x1234") -> 4660.0.
  132. // StringToDouble("0x1234K") -> 4660.0.
  133. // StringToDouble("") -> 0.0 // empty_string_value.
  134. // StringToDouble(" ") -> NaN // junk_string_value.
  135. // StringToDouble(" 1") -> NaN // junk_string_value.
  136. // StringToDouble("0x") -> NaN // junk_string_value.
  137. // StringToDouble("-123.45") -> -123.45.
  138. // StringToDouble("--123.45") -> NaN // junk_string_value.
  139. // StringToDouble("123e45") -> 123e45.
  140. // StringToDouble("123E45") -> 123e45.
  141. // StringToDouble("123e+45") -> 123e45.
  142. // StringToDouble("123E-45") -> 123e-45.
  143. // StringToDouble("123e") -> 123.0 // trailing junk ignored.
  144. // StringToDouble("123e-") -> 123.0 // trailing junk ignored.
  145. // StringToDouble("+NaN") -> NaN // NaN string literal.
  146. // StringToDouble("-infinity") -> -inf. // infinity literal.
  147. // StringToDouble("Infinity") -> NaN // junk_string_value.
  148. //
  149. // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
  150. // empty_string_value = 0.0,
  151. // junk_string_value = NaN,
  152. // infinity_symbol = NULL,
  153. // nan_symbol = NULL:
  154. // StringToDouble("0x1234") -> NaN // junk_string_value.
  155. // StringToDouble("01234") -> 668.0.
  156. // StringToDouble("") -> 0.0 // empty_string_value.
  157. // StringToDouble(" ") -> 0.0 // empty_string_value.
  158. // StringToDouble(" 1") -> 1.0
  159. // StringToDouble("0x") -> NaN // junk_string_value.
  160. // StringToDouble("0123e45") -> NaN // junk_string_value.
  161. // StringToDouble("01239E45") -> 1239e45.
  162. // StringToDouble("-infinity") -> NaN // junk_string_value.
  163. // StringToDouble("NaN") -> NaN // junk_string_value.
  164. //
  165. // flags = NO_FLAGS,
  166. // separator = ' ':
  167. // StringToDouble("1 2 3 4") -> 1234.0
  168. // StringToDouble("1 2") -> NaN // junk_string_value
  169. // StringToDouble("1 000 000.0") -> 1000000.0
  170. // StringToDouble("1.000 000") -> 1.0
  171. // StringToDouble("1.0e1 000") -> NaN // junk_string_value
  172. StringToDoubleConverter(int flags,
  173. double empty_string_value,
  174. double junk_string_value,
  175. const char* infinity_symbol,
  176. const char* nan_symbol,
  177. uc16 separator = kNoSeparator)
  178. : flags_(flags),
  179. empty_string_value_(empty_string_value),
  180. junk_string_value_(junk_string_value),
  181. infinity_symbol_(infinity_symbol),
  182. nan_symbol_(nan_symbol),
  183. separator_(separator) {
  184. }
  185. // Performs the conversion.
  186. // The output parameter 'processed_characters_count' is set to the number
  187. // of characters that have been processed to read the number.
  188. // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
  189. // in the 'processed_characters_count'. Trailing junk is never included.
  190. double StringToDouble(const char* buffer,
  191. int length,
  192. int* processed_characters_count) const;
  193. // Same as StringToDouble above but for 16 bit characters.
  194. double StringToDouble(const uc16* buffer,
  195. int length,
  196. int* processed_characters_count) const;
  197. // Same as StringToDouble but reads a float.
  198. // Note that this is not equivalent to static_cast<float>(StringToDouble(...))
  199. // due to potential double-rounding.
  200. float StringToFloat(const char* buffer,
  201. int length,
  202. int* processed_characters_count) const;
  203. // Same as StringToFloat above but for 16 bit characters.
  204. float StringToFloat(const uc16* buffer,
  205. int length,
  206. int* processed_characters_count) const;
  207. private:
  208. const int flags_;
  209. const double empty_string_value_;
  210. const double junk_string_value_;
  211. const char* const infinity_symbol_;
  212. const char* const nan_symbol_;
  213. const uc16 separator_;
  214. template <class Iterator>
  215. double StringToIeee(Iterator start_pointer,
  216. int length,
  217. bool read_as_double,
  218. int* processed_characters_count) const;
  219. DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
  220. };
  221. } // namespace double_conversion
  222. // ICU PATCH: Close ICU namespace
  223. U_NAMESPACE_END
  224. #endif // DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
  225. #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING