Because formatting using c++ streams is stateful, drop down to C is required Alternatively we could use stringstream, but its performance is... not good.
| 18281 | // Because formatting using c++ streams is stateful, drop down to C is required |
| 18282 | // Alternatively we could use stringstream, but its performance is... not good. |
| 18283 | std::string getFormattedDuration(double duration) |
| 18284 | { |
| 18285 | // Max exponent + 1 is required to represent the whole part |
| 18286 | // + 1 for decimal point |
| 18287 | // + 3 for the 3 decimal places |
| 18288 | // + 1 for null terminator |
| 18289 | const std::size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1; |
| 18290 | char buffer[maxDoubleSize]; |
| 18291 | |
| 18292 | // Save previous errno, to prevent sprintf from overwriting it |
| 18293 | ErrnoGuard guard; |
| 18294 | #ifdef _MSC_VER |
| 18295 | sprintf_s(buffer, "%.3f", duration); |
| 18296 | #else |
| 18297 | std::sprintf(buffer, "%.3f", duration); |
| 18298 | #endif |
| 18299 | return std::string(buffer); |
| 18300 | } |
| 18301 | |
| 18302 | bool shouldShowDuration(IConfig const &config, double duration) |
| 18303 | { |