X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/504f8052fd01fdc49848d29a5ade7b061f419b81..0bbe61b8c18a1795189f0cf73cc61c14a0fb846d:/src/mac/corefoundation/cfstring.cpp diff --git a/src/mac/corefoundation/cfstring.cpp b/src/mac/corefoundation/cfstring.cpp index 9ac251a09f..dbe52f6e57 100644 --- a/src/mac/corefoundation/cfstring.cpp +++ b/src/mac/corefoundation/cfstring.cpp @@ -610,12 +610,23 @@ wxCFStringRef::wxCFStringRef( const wxString &st , wxFontEncoding WXUNUSED_IN_UN (UniChar*)str.wc_str() , str.Len() ) ); #else wxMBConvUTF16 converter ; - size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ; - UniChar *unibuf = new UniChar[ unicharlen / sizeof(UniChar) + 1 ] ; - converter.WC2MB( (char*)unibuf , str.wc_str() , unicharlen ) ; - m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault , - unibuf , unicharlen / sizeof(UniChar) ) ; - delete[] unibuf ; + size_t unicharbytes = converter.FromWChar( NULL , 0 , str.wc_str() , str.Length() ) ; + wxASSERT( unicharbytes != wxCONV_FAILED ); + if ( unicharbytes == wxCONV_FAILED ) + { + // create an empty string + reset( wxCFRetain( CFSTR("") ) ); + } + else + { + // unicharbytes: number of bytes needed for UTF-16 encoded string (without terminating null) + // unichars: number of UTF-16 characters (without terminating null) + size_t unichars = unicharbytes / sizeof(UniChar) ; + UniChar *unibuf = new UniChar[ unichars ] ; + converter.FromWChar( (char*)unibuf , unicharbytes , str.wc_str() , str.Length() ) ; + reset( CFStringCreateWithCharacters( kCFAllocatorDefault , unibuf , unichars ) ) ; + delete[] unibuf ; + } #endif #else // not wxUSE_UNICODE reset( CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,