diff --git a/include/wx/catch_cppunit.h b/include/wx/catch_cppunit.h index 22ebcf12f9..66e660b158 100644 --- a/include/wx/catch_cppunit.h +++ b/include/wx/catch_cppunit.h @@ -73,6 +73,32 @@ namespace Catch return wxString(ucr).ToStdString(wxConvUTF8); } }; + + // While this conversion already works due to the existence of the stream + // insertion operator for wxString, define a custom one making it more + // obvious when strings containing non-printable characters differ. + template <> + struct StringMaker + { + static std::string convert(const wxString& wxs) + { + std::string s; + s.reserve(wxs.length()); + for ( wxString::const_iterator i = wxs.begin(); + i != wxs.end(); + ++i ) + { +#if wxUSE_UNICODE + if ( !iswprint(*i) ) + s += wxString::Format("\\u%04X", *i).ToStdString(); + else +#endif // wxUSE_UNICODE + s += *i; + } + + return s; + } + }; } // Use a different namespace for our mock ups of the real declarations in diff --git a/include/wx/string.h b/include/wx/string.h index b80998cc44..a4e682b8ff 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1162,6 +1162,9 @@ public: wxString(const wxScopedWCharBuffer& buf) { assign(buf.data(), buf.length()); } + wxString(const wxScopedCharBuffer& buf, const wxMBConv& conv) + { assign(buf, conv); } + // NB: this version uses m_impl.c_str() to force making a copy of the // string, so that "wxString(str.c_str())" idiom for passing strings // between threads works @@ -2539,6 +2542,13 @@ public: { return assign(str.AsString()); } wxString& assign(const wxScopedCharBuffer& str) { return assign(str.data(), str.length()); } + wxString& assign(const wxScopedCharBuffer& buf, const wxMBConv& conv) + { + SubstrBufFromMB str(ImplStr(buf.data(), buf.length(), conv)); + m_impl.assign(str.data, str.len); + + return *this; + } wxString& assign(const wxScopedWCharBuffer& str) { return assign(str.data(), str.length()); } wxString& assign(const wxCStrData& str, size_t len) diff --git a/src/common/file.cpp b/src/common/file.cpp index 9da9aba608..0e976273dc 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -281,8 +281,7 @@ bool wxFile::ReadAll(wxString *str, const wxMBConv& conv) length -= nread; } - wxString strTmp(buf, conv, length); - str->swap(strTmp); + str->assign(buf, conv); return true; } diff --git a/tests/file/filetest.cpp b/tests/file/filetest.cpp index 31870164c3..fdad2d8e4a 100644 --- a/tests/file/filetest.cpp +++ b/tests/file/filetest.cpp @@ -116,6 +116,16 @@ void FileTestCase::DoRoundTripTest(const wxMBConv& conv) wxString dataReadBack(buf, conv, len); CPPUNIT_ASSERT_EQUAL( data, dataReadBack ); } + + { + wxFile fin(tf.GetName(), wxFile::read); + CPPUNIT_ASSERT( fin.IsOpened() ); + + wxString dataReadBack; + CPPUNIT_ASSERT( fin.ReadAll(&dataReadBack, conv) ); + + CPPUNIT_ASSERT_EQUAL( data, dataReadBack ); + } } #endif // wxUSE_UNICODE