From 84b2ba40ee47cca1b3b0dca9537278b469d490ce Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 3 Nov 2017 18:47:06 +0100 Subject: [PATCH] Fix wrong wxStringOutputStream optimization for UTF-8 build We can only use utf8_length() if the conversion used with this object uses UTF-8 too, otherwise we still need to do the conversion to find out how many bytes does the string content take in the given encoding. See #17985. --- src/common/sstream.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/common/sstream.cpp b/src/common/sstream.cpp index a67cd23152..2ab8557bbc 100644 --- a/src/common/sstream.cpp +++ b/src/common/sstream.cpp @@ -136,10 +136,16 @@ wxStringOutputStream::wxStringOutputStream(wxString *pString, wxMBConv& conv) { m_str = pString ? pString : &m_strInternal; -#if wxUSE_UNICODE_WCHAR - m_pos = m_conv.FromWChar(NULL, 0, m_str->wc_str(), m_str->length()); -#elif wxUSE_UNICODE_UTF8 - m_pos = m_str->utf8_length(); +#if wxUSE_UNICODE + // We can avoid doing the conversion in the common case of using UTF-8 + // conversion in UTF-8 build, as it is exactly the same as the string + // length anyhow in this case. +#if wxUSE_UNICODE_UTF8 + if ( conv.IsUTF8() ) + m_pos = m_str->utf8_length(); + else +#endif // wxUSE_UNICODE_UTF8 + m_pos = m_conv.FromWChar(NULL, 0, m_str->wc_str(), m_str->length()); #else // !wxUSE_UNICODE m_pos = m_str->length(); #endif // wxUSE_UNICODE/!wxUSE_UNICODE