Merge branch 'file-readall-len'

See https://github.com/wxWidgets/wxWidgets/pull/629
This commit is contained in:
Vadim Zeitlin 2017-12-06 16:42:58 +01:00
commit f3f158403c
4 changed files with 47 additions and 2 deletions

View File

@ -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<wxString>
{
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

View File

@ -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)

View File

@ -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;
}

View File

@ -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