// because we want the buffer to always be NUL-terminated, even if the
// input isn't (as otherwise the caller has no way to know its length)
wxWCharBuffer wbuf(dstLen);
- wbuf.data()[dstLen] = L'\0';
if ( ToWChar(wbuf.data(), dstLen, inBuff, inLen) != wxCONV_FAILED )
{
if ( outLen )
}
}
- return wxWCharBuffer();
+ return wxScopedWCharBuffer::CreateNonOwned(L"", 0);
}
const wxCharBuffer wxMBConv::cWC2MB(const wxScopedWCharBuffer& wbuf) const
}
}
- return wxCharBuffer();
+ return wxScopedCharBuffer::CreateNonOwned("", 0);
}
// ----------------------------------------------------------------------------
for ( const char *p = src; ; p++ )
{
- if ( !(srcLen == wxNO_LEN ? *p : srcLen) )
+ if ( (srcLen == wxNO_LEN ? !*p : !srcLen) )
{
// all done successfully, just add the trailing NULL if we are not
// using explicit length
for ( const wchar_t *wp = src; ; wp++ )
{
- if ( !(srcLen == wxNO_LEN ? *wp : srcLen) )
+ if ( (srcLen == wxNO_LEN ? !*wp : !srcLen) )
{
// all done successfully, just add the trailing NULL if we are not
// using explicit length
{
// skip the next char too as we decoded a surrogate
wp++;
+ if ( srcLen != wxNO_LEN )
+ srcLen--;
}
#else // wchar_t is UTF-32
code = *wp & 0x7fffffff;
size_t len = 0;
- while ((srcLen == wxNO_LEN ? *psz : srcLen--) && ((!buf) || (len < n)))
+ // The length can be either given explicitly or computed implicitly for the
+ // NUL-terminated strings.
+ const bool isNulTerminated = srcLen == wxNO_LEN;
+ while ((isNulTerminated ? *psz : srcLen--) && ((!buf) || (len < n)))
{
const char *opsz = psz;
bool invalid = false;
}
}
- if (srcLen == wxNO_LEN && buf && (len < n))
- *buf = 0;
+ if ( isNulTerminated )
+ {
+ // Add the trailing NUL in this case if we have a large enough buffer.
+ if ( buf && (len < n) )
+ *buf = 0;
+
+ // And count it in any case.
+ len++;
+ }
- return len + 1;
+ return len;
}
static inline bool isoctal(wchar_t wch)
size_t len = 0;
- while ((srcLen == wxNO_LEN ? *psz : srcLen--) && ((!buf) || (len < n)))
+ // The length can be either given explicitly or computed implicitly for the
+ // NUL-terminated strings.
+ const bool isNulTerminated = srcLen == wxNO_LEN;
+ while ((isNulTerminated ? *psz : srcLen--) && ((!buf) || (len < n)))
{
wxUint32 cc;
}
}
- if (srcLen == wxNO_LEN && buf && (len < n))
- *buf = 0;
+ if ( isNulTerminated )
+ {
+ // Add the trailing NUL in this case if we have a large enough buffer.
+ if ( buf && (len < n) )
+ *buf = 0;
+
+ // And count it in any case.
+ len++;
+ }
- return len + 1;
+ return len;
}
// ============================================================================
wxUint16 *outBuff = reinterpret_cast<wxUint16 *>(dst);
for ( size_t n = 0; n < srcLen; n++ )
{
- wxUint16 cc[2];
+ wxUint16 cc[2] = { 0 };
const size_t numChars = encode_utf16(*src++, cc);
if ( numChars == wxCONV_FAILED )
return wxCONV_FAILED;
wxUint16 *outBuff = reinterpret_cast<wxUint16 *>(dst);
for ( const wchar_t *srcEnd = src + srcLen; src < srcEnd; src++ )
{
- wxUint16 cc[2];
+ wxUint16 cc[2] = { 0 };
const size_t numChars = encode_utf16(*src, cc);
if ( numChars == wxCONV_FAILED )
return wxCONV_FAILED;
size_t outLen = 0;
for ( size_t n = 0; n < inLen; n++ )
{
- wxUint16 cc[2];
+ wxUint16 cc[2] = { 0 };
const size_t numChars = encode_utf16(*inBuff++, cc);
if ( numChars == wxCONV_FAILED )
return wxCONV_FAILED;
size_t outLen = 0;
for ( size_t n = 0; n < inLen; n++, inBuff++ )
{
- wxUint16 cc[2];
+ wxUint16 cc[2] = { 0 };
const size_t numChars = encode_utf16(wxUINT32_SWAP_ALWAYS(*inBuff), cc);
if ( numChars == wxCONV_FAILED )
return wxCONV_FAILED;
virtual wxMBConv *Clone() const
{
- wxMBConv_iconv *p = new wxMBConv_iconv(m_name.ToAscii());
+ wxMBConv_iconv *p = new wxMBConv_iconv(m_name);
p->m_minMBCharWidth = m_minMBCharWidth;
return p;
}
// name of the encoding handled by this conversion
- wxString m_name;
+ const char *m_name;
// cached result of GetMBNulLen(); set to 0 meaning "unknown"
// initially
bool wxMBConv_iconv::ms_wcNeedsSwap = false;
wxMBConv_iconv::wxMBConv_iconv(const char *name)
- : m_name(name)
+ : m_name(wxStrdup(name))
{
m_minMBCharWidth = 0;
{
#if SIZEOF_WCHAR_T == 4
wxT("UCS-4"),
-#elif SIZEOF_WCHAR_T = 2
+#elif SIZEOF_WCHAR_T == 2
wxT("UCS-2"),
#endif
NULL
wxMBConv_iconv::~wxMBConv_iconv()
{
+ free(const_cast<char *>(m_name));
+
if ( m2w != ICONV_T_INVALID )
iconv_close(m2w);
if ( w2m != ICONV_T_INVALID )
{
m_name = NULL;
m_convReal = NULL;
- m_deferred = true;
+}
+
+void wxCSConv::SetEncoding(wxFontEncoding encoding)
+{
+ switch ( encoding )
+ {
+ case wxFONTENCODING_MAX:
+ case wxFONTENCODING_SYSTEM:
+ if ( m_name )
+ {
+ // It's ok to not have encoding value if we have a name for it.
+ m_encoding = wxFONTENCODING_SYSTEM;
+ }
+ else // No name neither.
+ {
+ // Fall back to the system default encoding in this case (not
+ // sure how much sense does this make but this is how the old
+ // code used to behave).
+#if wxUSE_INTL
+ m_encoding = wxLocale::GetSystemEncoding();
+ if ( m_encoding == wxFONTENCODING_SYSTEM )
+#endif // wxUSE_INTL
+ m_encoding = wxFONTENCODING_ISO8859_1;
+ }
+ break;
+
+ case wxFONTENCODING_DEFAULT:
+ // wxFONTENCODING_DEFAULT is same as US-ASCII in this context
+ m_encoding = wxFONTENCODING_ISO8859_1;
+ break;
+
+ default:
+ // Just use the provided encoding.
+ m_encoding = encoding;
+ }
}
wxCSConv::wxCSConv(const wxString& charset)
}
#if wxUSE_FONTMAP
- m_encoding = wxFontMapperBase::GetEncodingFromName(charset);
- if ( m_encoding == wxFONTENCODING_MAX )
- {
- // set to unknown/invalid value
- m_encoding = wxFONTENCODING_SYSTEM;
- }
- else if ( m_encoding == wxFONTENCODING_DEFAULT )
- {
- // wxFONTENCODING_DEFAULT is same as US-ASCII in this context
- m_encoding = wxFONTENCODING_ISO8859_1;
- }
+ SetEncoding(wxFontMapperBase::GetEncodingFromName(charset));
#else
- m_encoding = wxFONTENCODING_SYSTEM;
+ SetEncoding(wxFONTENCODING_SYSTEM);
#endif
+
+ m_convReal = DoCreate();
}
wxCSConv::wxCSConv(wxFontEncoding encoding)
Init();
- m_encoding = encoding;
+ SetEncoding(encoding);
+
+ m_convReal = DoCreate();
}
wxCSConv::~wxCSConv()
Init();
SetName(conv.m_name);
- m_encoding = conv.m_encoding;
+ SetEncoding(conv.m_encoding);
+
+ m_convReal = DoCreate();
}
wxCSConv& wxCSConv::operator=(const wxCSConv& conv)
Clear();
SetName(conv.m_name);
- m_encoding = conv.m_encoding;
+ SetEncoding(conv.m_encoding);
+
+ m_convReal = DoCreate();
return *this;
}
void wxCSConv::Clear()
{
free(m_name);
- delete m_convReal;
-
m_name = NULL;
- m_convReal = NULL;
+
+ wxDELETE(m_convReal);
}
void wxCSConv::SetName(const char *charset)
{
- if (charset)
- {
+ if ( charset )
m_name = wxStrdup(charset);
- m_deferred = true;
- }
}
#if wxUSE_FONTMAP
// check for the special case of ASCII or ISO8859-1 charset: as we have
// special knowledge of it anyhow, we don't need to create a special
// conversion object
- if ( m_encoding == wxFONTENCODING_ISO8859_1 ||
- m_encoding == wxFONTENCODING_DEFAULT )
+ if ( m_encoding == wxFONTENCODING_ISO8859_1 )
{
// don't convert at all
return NULL;
return NULL;
}
-void wxCSConv::CreateConvIfNeeded() const
-{
- if ( m_deferred )
- {
- wxCSConv *self = (wxCSConv *)this; // const_cast
-
- // if we don't have neither the name nor the encoding, use the default
- // encoding for this system
- if ( !m_name && m_encoding == wxFONTENCODING_SYSTEM )
- {
-#if wxUSE_INTL
- self->m_encoding = wxLocale::GetSystemEncoding();
-#else
- // fallback to some reasonable default:
- self->m_encoding = wxFONTENCODING_ISO8859_1;
-#endif // wxUSE_INTL
- }
-
- self->m_convReal = DoCreate();
- self->m_deferred = false;
- }
-}
-
bool wxCSConv::IsOk() const
{
- CreateConvIfNeeded();
-
// special case: no convReal created for wxFONTENCODING_ISO8859_1
if ( m_encoding == wxFONTENCODING_ISO8859_1 )
return true; // always ok as we do it ourselves
size_t wxCSConv::ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen) const
{
- CreateConvIfNeeded();
-
if (m_convReal)
return m_convReal->ToWChar(dst, dstLen, src, srcLen);
size_t wxCSConv::FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen) const
{
- CreateConvIfNeeded();
-
if (m_convReal)
return m_convReal->FromWChar(dst, dstLen, src, srcLen);
size_t wxCSConv::GetMBNulLen() const
{
- CreateConvIfNeeded();
-
if ( m_convReal )
- {
return m_convReal->GetMBNulLen();
- }
// otherwise, we are ISO-8859-1
return 1;
#if wxUSE_UNICODE_UTF8
bool wxCSConv::IsUTF8() const
{
- CreateConvIfNeeded();
-
if ( m_convReal )
- {
return m_convReal->IsUTF8();
- }
// otherwise, we are ISO-8859-1
return false;
WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvUI = wxGet_wxConvLocalPtr();
#ifdef __DARWIN__
-// The xnu kernel always communicates file paths in decomposed UTF-8.
-// WARNING: Are we sure that CFString's conversion will cause decomposition?
+// It is important to use this conversion object under Darwin as it ensures
+// that Unicode strings are (re)composed correctly even though xnu kernel uses
+// decomposed form internally (at least for the file names).
static wxMBConv_cf wxConvMacUTF8DObj(wxFONTENCODING_UTF8);
#endif