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