| 1267 | } |
| 1268 | |
| 1269 | char* DoubleToBuffer(double value, char* buffer) { |
| 1270 | // DBL_DIG is 15 for IEEE-754 doubles, which are used on almost all |
| 1271 | // platforms these days. Just in case some system exists where DBL_DIG |
| 1272 | // is significantly larger -- and risks overflowing our buffer -- we have |
| 1273 | // this assert. |
| 1274 | COMPILE_ASSERT(DBL_DIG < 20, DBL_DIG_is_too_big); |
| 1275 | |
| 1276 | int snprintf_result = |
| 1277 | snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG, value); |
| 1278 | |
| 1279 | // The snprintf should never overflow because the buffer is significantly |
| 1280 | // larger than the precision we asked for. |
| 1281 | DCHECK(snprintf_result > 0 && snprintf_result < kDoubleToBufferSize); |
| 1282 | |
| 1283 | if (strtod(buffer, nullptr) != value) { |
| 1284 | snprintf_result = |
| 1285 | snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG+2, value); |
| 1286 | |
| 1287 | // Should never overflow; see above. |
| 1288 | DCHECK(snprintf_result > 0 && snprintf_result < kDoubleToBufferSize); |
| 1289 | } |
| 1290 | return buffer; |
| 1291 | } |
| 1292 | |
| 1293 | char* FloatToBuffer(float value, char* buffer) { |
| 1294 | // FLT_DIG is 6 for IEEE-754 floats, which are used on almost all |
no outgoing calls
no test coverage detected