From adbd7fbd4ac7e7ae5fe38014f6f9fa62032d0218 Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Thu, 11 Nov 2010 04:59:50 +0000 Subject: [PATCH] Fix StringFromFormat hopefully for the last time. This should be more efficient than before. Thanks to shuffle2 for the windows code. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6379 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/StringUtil.cpp | 41 +++++++++++---------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/Source/Core/Common/Src/StringUtil.cpp b/Source/Core/Common/Src/StringUtil.cpp index 1abb93183f..1015c8b97d 100644 --- a/Source/Core/Common/Src/StringUtil.cpp +++ b/Source/Core/Common/Src/StringUtil.cpp @@ -53,35 +53,28 @@ bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list ar std::string StringFromFormat(const char* format, ...) { - int writtenCount = -1; - size_t newSize = strlen(format) + 5; - char *buf = NULL; va_list args; - while (writtenCount < 0) - { - delete[] buf; - buf = new char[newSize]; - - va_start(args, format); - writtenCount = vsnprintf(buf, newSize, format, args); - va_end(args); + char *buf = NULL; +#ifdef _WIN32 + int required = 0; -#ifndef _WIN32 - // vsnprintf does not return -1 on truncation in linux! - // Instead it returns the size of the string we need. - if (writtenCount > (int)newSize) - { - newSize = writtenCount + 1; - writtenCount = -1; - } -#else - newSize *= 2; -#endif - } + va_start(args, format); + required = _vscprintf(format, args); + buf = new char[required + 1]; + vsnprintf(buf, required, format, args); + va_end(args); - buf[writtenCount] = '\0'; + buf[required] = '\0'; std::string temp = buf; delete[] buf; +#else + va_start(args, format); + vasprintf(&buf, format, args); + va_end(args); + + std::string temp = buf; + free(buf); +#endif return temp; }