Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
double_conversion::DoubleToStringConverter Class Reference

#include <double-to-string.h>

Public Types

enum  Flags {
  NO_FLAGS = 0 , EMIT_POSITIVE_EXPONENT_SIGN = 1 , EMIT_TRAILING_DECIMAL_POINT = 2 , EMIT_TRAILING_ZERO_AFTER_POINT = 4 ,
  UNIQUE_ZERO = 8 , NO_TRAILING_ZERO = 16 , EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL = 32 , EMIT_TRAILING_ZERO_AFTER_POINT_IN_EXPONENTIAL = 64
}
 
enum  DtoaMode { SHORTEST , SHORTEST_SINGLE , FIXED , PRECISION }
 

Public Member Functions

 DoubleToStringConverter (int flags, const char *infinity_symbol, const char *nan_symbol, char exponent_character, int decimal_in_shortest_low, int decimal_in_shortest_high, int max_leading_padding_zeroes_in_precision_mode, int max_trailing_padding_zeroes_in_precision_mode, int min_exponent_width=0)
 
bool ToShortest (double value, StringBuilder *result_builder) const
 
bool ToShortestSingle (float value, StringBuilder *result_builder) const
 
bool ToFixed (double value, int requested_digits, StringBuilder *result_builder) const
 
bool ToExponential (double value, int requested_digits, StringBuilder *result_builder) const
 
bool ToPrecision (double value, int precision, StringBuilder *result_builder) const
 

Static Public Member Functions

static const DoubleToStringConverterEcmaScriptConverter ()
 
static void DoubleToAscii (double v, DtoaMode mode, int requested_digits, char *buffer, int buffer_length, bool *sign, int *length, int *point)
 

Static Public Attributes

static const int kMaxFixedDigitsBeforePoint = 60
 
static const int kMaxFixedDigitsAfterPoint = 100
 
static const int kMaxExponentialDigits = 120
 
static const int kMinPrecisionDigits = 1
 
static const int kMaxPrecisionDigits = 120
 
static const int kBase10MaximalLength = 17
 
static const int kBase10MaximalLengthSingle = 9
 
static const int kMaxCharsEcmaScriptShortest = 25
 

Detailed Description

Definition at line 35 of file double-to-string.h.

Member Enumeration Documentation

◆ DtoaMode

Enumerator
SHORTEST 
SHORTEST_SINGLE 
FIXED 
PRECISION 

Definition at line 364 of file double-to-string.h.

364 {
365 // Produce the shortest correct representation.
366 // For example the output of 0.299999999999999988897 is (the less accurate
367 // but correct) 0.3.
368 SHORTEST,
369 // Same as SHORTEST, but for single-precision floats.
371 // Produce a fixed number of digits after the decimal point.
372 // For instance fixed(0.1, 4) becomes 0.1000
373 // If the input number is big, the output will be big.
374 FIXED,
375 // Fixed number of digits (independent of the decimal point).
377 };

◆ Flags

Enumerator
NO_FLAGS 
EMIT_POSITIVE_EXPONENT_SIGN 
EMIT_TRAILING_DECIMAL_POINT 
EMIT_TRAILING_ZERO_AFTER_POINT 
UNIQUE_ZERO 
NO_TRAILING_ZERO 
EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL 
EMIT_TRAILING_ZERO_AFTER_POINT_IN_EXPONENTIAL 

Definition at line 75 of file double-to-string.h.

Constructor & Destructor Documentation

◆ DoubleToStringConverter()

double_conversion::DoubleToStringConverter::DoubleToStringConverter ( int  flags,
const char *  infinity_symbol,
const char *  nan_symbol,
char  exponent_character,
int  decimal_in_shortest_low,
int  decimal_in_shortest_high,
int  max_leading_padding_zeroes_in_precision_mode,
int  max_trailing_padding_zeroes_in_precision_mode,
int  min_exponent_width = 0 
)
inline

Definition at line 165 of file double-to-string.h.

