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_
14 #include "wx/chartype.h"
16 class WXDLLIMPEXP_BASE wxUniCharRef
;
18 // This class represents single Unicode character. It can be converted to
19 // and from char or wchar_t and implements commonly used character operations.
20 class WXDLLIMPEXP_BASE wxUniChar
23 // NB: this is not wchar_t on purpose, it needs to represent the entire
24 // Unicode code points range and wchar_t may be too small for that
25 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
26 typedef unsigned int unicode_type
;
28 wxUniChar() : m_value(0) {}
30 // Create the character from 8bit character value encoded in the current
32 wxUniChar(char c
) { m_value
= From8bit(c
); }
34 // Create the character from a wchar_t character value.
35 wxUniChar(wchar_t c
) { m_value
= c
; }
37 #ifndef wxWINT_T_IS_TYPEDEF
38 // Create the character from a wint_t character value.
39 wxUniChar(wint_t c
) { m_value
= c
; }
42 wxUniChar(int c
) { m_value
= c
; }
44 wxUniChar(const wxUniCharRef
& c
);
46 // Returns Unicode code point value of the character
47 unicode_type
GetValue() const { return m_value
; }
49 // Casts to char and wchar_t types:
50 operator char() const { return To8bit(m_value
); }
51 operator wchar_t() const { return m_value
; }
52 #ifndef wxWINT_T_IS_TYPEDEF
53 operator wint_t() const { return m_value
; }
55 operator int() const { return m_value
; }
57 // We need this operator for the "*p" part of expressions like "for (
58 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
59 // compilation would fail without it because the conversion to bool would
60 // be ambiguous (there are all these int types conversions...). (And adding
61 // operator unspecified_bool_type() would only makes the ambiguity worse.)
62 operator bool() const { return m_value
!= 0; }
63 bool operator!() const { return !((bool)*this); }
64 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
65 defined(__DIGITALMARS__) || defined(__BORLANDC__)
66 // We need this for VC++ < 8 or DigitalMars and expressions like
68 bool operator&&(bool v
) const { return (bool)*this && v
; }
71 // Assignment operators:
72 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
73 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
74 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
75 #ifndef wxWINT_T_IS_TYPEDEF
76 wxUniChar
& operator=(wint_t c
) { m_value
= c
; return *this; }
79 // Comparision operators:
80 bool operator==(const wxUniChar
& c
) const { return m_value
== c
.m_value
; }
81 bool operator==(char c
) const { return m_value
== From8bit(c
); }
82 bool operator==(wchar_t c
) const { return m_value
== (unicode_type
)c
; }
83 #ifndef wxWINT_T_IS_TYPEDEF
84 bool operator==(wint_t c
) const { return m_value
== (unicode_type
)c
; }
87 bool operator!=(const wxUniChar
& c
) const { return m_value
!= c
.m_value
; }
88 bool operator!=(char c
) const { return m_value
!= From8bit(c
); }
89 bool operator!=(wchar_t c
) const { return m_value
!= (unicode_type
)c
; }
90 #ifndef wxWINT_T_IS_TYPEDEF
91 bool operator!=(wint_t c
) const { return m_value
!= (unicode_type
)c
; }
94 bool operator>(const wxUniChar
& c
) const { return m_value
> c
.m_value
; }
95 bool operator>(char c
) const { return m_value
> (unicode_type
)c
; }
96 bool operator>(wchar_t c
) const { return m_value
> (unicode_type
)c
; }
97 #ifndef wxWINT_T_IS_TYPEDEF
98 bool operator>(wint_t c
) const { return m_value
> (unicode_type
)c
; }
101 bool operator<(const wxUniChar
& c
) const { return m_value
< c
.m_value
; }
102 bool operator<(char c
) const { return m_value
< From8bit(c
); }
103 bool operator<(wchar_t c
) const { return m_value
< (unicode_type
)c
; }
104 #ifndef wxWINT_T_IS_TYPEDEF
105 bool operator<(wint_t c
) const { return m_value
< (unicode_type
)c
; }
108 bool operator>=(const wxUniChar
& c
) const { return m_value
>= c
.m_value
; }
109 bool operator>=(char c
) const { return m_value
>= From8bit(c
); }
110 bool operator>=(wchar_t c
) const { return m_value
>= (unicode_type
)c
; }
111 #ifndef wxWINT_T_IS_TYPEDEF
112 bool operator>=(wint_t c
) const { return m_value
>= (unicode_type
)c
; }
115 bool operator<=(const wxUniChar
& c
) const { return m_value
<= c
.m_value
; }
116 bool operator<=(char c
) const { return m_value
<= From8bit(c
); }
117 bool operator<=(wchar_t c
) const { return m_value
<= (unicode_type
)c
; }
118 #ifndef wxWINT_T_IS_TYPEDEF
119 bool operator<=(wint_t c
) const { return m_value
<= (unicode_type
)c
; }
122 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
123 int operator-(char c
) const { return m_value
- From8bit(c
); }
124 int operator-(wchar_t c
) const { return m_value
- (unicode_type
)c
; }
125 #ifndef wxWINT_T_IS_TYPEDEF
126 int operator-(wint_t c
) const { return m_value
- (unicode_type
)c
; }
130 static unicode_type
From8bit(char c
);
131 static char To8bit(unicode_type c
);
134 unicode_type m_value
;
138 // Writeable reference to a character in wxString.
140 // This class can be used in the same way wxChar is used, except that changing
141 // its value updates the underlying string object.
142 class WXDLLIMPEXP_BASE wxUniCharRef
145 // create the reference
146 // FIXME-UTF8: the interface will need changes for UTF-8 build
147 wxUniCharRef(wxChar
*pos
) : m_pos(pos
) {}
150 // NB: we have to make this public, because we don't have wxString
151 // declaration available here and so can't declare wxString::iterator
152 // as friend; so at least don't use a ctor but a static function
153 // that must be used explicitly (this is more than using 'explicit'
154 // keyword on ctor!):
156 // FIXME-UTF8: the interface will need changes for UTF-8 build
157 static wxUniCharRef
CreateForString(wxChar
*pos
)
158 { return wxUniCharRef(pos
); }
160 wxUniChar::unicode_type
GetValue() const { return UniChar().GetValue(); }
162 // Assignment operators:
163 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
169 wxUniCharRef
& operator=(const wxUniChar
& c
)
175 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
176 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
178 // Casts to wxUniChar type:
179 operator char() const { return UniChar(); }
180 operator wchar_t() const { return UniChar(); }
181 #ifndef wxWINT_T_IS_TYPEDEF
182 operator wint_t() const { return UniChar(); }
184 operator int() const { return UniChar(); }
186 // see wxUniChar::operator bool etc. for explanation
187 operator bool() const { return (bool)UniChar(); }
188 bool operator!() const { return !UniChar(); }
189 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
190 defined(__DIGITALMARS__) || defined(__BORLANDC__)
191 bool operator&&(bool v
) const { return UniChar() && v
; }
194 // Comparision operators:
195 bool operator==(const wxUniCharRef
& c
) const { return m_pos
== c
.m_pos
; }
196 bool operator==(const wxUniChar
& c
) const { return UniChar() == c
; }
197 bool operator==(char c
) const { return UniChar() == c
; }
198 bool operator==(wchar_t c
) const { return UniChar() == c
; }
199 #ifndef wxWINT_T_IS_TYPEDEF
200 bool operator==(wint_t c
) const { return UniChar() == c
; }
203 bool operator!=(const wxUniCharRef
& c
) const { return m_pos
!= c
.m_pos
; }
204 bool operator!=(const wxUniChar
& c
) const { return UniChar() != c
; }
205 bool operator!=(char c
) const { return UniChar() != c
; }
206 bool operator!=(wchar_t c
) const { return UniChar() != c
; }
207 #ifndef wxWINT_T_IS_TYPEDEF
208 bool operator!=(wint_t c
) const { return UniChar() != c
; }
211 bool operator>(const wxUniCharRef
& c
) const { return UniChar() > c
.UniChar(); }
212 bool operator>(const wxUniChar
& c
) const { return UniChar() > c
; }
213 bool operator>(char c
) const { return UniChar() > c
; }
214 bool operator>(wchar_t c
) const { return UniChar() > c
; }
215 #ifndef wxWINT_T_IS_TYPEDEF
216 bool operator>(wint_t c
) const { return UniChar() > c
; }
219 bool operator<(const wxUniCharRef
& c
) const { return UniChar() < c
.UniChar(); }
220 bool operator<(const wxUniChar
& c
) const { return UniChar() < c
; }
221 bool operator<(char c
) const { return UniChar() < c
; }
222 bool operator<(wchar_t c
) const { return UniChar() < c
; }
223 #ifndef wxWINT_T_IS_TYPEDEF
224 bool operator<(wint_t c
) const { return UniChar() < c
; }
227 bool operator>=(const wxUniCharRef
& c
) const { return UniChar() >= c
.UniChar(); }
228 bool operator>=(const wxUniChar
& c
) const { return UniChar() >= c
; }
229 bool operator>=(char c
) const { return UniChar() >= c
; }
230 bool operator>=(wchar_t c
) const { return UniChar() >= c
; }
231 #ifndef wxWINT_T_IS_TYPEDEF
232 bool operator>=(wint_t c
) const { return UniChar() >= c
; }
235 bool operator<=(const wxUniCharRef
& c
) const { return UniChar() <= c
.UniChar(); }
236 bool operator<=(const wxUniChar
& c
) const { return UniChar() <= c
; }
237 bool operator<=(char c
) const { return UniChar() <= c
; }
238 bool operator<=(wchar_t c
) const { return UniChar() <= c
; }
239 #ifndef wxWINT_T_IS_TYPEDEF
240 bool operator<=(wint_t c
) const { return UniChar() <= c
; }
243 // for expressions like c-'A':
244 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
245 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
246 int operator-(char c
) const { return UniChar() - c
; }
247 int operator-(wchar_t c
) const { return UniChar() - c
; }
248 #ifndef wxWINT_T_IS_TYPEDEF
249 int operator-(wint_t c
) const { return UniChar() - c
; }
253 wxUniChar
UniChar() const { return *m_pos
; }
254 friend class WXDLLIMPEXP_BASE wxUniChar
;
257 // pointer to the character in string
261 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
263 m_value
= c
.UniChar().m_value
;
266 // Comparision operators for the case when wxUniChar(Ref) is the second operand:
267 inline bool operator==(char c1
, const wxUniChar
& c2
) { return c2
== c1
; }
268 inline bool operator==(wchar_t c1
, const wxUniChar
& c2
) { return c2
== c1
; }
269 #ifndef wxWINT_T_IS_TYPEDEF
270 inline bool operator==(wint_t c1
, const wxUniChar
& c2
) { return c2
== c1
; }
273 inline bool operator!=(char c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
274 inline bool operator!=(wchar_t c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
275 #ifndef wxWINT_T_IS_TYPEDEF
276 inline bool operator!=(wint_t c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
279 inline bool operator>(char c1
, const wxUniChar
& c2
) { return c2
< c1
; }
280 inline bool operator>(wchar_t c1
, const wxUniChar
& c2
) { return c2
< c1
; }
281 #ifndef wxWINT_T_IS_TYPEDEF
282 inline bool operator>(wint_t c1
, const wxUniChar
& c2
) { return c2
< c1
; }
285 inline bool operator<(char c1
, const wxUniChar
& c2
) { return c2
> c1
; }
286 inline bool operator<(wchar_t c1
, const wxUniChar
& c2
) { return c2
> c1
; }
287 #ifndef wxWINT_T_IS_TYPEDEF
288 inline bool operator<(wint_t c1
, const wxUniChar
& c2
) { return c2
> c1
; }
291 inline bool operator>=(char c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
292 inline bool operator>=(wchar_t c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
293 #ifndef wxWINT_T_IS_TYPEDEF
294 inline bool operator>=(wint_t c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
297 inline bool operator<=(char c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
298 inline bool operator<=(wchar_t c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
299 #ifndef wxWINT_T_IS_TYPEDEF
300 inline bool operator<=(wint_t c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
304 inline bool operator==(char c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
305 inline bool operator==(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
306 #ifndef wxWINT_T_IS_TYPEDEF
307 inline bool operator==(wint_t c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
309 inline bool operator==(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
311 inline bool operator!=(char c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
312 inline bool operator!=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
313 #ifndef wxWINT_T_IS_TYPEDEF
314 inline bool operator!=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
316 inline bool operator!=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
318 inline bool operator>(char c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
319 inline bool operator>(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
320 #ifndef wxWINT_T_IS_TYPEDEF
321 inline bool operator>(wint_t c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
323 inline bool operator>(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
325 inline bool operator<(char c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
326 inline bool operator<(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
327 #ifndef wxWINT_T_IS_TYPEDEF
328 inline bool operator<(wint_t c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
330 inline bool operator<(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
332 inline bool operator>=(char c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
333 inline bool operator>=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
334 #ifndef wxWINT_T_IS_TYPEDEF
335 inline bool operator>=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
337 inline bool operator>=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
339 inline bool operator<=(char c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
340 inline bool operator<=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
341 #ifndef wxWINT_T_IS_TYPEDEF
342 inline bool operator<=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
344 inline bool operator<=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
346 // for expressions like c-'A':
347 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
348 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
349 #ifndef wxWINT_T_IS_TYPEDEF
350 inline int operator-(wint_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
352 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
354 #endif /* _WX_UNICHAR_H_ */