Fix wxMBConv::cWC2MB() and cMB2WC() returned buffer length
This commit refactors the overloads of cMB2WC() and cWC2MB() methods taking raw pointers and buffers to reuse the same code and fixes the wrong length of the buffer returned by cWC2MB(wchar_t*) overload for conversions using multiple bytes to represent the NUL terminator character (it previously was wrong for UTF-16 and UTF-32 conversions due to wrongly subtracting 1 from the length when creating it instead of correctly subtracting GetMBNulLen()) and the wrong length of the buffer returned from cMB2WC(char*) overload where no adjustment for the trailing NUL was done at all. Also return simple default-constructed buffers from these methods in case of failure instead of using wxScopedCharBuffer::CreateNonOwned() which is less obvious and less efficient (even if the latter probably doesn't matter here because it's only done in case of an error). Finally, add tests checking that using WC2MB() or either of cWC2MB() overloads returns the buffers of the same length and with the same contents.
This commit is contained in:
@@ -74,11 +74,15 @@ public:
|
||||
// Convenience functions for translating NUL-terminated strings: return
|
||||
// the buffer containing the converted string or empty buffer if the
|
||||
// conversion failed.
|
||||
wxWCharBuffer cMB2WC(const char *in) const;
|
||||
wxCharBuffer cWC2MB(const wchar_t *in) const;
|
||||
wxWCharBuffer cMB2WC(const char *in) const
|
||||
{ return DoConvertMB2WC(in, wxNO_LEN); }
|
||||
wxCharBuffer cWC2MB(const wchar_t *in) const
|
||||
{ return DoConvertWC2MB(in, wxNO_LEN); }
|
||||
|
||||
wxWCharBuffer cMB2WC(const wxScopedCharBuffer& in) const;
|
||||
wxCharBuffer cWC2MB(const wxScopedWCharBuffer& in) const;
|
||||
wxWCharBuffer cMB2WC(const wxScopedCharBuffer& in) const
|
||||
{ return DoConvertMB2WC(in, in.length()); }
|
||||
wxCharBuffer cWC2MB(const wxScopedWCharBuffer& in) const
|
||||
{ return DoConvertWC2MB(in, in.length()); }
|
||||
|
||||
|
||||
// Convenience functions for converting strings which may contain embedded
|
||||
@@ -161,6 +165,11 @@ public:
|
||||
|
||||
// virtual dtor for any base class
|
||||
virtual ~wxMBConv() { }
|
||||
|
||||
private:
|
||||
// Common part of single argument cWC2MB() and cMB2WC() overloads above.
|
||||
wxCharBuffer DoConvertWC2MB(const wchar_t* pwz, size_t srcLen) const;
|
||||
wxWCharBuffer DoConvertMB2WC(const char* psz, size_t srcLen) const;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user