From a6bb7a282d72006cea36444b57f38c708fcd3a33 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Mon, 30 Mar 2009 11:54:41 +0000 Subject: [PATCH] fixed size of buffer returned by wxFormatConverter (it was too large before) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59939 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/buffer.h | 11 +++++++++++ interface/wx/buffer.h | 19 +++++++++++++++++++ src/common/strvararg.cpp | 10 ++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 66450e77f7..f3c4e34f43 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -330,6 +330,17 @@ public: return true; } + + void shrink(size_t len) + { + wxASSERT_MSG( this->m_data->m_owned, "cannot shrink non-owned buffer" ); + wxASSERT_MSG( this->m_data->m_ref == 1, "can't shrink shared buffer" ); + + wxASSERT( len <= this->length() ); + + this->m_data->m_length = len; + this->data()[len] = 0; + } }; WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer ) diff --git a/interface/wx/buffer.h b/interface/wx/buffer.h index 3d5287b6c7..b6052f47d9 100644 --- a/interface/wx/buffer.h +++ b/interface/wx/buffer.h @@ -200,8 +200,27 @@ public: Can only be called on buffers that don't share data with another buffer (i.e. reference count of the data is 1). + + @see shrink() */ bool extend(size_t len); + + /** + Shrinks the buffer to have size @a len and NUL-terminates the string + at this length. + + Can only be called on buffers that don't share data with another + buffer (i.e. reference count of the data is 1). + + @param len Length to shrink to. Must not be larger than current length. + + @note The string is not reallocated to take less memory. + + @since 2.9.0 + + @see extend() + */ + bool shrink(size_t len); }; /** diff --git a/src/common/strvararg.cpp b/src/common/strvararg.cpp index e89bdf35b5..e71c060b10 100644 --- a/src/common/strvararg.cpp +++ b/src/common/strvararg.cpp @@ -152,7 +152,7 @@ public: m_nCopied = 0; } - wxCharTypeBuffer Convert(const CharType *format) + wxScopedCharTypeBuffer Convert(const CharType *format) { // this is reset to NULL if we modify the format string m_fmtOrig = format; @@ -263,12 +263,14 @@ public: // format if ( m_fmtOrig ) { - return wxCharTypeBuffer::CreateNonOwned(m_fmtOrig); + return wxScopedCharTypeBuffer::CreateNonOwned(m_fmtOrig); } else { - // NULL-terminate converted format string: - *m_fmtLast = 0; + // shrink converted format string to actual size (instead of + // over-sized allocation from CopyAllBefore()) and NUL-terminate + // it: + m_fmt.shrink(m_fmtLast - m_fmt.data()); return m_fmt; } } -- 2.47.2