]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/unichar.cpp
added wxListCtrl::SetItemPtrData()
[wxWidgets.git] / src / common / unichar.cpp
index 5dbe8b0825988fc5951dd06bd8b79eb3bfbc2c8b..fa9890365fd572a55ffde76ae1b7647271ad1cbe 100644 (file)
     #pragma hdrstop
 #endif
 
+#ifndef WX_PRECOMP
+    #include "wx/strconv.h"  // wxConvLibc
+#endif
+
 #include "wx/unichar.h"
+#include "wx/stringops.h"
 
 // ===========================================================================
 // implementation
 // ===========================================================================
 
+// ---------------------------------------------------------------------------
+// wxUniChar
+// ---------------------------------------------------------------------------
+
 /* static */
-wxUniChar::unicode_type wxUniChar::From8bit(char c)
+wxUniChar::value_type wxUniChar::From8bit(char c)
 {
     // all supported charsets have the first 128 characters same as ASCII:
     if ( (unsigned char)c < 0x80 )
         return c;
 
+#if wxUSE_UTF8_LOCALE_ONLY
+    wxFAIL_MSG( _T("invalid UTF-8 character") );
+    return wxT('?'); // FIXME-UTF8: what to use as failure character?
+#else
     wchar_t buf[2];
     if ( wxConvLibc.ToWChar(buf, 2, &c, 1) != 2 )
         return wxT('?'); // FIXME-UTF8: what to use as failure character?
     return buf[0];
+#endif
 }
 
 /* static */
-char wxUniChar::To8bit(wxUniChar::unicode_type c)
+char wxUniChar::To8bit(wxUniChar::value_type c)
 {
     // all supported charsets have the first 128 characters same as ASCII:
     if ( c < 0x80 )
         return c;
 
+#if wxUSE_UTF8_LOCALE_ONLY
+    wxFAIL_MSG( _T("character cannot be converted to single UTF-8 byte") );
+    return '?'; // FIXME-UTF8: what to use as failure character?
+#else
     wchar_t in = c;
     char buf[2];
     if ( wxConvLibc.FromWChar(buf, 2, &in, 1) != 2 )
         return '?'; // FIXME-UTF8: what to use as failure character?
     return buf[0];
+#endif
+}
+
+
+// ---------------------------------------------------------------------------
+// wxUniCharRef
+// ---------------------------------------------------------------------------
+
+#if wxUSE_UNICODE_UTF8
+wxUniChar wxUniCharRef::UniChar() const
+{
+    return wxStringOperations::DecodeChar(m_pos);
+}
+
+wxUniCharRef& wxUniCharRef::operator=(const wxUniChar& c)
+{
+    wxStringOperations::Utf8CharBuffer utf(wxStringOperations::EncodeChar(c));
+    size_t lenOld = wxStringOperations::GetUtf8CharLength(*m_pos);
+    size_t lenNew = wxStringOperations::GetUtf8CharLength(utf[0]);
+
+    if ( lenNew == lenOld )
+    {
+        iterator pos(m_pos);
+        for ( size_t i = 0; i < lenNew; ++i, ++pos )
+            *pos = utf[i];
+    }
+    else
+    {
+        size_t idx = m_pos - m_str.begin();
+
+        m_str.replace(m_pos, m_pos + lenOld, utf, lenNew);
+
+        // this is needed to keep m_pos valid:
+        m_pos = m_str.begin() + idx;
+    }
+
+    return *this;
 }
+#endif // wxUSE_UNICODE_UTF8