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