Use length already stored in wxCharBuffer

Resolve issues pointed out by a couple of FIXME comments speaking about
wxCharBufferWithLength class: we don't have such class, but wxCharBuffer
itself does store the length, so we can use it instead of unnecessarily
calling strlen().

Simply remove another occurrence of the same comment, as we really have
to use the length returned by cWC2MB() there in order to handle strings
with embedded NULs correctly.

No real changes.
This commit is contained in:
Vadim Zeitlin 2022-10-28 20:20:32 +01:00
parent d9efd6a4ee
commit 8ef897bbc8
3 changed files with 11 additions and 13 deletions

View file

@ -35,14 +35,14 @@ protected:
virtual size_t OnSysRead(void *buffer, size_t size) override;
private:
// Return the length of the string in bytes.
size_t GetBufferSize() const { return m_buf.length(); }
// the string that was passed in the ctor
wxString m_str;
// the buffer we're reading from
wxCharBuffer m_buf;
// length of the buffer we're reading from
size_t m_len;
const wxCharBuffer m_buf;
// position in the stream in bytes, *not* in chars
size_t m_pos;

View file

@ -32,11 +32,8 @@
// construction/destruction
// ----------------------------------------------------------------------------
// TODO: Do we want to include the null char in the stream? If so then
// just add +1 to m_len in the ctor
wxStringInputStream::wxStringInputStream(const wxString& s)
// FIXME-UTF8: use wxCharBufferWithLength if we have it
: m_str(s), m_buf(s.utf8_str()), m_len(strlen(m_buf))
: m_str(s), m_buf(s.utf8_str())
{
wxASSERT_MSG(m_buf.data() != nullptr, wxT("Could not convert string to UTF8!"));
m_pos = 0;
@ -48,7 +45,7 @@ wxStringInputStream::wxStringInputStream(const wxString& s)
wxFileOffset wxStringInputStream::GetLength() const
{
return m_len;
return GetBufferSize();
}
// ----------------------------------------------------------------------------
@ -64,7 +61,7 @@ wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode)
break;
case wxFromEnd:
ofs += m_len;
ofs += GetBufferSize();
break;
case wxFromCurrent:
@ -76,7 +73,7 @@ wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode)
return wxInvalidOffset;
}
if ( ofs < 0 || ofs > static_cast<wxFileOffset>(m_len) )
if ( ofs < 0 || ofs > static_cast<wxFileOffset>(GetBufferSize()) )
return wxInvalidOffset;
// FIXME: this can't be right
@ -96,7 +93,7 @@ wxFileOffset wxStringInputStream::OnSysTell() const
size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
{
const size_t sizeMax = m_len - m_pos;
const size_t sizeMax = GetBufferSize() - m_pos;
if ( size >= sizeMax )
{

View file

@ -520,7 +520,8 @@ void wxTextOutputStream::WriteString(const wxString& string)
out << c;
}
// FIXME-UTF8: use wxCharBufferWithLength if/when we have it
// Note that we have to use the length returned by cWC2MB() here to handle
// string with the embedded NULs correctly.
wxCharBuffer buffer = m_conv->cWC2MB(out.wc_str(), out.length(), &len);
m_output.Write(buffer, len);
}