| 359 | } |
| 360 | |
| 361 | size_t FloatToBuffer(float value, char* buffer) { |
| 362 | // FLT_DIG is 6 for IEEE-754 floats, which are used on almost all |
| 363 | // platforms these days. Just in case some system exists where FLT_DIG |
| 364 | // is significantly larger -- and risks overflowing our buffer -- we have |
| 365 | // this assert. |
| 366 | static_assert(FLT_DIG < 10, "FLT_DIG is too big"); |
| 367 | |
| 368 | int snprintf_result = |
| 369 | snprintf(buffer, kFastToBufferSize, "%.*g", FLT_DIG, value); |
| 370 | |
| 371 | // The snprintf should never overflow because the buffer is significantly |
| 372 | // larger than the precision we asked for. |
| 373 | DCHECK(snprintf_result > 0 && snprintf_result < kFastToBufferSize); |
| 374 | |
| 375 | float parsed_value; |
| 376 | if (!safe_strtof(buffer, &parsed_value) || parsed_value != value) { |
| 377 | snprintf_result = |
| 378 | snprintf(buffer, kFastToBufferSize, "%.*g", FLT_DIG + 3, value); |
| 379 | |
| 380 | // Should never overflow; see above. |
| 381 | DCHECK(snprintf_result > 0 && snprintf_result < kFastToBufferSize); |
| 382 | } |
| 383 | return snprintf_result; |
| 384 | } |
| 385 | |
| 386 | string FpToString(Fprint fp) { |
| 387 | char buf[17]; |
no test coverage detected