use utf8_str() in wxDFB
[wxWidgets.git] / include / wx / string.h
index 346316a4928fe647a55f002f30f5bda8d69b1421..6a47d751a09d423ca50dbe2ccd57fa291b8637fa 100644 (file)
@@ -201,7 +201,7 @@ public:
     const wchar_t* AsWChar() const;
     operator const wchar_t*() const { return AsWChar(); }
 
-#if !wxUSE_UNICODE
+#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
     inline
 #endif
     const char* AsChar() const;
@@ -329,7 +329,7 @@ public:
 
     // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
     WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
-                                  Format, 1, (const wxString&),
+                                  Format, 1, (const wxFormatString&),
                                   DoFormatWchar, DoFormatUtf8)
     // We have to implement the version without template arguments manually
     // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
@@ -339,16 +339,16 @@ public:
 
     template<typename T>
     inline static typename StringReturnType<T>::type
-    Format(const wxString& fmt, FormatDummyArg dummy = FormatDummyArg())
+    Format(const wxFormatString& fmt, FormatDummyArg dummy = FormatDummyArg())
     {
-        return DoFormat(fmt);
+        return DoFormatWchar(fmt);
     }
 
     // int Printf(const wxString& format, ...);
-    WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxString&),
+    WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
                           DoPrintfWchar, DoPrintfUtf8)
     // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
-    WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxString&),
+    WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
                           DoPrintfWchar, DoPrintfUtf8)
 
 protected:
@@ -477,9 +477,6 @@ private:
 
 #else // wxUSE_UNICODE_UTF8
 
-  // FIXME-UTF8: return as-is without copying under UTF8 locale, return
-  //             converted string under other locales - needs wxCharBuffer
-  //             changes
   static wxCharBuffer ImplStr(const char* str,
                               const wxMBConv& conv = wxConvLibc)
     { return ConvertStr(str, npos, conv).data; }
@@ -931,8 +928,7 @@ public:
         { return wxStdWideString(wc_str()); }
   #endif
 
-  #if !wxUSE_UNICODE && wxUSE_STL_BASED_WXSTRING
-    // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
+  #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
     // wxStringImpl is std::string in the encoding we want
     operator const std::string&() const { return m_impl; }
   #else
@@ -941,8 +937,7 @@ public:
         // FIXME-UTF8: broken for embedded NULs
         { return std::string(mb_str()); }
   #endif
-
-#endif // wxUSE_STD_STRING
+#endif // wxUSE_STL
 
   // first valid index position
   const_iterator begin() const { return const_iterator(m_impl.begin()); }
@@ -1134,6 +1129,36 @@ public:
     const char *ToAscii() const { return c_str(); }
 #endif // Unicode/!Unicode
 
+    // conversion to/from UTF-8:
+#if wxUSE_UNICODE_UTF8
+    static wxString FromUTF8(const char *utf8)
+    {
+      wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
+      return FromImpl(wxStringImpl(utf8));
+    }
+    static wxString FromUTF8(const char *utf8, size_t len)
+    {
+      wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
+      return FromImpl(wxStringImpl(utf8, len));
+    }
+    const char* utf8_str() const { return wx_str(); }
+    const char* ToUTF8() const { return wx_str(); }
+#elif wxUSE_UNICODE_WCHAR
+    static wxString FromUTF8(const char *utf8)
+      { return wxString(utf8, wxConvUTF8); }
+    static wxString FromUTF8(const char *utf8, size_t len)
+      { return wxString(utf8, wxConvUTF8, len); }
+    const wxCharBuffer utf8_str() const { return mb_str(wxConvUTF8); }
+    const wxCharBuffer ToUTF8() const { return utf8_str(); }
+#else // ANSI
+    static wxString FromUTF8(const char *utf8)
+      { return wxString(wxConvUTF8.cMB2WC(utf8)); }
+    static wxString FromUTF8(const char *utf8, size_t len)
+      { return wxString(wxConvUTF8.cMB2WC(utf8, len == npos ? wxNO_LEN : len)); }
+    const wxCharBuffer utf8_str() const { return wxConvUTF8.cWC2MB(wc_str()); }
+    const wxCharBuffer ToUTF8() const { return utf8_str(); }
+#endif
+
     // functions for storing binary data in wxString:
 #if wxUSE_UNICODE
     static wxString From8BitData(const char *data, size_t len)
@@ -1161,7 +1186,13 @@ public:
     // type differs because a function may either return pointer to the buffer
     // directly or have to use intermediate buffer for translation.
 #if wxUSE_UNICODE
+
+#if wxUSE_UTF8_LOCALE_ONLY
+    const char* mb_str() const { return wx_str(); }
+    const wxCharBuffer mb_str(const wxMBConv& conv) const;
+#else
     const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
+#endif
 
     const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
 
@@ -1541,14 +1572,14 @@ public:
     // as sprintf(), returns the number of characters written or < 0 on error
     // (take 'this' into account in attribute parameter count)
   // int Printf(const wxString& format, ...);
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxString&),
+  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
                         DoPrintfWchar, DoPrintfUtf8)
 #ifdef __WATCOMC__
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const char*)
+  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const char*),
                         DoPrintfWchar, DoPrintfUtf8)
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wchar_t*)
+  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wchar_t*),
                         DoPrintfWchar, DoPrintfUtf8)
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxCStrData&)
+  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxCStrData&),
                         DoPrintfWchar, DoPrintfUtf8)
 #endif
 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
@@ -1558,7 +1589,7 @@ public:
 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
     // returns the string containing the result of Printf() to it
   // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
-  WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxString&),
+  WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
                         DoFormatWchar, DoFormatUtf8)
 #ifdef __WATCOMC__
   // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
@@ -1605,7 +1636,7 @@ public:
   // use Printf()
   // (take 'this' into account in attribute parameter count)
   // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
-  WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxString&),
+  WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
                         DoPrintfWchar, DoPrintfUtf8)
 #ifdef __WATCOMC__
   // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
@@ -2428,7 +2459,7 @@ private:
 
       T *m_buf;
   };
-#if wxUSE_UNICODE
+#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
   ConvertedBuffer<char> m_convertedToChar;
 #endif
 #if !wxUSE_UNICODE_WCHAR
@@ -2821,10 +2852,10 @@ inline const wchar_t* wxCStrData::AsWChar() const
 }
 #endif // wxUSE_UNICODE_WCHAR
 
-#if !wxUSE_UNICODE
+#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
 inline const char* wxCStrData::AsChar() const
 {
-    return m_str->wx_str() + m_offset;
+    return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
 }
 #endif // !wxUSE_UNICODE