174 : flags_(flags),
175 infinity_symbol_(infinity_symbol),
176 nan_symbol_(nan_symbol),
177 exponent_character_(exponent_character),
178 decimal_in_shortest_low_(decimal_in_shortest_low),
179 decimal_in_shortest_high_(decimal_in_shortest_high),
180 max_leading_padding_zeroes_in_precision_mode_(
181 max_leading_padding_zeroes_in_precision_mode),
182 max_trailing_padding_zeroes_in_precision_mode_(
183 max_trailing_padding_zeroes_in_precision_mode),
184 min_exponent_width_(min_exponent_width) {
185 // When 'trailing zero after the point' is set, then 'trailing point'
186 // must be set too.
189 }
FlutterSemanticsFlag flags
#define DOUBLE_CONVERSION_ASSERT(condition)
Definition utils.h:46

Member Function Documentation

◆ DoubleToAscii()

void double_conversion::DoubleToStringConverter::DoubleToAscii ( double  v,
DtoaMode  mode,
int  requested_digits,
char *  buffer,
int  buffer_length,
bool *  sign,
int length,
int point 
)
static

Definition at line 386 of file double-to-string.cc.

393 {
394 Vector<char> vector(buffer, buffer_length);
395 DOUBLE_CONVERSION_ASSERT(!Double(v).IsSpecial());
396 DOUBLE_CONVERSION_ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0);
397
398 if (Double(v).Sign() < 0) {
399 *sign = true;
400 v = -v;
401 } else {
402 *sign = false;
403 }
404
405 if (mode == PRECISION && requested_digits == 0) {
406 vector[0] = '\0';
407 *length = 0;
408 return;
409 }
410
411 if (v == 0) {
412 vector[0] = '0';
413 vector[1] = '\0';
414 *length = 1;
415 *point = 1;
416 return;
417 }
418
419 bool fast_worked;
420 switch (mode) {
421 case SHORTEST:
422 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point);
423 break;
424 case SHORTEST_SINGLE:
425 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0,
426 vector, length, point);
427 break;
428 case FIXED:
429 fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point);
430 break;
431 case PRECISION:
432 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
433 vector, length, point);
434 break;
435 default:
436 fast_worked = false;
438 }
439 if (fast_worked) return;
440
441 // If the fast dtoa didn't succeed use the slower bignum version.
442 BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
443 BignumDtoa(v, bignum_mode, requested_digits, vector, length, point);
444 vector[*length] = '\0';
445}
static int sign(SkScalar x)
Definition SkPath.cpp:2141
static const uint8_t buffer[]
size_t length
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, Vector< char > buffer, int *length, int *decimal_point)
static BignumDtoaMode DtoaToBignumDtoaMode(DoubleToStringConverter::DtoaMode dtoa_mode)
bool FastDtoa(double v, FastDtoaMode mode, int requested_digits, Vector< char > buffer, int *length, int *decimal_point)
Definition fast-dtoa.cc:635
bool FastFixedDtoa(double v, int fractional_count, Vector< char > buffer, int *length, int *decimal_point)
#define DOUBLE_CONVERSION_UNREACHABLE()
Definition utils.h:77

◆ EcmaScriptConverter()

const DoubleToStringConverter & double_conversion::DoubleToStringConverter::EcmaScriptConverter ( )
static

Definition at line 42 of file double-to-string.cc.

42 {
45 "Infinity",
46 "NaN",
47 'e',
48 -6, 21,
49 6, 0);
50 return converter;
51}
DoubleToStringConverter(int flags, const char *infinity_symbol, const char *nan_symbol, char exponent_character, int decimal_in_shortest_low, int decimal_in_shortest_high, int max_leading_padding_zeroes_in_precision_mode, int max_trailing_padding_zeroes_in_precision_mode, int min_exponent_width=0)

◆ ToExponential()

bool double_conversion::DoubleToStringConverter::ToExponential ( double  value,
int  requested_digits,
StringBuilder result_builder 
) const

Definition at line 248 of file double-to-string.cc.

