Merge branch 'strstream-pos-fix'

See https://github.com/wxWidgets/wxWidgets/pull/580

Closes #17985.
This commit is contained in:
Vadim Zeitlin
2017-11-03 17:24:35 +01:00
4 changed files with 49 additions and 11 deletions

View File

@@ -62,16 +62,8 @@ public:
//
// Note that the conversion object should have the life time greater than
// this stream.
wxStringOutputStream(wxString *pString = NULL,
wxMBConv& conv = wxConvUTF8)
: m_conv(conv)
#if wxUSE_UNICODE
, m_unconv(0)
#endif // wxUSE_UNICODE
{
m_str = pString ? pString : &m_strInternal;
m_pos = m_str->length() / sizeof(wxChar);
}
explicit wxStringOutputStream(wxString *pString = NULL,
wxMBConv& conv = wxConvUTF8);
// get the string containing current output
const wxString& GetString() const { return *m_str; }

View File

@@ -62,7 +62,7 @@ public:
with default value of this argument the data written to the stream must
be valid UTF-8, pass @c wxConvISO8859_1 to deal with arbitrary 8 bit data.
*/
wxStringOutputStream(wxString* pString = 0, wxMBConv& conv = wxConvUTF8);
explicit wxStringOutputStream(wxString* pString = NULL, wxMBConv& conv = wxConvUTF8);
/**
Returns the string containing all the data written to the stream so far.

View File

@@ -128,6 +128,23 @@ size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
// wxStringOutputStream implementation
// ============================================================================
wxStringOutputStream::wxStringOutputStream(wxString *pString, wxMBConv& conv)
: m_conv(conv)
#if wxUSE_UNICODE
, m_unconv(0)
#endif // wxUSE_UNICODE
{
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();
#else // !wxUSE_UNICODE
m_pos = m_str->length();
#endif // wxUSE_UNICODE/!wxUSE_UNICODE
}
// ----------------------------------------------------------------------------
// seek/tell
// ----------------------------------------------------------------------------

View File

@@ -119,3 +119,32 @@ void strStream::Output_Check()
// Register the stream sub suite, by using some stream helper macro.
STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(strStream)
TEST_CASE("wxStringOutputStream::Tell", "[stream]")
{
wxStringOutputStream ss;
CHECK( ss.TellO() == 0 );
const char* const s = "Hello world";
const wxFileOffset len = strlen(s);
ss.Write(s, len);
CHECK( ss.TellO() == len );
wxString str(s);
CHECK( wxStringOutputStream(&str).TellO() == len );
wxMBConvUTF16 convUTF16;
wxStringOutputStream ss16(NULL, convUTF16);
CHECK( ss16.TellO() == 0 );
const wxCharBuffer s16 = convUTF16.cWC2MB(wxWCharBuffer(str.wc_str()));
ss16.Write(s16, s16.length());
CHECK( ss16.TellO() == 2*len );
CHECK( wxStringOutputStream(&str, convUTF16).TellO() == 2*len );
// The U+2070D character is represented by a surrogate pair in UTF-16.
wxString u2070D = wxString::FromUTF8("\xF0\xA0\x9C\x8D");
CHECK( wxStringOutputStream(&u2070D, convUTF16).TellO() == 4 );
}