always return (owned or non-owned, depending on build) wxScopedCharBuffer from utf8_str() and ToUTF8()

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59945 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2009-03-30 18:28:42 +00:00
parent 911907f4ad
commit 197380a09f
8 changed files with 18 additions and 39 deletions

View File

@@ -372,19 +372,13 @@ const char *p = s.ToUTF8();
puts(p); // or call any other function taking const char *
@endcode
does @b not work because the temporary buffer returned by wxString::ToUTF8() is
destroyed and @c p is left pointing nowhere. To correct this you may use
destroyed and @c p is left pointing nowhere. To correct this you should use
@code
wxCharBuffer p(s.ToUTF8());
const wxScopedCharBuffer p(s.ToUTF8());
puts(p);
@endcode
which does work but results in an unnecessary copy of string data in the build
configurations when wxString::ToUTF8() returns the pointer to internal string buffer.
If this inefficiency is important you may write
@code
const wxUTF8Buf p(s.ToUTF8());
puts(p);
@endcode
where @c wxUTF8Buf is the type corresponding to the real return type of wxString::ToUTF8().
which does work.
Similarly, wxWX2WCbuf can be used for the return type of wxString::wc_str().
But, once again, none of these cryptic types is really needed if you just pass
the return value of any of the functions mentioned in this section to another

View File

@@ -427,13 +427,6 @@ typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer;
#define wxWX2WCbuf wxWCharBuffer
#endif // Unicode/ANSI
// type of the value returned by wxString::utf8_str()
#if wxUSE_UNICODE_UTF8
#define wxUTF8Buf char *
#else
#define wxUTF8Buf wxCharBuffer
#endif
// ----------------------------------------------------------------------------
// A class for holding growable data buffers (not necessarily strings)
// ----------------------------------------------------------------------------

View File

@@ -72,7 +72,7 @@ public:
: size, wxIPC_UNICODETEXT); }
bool Execute(const wxString& s)
{
const wxUTF8Buf buf = s.utf8_str();
const wxScopedCharBuffer buf = s.utf8_str();
return DoExecute(buf, strlen(buf) + 1, wxIPC_UTF8TEXT);
}
bool Execute(const wxCStrData& cs)
@@ -94,7 +94,7 @@ public:
: size, wxIPC_UNICODETEXT); }
bool Poke(const wxString& item, const wxString s)
{
const wxUTF8Buf buf = s.utf8_str();
const wxScopedCharBuffer buf = s.utf8_str();
return DoPoke(item, buf, strlen(buf) + 1, wxIPC_UTF8TEXT);
}
bool Poke(const wxString& item, const wxCStrData& cs)
@@ -116,7 +116,7 @@ public:
: size, wxIPC_UNICODETEXT); }
bool Advise(const wxString& item, const wxString s)
{
const wxUTF8Buf buf = s.utf8_str();
const wxScopedCharBuffer buf = s.utf8_str();
return DoAdvise(item, buf, strlen(buf) + 1, wxIPC_UTF8TEXT);
}
bool Advise(const wxString& item, const wxCStrData& cs)

View File

@@ -1710,8 +1710,10 @@ public:
return FromImpl(wxStringImpl(utf8, len));
}
const char* utf8_str() const { return wx_str(); }
const char* ToUTF8() const { return wx_str(); }
const wxScopedCharBuffer utf8_str() const
{ return wxCharBuffer::CreateNonOwned(wx_str()); }
const wxScopedCharBuffer ToUTF8() const
{ return wxCharBuffer::CreateNonOwned(wx_str()); }
// this function exists in UTF-8 build only and returns the length of the
// internal UTF-8 representation

View File

@@ -420,12 +420,7 @@ public:
@see wc_str(), c_str(), mb_str()
*/
const char* utf8_str() const;
/**
@overload
*/
const wxCharBuffer utf8_str() const;
const wxScopedCharBuffer utf8_str() const;
/**
Converts the strings contents to the wide character represention
@@ -497,12 +492,7 @@ public:
/**
Same as utf8_str().
*/
const char* ToUTF8() const;
/**
@overload
*/
const wxCharBuffer ToUTF8() const;
const wxScopedCharBuffer ToUTF8() const;
//@}

View File

@@ -479,7 +479,7 @@ void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
_("Test 2 ..."),
_("Yes I like wxWidgets!"));
const wxUTF8Buf msg1(s.utf8_str());
const wxScopedCharBuffer msg1(s.utf8_str());
size_t len = wxStrlen(msg1) + 1;
wxCharBuffer msg2(wxStrlen(msg1));

View File

@@ -508,7 +508,7 @@ bool wxSockAddressImpl::SetHostName4(const wxString& name)
if ( !addr )
return false;
const wxUTF8Buf namebuf(name.utf8_str());
const wxScopedCharBuffer namebuf(name.utf8_str());
// first check if this is an address in quad dotted notation
#if defined(HAVE_INET_ATON)
@@ -695,7 +695,7 @@ bool wxSockAddressImpl::SetPath(const wxString& path)
if ( !addr )
return false;
const wxUTF8Buf buf(path.utf8_str());
const wxScopedCharBuffer buf(path.utf8_str());
if ( strlen(buf) >= UNIX_PATH_MAX )
return false;

View File

@@ -1700,7 +1700,7 @@ void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCo
bool underlined = m_font.Ok() && m_font.GetUnderlined();
const wxUTF8Buf data = text.utf8_str();
const wxScopedCharBuffer data = text.utf8_str();
size_t datalen = strlen(data);
pango_layout_set_text( m_layout, data, datalen);
@@ -2102,7 +2102,7 @@ void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width,
cairo_scale(m_cairo, m_scaleX, m_scaleY);
// Set layout's text
const wxUTF8Buf dataUTF8 = string.utf8_str();
const wxScopedCharBuffer dataUTF8 = string.utf8_str();
gint oldSize=0;
if ( theFont )