From: Václav Slavík Date: Sat, 5 May 2007 18:53:24 +0000 (+0000) Subject: added wxString::FromUTF8/ToUTF8/utf8_str() X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/5f167b77d20f656f9e855113175849bd2e79cc58?ds=inline added wxString::FromUTF8/ToUTF8/utf8_str() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45842 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/latex/wx/wxstring.tex b/docs/latex/wx/wxstring.tex index 3a35293c24..20b9f19f69 100644 --- a/docs/latex/wx/wxstring.tex +++ b/docs/latex/wx/wxstring.tex @@ -779,6 +779,19 @@ Use \helpref{wxString constructors}{wxstringconstruct} if you need to convert from another charset. +\membersection{wxString::FromUTF8}\label{wxstringfromutf8} + +\func{static wxString }{FromUTF8}{\param{const char*}{ s}} + +\func{static wxString }{FromUTF8}{\param{const char*}{ s}, \param{size\_t}{ len}} + +Converts C string encoded in UTF-8 to wxString. + +Note that this method assumes that \arg{s} is a valid UTF-8 sequence and +doesn't do any validation in release builds, it's validity is only checked in +debug builds. + + \membersection{wxString::GetChar}\label{wxstringgetchar} \constfunc{wxChar}{GetChar}{\param{size\_t}{ n}} @@ -1166,12 +1179,10 @@ This is a convenience method useful when storing binary data in wxString. \constfunc{const char*}{ToAscii}{\void} -Converts the string to an ASCII, 7-bit string (ANSI builds only). - \constfunc{const wxCharBuffer}{ToAscii}{\void} Converts the string to an ASCII, 7-bit string in the form of -a wxCharBuffer (Unicode builds only). +a wxCharBuffer (Unicode builds only) or a C string (ANSI builds). Note that this conversion only works if the string contains only ASCII characters. The \helpref{mb\_str}{wxstringmbstr} method provides more @@ -1263,6 +1274,15 @@ bit integer numbers. Please see \helpref{ToLongLong}{wxstringtolonglong} for additional remarks. +\membersection{wxString::ToUTF8}\label{wxstringtoutf8} + +\constfunc{const char*}{ToUTF8}{\void} + +\constfunc{const wxCharBuffer}{ToUF8}{\void} + +Same as \helpref{utf8\_str}{wxstringutf8str}. + + \membersection{wxString::Trim}\label{wxstringtrim} \func{wxString\&}{Trim}{\param{bool}{ fromRight = true}} @@ -1316,6 +1336,18 @@ The same as MakeUpper. This is a wxWidgets 1.xx compatibility function; you should not use it in new code. +\membersection{wxString::utf8\_str}\label{wxstringutf8str} + +\constfunc{const char*}{utf8\_str}{\void} + +\constfunc{const wxCharBuffer}{utf8\_str}{\void} + +Converts the strings contents to UTF-8 and returns it either as a temporary +wxCharBuffer object or as a pointer to the internal string contents in +UTF-8 build. +% FIXME-UTF8: link to a topic explaining UTF-8 build here + + \membersection{wxString::wc\_str}\label{wxstringwcstr} \constfunc{const wchar\_t*}{wc\_str}{\param{wxMBConv\&}{ conv}} diff --git a/include/wx/string.h b/include/wx/string.h index 3da1f2f10d..6a47d751a0 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1129,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)