251 {
252 if (Double(value).IsSpecial()) {
253 return HandleSpecialValues(value, result_builder);
254 }
255
256 if (requested_digits < -1) return false;
257 if (requested_digits > kMaxExponentialDigits) return false;
258
259 int decimal_point;
260 bool sign;
261 // Add space for digit before the decimal point and the '\0' character.
262 const int kDecimalRepCapacity = kMaxExponentialDigits + 2;
263 DOUBLE_CONVERSION_ASSERT(kDecimalRepCapacity > kBase10MaximalLength);
264 char decimal_rep[kDecimalRepCapacity];
265#ifndef NDEBUG
266 // Problem: there is an assert in StringBuilder::AddSubstring() that
267 // will pass this buffer to strlen(), and this buffer is not generally
268 // null-terminated.
269 memset(decimal_rep, 0, sizeof(decimal_rep));
270#endif
271 int decimal_rep_length;
272
273 if (requested_digits == -1) {
274 DoubleToAscii(value, SHORTEST, 0,
275 decimal_rep, kDecimalRepCapacity,
276 &sign, &decimal_rep_length, &decimal_point);
277 } else {
278 DoubleToAscii(value, PRECISION, requested_digits + 1,
279 decimal_rep, kDecimalRepCapacity,
280 &sign, &decimal_rep_length, &decimal_point);
281 DOUBLE_CONVERSION_ASSERT(decimal_rep_length <= requested_digits + 1);
282
283 for (int i = decimal_rep_length; i < requested_digits + 1; ++i) {
284 decimal_rep[i] = '0';
285 }
286 decimal_rep_length = requested_digits + 1;
287 }
288
289 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
290 if (sign && (value != 0.0 || !unique_zero)) {
291 result_builder->AddCharacter('-');
292 }
293
294 int exponent = decimal_point - 1;
295 CreateExponentialRepresentation(decimal_rep,
296 decimal_rep_length,
297 exponent,
298 result_builder);
299 return true;
300}
static void DoubleToAscii(double v, DtoaMode mode, int requested_digits, char *buffer, int buffer_length, bool *sign, int *length, int *point)

◆ ToFixed()

bool double_conversion::DoubleToStringConverter::ToFixed ( double  value,
int  requested_digits,
StringBuilder result_builder 
) const

Definition at line 212 of file double-to-string.cc.

214 {
216 const double kFirstNonFixed = 1e60;
217
218 if (Double(value).IsSpecial()) {
219 return HandleSpecialValues(value, result_builder);
220 }
221
222 if (requested_digits > kMaxFixedDigitsAfterPoint) return false;
223 if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false;
224
225 // Find a sufficiently precise decimal representation of n.
226 int decimal_point;
227 bool sign;
228 // Add space for the '\0' byte.
229 const int kDecimalRepCapacity =
231 char decimal_rep[kDecimalRepCapacity];
232 int decimal_rep_length;
233 DoubleToAscii(value, FIXED, requested_digits,
234 decimal_rep, kDecimalRepCapacity,
235 &sign, &decimal_rep_length, &decimal_point);
236
237 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
238 if (sign && (value != 0.0 || !unique_zero)) {
239 result_builder->AddCharacter('-');
240 }
241
242 CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
243 requested_digits, result_builder);
244 return true;
245}

◆ ToPrecision()

bool double_conversion::DoubleToStringConverter::ToPrecision ( double  value,
int  precision,
StringBuilder result_builder 
) const

Definition at line 303 of file double-to-string.cc.

