}
#endif
-#if wxUSE_UNICODE
+#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
const char* wxCStrData::AsChar() const
{
+#if wxUSE_UNICODE_UTF8
+ if ( wxLocaleIsUtf8 )
+ return AsInternal();
+#endif
+ // under non-UTF8 locales, we have to convert the internal UTF-8
+ // representation using wxConvLibc and cache the result
+
wxString *str = wxConstCast(m_str, wxString);
// convert the string:
// and keep it:
return str->m_convertedToChar + m_offset;
}
-#endif // wxUSE_UNICODE
+#endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
#if !wxUSE_UNICODE_WCHAR
const wchar_t* wxCStrData::AsWChar() const
wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength,
const wxMBConv& conv)
{
- // FIXME-UTF8: return as-is without copying under UTF8 locale, return
- // converted string under other locales - needs wxCharBuffer
- // changes
-
// anything to do?
if ( !psz || nLength == 0 )
return SubstrBufFromMB("", 0);
+ // if psz is already in UTF-8, we don't have to do the roundtrip to
+ // wchar_t* and back:
+ if ( conv.IsUTF8() )
+ {
+ // we need to validate the input because UTF8 iterators assume valid
+ // UTF-8 sequence and psz may be invalid:
+ if ( wxStringOperations::IsValidUtf8String(psz, nLength) )
+ {
+ return SubstrBufFromMB(wxCharBuffer::CreateNonOwned(psz), nLength);
+ }
+ // else: do the roundtrip through wchar_t*
+ }
+
if ( nLength == npos )
nLength = wxNO_LEN;
const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const
{
- // FIXME-UTF8: optimize the case when conv==wxConvUTF8 or wxConvLibc
- // under UTF8 locale
+ if ( conv.IsUTF8() )
+ return wxCharBuffer::CreateNonOwned(m_impl.c_str());
+
// FIXME-UTF8: use wc_str() here once we have buffers with length
size_t wcLen;
if (!ascii)
return wxEmptyString;
- size_t len = strlen( ascii );
+ size_t len = strlen(ascii);
wxString res;
if ( len )
{
- wxStringBuffer buf(res, len);
-
- wchar_t *dest = buf;
+ wxImplStringBuffer buf(res, len);
+ wxStringCharType *dest = buf;
for ( ;; )
{
- if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' )
- break;
+ unsigned char c = (unsigned char)*ascii++;
+ wxASSERT_MSG( c < 0x80,
+ _T("Non-ASCII value passed to FromAscii().") );
+
+ *dest++ = (wchar_t)c;
+
+ if ( c == '\0' )
+ break;
}
}
{
// What do we do with '\0' ?
- wxString res;
- res += (wchar_t)(unsigned char) ascii;
+ unsigned char c = (unsigned char)ascii;
- return res;
+ wxASSERT_MSG( c < 0x80, _T("Non-ASCII value passed to FromAscii().") );
+
+ // NB: the cast to wchar_t causes interpretation of 'ascii' as Latin1 value
+ return wxString(wxUniChar((wchar_t)c));
}
const wxCharBuffer wxString::ToAscii() const
{
// this will allocate enough space for the terminating NUL too
wxCharBuffer buffer(length());
-
-
char *dest = buffer.data();
- const wchar_t *pwc = c_str();
- for ( ;; )
+ for ( const_iterator i = begin(); i != end(); ++i )
{
- *dest++ = (char)(*pwc > SCHAR_MAX ? wxT('_') : *pwc);
+ wxUniChar c(*i);
+ // FIXME-UTF8: unify substituted char ('_') with wxUniChar ('?')
+ *dest++ = c.IsAscii() ? (char)c : '_';
// the output string can't have embedded NULs anyhow, so we can safely
// stop at first of them even if we do have any
- if ( !*pwc++ )
+ if ( !c )
break;
}
return buffer;
}
-#endif // Unicode
+#endif // wxUSE_UNICODE
// extract string of length nCount starting at nFirst
wxString wxString::Mid(size_t nFirst, size_t nCount) const
// formatted output
// ---------------------------------------------------------------------------
+#if !wxUSE_UTF8_LOCALE_ONLY
/* static */
#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
-wxString wxStringPrintfMixinBase::DoFormat(const wxString& format, ...)
+wxString wxStringPrintfMixinBase::DoFormatWchar(const wxChar *format, ...)
#else
-wxString wxString::DoFormat(const wxString& format, ...)
+wxString wxString::DoFormatWchar(const wxChar *format, ...)
#endif
{
va_list argptr;
return s;
}
+#endif // !wxUSE_UTF8_LOCALE_ONLY
+
+#if wxUSE_UNICODE_UTF8
+/* static */
+wxString wxString::DoFormatUtf8(const char *format, ...)
+{
+ va_list argptr;
+ va_start(argptr, format);
+
+ wxString s;
+ s.PrintfV(format, argptr);
+
+ va_end(argptr);
+
+ return s;
+}
+#endif // wxUSE_UNICODE_UTF8
/* static */
wxString wxString::FormatV(const wxString& format, va_list argptr)
return s;
}
+#if !wxUSE_UTF8_LOCALE_ONLY
#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
-int wxStringPrintfMixinBase::DoPrintf(const wxString& format, ...)
+int wxStringPrintfMixinBase::DoPrintfWchar(const wxChar *format, ...)
#else
-int wxString::DoPrintf(const wxString& format, ...)
+int wxString::DoPrintfWchar(const wxChar *format, ...)
#endif
{
va_list argptr;
return iLen;
}
+#endif // !wxUSE_UTF8_LOCALE_ONLY
+
+#if wxUSE_UNICODE_UTF8
+int wxString::DoPrintfUtf8(const char *format, ...)
+{
+ va_list argptr;
+ va_start(argptr, format);
+
+ int iLen = PrintfV(format, argptr);
+
+ va_end(argptr);
+
+ return iLen;
+}
+#endif // wxUSE_UNICODE_UTF8
#if wxUSE_UNICODE_UTF8
template<typename BufferType>