From de2589be08dc62a6dc026edb4bb113bccfb8ac5a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 30 Nov 2005 14:49:47 +0000 Subject: [PATCH] avoid multiple reallocations in wxString::PrintfV() if vsnprintf() returns the total required size git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@36303 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/string.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/common/string.cpp b/src/common/string.cpp index c2abb44c19..5bb461cb17 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1813,7 +1813,7 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) for ( ;; ) { wxStringBuffer tmp(*this, size + 1); - wxChar* buf = tmp; + wxChar *buf = tmp; if ( !buf ) { @@ -1836,14 +1836,20 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) // vsnprintf() may return either -1 (traditional Unix behaviour) or the // total number of characters which would have been written if the // buffer were large enough (newer standards such as Unix98) - if ( len >= 0 && len <= size ) + if ( len < 0 ) + { + // still not enough, as we don't know how much we need, double the + // current size of the buffer + size *= 2; + } + else if ( len > size ) + { + size = len; + } + else // ok, there was enough space { - // ok, there was enough space break; } - - // still not enough, double it again - size *= 2; } // we could have overshot -- 2.45.2