From d4dd1b2a82542d604fb088004ef310a8a246ddc2 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Fri, 23 Jul 2004 05:51:14 +0000 Subject: [PATCH] fix CString always being ~2k after ssprintf, even for ssprintf("%i", 1) --- stepmania/src/StdString.h | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/stepmania/src/StdString.h b/stepmania/src/StdString.h index 3d8e5c676c..46d4d0bafb 100644 --- a/stepmania/src/StdString.h +++ b/stepmania/src/StdString.h @@ -939,24 +939,38 @@ public: void FormatV(const CT* szFormat, va_list argList) { #ifdef SS_ANSI + static bool bExactSizeSupported; + static bool bInitialized = false; + if( !bInitialized ) + { + /* Some systems return the actual size required when snprintf + * doesn't have enough space. This lets us avoid wasting time + * iterating, and wasting memory. */ + bInitialized = true; + char ignore; + bExactSizeSupported = ( snprintf( &ignore, 0, "Hello World" ) == 11 ); + } + + if( bExactSizeSupported ) + { + char ignore; + int iNeeded = ssvsprintf( &ignore, 0, szFormat, argList ); + char *buf = GetBuffer( iNeeded+1 ); + ssvsprintf( buf, iNeeded+1, szFormat, argList ); + ReleaseBuffer( iNeeded ); + return; + } + int nChars = FMT_BLOCK_SIZE; int nTry = 1; - do { // Grow more than linearly (e.g. 512, 1536, 3072, etc) char *buf = GetBuffer(nChars); int nUsed = ssvsprintf(buf, nChars-1, szFormat, argList); - /* -1 means "not enough, try harder"; > nChars is telling us how - * much. */ - if(nUsed > nChars) { - nChars = nUsed + 1; - ReleaseBuffer(); - continue; - } - - if(nUsed == -1) { + if(nUsed == -1) + { nChars += ((nTry+1) * FMT_BLOCK_SIZE); ReleaseBuffer(); continue;