From e4ef491fdf160c0b92debaeb589b0f900be4293c Mon Sep 17 00:00:00 2001 From: Dedmen Miller Date: Thu, 7 Feb 2019 13:14:31 +0100 Subject: [PATCH 1/3] Cleaner TimeToString --- server/TracyView.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 310e53bc..e35b41e2 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -47,48 +47,41 @@ static const char* TimeToString( int64_t ns ) enum { Pool = 8 }; static char bufpool[Pool][64]; static int bufsel = 0; - char* buf = bufpool[bufsel]; + char* buf = bufpool[bufsel] + 1; bufsel = ( bufsel + 1 ) % Pool; - const char* sign = ""; - if( ns < 0 ) - { - sign = "-"; - ns = -ns; - } - if( ns < 1000 ) { - sprintf( buf, "%s%" PRIi64 " ns", sign, ns ); + sprintf( buf, "%" PRIi64 " ns", ns ); } else if( ns < 1000ll * 1000 ) { #ifdef TRACY_EXTENDED_FONT - sprintf( buf, "%s%.2f \xce\xbcs", sign, ns / 1000. ); + sprintf( buf, "%.2f \xce\xbcs", ns / 1000. ); #else - sprintf( buf, "%s%.2f us", sign, ns / 1000. ); + sprintf( buf, "%.2f us", ns / 1000. ); #endif } else if( ns < 1000ll * 1000 * 1000 ) { - sprintf( buf, "%s%.2f ms", sign, ns / ( 1000. * 1000. ) ); + sprintf( buf, "%.2f ms", ns / ( 1000. * 1000. ) ); } else if( ns < 1000ll * 1000 * 1000 * 60 ) { - sprintf( buf, "%s%.2f s", sign, ns / ( 1000. * 1000. * 1000. ) ); + sprintf( buf, "%.2f s", ns / ( 1000. * 1000. * 1000. ) ); } else if( ns < 1000ll * 1000 * 1000 * 60 * 60 ) { const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) ); const auto s = int64_t( ns - m * ( 1000ll * 1000 * 1000 * 60 ) ) / ( 1000. * 1000. * 1000. ); - sprintf( buf, "%s%" PRIi64 ":%04.1f", sign, m, s ); + sprintf( buf, "%" PRIi64 ":%04.1f", m, s ); } else if( ns < 1000ll * 1000 * 1000 * 60 * 60 * 24 ) { const auto h = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 ) ); const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) - h * 60 ); const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - h * ( 60 * 60 ) - m * 60 ); - sprintf( buf, "%s%" PRIi64 ":%02" PRIi64 ":%02" PRIi64, sign, h, m, s ); + sprintf( buf, "%" PRIi64 ":%02" PRIi64 ":%02" PRIi64, h, m, s ); } else { @@ -96,8 +89,15 @@ static const char* TimeToString( int64_t ns ) const auto h = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 ) - d * 24 ); const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) - d * ( 60 * 24 ) - h * 60 ); const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - d * ( 60 * 60 * 24 ) - h * ( 60 * 60 ) - m * 60 ); - sprintf( buf, "%s%" PRIi64 "d%02" PRIi64 ":%02" PRIi64 ":%02" PRIi64, sign, d, h, m, s ); + sprintf( buf, "%" PRIi64 "d%02" PRIi64 ":%02" PRIi64 ":%02" PRIi64, d, h, m, s ); } + + if( ns < 0 ) + { + buf[-1] = '-'; + return buf - 1; + } + return buf; } From bfa5386bbe792ba91ceec91e056c36e72c51f94f Mon Sep 17 00:00:00 2001 From: Dedmen Miller Date: Thu, 7 Feb 2019 14:36:31 +0100 Subject: [PATCH 2/3] Cleanup --- server/TracyView.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/server/TracyView.cpp b/server/TracyView.cpp index e35b41e2..7a0aa813 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -47,9 +47,16 @@ static const char* TimeToString( int64_t ns ) enum { Pool = 8 }; static char bufpool[Pool][64]; static int bufsel = 0; - char* buf = bufpool[bufsel] + 1; + char* buf = bufpool[bufsel]; bufsel = ( bufsel + 1 ) % Pool; + if( ns < 0 ) + { + *buf = '-'; + buf++; + ns = -ns; + } + if( ns < 1000 ) { sprintf( buf, "%" PRIi64 " ns", ns ); @@ -91,13 +98,6 @@ static const char* TimeToString( int64_t ns ) const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - d * ( 60 * 60 * 24 ) - h * ( 60 * 60 ) - m * 60 ); sprintf( buf, "%" PRIi64 "d%02" PRIi64 ":%02" PRIi64 ":%02" PRIi64, d, h, m, s ); } - - if( ns < 0 ) - { - buf[-1] = '-'; - return buf - 1; - } - return buf; } From 7361d696c5b27a639f894d892299aceff6d00277 Mon Sep 17 00:00:00 2001 From: Dedmen Miller Date: Thu, 7 Feb 2019 14:38:42 +0100 Subject: [PATCH 3/3] Return proper buf --- server/TracyView.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 7a0aa813..b8675abf 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -48,6 +48,7 @@ static const char* TimeToString( int64_t ns ) static char bufpool[Pool][64]; static int bufsel = 0; char* buf = bufpool[bufsel]; + char* bufstart = buf; bufsel = ( bufsel + 1 ) % Pool; if( ns < 0 ) @@ -98,7 +99,7 @@ static const char* TimeToString( int64_t ns ) const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - d * ( 60 * 60 * 24 ) - h * ( 60 * 60 ) - m * 60 ); sprintf( buf, "%" PRIi64 "d%02" PRIi64 ":%02" PRIi64 ":%02" PRIi64, d, h, m, s ); } - return buf; + return bufstart; } static const char* TimeToStringInteger( int64_t ns )