305 {
306 if (Double(value).IsSpecial()) {
307 return HandleSpecialValues(value, result_builder);
308 }
309
310 if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) {
311 return false;
312 }
313
314 // Find a sufficiently precise decimal representation of n.
315 int decimal_point;
316 bool sign;
317 // Add one for the terminating null character.
318 const int kDecimalRepCapacity = kMaxPrecisionDigits + 1;
319 char decimal_rep[kDecimalRepCapacity];
320 int decimal_rep_length;
321
322 DoubleToAscii(value, PRECISION, precision,
323 decimal_rep, kDecimalRepCapacity,
324 &sign, &decimal_rep_length, &decimal_point);
325 DOUBLE_CONVERSION_ASSERT(decimal_rep_length <= precision);
326
327 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
328 if (sign && (value != 0.0 || !unique_zero)) {
329 result_builder->AddCharacter('-');
330 }
331
332 // The exponent if we print the number as x.xxeyyy. That is with the
333 // decimal point after the first digit.
334 int exponent = decimal_point - 1;
335
336 int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
337 bool as_exponential =
338 (-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
339 (decimal_point - precision + extra_zero >
340 max_trailing_padding_zeroes_in_precision_mode_);
341 if ((flags_ & NO_TRAILING_ZERO) != 0) {
342 // Truncate trailing zeros that occur after the decimal point (if exponential,
343 // that is everything after the first digit).
344 int stop = as_exponential ? 1 : std::max(1, decimal_point);
345 while (decimal_rep_length > stop && decimal_rep[decimal_rep_length - 1] == '0') {
346 --decimal_rep_length;
347 }
348 // Clamp precision to avoid the code below re-adding the zeros.
349 precision = std::min(precision, decimal_rep_length);
350 }
351 if (as_exponential) {
352 // Fill buffer to contain 'precision' digits.
353 // Usually the buffer is already at the correct length, but 'DoubleToAscii'
354 // is allowed to return less characters.
355 for (int i = decimal_rep_length; i < precision; ++i) {
356 decimal_rep[i] = '0';
357 }
358
359 CreateExponentialRepresentation(decimal_rep,
360 precision,
361 exponent,
362 result_builder);
363 } else {
364 CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
365 (std::max)(0, precision - decimal_point),
366 result_builder);
367 }
368 return true;
369}

◆ ToShortest()

bool double_conversion::DoubleToStringConverter::ToShortest ( double  value,
StringBuilder result_builder 
) const
inline

Definition at line 240 of file double-to-string.h.

240 {
241 return ToShortestIeeeNumber(value, result_builder, SHORTEST);
242 }

◆ ToShortestSingle()

bool double_conversion::DoubleToStringConverter::ToShortestSingle ( float  value,
StringBuilder result_builder 
) const
inline

Definition at line 245 of file double-to-string.h.

245 {
246 return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
247 }

Member Data Documentation

◆ kBase10MaximalLength

const int double_conversion::DoubleToStringConverter::kBase10MaximalLength = 17
static

Definition at line 59 of file double-to-string.h.

◆ kBase10MaximalLengthSingle

const int double_conversion::DoubleToStringConverter::kBase10MaximalLengthSingle = 9
static

Definition at line 65 of file double-to-string.h.

◆ kMaxCharsEcmaScriptShortest

const int double_conversion::DoubleToStringConverter::kMaxCharsEcmaScriptShortest = 25
static

Definition at line 73 of file double-to-string.h.

◆ kMaxExponentialDigits

const int double_conversion::DoubleToStringConverter::kMaxExponentialDigits = 120
static

Definition at line 45 of file double-to-string.h.

◆ kMaxFixedDigitsAfterPoint

const int double_conversion::DoubleToStringConverter::kMaxFixedDigitsAfterPoint = 100
static

Definition at line 41 of file double-to-string.h.

◆ kMaxFixedDigitsBeforePoint

const int double_conversion::DoubleToStringConverter::kMaxFixedDigitsBeforePoint = 60
static

Definition at line 40 of file double-to-string.h.

◆ kMaxPrecisionDigits

const int double_conversion::DoubleToStringConverter::kMaxPrecisionDigits = 120
static

Definition at line 51 of file double-to-string.h.

◆ kMinPrecisionDigits

const int double_conversion::DoubleToStringConverter::kMinPrecisionDigits = 1
static

Definition at line 50 of file double-to-string.h.


The documentation for this class was generated from the following files: