check that string passed to FromUTF8() is valid even in release build, this is safer; add a separate FromUTF8Unchecked() which can be used for maximal efficiency

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54721 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-07-19 19:59:59 +00:00
parent 9f10e7c758
commit cc209a518f
5 changed files with 61 additions and 12 deletions

View File

@@ -1268,7 +1268,7 @@ public:
// conversion to/from UTF-8:
#if wxUSE_UNICODE_UTF8
static wxString FromUTF8(const char *utf8)
static wxString FromUTF8Unchecked(const char *utf8)
{
if ( !utf8 )
return wxEmptyString;
@@ -1276,16 +1276,35 @@ public:
wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
return FromImpl(wxStringImpl(utf8));
}
static wxString FromUTF8(const char *utf8, size_t len)
static wxString FromUTF8Unchecked(const char *utf8, size_t len)
{
if ( !utf8 )
return wxEmptyString;
if ( len == npos )
return FromUTF8(utf8);
return FromUTF8Unchecked(utf8);
wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
return FromImpl(wxStringImpl(utf8, len));
}
static wxString FromUTF8(const char *utf8)
{
if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8) )
return "";
return FromImpl(wxStringImpl(utf8));
}
static wxString FromUTF8(const char *utf8, size_t len)
{
if ( len == npos )
return FromUTF8(utf8);
if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8, len) )
return "";
return FromImpl(wxStringImpl(utf8, len));
}
const char* utf8_str() const { return wx_str(); }
const char* ToUTF8() const { return wx_str(); }
@@ -1293,10 +1312,15 @@ public:
// internal UTF-8 representation
size_t utf8_length() const { return m_impl.length(); }
#elif wxUSE_UNICODE_WCHAR
static wxString FromUTF8(const char *utf8)
{ return wxString(utf8, wxMBConvUTF8()); }
static wxString FromUTF8(const char *utf8, size_t len)
static wxString FromUTF8(const char *utf8, size_t len = npos)
{ return wxString(utf8, wxMBConvUTF8(), len); }
static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
{
const wxString s(utf8, wxMBConvUTF8(), len);
wxASSERT_MSG( !utf8 || !*utf8 || !s.empty(),
"string must be valid UTF-8" );
return s;
}
const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
const wxCharBuffer ToUTF8() const { return utf8_str(); }
#else // ANSI