Use std::to_chars() in RealToString().

This commit is contained in:
Bartosz Taudul 2020-01-31 01:44:38 +01:00
parent f017b27ae2
commit 1475635382
2 changed files with 15 additions and 5 deletions

View File

@ -222,26 +222,26 @@ const char* RealToString( double val )
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
sprintf( buf, "%f", val );
*PrintFloat( buf, buf+64, val ) = '\0';
auto ptr = buf;
if( *ptr == '-' ) ptr++;
const auto vbegin = ptr;
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
while( *ptr != '\0' && *ptr != '.' ) ptr++;
auto end = ptr;
while( *end != '\0' ) end++;
auto sz = end - ptr;
auto sz = end - ptr + 1;
while( ptr - vbegin > 3 )
{
ptr -= 3;
memmove( ptr+1, ptr, sz );
memmove( ptr+1, ptr, sz+3 );
*ptr = ',';
sz += 4;
}
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
while( *ptr != '\0' && *ptr != '.' ) ptr++;
if( *ptr == '\0' ) return buf;
while( *ptr != '\0' ) ptr++;

View File

@ -20,6 +20,16 @@ static inline char* PrintFloat( char* begin, char* end, T value, int precision )
#endif
}
template<typename T>
static inline char* PrintFloat( char* begin, char* end, T value )
{
#if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L
return std::to_chars( begin, end, value, std::chars_format::fixed ).ptr;
#else
return begin + sprintf( begin, "%f", value );
#endif
}
const char* TimeToString( int64_t ns );
const char* RealToString( double val );
const char* MemSizeToString( int64_t val );