1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
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 #ifndef wxWCHAR_T_IS_SEPARATE_TYPE
19 // older versions of VC++ have wchar_t as typedef by default; this is
20 // configurable, so we have to check which behaviour is enabled
21 #if defined(__VISUALC__) && !defined(_NATIVE_WCHAR_T_DEFINED)
22 #define wxWCHAR_T_IS_SEPARATE_TYPE 0
24 #define wxWCHAR_T_IS_SEPARATE_TYPE 1
28 // helper macro for doing something dependent on whether wchar_t is or isn't a
29 // typedef inside another macro
30 #if wxWCHAR_T_IS_SEPARATE_TYPE
31 #define wxIF_WCHAR_T_TYPE(x) x
32 #else // !wxWCHAR_T_IS_SEPARATE_TYPE
33 #define wxIF_WCHAR_T_TYPE(x)
34 #endif // wxWCHAR_T_IS_SEPARATE_TYPE/!wxWCHAR_T_IS_SEPARATE_TYPE
36 class WXDLLIMPEXP_BASE wxUniCharRef
;
37 class WXDLLIMPEXP_BASE wxStringIteratorNode
;
39 // This class represents single Unicode character. It can be converted to
40 // and from char or wchar_t and implements commonly used character operations.
41 class WXDLLIMPEXP_BASE wxUniChar
44 // NB: this is not wchar_t on purpose, it needs to represent the entire
45 // Unicode code points range and wchar_t may be too small for that
46 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
47 typedef wxUint32 value_type
;
49 wxUniChar() : m_value(0) {}
51 // Create the character from 8bit character value encoded in the current
53 wxUniChar(char c
) { m_value
= From8bit(c
); }
54 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
56 // Create the character from a wchar_t character value.
57 #if wxWCHAR_T_IS_SEPARATE_TYPE
58 wxUniChar(wchar_t c
) { m_value
= c
; }
61 wxUniChar(int c
) { m_value
= c
; }
62 wxUniChar(unsigned int c
) { m_value
= c
; }
63 wxUniChar(long int c
) { m_value
= c
; }
64 wxUniChar(unsigned long int c
) { m_value
= c
; }
65 wxUniChar(short int c
) { m_value
= c
; }
66 wxUniChar(unsigned short int c
) { m_value
= c
; }
68 wxUniChar(const wxUniCharRef
& c
);
70 // Returns Unicode code point value of the character
71 value_type
GetValue() const { return m_value
; }
73 #if wxUSE_UNICODE_UTF8
74 // buffer for single UTF-8 character
78 operator const char*() const { return data
; }
81 // returns the character encoded as UTF-8
82 // (NB: implemented in stringops.cpp)
83 Utf8CharBuffer
AsUTF8() const;
84 #endif // wxUSE_UNICODE_UTF8
86 // Returns true if the character is an ASCII character:
87 bool IsAscii() const { return m_value
< 0x80; }
89 // Conversions to char and wchar_t types: all of those are needed to be
90 // able to pass wxUniChars to verious standard narrow and wide character
92 operator char() const { return To8bit(m_value
); }
93 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
94 #if wxWCHAR_T_IS_SEPARATE_TYPE
95 operator wchar_t() const { return (wchar_t)m_value
; }
97 operator int() const { return (int)m_value
; }
98 operator unsigned int() const { return (unsigned int)m_value
; }
99 operator long int() const { return (long int)m_value
; }
100 operator unsigned long int() const { return (unsigned long)m_value
; }
101 operator short int() const { return (short int)m_value
; }
102 operator unsigned short int() const { return (unsigned short int)m_value
; }
104 // We need this operator for the "*p" part of expressions like "for (
105 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
106 // compilation would fail without it because the conversion to bool would
107 // be ambiguous (there are all these int types conversions...). (And adding
108 // operator unspecified_bool_type() would only makes the ambiguity worse.)
109 operator bool() const { return m_value
!= 0; }
110 bool operator!() const { return !((bool)*this); }
112 // And this one is needed by some (not all, but not using ifdefs makes the
113 // code easier) compilers to parse "str[0] && *p" successfully
114 bool operator&&(bool v
) const { return (bool)*this && v
; }
116 // Assignment operators:
117 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
118 wxUniChar
& operator=(const wxUniCharRef
& c
);
119 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
120 wxUniChar
& operator=(unsigned char c
) { m_value
= From8bit((char)c
); return *this; }
121 #if wxWCHAR_T_IS_SEPARATE_TYPE
122 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
124 wxUniChar
& operator=(int c
) { m_value
= c
; return *this; }
125 wxUniChar
& operator=(unsigned int c
) { m_value
= c
; return *this; }
126 wxUniChar
& operator=(long int c
) { m_value
= c
; return *this; }
127 wxUniChar
& operator=(unsigned long int c
) { m_value
= c
; return *this; }
128 wxUniChar
& operator=(short int c
) { m_value
= c
; return *this; }
129 wxUniChar
& operator=(unsigned short int c
) { m_value
= c
; return *this; }
131 // Comparison operators:
133 // define the given comparison operator for all the types
134 #define wxDEFINE_UNICHAR_OPERATOR(op) \
135 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
136 bool operator op(char c) const { return m_value op From8bit(c); } \
137 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
138 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
139 bool operator op(int c) const { return m_value op (value_type)c; } \
140 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
141 bool operator op(short int c) const { return m_value op (value_type)c; } \
142 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
143 bool operator op(long int c) const { return m_value op (value_type)c; } \
144 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
146 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
148 #undef wxDEFINE_UNICHAR_OPERATOR
150 // this is needed for expressions like 'Z'-c
151 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
152 int operator-(char c
) const { return m_value
- From8bit(c
); }
153 int operator-(unsigned char c
) const { return m_value
- From8bit((char)c
); }
154 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
158 static value_type
From8bit(char c
);
159 static char To8bit(value_type c
);
166 // Writeable reference to a character in wxString.
168 // This class can be used in the same way wxChar is used, except that changing
169 // its value updates the underlying string object.
170 class WXDLLIMPEXP_BASE wxUniCharRef
173 typedef wxStringImpl::iterator iterator
;
175 // create the reference
176 #if wxUSE_UNICODE_UTF8
177 wxUniCharRef(wxStringIteratorNode
& node
, iterator pos
) : m_node(node
), m_pos(pos
) {}
179 wxUniCharRef(iterator pos
) : m_pos(pos
) {}
183 // NB: we have to make this public, because we don't have wxString
184 // declaration available here and so can't declare wxString::iterator
185 // as friend; so at least don't use a ctor but a static function
186 // that must be used explicitly (this is more than using 'explicit'
187 // keyword on ctor!):
188 #if wxUSE_UNICODE_UTF8
189 static wxUniCharRef
CreateForString(wxStringIteratorNode
& node
, iterator pos
)
190 { return wxUniCharRef(node
, pos
); }
192 static wxUniCharRef
CreateForString(iterator pos
)
193 { return wxUniCharRef(pos
); }
196 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
198 #if wxUSE_UNICODE_UTF8
199 wxUniChar::Utf8CharBuffer
AsUTF8() const { return UniChar().AsUTF8(); }
200 #endif // wxUSE_UNICODE_UTF8
202 bool IsAscii() const { return UniChar().IsAscii(); }
204 // Assignment operators:
205 #if wxUSE_UNICODE_UTF8
206 wxUniCharRef
& operator=(const wxUniChar
& c
);
208 wxUniCharRef
& operator=(const wxUniChar
& c
) { *m_pos
= c
; return *this; }
211 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
212 { return *this = c
.UniChar(); }
214 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
215 wxUniCharRef
& operator=(unsigned char c
) { return *this = wxUniChar(c
); }
216 #if wxWCHAR_T_IS_SEPARATE_TYPE
217 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
219 wxUniCharRef
& operator=(int c
) { return *this = wxUniChar(c
); }
220 wxUniCharRef
& operator=(unsigned int c
) { return *this = wxUniChar(c
); }
221 wxUniCharRef
& operator=(short int c
) { return *this = wxUniChar(c
); }
222 wxUniCharRef
& operator=(unsigned short int c
) { return *this = wxUniChar(c
); }
223 wxUniCharRef
& operator=(long int c
) { return *this = wxUniChar(c
); }
224 wxUniCharRef
& operator=(unsigned long int c
) { return *this = wxUniChar(c
); }
226 // Conversions to the same types as wxUniChar is convertible too:
227 operator char() const { return UniChar(); }
228 operator unsigned char() const { return UniChar(); }
229 #if wxWCHAR_T_IS_SEPARATE_TYPE
230 operator wchar_t() const { return UniChar(); }
232 operator int() const { return UniChar(); }
233 operator unsigned int() const { return UniChar(); }
234 operator short int() const { return UniChar(); }
235 operator unsigned short int() const { return UniChar(); }
236 operator long int() const { return UniChar(); }
237 operator unsigned long int() const { return UniChar(); }
239 // see wxUniChar::operator bool etc. for explanation
240 operator bool() const { return (bool)UniChar(); }
241 bool operator!() const { return !UniChar(); }
242 bool operator&&(bool v
) const { return UniChar() && v
; }
244 // Comparison operators:
245 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
246 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
247 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
248 bool operator op(char c) const { return UniChar() op c; } \
249 bool operator op(unsigned char c) const { return UniChar() op c; } \
250 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
251 bool operator op(int c) const { return UniChar() op c; } \
252 bool operator op(unsigned int c) const { return UniChar() op c; } \
253 bool operator op(short int c) const { return UniChar() op c; } \
254 bool operator op(unsigned short int c) const { return UniChar() op c; } \
255 bool operator op(long int c) const { return UniChar() op c; } \
256 bool operator op(unsigned long int c) const { return UniChar() op c; }
258 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
260 #undef wxDEFINE_UNICHARREF_OPERATOR
262 // for expressions like c-'A':
263 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
264 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
265 int operator-(char c
) const { return UniChar() - c
; }
266 int operator-(unsigned char c
) const { return UniChar() - c
; }
267 int operator-(wchar_t c
) const { return UniChar() - c
; }
270 #if wxUSE_UNICODE_UTF8
271 wxUniChar
UniChar() const;
273 wxUniChar
UniChar() const { return *m_pos
; }
276 friend class WXDLLIMPEXP_BASE wxUniChar
;
279 // reference to the string and pointer to the character in string
280 #if wxUSE_UNICODE_UTF8
281 wxStringIteratorNode
& m_node
;
286 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
288 m_value
= c
.UniChar().m_value
;
291 inline wxUniChar
& wxUniChar::operator=(const wxUniCharRef
& c
)
293 m_value
= c
.UniChar().m_value
;
297 // Comparison operators for the case when wxUniChar(Ref) is the second operand
298 // implemented in terms of member comparison functions
300 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
302 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
303 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
305 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
306 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
308 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
312 // for expressions like c-'A':
313 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
314 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
315 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
317 #endif /* _WX_UNICHAR_H_ */