Flutter Engine
The Flutter Engine
string-to-double.h
Go to the documentation of this file.
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
33namespace double_conversion {
34
36 public:
37 // Enumeration for allowing octals and ignoring junk when converting
38 // strings to numbers.
39 enum Flags {
48 ALLOW_CASE_INSENSIBILITY = 64, // Deprecated
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 indented.
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
168 double empty_string_value,
169 double junk_string_value,
170 const char* infinity_symbol,
171 const char* nan_symbol,
172 uc16 separator = kNoSeparator)
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>
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_
float StringToFloat(const char *buffer, int length, int *processed_characters_count) const
StringToDoubleConverter(int flags, double empty_string_value, double junk_string_value, const char *infinity_symbol, const char *nan_symbol, uc16 separator=kNoSeparator)
T StringTo(const uc16 *buffer, int length, int *processed_characters_count) const
double StringToDouble(const char *buffer, int length, int *processed_characters_count) const
T StringTo(const char *buffer, int length, int *processed_characters_count) const
FlutterSemanticsFlag flags
size_t length
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
#define T
Definition: precompiler.cc:65
uint16_t uc16
Definition: utils.h:190