From 3bc0d1ed927ef4d5a45339d14e695f351638ec9c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 28 Oct 2022 19:19:05 +0100 Subject: [PATCH] Deprecate unused wxMBConv parameters in wxString functions Some wxString functions using wide strings still took wxMBConv just for consistency with the same functions taking narrow strings in ANSI build, but this doesn't really make sense any longer because the same code can't be compiled with different values of wxChar -- it is always the same thing as wchar_t now, and so we shouldn't pass unused conversion objects to these functions any more. So give deprecation warning when these functions are used (but without formally deprecating them, as it doesn't cost much to keep them) and avoid using them in the library code. --- include/wx/msw/ole/oleutils.h | 2 +- include/wx/string.h | 10 ++++++++-- src/common/regex.cpp | 7 +++---- src/common/tarstrm.cpp | 8 ++++---- src/common/txtstrm.cpp | 2 +- src/msw/graphics.cpp | 6 +++--- src/msw/ole/oleutils.cpp | 2 +- tests/strings/unicode.cpp | 4 ++-- 8 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/wx/msw/ole/oleutils.h b/include/wx/msw/ole/oleutils.h index 435e734e69..594cb59c25 100644 --- a/include/wx/msw/ole/oleutils.h +++ b/include/wx/msw/ole/oleutils.h @@ -71,7 +71,7 @@ public: // Constructs with the owned BSTR created from a wxString wxBasicString(const wxString& str) - : m_bstrBuf(SysAllocString(str.wc_str(*wxConvCurrent))) {} + : m_bstrBuf(SysAllocString(str.wc_str())) {} // Constructs with the owned BSTR as a copy of the BSTR owned by bstr wxBasicString(const wxBasicString& bstr) : m_bstrBuf(bstr.Copy()) {} diff --git a/include/wx/string.h b/include/wx/string.h index 693f3b8d49..e10b95f285 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1171,10 +1171,15 @@ public: // ctors from wchar_t* strings: wxString(const wchar_t *pwz) : m_impl(ImplStr(pwz)) {} - wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv)) - : m_impl(ImplStr(pwz)) {} wxString(const wchar_t *pwz, size_t nLength) { assign(pwz, nLength); } + + // These ctors only existed for compatibility with the ANSI build and + // shouldn't be used any longer. + wxDEPRECATED_MSG("remove the wxMBConv ctor argument") + wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv)) + : m_impl(ImplStr(pwz)) {} + wxDEPRECATED_MSG("remove the wxMBConv ctor argument") wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength) { assign(pwz, nLength); } @@ -1773,6 +1778,7 @@ public: { return AsWCharBuf(wxMBConvStrictUTF8()); } #endif // for compatibility only + wxDEPRECATED_MSG("remove the wxMBConv argument") const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const { return wc_str(); } diff --git a/src/common/regex.cpp b/src/common/regex.cpp index 799651f44b..b944aabfc8 100644 --- a/src/common/regex.cpp +++ b/src/common/regex.cpp @@ -1342,8 +1342,7 @@ int wxRegExImpl::Replace(wxString *text, } else { - textNew += wxString(textstr + matchStart + start, - wxConvUTF8, len); + textNew += wxString(textstr + matchStart + start, len); mayHaveBackrefs = true; } @@ -1369,7 +1368,7 @@ int wxRegExImpl::Replace(wxString *text, if (result.capacity() < result.length() + start + textNew.length()) result.reserve(2 * result.length()); - result.append(wxString(textstr + matchStart, wxConvUTF8, start)); + result.append(wxString(textstr + matchStart, start)); matchStart += start; result.append(textNew); @@ -1378,7 +1377,7 @@ int wxRegExImpl::Replace(wxString *text, matchStart += len; } - result.append(wxString(textstr + matchStart, wxConvUTF8)); + result.append(wxString(textstr + matchStart)); *text = result; return countRepl; diff --git a/src/common/tarstrm.cpp b/src/common/tarstrm.cpp index 73d91683a9..57e8e9a938 100644 --- a/src/common/tarstrm.cpp +++ b/src/common/tarstrm.cpp @@ -837,14 +837,14 @@ wxString wxTarInputStream::GetExtendedHeader(const wxString& key) const if (m_HeaderRecs) { it = m_HeaderRecs->find(key); if (it != m_HeaderRecs->end()) - return wxString(it->second.wc_str(wxConvUTF8), GetConv()); + return it->second; } // if not found, look at the global header records if (m_GlobalHeaderRecs) { it = m_GlobalHeaderRecs->find(key); if (it != m_GlobalHeaderRecs->end()) - return wxString(it->second.wc_str(wxConvUTF8), GetConv()); + return it->second; } return wxEmptyString; @@ -966,8 +966,8 @@ bool wxTarInputStream::ReadExtendedHeader(wxTarHeaderRecords*& recs) // replace the '=' with a nul, to terminate the key *p++ = 0; - wxString key(wxConvUTF8.cMB2WC(pKey), GetConv()); - wxString value(wxConvUTF8.cMB2WC(p), GetConv()); + wxString key = wxString::FromUTF8(pKey); + wxString value = wxString::FromUTF8(p); // an empty value unsets a previously given value if (value.empty()) diff --git a/src/common/txtstrm.cpp b/src/common/txtstrm.cpp index b002c51437..fa77354ff4 100644 --- a/src/common/txtstrm.cpp +++ b/src/common/txtstrm.cpp @@ -585,7 +585,7 @@ wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c) } } #else // SIZEOF_WCHAR_T == 4 - WriteString( wxString(&c, *m_conv, 1) ); + WriteString( wxString(&c, 1) ); #endif // SIZEOF_WCHAR_T == 2 or 4 return *this; } diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index 3a409c9fc7..3793213636 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -2340,7 +2340,7 @@ void wxGDIPlusContext::DoDrawText(const wxString& str, m_context->DrawString ( - str.wc_str(*wxConvUI), // string to draw, always Unicode + str.wc_str(), // string to draw, always Unicode -1, // length: string is NUL-terminated fontData->GetGDIPlusFont(), PointF(x, y), @@ -2354,7 +2354,7 @@ void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDo { wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") ); - wxWCharBuffer s = str.wc_str( *wxConvUI ); + wxWCharBuffer s = str.wc_str(); Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont(); // Get the font metrics if we actually need them. @@ -2428,7 +2428,7 @@ void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble return; Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont(); - wxWCharBuffer ws = text.wc_str( *wxConvUI ); + wxWCharBuffer ws = text.wc_str(); size_t len = wcslen( ws ) ; wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations")); diff --git a/src/msw/ole/oleutils.cpp b/src/msw/ole/oleutils.cpp index 275c4c1841..70cef54b73 100644 --- a/src/msw/ole/oleutils.cpp +++ b/src/msw/ole/oleutils.cpp @@ -56,7 +56,7 @@ WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr) void wxBasicString::AssignFromString(const wxString& str) { SysFreeString(m_bstrBuf); - m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent)); + m_bstrBuf = SysAllocString(str.wc_str()); } BSTR wxBasicString::Detach() diff --git a/tests/strings/unicode.cpp b/tests/strings/unicode.cpp index a1c828ba7f..5d059cadc0 100644 --- a/tests/strings/unicode.cpp +++ b/tests/strings/unicode.cpp @@ -208,7 +208,7 @@ void UnicodeTestCase::ConstructorsWithConversion() wxString s3(utf8, wxConvUTF8, 4); CPPUNIT_ASSERT_EQUAL( sub, s3 ); - wxString s4(wchar, wxConvUTF8, 3); + wxString s4(wchar, 3); CPPUNIT_ASSERT_EQUAL( sub, s4 ); // conversion should stop with failure at pos 35 @@ -248,7 +248,7 @@ void UnicodeTestCase::ConversionWithNULs() { static const size_t lenNulString = 10; - wxString szTheString(L"The\0String", wxConvLibc, lenNulString); + wxString szTheString(L"The\0String", lenNulString); wxCharBuffer theBuffer = szTheString.mb_str(wxConvLibc); CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String",