fixes or embedded NULs handling in wxString

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59940 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2009-03-30 15:35:47 +00:00
parent a6bb7a282d
commit 38d26d6040
2 changed files with 39 additions and 34 deletions

View File

@@ -1277,9 +1277,9 @@ public:
{ assign(pwz, nLength); } { assign(pwz, nLength); }
wxString(const wxScopedCharBuffer& buf) wxString(const wxScopedCharBuffer& buf)
{ assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length { assign(buf.data(), buf.length()); }
wxString(const wxScopedWCharBuffer& buf) wxString(const wxScopedWCharBuffer& buf)
{ assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length { assign(buf.data(), buf.length()); }
// NB: this version uses m_impl.c_str() to force making a copy of the // 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 // string, so that "wxString(str.c_str())" idiom for passing strings
@@ -1341,8 +1341,10 @@ public:
#else #else
// wxStringImpl is either not std::string or needs conversion // wxStringImpl is either not std::string or needs conversion
operator wxStdWideString() const operator wxStdWideString() const
// FIXME-UTF8: broken for embedded NULs {
{ return wxStdWideString(wc_str()); } wxScopedWCharBuffer buf(wc_str());
return wxStdWideString(buf.data(), buf.length());
}
#endif #endif
#if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
@@ -1351,8 +1353,10 @@ public:
#else #else
// wxStringImpl is either not std::string or needs conversion // wxStringImpl is either not std::string or needs conversion
operator std::string() const operator std::string() const
// FIXME-UTF8: broken for embedded NULs {
{ return std::string(mb_str()); } wxScopedCharBuffer buf(mb_str());
return std::string(buf.data(), buf.length());
}
#endif #endif
#endif // wxUSE_STL #endif // wxUSE_STL
@@ -1921,10 +1925,10 @@ public:
// from wxScopedWCharBuffer // from wxScopedWCharBuffer
wxString& operator=(const wxScopedWCharBuffer& s) wxString& operator=(const wxScopedWCharBuffer& s)
{ return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs { return assign(s); }
// from wxScopedCharBuffer // from wxScopedCharBuffer
wxString& operator=(const wxScopedCharBuffer& s) wxString& operator=(const wxScopedCharBuffer& s)
{ return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs { return assign(s); }
// string concatenation // string concatenation
// in place concatenation // in place concatenation
@@ -1960,9 +1964,9 @@ public:
// string += buffer (i.e. from wxGetString) // string += buffer (i.e. from wxGetString)
wxString& operator<<(const wxScopedWCharBuffer& s) wxString& operator<<(const wxScopedWCharBuffer& s)
{ return operator<<((const wchar_t *)s); } { return append(s); }
wxString& operator<<(const wxScopedCharBuffer& s) wxString& operator<<(const wxScopedCharBuffer& s)
{ return operator<<((const char *)s); } { return append(s); }
// string += C string // string += C string
wxString& Append(const wxString& s) wxString& Append(const wxString& s)
@@ -2464,9 +2468,9 @@ public:
wxString& append(const wxCStrData& str) wxString& append(const wxCStrData& str)
{ return append(str.AsString()); } { return append(str.AsString()); }
wxString& append(const wxScopedCharBuffer& str) wxString& append(const wxScopedCharBuffer& str)
{ return append(str.data()); } { return append(str.data(), str.length()); }
wxString& append(const wxScopedWCharBuffer& str) wxString& append(const wxScopedWCharBuffer& str)
{ return append(str.data()); } { return append(str.data(), str.length()); }
wxString& append(const wxCStrData& str, size_t n) wxString& append(const wxCStrData& str, size_t n)
{ return append(str.AsString(), 0, n); } { return append(str.AsString(), 0, n); }
wxString& append(const wxScopedCharBuffer& str, size_t n) wxString& append(const wxScopedCharBuffer& str, size_t n)
@@ -2596,9 +2600,9 @@ public:
wxString& assign(const wxCStrData& str) wxString& assign(const wxCStrData& str)
{ return assign(str.AsString()); } { return assign(str.AsString()); }
wxString& assign(const wxScopedCharBuffer& str) wxString& assign(const wxScopedCharBuffer& str)
{ return assign(str.data()); } { return assign(str.data(), str.length()); }
wxString& assign(const wxScopedWCharBuffer& str) wxString& assign(const wxScopedWCharBuffer& str)
{ return assign(str.data()); } { return assign(str.data(), str.length()); }
wxString& assign(const wxCStrData& str, size_t len) wxString& assign(const wxCStrData& str, size_t len)
{ return assign(str.AsString(), len); } { return assign(str.AsString(), len); }
wxString& assign(const wxScopedCharBuffer& str, size_t len) wxString& assign(const wxScopedCharBuffer& str, size_t len)
@@ -3352,9 +3356,9 @@ public:
return *this; return *this;
} }
wxString& operator+=(const wxScopedCharBuffer& s) wxString& operator+=(const wxScopedCharBuffer& s)
{ return operator+=(s.data()); } { return append(s); }
wxString& operator+=(const wxScopedWCharBuffer& s) wxString& operator+=(const wxScopedWCharBuffer& s)
{ return operator+=(s.data()); } { return append(s); }
// string += char // string += char
wxString& operator+=(wxUniChar ch) wxString& operator+=(wxUniChar ch)
{ {

View File

@@ -499,7 +499,7 @@ wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength,
// we must pass the real string length to SubstrBufFromMB ctor // we must pass the real string length to SubstrBufFromMB ctor
if ( nLength == npos ) if ( nLength == npos )
nLength = psz ? strlen(psz) : 0; nLength = psz ? strlen(psz) : 0;
return SubstrBufFromMB(wxCharBuffer::CreateNonOwned(psz, nLength), return SubstrBufFromMB(wxScopedCharBuffer::CreateNonOwned(psz, nLength),
nLength); nLength);
} }
// else: do the roundtrip through wchar_t* // else: do the roundtrip through wchar_t*
@@ -550,17 +550,25 @@ wxString::SubstrBufFromWC wxString::ConvertStr(const wchar_t *pwz, size_t nLengt
//Convert wxString in Unicode mode to a multi-byte string //Convert wxString in Unicode mode to a multi-byte string
const wxScopedCharBuffer wxString::mb_str(const wxMBConv& conv) const const wxScopedCharBuffer wxString::mb_str(const wxMBConv& conv) const
{ {
return conv.cWC2MB(wx_str(), length() + 1 /* size, not length */, NULL); // NB: Length passed to cWC2MB() doesn't include terminating NUL, it's
// added by it automatically. If we passed length()+1 here, it would
// create a buffer with 2 trailing NULs of length one greater than
// expected.
return conv.cWC2MB(wx_str(), length(), NULL);
} }
#elif wxUSE_UNICODE_UTF8 #elif wxUSE_UNICODE_UTF8
const wxScopedWCharBuffer wxString::wc_str() const const wxScopedWCharBuffer wxString::wc_str() const
{ {
// NB: Length passed to cMB2WC() doesn't include terminating NUL, it's
// added by it automatically. If we passed length()+1 here, it would
// create a buffer with 2 trailing NULs of length one greater than
// expected.
return wxMBConvStrictUTF8().cMB2WC return wxMBConvStrictUTF8().cMB2WC
( (
m_impl.c_str(), m_impl.c_str(),
m_impl.length() + 1, // size, not length m_impl.length(),
NULL NULL
); );
} }
@@ -570,22 +578,11 @@ const wxScopedCharBuffer wxString::mb_str(const wxMBConv& conv) const
if ( conv.IsUTF8() ) if ( conv.IsUTF8() )
return wxScopedCharBuffer::CreateNonOwned(m_impl.c_str(), m_impl.length()); return wxScopedCharBuffer::CreateNonOwned(m_impl.c_str(), m_impl.length());
// FIXME-UTF8: use wc_str() here once we have buffers with length wxScopedWCharBuffer wcBuf(wc_str());
if ( !wcBuf.length() )
size_t wcLen;
wxScopedWCharBuffer wcBuf
(
wxMBConvStrictUTF8().cMB2WC
(
m_impl.c_str(),
m_impl.length() + 1, // size
&wcLen
)
);
if ( !wcLen )
return wxCharBuffer(""); return wxCharBuffer("");
return conv.cWC2MB(wcBuf, wcLen+1, NULL); return conv.cWC2MB(wcBuf.data(), wcBuf.length(), NULL);
} }
#else // ANSI #else // ANSI
@@ -594,7 +591,11 @@ const wxScopedCharBuffer wxString::mb_str(const wxMBConv& conv) const
//mode is not enabled and wxUSE_WCHAR_T is enabled //mode is not enabled and wxUSE_WCHAR_T is enabled
const wxScopedWCharBuffer wxString::wc_str(const wxMBConv& conv) const const wxScopedWCharBuffer wxString::wc_str(const wxMBConv& conv) const
{ {
return conv.cMB2WC(wx_str(), length() + 1 /* size, not length */, NULL); // NB: Length passed to cMB2WC() doesn't include terminating NUL, it's
// added by it automatically. If we passed length()+1 here, it would
// create a buffer with 2 trailing NULs of length one greater than
// expected.
return conv.cMB2WC(wx_str(), length(), NULL);
} }
#endif // Unicode/ANSI #endif // Unicode/ANSI