From 1fc10687bf85c1d2ccce2c43c8300a4b0a0edbce Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Fri, 8 Jun 2007 20:17:34 +0000 Subject: [PATCH] added wxUniChar::AsUTF8() for easy conversion in UTF8 build git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46376 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/stringops.h | 11 +++-------- include/wx/unichar.h | 18 ++++++++++++++++++ src/common/stringops.cpp | 7 ++++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/include/wx/stringops.h b/include/wx/stringops.h index f3046794be..b8ba8abd74 100644 --- a/include/wx/stringops.h +++ b/include/wx/stringops.h @@ -140,15 +140,10 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8 return dist; } - // buffer for single UTF-8 character - struct Utf8CharBuffer - { - char data[5]; - operator const char*() const { return data; } - }; - // encodes the character as UTF-8: - static Utf8CharBuffer EncodeChar(const wxUniChar& ch); + typedef wxUniChar::Utf8CharBuffer Utf8CharBuffer; + static Utf8CharBuffer EncodeChar(const wxUniChar& ch) + { return ch.AsUTF8(); } // returns n copies of ch encoded in UTF-8 string static wxCharBuffer EncodeNChars(size_t n, const wxUniChar& ch); diff --git a/include/wx/unichar.h b/include/wx/unichar.h index 352018ef25..0ecba58e9a 100644 --- a/include/wx/unichar.h +++ b/include/wx/unichar.h @@ -71,6 +71,19 @@ public: // Returns Unicode code point value of the character value_type GetValue() const { return m_value; } +#if wxUSE_UNICODE_UTF8 + // buffer for single UTF-8 character + struct Utf8CharBuffer + { + char data[5]; + operator const char*() const { return data; } + }; + + // returns the character encoded as UTF-8 + // (NB: implemented in stringops.cpp) + Utf8CharBuffer AsUTF8() const; +#endif // wxUSE_UNICODE_UTF8 + // Returns true if the character is an ASCII character: bool IsAscii() const { return m_value < 0x80; } @@ -182,6 +195,11 @@ public: #endif wxUniChar::value_type GetValue() const { return UniChar().GetValue(); } + +#if wxUSE_UNICODE_UTF8 + wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); } +#endif // wxUSE_UNICODE_UTF8 + bool IsAscii() const { return UniChar().IsAscii(); } // Assignment operators: diff --git a/src/common/stringops.cpp b/src/common/stringops.cpp index 66a461a6bb..2050048062 100644 --- a/src/common/stringops.cpp +++ b/src/common/stringops.cpp @@ -190,13 +190,14 @@ bool wxStringOperationsUtf8::IsValidUtf8LeadByte(unsigned char c) #endif -wxStringOperationsUtf8::Utf8CharBuffer -wxStringOperationsUtf8::EncodeChar(const wxUniChar& ch) +// NB: this is in this file and not unichar.cpp to keep all UTF-8 encoding +// code in single place +wxUniChar::Utf8CharBuffer wxUniChar::AsUTF8() const { Utf8CharBuffer buf; char *out = buf.data; - wxUniChar::value_type code = ch.GetValue(); + value_type code = GetValue(); // Char. number range | UTF-8 octet sequence // (hexadecimal) | (binary) -- 2.45.2