]> git.saurik.com Git - wxWidgets.git/commitdiff
added wxUTF8StringBuffer for writing UTF8 data into wxString efficiently, similarly...
authorVáclav Slavík <vslavik@fastmail.fm>
Mon, 20 Aug 2007 15:19:15 +0000 (15:19 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Mon, 20 Aug 2007 15:19:15 +0000 (15:19 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48222 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/string.h
src/common/string.cpp
tests/strings/strings.cpp

index 3f4fe1a6bff42e533018d3a999f31e21fedb9389..1de67ef9c56f11b4e52364e7a7d0dacafe2f16ad 100644 (file)
@@ -2787,12 +2787,6 @@ public:
         : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
         { }
 
         : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
         { }
 
-    ~wxStringTypeBufferLengthBase()
-    {
-        wxASSERT(m_lenSet);
-        m_str.assign(m_buf.data(), m_len);
-    }
-
     operator CharType*() { return m_buf.data(); }
     void SetLength(size_t length) { m_len = length; m_lenSet = true; }
 
     operator CharType*() { return m_buf.data(); }
     void SetLength(size_t length) { m_len = length; m_lenSet = true; }
 
@@ -2870,6 +2864,33 @@ typedef wxStringInternalBuffer                wxStringBuffer;
 typedef wxStringInternalBufferLength          wxStringBufferLength;
 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
 
 typedef wxStringInternalBufferLength          wxStringBufferLength;
 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
 
+#if wxUSE_UNICODE_UTF8 && !wxUSE_STL_BASED_WXSTRING
+typedef wxStringInternalBuffer                wxUTF8StringBuffer;
+typedef wxStringInternalBufferLength          wxUTF8StringBufferLength;
+#elif wxUSE_UNICODE // !wxUSE_UNICODE_UTF8 || wxUSE_STL_BASED_WXSTRING
+class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
+{
+public:
+    wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
+        : wxStringTypeBufferBase<char>(str, lenWanted) {}
+    ~wxUTF8StringBuffer();
+
+    DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer)
+};
+
+class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
+    : public wxStringTypeBufferLengthBase<char>
+{
+public:
+    wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
+        : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
+    ~wxUTF8StringBufferLength();
+
+    DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength)
+};
+#endif // wxUSE_UNICODE_UTF8 && !wxUSE_STL_BASED_WXSTRING or not
+
+
 // ---------------------------------------------------------------------------
 // wxString comparison functions: operator versions are always case sensitive
 // ---------------------------------------------------------------------------
 // ---------------------------------------------------------------------------
 // wxString comparison functions: operator versions are always case sensitive
 // ---------------------------------------------------------------------------
index d154b3c937c1b28bda7e92c94cee00f6e0ab96f3..852ea132ecaa6f9bc030b023c1a2015a12fe6612 100644 (file)
@@ -1832,3 +1832,32 @@ wxString wxString::Upper() const
 
 // convert to lower case, return the copy of the string
 wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); }
 
 // convert to lower case, return the copy of the string
 wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); }
+
+// ----------------------------------------------------------------------------
+// wxUTF8StringBuffer
+// ----------------------------------------------------------------------------
+
+#if wxUSE_UNICODE && (!wxUSE_UNICODE_UTF8 || wxUSE_STL_BASED_WXSTRING)
+wxUTF8StringBuffer::~wxUTF8StringBuffer()
+{
+    wxMBConvStrictUTF8 conv;
+    size_t wlen = conv.ToWChar(NULL, 0, m_buf);
+    wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
+
+    wxStringInternalBuffer wbuf(m_str, wlen);
+    conv.ToWChar(wbuf, wlen, m_buf);
+}
+
+wxUTF8StringBufferLength::~wxUTF8StringBufferLength()
+{
+    wxCHECK_RET(m_lenSet, "length not set");
+
+    wxMBConvStrictUTF8 conv;
+    size_t wlen = conv.ToWChar(NULL, 0, m_buf, m_len);
+    wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
+
+    wxStringInternalBufferLength wbuf(m_str, wlen);
+    conv.ToWChar(wbuf, wlen, m_buf, m_len);
+    wbuf.SetLength(wlen);
+}
+#endif // wxUSE_UNICODE && (!wxUSE_UNICODE_UTF8 || wxUSE_STL_BASED_WXSTRING)
index 473f81714f8d2ce564e8685612a26f794e950f94..03afb6767533e4a4a14bca6ba0610cb9ce68c3f6 100644 (file)
@@ -53,6 +53,7 @@ private:
 #endif // wxLongLong_t
         CPPUNIT_TEST( ToDouble );
         CPPUNIT_TEST( WriteBuf );
 #endif // wxLongLong_t
         CPPUNIT_TEST( ToDouble );
         CPPUNIT_TEST( WriteBuf );
+        CPPUNIT_TEST( UTF8Buf );
         CPPUNIT_TEST( CStrDataTernaryOperator );
         CPPUNIT_TEST( CStrDataOperators );
         CPPUNIT_TEST( CStrDataImplicitConversion );
         CPPUNIT_TEST( CStrDataTernaryOperator );
         CPPUNIT_TEST( CStrDataOperators );
         CPPUNIT_TEST( CStrDataImplicitConversion );
@@ -80,6 +81,7 @@ private:
 #endif // wxLongLong_t
     void ToDouble();
     void WriteBuf();
 #endif // wxLongLong_t
     void ToDouble();
     void WriteBuf();
+    void UTF8Buf();
     void CStrDataTernaryOperator();
     void DoCStrDataTernaryOperator(bool cond);
     void CStrDataOperators();
     void CStrDataTernaryOperator();
     void DoCStrDataTernaryOperator(bool cond);
     void CStrDataOperators();
@@ -676,6 +678,26 @@ void StringTestCase::WriteBuf()
     CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s) );
 }
 
     CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s) );
 }
 
+void StringTestCase::UTF8Buf()
+{
+#if wxUSE_UNICODE
+    // "czech" in Czech ("cestina"):
+    static const char *textUTF8 = "\304\215e\305\241tina";
+    static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
+
+    wxString s;
+    wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8);
+    CPPUNIT_ASSERT(s == textUTF16);
+
+    {
+        wxUTF8StringBufferLength buf(s, 20);
+        wxStrcpy(buf, textUTF8);
+        buf.SetLength(5);
+    }
+    CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3));
+#endif // wxUSE_UNICODE
+}
+
 
 
 void StringTestCase::CStrDataTernaryOperator()
 
 
 void StringTestCase::CStrDataTernaryOperator()