Use internal linkage for float stringification helper

This commit is contained in:
Martin Hořeňovský 2020-05-10 06:54:19 +02:00
parent cb25c4a8a3
commit a595066ff9
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -41,8 +41,28 @@ namespace Detail {
return value ? Little : Big; return value ? Little : Big;
} }
}; };
template<typename T>
std::string fpToString(T value, int precision) {
if (Catch::isnan(value)) {
return "nan";
} }
ReusableStringStream rss;
rss << std::setprecision(precision)
<< std::fixed
<< value;
std::string d = rss.str();
std::size_t i = d.find_last_not_of('0');
if (i != std::string::npos && i != d.size() - 1) {
if (d[i] == '.')
i++;
d = d.substr(0, i + 1);
}
return d;
}
} // end unnamed namespace
std::string rawMemoryToString( const void *object, std::size_t size ) { std::string rawMemoryToString( const void *object, std::size_t size ) {
// Reverse order for little endian architectures // Reverse order for little endian architectures
int i = 0, end = static_cast<int>( size ), inc = 1; int i = 0, end = static_cast<int>( size ), inc = 1;
@ -58,29 +78,9 @@ namespace Detail {
rss << std::setw(2) << static_cast<unsigned>(bytes[i]); rss << std::setw(2) << static_cast<unsigned>(bytes[i]);
return rss.str(); return rss.str();
} }
} } // end Detail namespace
template<typename T>
std::string fpToString( T value, int precision ) {
if (Catch::isnan(value)) {
return "nan";
}
ReusableStringStream rss;
rss << std::setprecision( precision )
<< std::fixed
<< value;
std::string d = rss.str();
std::size_t i = d.find_last_not_of( '0' );
if( i != std::string::npos && i != d.size()-1 ) {
if( d[i] == '.' )
i++;
d = d.substr( 0, i+1 );
}
return d;
}
//// ======================================================= //// //// ======================================================= ////
// //
@ -228,13 +228,13 @@ std::string StringMaker<unsigned char>::convert(unsigned char c) {
int StringMaker<float>::precision = 5; int StringMaker<float>::precision = 5;
std::string StringMaker<float>::convert(float value) { std::string StringMaker<float>::convert(float value) {
return fpToString(value, precision) + 'f'; return Detail::fpToString(value, precision) + 'f';
} }
int StringMaker<double>::precision = 10; int StringMaker<double>::precision = 10;
std::string StringMaker<double>::convert(double value) { std::string StringMaker<double>::convert(double value) {
return fpToString(value, precision); return Detail::fpToString(value, precision);
} }
} // end namespace Catch } // end namespace Catch