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
This commit is contained in:
Václav Slavík 2007-06-08 20:17:34 +00:00
parent 9a5bfa1eab
commit 1fc10687bf
3 changed files with 25 additions and 11 deletions

View File

@ -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);

View File

@ -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:

View File

@ -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)