From 36fb1f96e2ccc151013e6ca7dfbb0eb8559826a2 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 31 Jan 2020 01:19:08 +0100 Subject: [PATCH] Custom float-printing function. --- server/TracyPrint.cpp | 11 +++++------ server/TracyPrint.hpp | 8 ++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/server/TracyPrint.cpp b/server/TracyPrint.cpp index 229ca194..fde2d0a4 100644 --- a/server/TracyPrint.cpp +++ b/server/TracyPrint.cpp @@ -280,29 +280,28 @@ const char* MemSizeToString( int64_t val ) }; Unit unit; + char* ptr; if( aval < 10000ll * 1024 ) { - sprintf( buf, "%.2f", val / 1024. ); + ptr = PrintFloat( buf, buf+64, val / 1024., 2 ); unit = Unit::Kilobyte; } else if( aval < 10000ll * 1024 * 1024 ) { - sprintf( buf, "%.2f", val / ( 1024. * 1024 ) ); + ptr = PrintFloat( buf, buf+64, val / ( 1024. * 1024 ), 2 ); unit = Unit::Megabyte; } else if( aval < 10000ll * 1024 * 1024 * 1024 ) { - sprintf( buf, "%.2f", val / ( 1024. * 1024 * 1024 ) ); + ptr = PrintFloat( buf, buf+64, val / ( 1024. * 1024 * 1024 ), 2 ); unit = Unit::Gigabyte; } else { - sprintf( buf, "%.2f", val / ( 1024. * 1024 * 1024 * 1024 ) ); + ptr = PrintFloat( buf, buf+64, val / ( 1024. * 1024 * 1024 * 1024 ), 2 ); unit = Unit::Terabyte; } - auto ptr = buf; - while( *ptr ) ptr++; ptr--; while( ptr >= buf && *ptr == '0' ) ptr--; if( *ptr != '.' ) ptr++; diff --git a/server/TracyPrint.hpp b/server/TracyPrint.hpp index 5f132d02..b6150391 100644 --- a/server/TracyPrint.hpp +++ b/server/TracyPrint.hpp @@ -1,9 +1,17 @@ #ifndef __TRACYPRINT_HPP__ #define __TRACYPRINT_HPP__ +#include + namespace tracy { +template +static inline char* PrintFloat( char* begin, char* end, T value, int precision ) +{ + return begin + sprintf( begin, "%.*f", precision, value ); +} + const char* TimeToString( int64_t ns ); const char* RealToString( double val, bool separator ); const char* MemSizeToString( int64_t val );