1
///////////////////////////////////////////////////////////////////////////////
3
// Purpose: wxUniChar and wxUniCharRef classes
4
// Author: Vaclav Slavik
6
// Copyright: (c) 2007 REA Elektronik GmbH
7
// Licence: wxWindows licence
8
///////////////////////////////////////////////////////////////////////////////
10
#ifndef _WX_UNICHAR_H_
11
#define _WX_UNICHAR_H_
14
#include "wx/chartype.h"
15
#include "wx/stringimpl.h"
17
class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
18
class WXDLLIMPEXP_FWD_BASE wxString;
20
// This class represents single Unicode character. It can be converted to
21
// and from char or wchar_t and implements commonly used character operations.
22
class WXDLLIMPEXP_BASE wxUniChar
25
// NB: this is not wchar_t on purpose, it needs to represent the entire
26
// Unicode code points range and wchar_t may be too small for that
27
// (e.g. on Win32 where wchar_t* is encoded in UTF-16)
28
typedef wxUint32 value_type;
30
wxUniChar() : m_value(0) {}
32
// Create the character from 8bit character value encoded in the current
34
wxUniChar(char c) { m_value = From8bit(c); }
35
wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
37
#define wxUNICHAR_DEFINE_CTOR(type) \
38
wxUniChar(type c) { m_value = (value_type)c; }
39
wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_CTOR)
40
#undef wxUNICHAR_DEFINE_CTOR
42
wxUniChar(const wxUniCharRef& c);
44
// Returns Unicode code point value of the character
45
value_type GetValue() const { return m_value; }
47
#if wxUSE_UNICODE_UTF8
48
// buffer for single UTF-8 character
52
operator const char*() const { return data; }
55
// returns the character encoded as UTF-8
56
// (NB: implemented in stringops.cpp)
57
Utf8CharBuffer AsUTF8() const;
58
#endif // wxUSE_UNICODE_UTF8
60
// Returns true if the character is an ASCII character:
61
bool IsAscii() const { return m_value < 0x80; }
63
// Returns true if the character is representable as a single byte in the
64
// current locale encoding and return this byte in output argument c (which
66
bool GetAsChar(char *c) const
71
#if !wxUSE_UTF8_LOCALE_ONLY
72
if ( GetAsHi8bit(m_value, c) )
74
#endif // !wxUSE_UTF8_LOCALE_ONLY
78
#endif // wxUSE_UNICODE
80
*c = wx_truncate_cast(char, m_value);
84
// Conversions to char and wchar_t types: all of those are needed to be
85
// able to pass wxUniChars to verious standard narrow and wide character
87
operator char() const { return To8bit(m_value); }
88
operator unsigned char() const { return (unsigned char)To8bit(m_value); }
90
#define wxUNICHAR_DEFINE_OPERATOR_PAREN(type) \
91
operator type() const { return (type)m_value; }
92
wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_OPERATOR_PAREN)
93
#undef wxUNICHAR_DEFINE_OPERATOR_PAREN
95
// We need this operator for the "*p" part of expressions like "for (
96
// const_iterator p = begin() + nStart; *p; ++p )". In this case,
97
// compilation would fail without it because the conversion to bool would
98
// be ambiguous (there are all these int types conversions...). (And adding
99
// operator unspecified_bool_type() would only makes the ambiguity worse.)
100
operator bool() const { return m_value != 0; }
101
bool operator!() const { return !((bool)*this); }
103
// And this one is needed by some (not all, but not using ifdefs makes the
104
// code easier) compilers to parse "str[0] && *p" successfully
105
bool operator&&(bool v) const { return (bool)*this && v; }
107
// Assignment operators:
108
wxUniChar& operator=(const wxUniChar& c) { if (&c != this) m_value = c.m_value; return *this; }
109
wxUniChar& operator=(const wxUniCharRef& c);
110
wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
111
wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
113
#define wxUNICHAR_DEFINE_OPERATOR_EQUAL(type) \
114
wxUniChar& operator=(type c) { m_value = (value_type)c; return *this; }
115
wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_OPERATOR_EQUAL)
116
#undef wxUNICHAR_DEFINE_OPERATOR_EQUAL
118
// Comparison operators:
119
#define wxDEFINE_UNICHAR_CMP_WITH_INT(T, op) \
120
bool operator op(T c) const { return m_value op (value_type)c; }
122
// define the given comparison operator for all the types
123
#define wxDEFINE_UNICHAR_OPERATOR(op) \
124
bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
125
bool operator op(char c) const { return m_value op From8bit(c); } \
126
bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
127
wxDO_FOR_INT_TYPES_1(wxDEFINE_UNICHAR_CMP_WITH_INT, op)
129
wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
131
#undef wxDEFINE_UNICHAR_OPERATOR
132
#undef wxDEFINE_UNCHAR_CMP_WITH_INT
134
// this is needed for expressions like 'Z'-c
135
int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
136
int operator-(char c) const { return m_value - From8bit(c); }
137
int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
138
int operator-(wchar_t c) const { return m_value - (value_type)c; }
142
// notice that we implement these functions inline for 7-bit ASCII
143
// characters purely for performance reasons
144
static value_type From8bit(char c)
147
if ( (unsigned char)c < 0x80 )
150
return FromHi8bit(c);
156
static char To8bit(value_type c)
160
return wx_truncate_cast(char, c);
168
// helpers of the functions above called to deal with non-ASCII chars
169
static value_type FromHi8bit(char c);
170
static char ToHi8bit(value_type v);
171
static bool GetAsHi8bit(value_type v, char *c);
178
// Writeable reference to a character in wxString.
180
// This class can be used in the same way wxChar is used, except that changing
181
// its value updates the underlying string object.
182
class WXDLLIMPEXP_BASE wxUniCharRef
185
typedef wxStringImpl::iterator iterator;
187
// create the reference
188
#if wxUSE_UNICODE_UTF8
189
wxUniCharRef(wxString& str, iterator pos) : m_str(str), m_pos(pos) {}
191
wxUniCharRef(iterator pos) : m_pos(pos) {}
195
// NB: we have to make this public, because we don't have wxString
196
// declaration available here and so can't declare wxString::iterator
197
// as friend; so at least don't use a ctor but a static function
198
// that must be used explicitly (this is more than using 'explicit'
199
// keyword on ctor!):
200
#if wxUSE_UNICODE_UTF8
201
static wxUniCharRef CreateForString(wxString& str, iterator pos)
202
{ return wxUniCharRef(str, pos); }
204
static wxUniCharRef CreateForString(iterator pos)
205
{ return wxUniCharRef(pos); }
208
wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
210
#if wxUSE_UNICODE_UTF8
211
wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
212
#endif // wxUSE_UNICODE_UTF8
214
bool IsAscii() const { return UniChar().IsAscii(); }
215
bool GetAsChar(char *c) const { return UniChar().GetAsChar(c); }
217
// Assignment operators:
218
#if wxUSE_UNICODE_UTF8
219
wxUniCharRef& operator=(const wxUniChar& c);
221
wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
224
wxUniCharRef& operator=(const wxUniCharRef& c)
225
{ if (&c != this) *this = c.UniChar(); return *this; }
227
#define wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL(type) \
228
wxUniCharRef& operator=(type c) { return *this = wxUniChar(c); }
229
wxDO_FOR_CHAR_INT_TYPES(wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL)
230
#undef wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL
232
// Conversions to the same types as wxUniChar is convertible too:
233
#define wxUNICHAR_REF_DEFINE_OPERATOR_PAREN(type) \
234
operator type() const { return UniChar(); }
235
wxDO_FOR_CHAR_INT_TYPES(wxUNICHAR_REF_DEFINE_OPERATOR_PAREN)
236
#undef wxUNICHAR_REF_DEFINE_OPERATOR_PAREN
238
// see wxUniChar::operator bool etc. for explanation
239
operator bool() const { return (bool)UniChar(); }
240
bool operator!() const { return !UniChar(); }
241
bool operator&&(bool v) const { return UniChar() && v; }
243
#define wxDEFINE_UNICHARREF_CMP_WITH_INT(T, op) \
244
bool operator op(T c) const { return UniChar() op c; }
246
// Comparison operators:
247
#define wxDEFINE_UNICHARREF_OPERATOR(op) \
248
bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
249
bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
250
wxDO_FOR_CHAR_INT_TYPES_1(wxDEFINE_UNICHARREF_CMP_WITH_INT, op)
252
wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
254
#undef wxDEFINE_UNICHARREF_OPERATOR
255
#undef wxDEFINE_UNICHARREF_CMP_WITH_INT
257
// for expressions like c-'A':
258
int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
259
int operator-(const wxUniChar& c) const { return UniChar() - c; }
260
int operator-(char c) const { return UniChar() - c; }
261
int operator-(unsigned char c) const { return UniChar() - c; }
262
int operator-(wchar_t c) const { return UniChar() - c; }
265
#if wxUSE_UNICODE_UTF8
266
wxUniChar UniChar() const;
268
wxUniChar UniChar() const { return *m_pos; }
271
friend class WXDLLIMPEXP_FWD_BASE wxUniChar;
274
// reference to the string and pointer to the character in string
275
#if wxUSE_UNICODE_UTF8
281
inline wxUniChar::wxUniChar(const wxUniCharRef& c)
283
m_value = c.UniChar().m_value;
286
inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
288
m_value = c.UniChar().m_value;
292
// Comparison operators for the case when wxUniChar(Ref) is the second operand
293
// implemented in terms of member comparison functions
295
wxDEFINE_COMPARISONS_BY_REV(char, const wxUniChar&)
296
wxDEFINE_COMPARISONS_BY_REV(char, const wxUniCharRef&)
298
wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniChar&)
299
wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniCharRef&)
301
wxDEFINE_COMPARISONS_BY_REV(const wxUniChar&, const wxUniCharRef&)
303
// for expressions like c-'A':
304
inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
305
inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
306
inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
308
#endif /* _WX_UNICHAR_H_ */