diff options
| author | Glenn Rice <glennricster@gmail.com> | 2010-11-11 04:59:50 +0000 |
|---|---|---|
| committer | Glenn Rice <glennricster@gmail.com> | 2010-11-11 04:59:50 +0000 |
| commit | adbd7fbd4ac7e7ae5fe38014f6f9fa62032d0218 (patch) | |
| tree | bfa62e622d8e260bbcc5c630924accfcadcedb82 | |
| parent | 36d43365e6f079b60864d2f709afac1d9dbedbb1 (diff) | |
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
| -rw-r--r-- | Source/Core/Common/Src/StringUtil.cpp | 43 |
1 files changed, 18 insertions, 25 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); - -#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 - } + char *buf = NULL; +#ifdef _WIN32 + int required = 0; - buf[writtenCount] = '\0'; + va_start(args, format); + required = _vscprintf(format, args); + buf = new char[required + 1]; + vsnprintf(buf, required, format, args); + va_end(args); + + 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; } |
