| 1291 | } |
| 1292 | |
| 1293 | char* FloatToBuffer(float value, char* buffer) { |
| 1294 | // FLT_DIG is 6 for IEEE-754 floats, which are used on almost all |
| 1295 | // platforms these days. Just in case some system exists where FLT_DIG |
| 1296 | // is significantly larger -- and risks overflowing our buffer -- we have |
| 1297 | // this assert. |
| 1298 | COMPILE_ASSERT(FLT_DIG < 10, FLT_DIG_is_too_big); |
| 1299 | |
| 1300 | int snprintf_result = |
| 1301 | snprintf(buffer, kFloatToBufferSize, "%.*g", FLT_DIG, value); |
| 1302 | |
| 1303 | // The snprintf should never overflow because the buffer is significantly |
| 1304 | // larger than the precision we asked for. |
| 1305 | DCHECK(snprintf_result > 0 && snprintf_result < kFloatToBufferSize); |
| 1306 | |
| 1307 | float parsed_value; |
| 1308 | if (!safe_strtof(buffer, &parsed_value) || parsed_value != value) { |
| 1309 | snprintf_result = |
| 1310 | snprintf(buffer, kFloatToBufferSize, "%.*g", FLT_DIG+2, value); |
| 1311 | |
| 1312 | // Should never overflow; see above. |
| 1313 | DCHECK(snprintf_result > 0 && snprintf_result < kFloatToBufferSize); |
| 1314 | } |
| 1315 | return buffer; |
| 1316 | } |
| 1317 | |
| 1318 | // ---------------------------------------------------------------------- |
| 1319 | // SimpleItoaWithCommas() |
no test coverage detected