diff --git a/docs/changes.txt b/docs/changes.txt index 77f30570b9..b4488dc104 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -72,6 +72,7 @@ All: - Add wxFileType::GetExpandedCommand() (troelsk). - Make it easier to convert to/from UTF-8-encoded std::string (ARATA Mizuki). - Add support for loading dynamic lexer in wxStyledTextCtrl (New Pagodi). +- Handle strings with embedded NULs in wxDataStream (Nitch). All (GUI): diff --git a/src/common/datstrm.cpp b/src/common/datstrm.cpp index 904e692e51..6320c91f02 100644 --- a/src/common/datstrm.cpp +++ b/src/common/datstrm.cpp @@ -187,7 +187,7 @@ wxString wxDataInputStream::ReadString() if ( tmp ) { m_input->Read(tmp.data(), len); - ret = m_conv->cMB2WX(tmp.data()); + ret = m_conv->cMB2WC(tmp.data(), len, NULL); } #else wxStringBuffer buf(ret, len); @@ -590,10 +590,11 @@ void wxDataOutputStream::WriteString(const wxString& string) { #if wxUSE_UNICODE const wxWX2MBbuf buf = string.mb_str(*m_conv); + size_t len = buf.length(); #else const wxWX2MBbuf buf = string.mb_str(); + size_t len = string.size(); #endif - size_t len = strlen(buf); Write32(len); if (len > 0) m_output->Write(buf, len); diff --git a/tests/streams/datastreamtest.cpp b/tests/streams/datastreamtest.cpp index 77c5662e06..ae6c1564ad 100644 --- a/tests/streams/datastreamtest.cpp +++ b/tests/streams/datastreamtest.cpp @@ -39,6 +39,7 @@ private: CPPUNIT_TEST_SUITE( DataStreamTestCase ); CPPUNIT_TEST( FloatRW ); CPPUNIT_TEST( DoubleRW ); + CPPUNIT_TEST( StringRW ); #if wxUSE_LONGLONG CPPUNIT_TEST( LongLongRW ); #endif @@ -66,6 +67,7 @@ private: void FloatRW(); void DoubleRW(); + void StringRW(); #if wxUSE_LONGLONG void LongLongRW(); #endif @@ -236,6 +238,19 @@ void DataStreamTestCase::DoubleRW() CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 ); } +void DataStreamTestCase::StringRW() +{ + wxString s(wxT("Test1")); + CPPUNIT_ASSERT_EQUAL( TestRW(s), s ); + + s.append(2, wxT('\0')); + s.append(wxT("Test2")); + CPPUNIT_ASSERT_EQUAL( TestRW(s), s ); + + s = wxString::FromUTF8("\xc3\xbc"); // U+00FC LATIN SMALL LETTER U WITH DIAERESIS + CPPUNIT_ASSERT_EQUAL( TestRW(s), s ); +} + #if wxUSE_LONGLONG void DataStreamTestCase::LongLongRW() {