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
20 #define wxWCHAR_T_IS_SEPARATE_TYPE
22 #if defined(__VISUALC__) && defined(_NATIVE_WCHAR_T_DEFINED)
23 #define wxWCHAR_T_IS_SEPARATE_TYPE
27 // helper macro for doing something dependent on whether wchar_t is or isn't a
28 // typedef inside another macro
29 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
30 #define wxIF_WCHAR_T_TYPE(x) x
31 #else // !wxWCHAR_T_IS_SEPARATE_TYPE
32 #define wxIF_WCHAR_T_TYPE(x)
33 #endif // wxWCHAR_T_IS_SEPARATE_TYPE/!wxWCHAR_T_IS_SEPARATE_TYPE
35 class WXDLLIMPEXP_BASE wxUniCharRef
;
36 class WXDLLIMPEXP_BASE wxStringIteratorNode
;
38 // This class represents single Unicode character. It can be converted to
39 // and from char or wchar_t and implements commonly used character operations.
40 class WXDLLIMPEXP_BASE wxUniChar
43 // NB: this is not wchar_t on purpose, it needs to represent the entire
44 // Unicode code points range and wchar_t may be too small for that
45 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
46 typedef wxUint32 value_type
;
48 wxUniChar() : m_value(0) {}
50 // Create the character from 8bit character value encoded in the current
52 wxUniChar(char c
) { m_value
= From8bit(c
); }
53 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
55 // Create the character from a wchar_t character value.
56 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
57 wxUniChar(wchar_t c
) { m_value
= c
; }
60 wxUniChar(int c
) { m_value
= c
; }
61 wxUniChar(unsigned int c
) { m_value
= c
; }
62 wxUniChar(long int c
) { m_value
= c
; }
63 wxUniChar(unsigned long int c
) { m_value
= c
; }
64 wxUniChar(short int c
) { m_value
= c
; }
65 wxUniChar(unsigned short int c
) { m_value
= c
; }
67 wxUniChar(const wxUniCharRef
& c
);
69 // Returns Unicode code point value of the character
70 value_type
GetValue() const { return m_value
; }
72 #if wxUSE_UNICODE_UTF8
73 // buffer for single UTF-8 character
77 operator const char*() const { return data
; }
80 // returns the character encoded as UTF-8
81 // (NB: implemented in stringops.cpp)
82 Utf8CharBuffer
AsUTF8() const;
83 #endif // wxUSE_UNICODE_UTF8
85 // Returns true if the character is an ASCII character:
86 bool IsAscii() const { return m_value
< 0x80; }
88 // Conversions to char and wchar_t types: all of those are needed to be
89 // able to pass wxUniChars to verious standard narrow and wide character
91 operator char() const { return To8bit(m_value
); }
92 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
93 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
94 operator wchar_t() const { return m_value
; }
96 operator int() const { return m_value
; }
97 operator unsigned int() const { return m_value
; }
98 operator long int() const { return m_value
; }
99 operator unsigned long int() const { return m_value
; }
100 operator short int() const { return m_value
; }
101 operator unsigned short int() const { return m_value
; }
103 // We need this operator for the "*p" part of expressions like "for (
104 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
105 // compilation would fail without it because the conversion to bool would
106 // be ambiguous (there are all these int types conversions...). (And adding
107 // operator unspecified_bool_type() would only makes the ambiguity worse.)
108 operator bool() const { return m_value
!= 0; }
109 bool operator!() const { return !((bool)*this); }
111 // And this one is needed by some (not all, but not using ifdefs makes the
112 // code easier) compilers to parse "str[0] && *p" successfully
113 bool operator&&(bool v
) const { return (bool)*this && v
; }
115 // Assignment operators:
116 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
117 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
118 wxUniChar
& operator=(unsigned char c
) { m_value
= From8bit((char)c
); return *this; }
119 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
120 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
122 wxUniChar
& operator=(int c
) { m_value
= c
; return *this; }
123 wxUniChar
& operator=(unsigned int c
) { m_value
= c
; return *this; }
124 wxUniChar
& operator=(long int c
) { m_value
= c
; return *this; }
125 wxUniChar
& operator=(unsigned long int c
) { m_value
= c
; return *this; }
126 wxUniChar
& operator=(short int c
) { m_value
= c
; return *this; }
127 wxUniChar
& operator=(unsigned short int c
) { m_value
= c
; return *this; }
129 // Comparison operators:
131 // define the given comparison operator for all the types
132 #define wxDEFINE_UNICHAR_OPERATOR(op) \
133 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
134 bool operator op(char c) const { return m_value op From8bit(c); } \
135 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
136 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
137 bool operator op(int c) const { return m_value op (value_type)c; } \
138 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
139 bool operator op(short int c) const { return m_value op (value_type)c; } \
140 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
141 bool operator op(long int c) const { return m_value op (value_type)c; } \
142 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
144 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
146 #undef wxDEFINE_UNICHAR_OPERATOR
148 // this is needed for expressions like 'Z'-c
149 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
150 int operator-(char c
) const { return m_value
- From8bit(c
); }
151 int operator-(unsigned char c
) const { return m_value
- From8bit((char)c
); }
152 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
156 static value_type
From8bit(char c
);
157 static char To8bit(value_type c
);
164 // Writeable reference to a character in wxString.
166 // This class can be used in the same way wxChar is used, except that changing
167 // its value updates the underlying string object.
168 class WXDLLIMPEXP_BASE wxUniCharRef
171 typedef wxStringImpl::iterator iterator
;
173 // create the reference
174 #if wxUSE_UNICODE_UTF8
175 wxUniCharRef(wxStringIteratorNode
& node
, iterator pos
) : m_node(node
), m_pos(pos
) {}
177 wxUniCharRef(iterator pos
) : m_pos(pos
) {}
181 // NB: we have to make this public, because we don't have wxString
182 // declaration available here and so can't declare wxString::iterator
183 // as friend; so at least don't use a ctor but a static function
184 // that must be used explicitly (this is more than using 'explicit'
185 // keyword on ctor!):
186 #if wxUSE_UNICODE_UTF8
187 static wxUniCharRef
CreateForString(wxStringIteratorNode
& node
, iterator pos
)
188 { return wxUniCharRef(node
, pos
); }
190 static wxUniCharRef
CreateForString(iterator pos
)
191 { return wxUniCharRef(pos
); }
194 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
196 #if wxUSE_UNICODE_UTF8
197 wxUniChar::Utf8CharBuffer
AsUTF8() const { return UniChar().AsUTF8(); }
198 #endif // wxUSE_UNICODE_UTF8
200 bool IsAscii() const { return UniChar().IsAscii(); }
202 // Assignment operators:
203 #if wxUSE_UNICODE_UTF8
204 wxUniCharRef
& operator=(const wxUniChar
& c
);
206 wxUniCharRef
& operator=(const wxUniChar
& c
) { *m_pos
= c
; return *this; }
209 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
210 { return *this = c
.UniChar(); }
212 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
213 wxUniCharRef
& operator=(unsigned char c
) { return *this = wxUniChar(c
); }
214 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
215 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
217 wxUniCharRef
& operator=(int c
) { return *this = wxUniChar(c
); }
218 wxUniCharRef
& operator=(unsigned int c
) { return *this = wxUniChar(c
); }
219 wxUniCharRef
& operator=(short int c
) { return *this = wxUniChar(c
); }
220 wxUniCharRef
& operator=(unsigned short int c
) { return *this = wxUniChar(c
); }
221 wxUniCharRef
& operator=(long int c
) { return *this = wxUniChar(c
); }
222 wxUniCharRef
& operator=(unsigned long int c
) { return *this = wxUniChar(c
); }
224 // Conversions to the same types as wxUniChar is convertible too:
225 operator char() const { return UniChar(); }
226 operator unsigned char() const { return UniChar(); }
227 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
228 operator wchar_t() const { return UniChar(); }
230 operator int() const { return UniChar(); }
231 operator unsigned int() const { return UniChar(); }
232 operator short int() const { return UniChar(); }
233 operator unsigned short int() const { return UniChar(); }
234 operator long int() const { return UniChar(); }
235 operator unsigned long int() const { return UniChar(); }
237 // see wxUniChar::operator bool etc. for explanation
238 operator bool() const { return (bool)UniChar(); }
239 bool operator!() const { return !UniChar(); }
240 bool operator&&(bool v
) const { return UniChar() && v
; }
242 // Comparison operators:
243 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
244 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
245 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
246 bool operator op(char c) const { return UniChar() op c; } \
247 bool operator op(unsigned char c) const { return UniChar() op c; } \
248 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
249 bool operator op(int c) const { return UniChar() op c; } \
250 bool operator op(unsigned int c) const { return UniChar() op c; } \
251 bool operator op(short int c) const { return UniChar() op c; } \
252 bool operator op(unsigned short int c) const { return UniChar() op c; } \
253 bool operator op(long int c) const { return UniChar() op c; } \
254 bool operator op(unsigned long int c) const { return UniChar() op c; }
256 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
258 #undef wxDEFINE_UNICHARREF_OPERATOR
260 // for expressions like c-'A':
261 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
262 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
263 int operator-(char c
) const { return UniChar() - c
; }
264 int operator-(unsigned char c
) const { return UniChar() - c
; }
265 int operator-(wchar_t c
) const { return UniChar() - c
; }
268 #if wxUSE_UNICODE_UTF8
269 wxUniChar
UniChar() const;
271 wxUniChar
UniChar() const { return *m_pos
; }
274 friend class WXDLLIMPEXP_BASE wxUniChar
;
277 // reference to the string and pointer to the character in string
278 #if wxUSE_UNICODE_UTF8
279 wxStringIteratorNode
& m_node
;
284 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
286 m_value
= c
.UniChar().m_value
;
289 // Comparison operators for the case when wxUniChar(Ref) is the second operand
290 // implemented in terms of member comparison functions
292 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
294 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
295 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
297 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
298 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
300 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
304 // for expressions like c-'A':
305 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
306 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
307 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
309 #endif /* _WX_UNICHAR_H_ */