1
///////////////////////////////////////////////////////////////////////////////
3
// Purpose: wxUniChar and wxUniCharRef classes
4
// Author: Vaclav Slavik
6
// RCS-ID: $Id: unichar.h 62738 2009-11-28 14:37:03Z VZ $
7
// Copyright: (c) 2007 REA Elektronik GmbH
8
// Licence: wxWindows licence
9
///////////////////////////////////////////////////////////////////////////////
11
#ifndef _WX_UNICHAR_H_
12
#define _WX_UNICHAR_H_
15
#include "wx/chartype.h"
16
#include "wx/stringimpl.h"
18
class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
19
class WXDLLIMPEXP_FWD_BASE wxString;
21
// This class represents single Unicode character. It can be converted to
22
// and from char or wchar_t and implements commonly used character operations.
23
class WXDLLIMPEXP_BASE wxUniChar
26
// NB: this is not wchar_t on purpose, it needs to represent the entire
27
// Unicode code points range and wchar_t may be too small for that
28
// (e.g. on Win32 where wchar_t* is encoded in UTF-16)
29
typedef wxUint32 value_type;
31
wxUniChar() : m_value(0) {}
33
// Create the character from 8bit character value encoded in the current
35
wxUniChar(char c) { m_value = From8bit(c); }
36
wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
38
// Create the character from a wchar_t character value.
39
#if wxWCHAR_T_IS_REAL_TYPE
40
wxUniChar(wchar_t c) { m_value = c; }
43
wxUniChar(int c) { m_value = c; }
44
wxUniChar(unsigned int c) { m_value = c; }
45
wxUniChar(long int c) { m_value = c; }
46
wxUniChar(unsigned long int c) { m_value = c; }
47
wxUniChar(short int c) { m_value = c; }
48
wxUniChar(unsigned short int c) { m_value = c; }
50
wxUniChar(const wxUniCharRef& c);
52
// Returns Unicode code point value of the character
53
value_type GetValue() const { return m_value; }
55
#if wxUSE_UNICODE_UTF8
56
// buffer for single UTF-8 character
60
operator const char*() const { return data; }
63
// returns the character encoded as UTF-8
64
// (NB: implemented in stringops.cpp)
65
Utf8CharBuffer AsUTF8() const;
66
#endif // wxUSE_UNICODE_UTF8
68
// Returns true if the character is an ASCII character:
69
bool IsAscii() const { return m_value < 0x80; }
71
// Returns true if the character is representable as a single byte in the
72
// current locale encoding and return this byte in output argument c (which
74
bool GetAsChar(char *c) const
79
#if !wxUSE_UTF8_LOCALE_ONLY
80
if ( GetAsHi8bit(m_value, c) )
82
#endif // !wxUSE_UTF8_LOCALE_ONLY
86
#endif // wxUSE_UNICODE
88
*c = wx_truncate_cast(char, m_value);
92
// Conversions to char and wchar_t types: all of those are needed to be
93
// able to pass wxUniChars to verious standard narrow and wide character
95
operator char() const { return To8bit(m_value); }
96
operator unsigned char() const { return (unsigned char)To8bit(m_value); }
97
#if wxWCHAR_T_IS_REAL_TYPE
98
operator wchar_t() const { return (wchar_t)m_value; }
100
operator int() const { return (int)m_value; }
101
operator unsigned int() const { return (unsigned int)m_value; }
102
operator long int() const { return (long int)m_value; }
103
operator unsigned long int() const { return (unsigned long)m_value; }
104
operator short int() const { return (short int)m_value; }
105
operator unsigned short int() const { return (unsigned short int)m_value; }
107
// We need this operator for the "*p" part of expressions like "for (
108
// const_iterator p = begin() + nStart; *p; ++p )". In this case,
109
// compilation would fail without it because the conversion to bool would
110
// be ambiguous (there are all these int types conversions...). (And adding
111
// operator unspecified_bool_type() would only makes the ambiguity worse.)
112
operator bool() const { return m_value != 0; }
113
bool operator!() const { return !((bool)*this); }
115
// And this one is needed by some (not all, but not using ifdefs makes the
116
// code easier) compilers to parse "str[0] && *p" successfully
117
bool operator&&(bool v) const { return (bool)*this && v; }
119
// Assignment operators:
120
wxUniChar& operator=(const wxUniChar& c) { if (&c != this) m_value = c.m_value; return *this; }
121
wxUniChar& operator=(const wxUniCharRef& c);
122
wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
123
wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
124
#if wxWCHAR_T_IS_REAL_TYPE
125
wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
127
wxUniChar& operator=(int c) { m_value = c; return *this; }
128
wxUniChar& operator=(unsigned int c) { m_value = c; return *this; }
129
wxUniChar& operator=(long int c) { m_value = c; return *this; }
130
wxUniChar& operator=(unsigned long int c) { m_value = c; return *this; }
131
wxUniChar& operator=(short int c) { m_value = c; return *this; }
132
wxUniChar& operator=(unsigned short int c) { m_value = c; return *this; }
134
// Comparison operators:
136
// define the given comparison operator for all the types
137
#define wxDEFINE_UNICHAR_OPERATOR(op) \
138
bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
139
bool operator op(char c) const { return m_value op From8bit(c); } \
140
bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
141
wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
142
bool operator op(int c) const { return m_value op (value_type)c; } \
143
bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
144
bool operator op(short int c) const { return m_value op (value_type)c; } \
145
bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
146
bool operator op(long int c) const { return m_value op (value_type)c; } \
147
bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
149
wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
151
#undef wxDEFINE_UNICHAR_OPERATOR
153
// this is needed for expressions like 'Z'-c
154
int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
155
int operator-(char c) const { return m_value - From8bit(c); }
156
int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
157
int operator-(wchar_t c) const { return m_value - (value_type)c; }
161
// notice that we implement these functions inline for 7-bit ASCII
162
// characters purely for performance reasons
163
static value_type From8bit(char c)
166
if ( (unsigned char)c < 0x80 )
169
return FromHi8bit(c);
175
static char To8bit(value_type c)
179
return wx_truncate_cast(char, c);
187
// helpers of the functions above called to deal with non-ASCII chars
188
static value_type FromHi8bit(char c);
189
static char ToHi8bit(value_type v);
190
static bool GetAsHi8bit(value_type v, char *c);
197
// Writeable reference to a character in wxString.
199
// This class can be used in the same way wxChar is used, except that changing
200
// its value updates the underlying string object.
201
class WXDLLIMPEXP_BASE wxUniCharRef
204
typedef wxStringImpl::iterator iterator;
206
// create the reference
207
#if wxUSE_UNICODE_UTF8
208
wxUniCharRef(wxString& str, iterator pos) : m_str(str), m_pos(pos) {}
210
wxUniCharRef(iterator pos) : m_pos(pos) {}
214
// NB: we have to make this public, because we don't have wxString
215
// declaration available here and so can't declare wxString::iterator
216
// as friend; so at least don't use a ctor but a static function
217
// that must be used explicitly (this is more than using 'explicit'
218
// keyword on ctor!):
219
#if wxUSE_UNICODE_UTF8
220
static wxUniCharRef CreateForString(wxString& str, iterator pos)
221
{ return wxUniCharRef(str, pos); }
223
static wxUniCharRef CreateForString(iterator pos)
224
{ return wxUniCharRef(pos); }
227
wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
229
#if wxUSE_UNICODE_UTF8
230
wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
231
#endif // wxUSE_UNICODE_UTF8
233
bool IsAscii() const { return UniChar().IsAscii(); }
234
bool GetAsChar(char *c) const { return UniChar().GetAsChar(c); }
236
// Assignment operators:
237
#if wxUSE_UNICODE_UTF8
238
wxUniCharRef& operator=(const wxUniChar& c);
240
wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
243
wxUniCharRef& operator=(const wxUniCharRef& c)
244
{ if (&c != this) *this = c.UniChar(); return *this; }
246
wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
247
wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
248
#if wxWCHAR_T_IS_REAL_TYPE
249
wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
251
wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
252
wxUniCharRef& operator=(unsigned int c) { return *this = wxUniChar(c); }
253
wxUniCharRef& operator=(short int c) { return *this = wxUniChar(c); }
254
wxUniCharRef& operator=(unsigned short int c) { return *this = wxUniChar(c); }
255
wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
256
wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); }
258
// Conversions to the same types as wxUniChar is convertible too:
259
operator char() const { return UniChar(); }
260
operator unsigned char() const { return UniChar(); }
261
#if wxWCHAR_T_IS_REAL_TYPE
262
operator wchar_t() const { return UniChar(); }
264
operator int() const { return UniChar(); }
265
operator unsigned int() const { return UniChar(); }
266
operator short int() const { return UniChar(); }
267
operator unsigned short int() const { return UniChar(); }
268
operator long int() const { return UniChar(); }
269
operator unsigned long int() const { return UniChar(); }
271
// see wxUniChar::operator bool etc. for explanation
272
operator bool() const { return (bool)UniChar(); }
273
bool operator!() const { return !UniChar(); }
274
bool operator&&(bool v) const { return UniChar() && v; }
276
// Comparison operators:
277
#define wxDEFINE_UNICHARREF_OPERATOR(op) \
278
bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
279
bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
280
bool operator op(char c) const { return UniChar() op c; } \
281
bool operator op(unsigned char c) const { return UniChar() op c; } \
282
wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
283
bool operator op(int c) const { return UniChar() op c; } \
284
bool operator op(unsigned int c) const { return UniChar() op c; } \
285
bool operator op(short int c) const { return UniChar() op c; } \
286
bool operator op(unsigned short int c) const { return UniChar() op c; } \
287
bool operator op(long int c) const { return UniChar() op c; } \
288
bool operator op(unsigned long int c) const { return UniChar() op c; }
290
wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
292
#undef wxDEFINE_UNICHARREF_OPERATOR
294
// for expressions like c-'A':
295
int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
296
int operator-(const wxUniChar& c) const { return UniChar() - c; }
297
int operator-(char c) const { return UniChar() - c; }
298
int operator-(unsigned char c) const { return UniChar() - c; }
299
int operator-(wchar_t c) const { return UniChar() - c; }
302
#if wxUSE_UNICODE_UTF8
303
wxUniChar UniChar() const;
305
wxUniChar UniChar() const { return *m_pos; }
308
friend class WXDLLIMPEXP_FWD_BASE wxUniChar;
311
// reference to the string and pointer to the character in string
312
#if wxUSE_UNICODE_UTF8
318
inline wxUniChar::wxUniChar(const wxUniCharRef& c)
320
m_value = c.UniChar().m_value;
323
inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
325
m_value = c.UniChar().m_value;
329
// Comparison operators for the case when wxUniChar(Ref) is the second operand
330
// implemented in terms of member comparison functions
332
#define wxCMP_REVERSE(c1, c2, op) c2 op c1
334
wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
335
wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
337
wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
338
wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
340
wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
344
// for expressions like c-'A':
345
inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
346
inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
347
inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
349
#endif /* _WX_UNICHAR_H_ */