Use precomputed string sizes when constructing std::strings

This commit is contained in:
Martin Hořeňovský 2021-05-30 16:28:23 +02:00
parent a01073d871
commit 0e2895934c
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
2 changed files with 6 additions and 4 deletions

View File

@ -69,11 +69,13 @@ namespace Catch {
// Save previous errno, to prevent sprintf from overwriting it // Save previous errno, to prevent sprintf from overwriting it
ErrnoGuard guard; ErrnoGuard guard;
#ifdef _MSC_VER #ifdef _MSC_VER
sprintf_s( buffer, "%.3f", duration ); size_t printedLength = static_cast<size_t>(
sprintf_s( buffer, "%.3f", duration ) );
#else #else
std::snprintf( buffer, maxDoubleSize, "%.3f", duration ); size_t printedLength = static_cast<size_t>(
std::snprintf( buffer, maxDoubleSize, "%.3f", duration ) );
#endif #endif
return std::string( buffer ); return std::string( buffer, printedLength );
} }
bool shouldShowDuration( IConfig const& config, double duration ) { bool shouldShowDuration( IConfig const& config, double duration ) {

View File

@ -38,7 +38,7 @@ namespace Catch {
std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); std::strftime(timeStamp, timeStampSize, fmt, &timeInfo);
return std::string(timeStamp); return std::string(timeStamp, timeStampSize - 1);
} }
std::string fileNameTag(std::vector<Tag> const& tags) { std::string fileNameTag(std::vector<Tag> const& tags) {