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 // wint_t is just a typedef for wchar_t for many old compilers but for modern
19 // ones it's a separate type and we must provide a conversion to it to allow
20 // passing wxUniChar[Ref] to functions taking wint_t such as iswalnum() &c
21 #if (defined(__GNUC__) && !defined(__DARWIN__) && !defined(__OS2__)) || \
22 (defined(__VISUALC__) && defined(_NATIVE_WCHAR_T_DEFINED))
23 #define wxWINT_T_IS_SEPARATE_TYPE
26 // helper macro for doing something dependent on whether wint_t is or isn't a
27 // typedef inside another macro
28 #ifdef wxWINT_T_IS_SEPARATE_TYPE
29 #define wxIF_WINT_T_TYPE(x) x
30 #else // !wxWINT_T_IS_SEPARATE_TYPE
31 #define wxIF_WINT_T_TYPE(x)
32 #endif // wxWINT_T_IS_SEPARATE_TYPE/!wxWINT_T_IS_SEPARATE_TYPE
34 // wchar_t seems to be defined as unsigned short by all Windows compilers but
35 // unsigned int everywhere else
37 #define wxWCHAR_T_IS_UINT
40 class WXDLLIMPEXP_BASE wxUniCharRef
;
42 // This class represents single Unicode character. It can be converted to
43 // and from char or wchar_t and implements commonly used character operations.
44 class WXDLLIMPEXP_BASE wxUniChar
47 // NB: this is not wchar_t on purpose, it needs to represent the entire
48 // Unicode code points range and wchar_t may be too small for that
49 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
50 typedef wxUint32 value_type
;
52 wxUniChar() : m_value(0) {}
54 // Create the character from 8bit character value encoded in the current
56 wxUniChar(char c
) { m_value
= From8bit(c
); }
57 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
59 // Create the character from a wchar_t character value.
60 wxUniChar(wchar_t c
) { m_value
= c
; }
61 #ifdef wxWINT_T_IS_SEPARATE_TYPE
62 wxUniChar(wint_t c
) { m_value
= c
; }
65 wxUniChar(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 // Returns true if the character is an ASCII character:
73 bool IsAscii() const { return m_value
< 0x80; }
75 // Conversions to char and wchar_t types: all of those are needed to be
76 // able to pass wxUniChars to verious standard narrow and wide character
78 operator char() const { return To8bit(m_value
); }
79 operator wchar_t() const { return m_value
; }
80 operator int() const { return m_value
; }
81 #ifndef wxWCHAR_T_IS_UINT
82 operator unsigned int() const { return m_value
; }
85 // More conversions needed for other standard functions: uchar is for VC++
86 // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined)
87 // and some wide character functions take wint_t which happens to be the
88 // same as wchar_t for Windows compilers but not for g++ (except for the
89 // special Apple version)
90 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
91 #ifdef wxWINT_T_IS_SEPARATE_TYPE
92 operator wint_t() const { return m_value
; }
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); }
102 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
103 defined(__DIGITALMARS__) || defined(__BORLANDC__)
104 // We need this for VC++ < 8 or DigitalMars and expressions like
106 bool operator&&(bool v
) const { return (bool)*this && v
; }
109 // Assignment operators:
110 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
111 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
112 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
113 wxUniChar
& operator=(int c
) { m_value
= c
; return *this; }
114 #ifdef wxWINT_T_IS_SEPARATE_TYPE
115 wxUniChar
& operator=(wint_t c
) { m_value
= c
; return *this; }
118 // Comparison operators:
120 // define the given comparison operator for all the types
121 #define wxDEFINE_UNICHAR_OPERATOR(op) \
122 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
123 bool operator op(char c) const { return m_value op From8bit(c); } \
124 bool operator op(wchar_t c) const { return m_value op (value_type)c; } \
125 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return m_value op (value_type)c; } )
127 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
129 #undef wxDEFINE_UNICHAR_OPERATOR
131 // this is needed for expressions like 'Z'-c
132 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
133 int operator-(char c
) const { return m_value
- From8bit(c
); }
134 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
135 #ifdef wxWINT_T_IS_SEPARATE_TYPE
136 int operator-(wint_t c
) const { return m_value
- (value_type
)c
; }
140 static value_type
From8bit(char c
);
141 static char To8bit(value_type c
);
148 // Writeable reference to a character in wxString.
150 // This class can be used in the same way wxChar is used, except that changing
151 // its value updates the underlying string object.
152 class WXDLLIMPEXP_BASE wxUniCharRef
155 typedef wxStringImpl::iterator iterator
;
157 // create the reference
158 #if wxUSE_UNICODE_UTF8
159 wxUniCharRef(wxStringImpl
& str
, iterator pos
) : m_str(str
), m_pos(pos
) {}
161 wxUniCharRef(iterator pos
) : m_pos(pos
) {}
165 // NB: we have to make this public, because we don't have wxString
166 // declaration available here and so can't declare wxString::iterator
167 // as friend; so at least don't use a ctor but a static function
168 // that must be used explicitly (this is more than using 'explicit'
169 // keyword on ctor!):
170 #if wxUSE_UNICODE_UTF8
171 static wxUniCharRef
CreateForString(wxStringImpl
& str
, iterator pos
)
172 { return wxUniCharRef(str
, pos
); }
174 static wxUniCharRef
CreateForString(iterator pos
)
175 { return wxUniCharRef(pos
); }
178 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
179 bool IsAscii() const { return UniChar().GetValue(); }
181 // Assignment operators:
182 #if wxUSE_UNICODE_UTF8
183 wxUniCharRef
& operator=(const wxUniChar
& c
);
185 wxUniCharRef
& operator=(const wxUniChar
& c
) { *m_pos
= c
; return *this; }
188 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
189 { return *this = c
.UniChar(); }
191 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
192 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
193 wxUniCharRef
& operator=(int c
) { return *this = wxUniChar(c
); }
194 #ifdef wxWINT_T_IS_SEPARATE_TYPE
195 wxUniCharRef
& operator=(wint_t c
) { return *this = wxUniChar(c
); }
198 // Conversions to the same types as wxUniChar is convertible too:
199 operator char() const { return UniChar(); }
200 operator int() const { return UniChar(); }
201 operator unsigned char() const { return UniChar(); }
202 operator wchar_t() const { return UniChar(); }
203 #ifdef wxWINT_T_IS_SEPARATE_TYPE
204 operator wint_t() const { return UniChar(); }
206 #ifndef wxWCHAR_T_IS_UINT
207 operator unsigned int() const { return UniChar(); }
210 // see wxUniChar::operator bool etc. for explanation
211 operator bool() const { return (bool)UniChar(); }
212 bool operator!() const { return !UniChar(); }
213 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
214 defined(__DIGITALMARS__) || defined(__BORLANDC__)
215 bool operator&&(bool v
) const { return UniChar() && v
; }
218 // Comparison operators:
219 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
220 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
221 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
222 bool operator op(char c) const { return UniChar() op c; } \
223 bool operator op(wchar_t c) const { return UniChar() op c; } \
224 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return UniChar() op c; } )
226 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
228 #undef wxDEFINE_UNICHARREF_OPERATOR
230 // for expressions like c-'A':
231 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
232 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
233 int operator-(char c
) const { return UniChar() - c
; }
234 int operator-(wchar_t c
) const { return UniChar() - c
; }
235 #ifdef wxWINT_T_IS_SEPARATE_TYPE
236 int operator-(wint_t c
) const { return UniChar() - c
; }
240 wxUniChar
UniChar() const
242 #if wxUSE_UNICODE_UTF8
243 return DecodeChar(m_pos
);
249 #if wxUSE_UNICODE_UTF8
250 // FIXME-UTF8: move this to a separate 'string operations' class
251 static wxUniChar
DecodeChar(wxStringImpl::const_iterator i
);
252 friend class WXDLLIMPEXP_BASE wxString
;
255 friend class WXDLLIMPEXP_BASE wxUniChar
;
258 // reference to the string and pointer to the character in string
259 #if wxUSE_UNICODE_UTF8
265 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
267 m_value
= c
.UniChar().m_value
;
270 // Comparison operators for the case when wxUniChar(Ref) is the second operand
271 // implemented in terms of member comparison functions
273 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
275 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
276 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
278 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
279 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
281 #ifdef wxWINT_T_IS_SEPARATE_TYPE
282 wxDEFINE_COMPARISONS(wint_t, const wxUniChar
&, wxCMP_REVERSE
)
283 wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef
&, wxCMP_REVERSE
)
286 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
290 // for expressions like c-'A':
291 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
292 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
293 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
294 #ifdef wxWINT_T_IS_SEPARATE_TYPE
295 inline int operator-(wint_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
298 #endif /* _WX_UNICHAR_H_ */