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"
17 class WXDLLIMPEXP_BASE wxUniCharRef
;
19 // This class represents single Unicode character. It can be converted to
20 // and from char or wchar_t and implements commonly used character operations.
21 class WXDLLIMPEXP_BASE wxUniChar
24 // NB: this is not wchar_t on purpose, it needs to represent the entire
25 // Unicode code points range and wchar_t may be too small for that
26 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
27 typedef wxUint32 value_type
;
29 wxUniChar() : m_value(0) {}
31 // Create the character from 8bit character value encoded in the current
33 wxUniChar(char c
) { m_value
= From8bit(c
); }
35 // Create the character from a wchar_t character value.
36 wxUniChar(wchar_t c
) { m_value
= c
; }
38 #ifndef wxWINT_T_IS_TYPEDEF
39 // Create the character from a wint_t character value.
40 wxUniChar(wint_t c
) { m_value
= c
; }
43 wxUniChar(int c
) { m_value
= c
; }
45 wxUniChar(const wxUniCharRef
& c
);
47 // Returns Unicode code point value of the character
48 value_type
GetValue() const { return m_value
; }
50 // Casts to char and wchar_t types:
51 operator char() const { return To8bit(m_value
); }
52 operator wchar_t() const { return m_value
; }
53 #ifndef wxWINT_T_IS_TYPEDEF
54 operator wint_t() const { return m_value
; }
56 operator int() const { return m_value
; }
58 // We need this operator for the "*p" part of expressions like "for (
59 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
60 // compilation would fail without it because the conversion to bool would
61 // be ambiguous (there are all these int types conversions...). (And adding
62 // operator unspecified_bool_type() would only makes the ambiguity worse.)
63 operator bool() const { return m_value
!= 0; }
64 bool operator!() const { return !((bool)*this); }
65 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
66 defined(__DIGITALMARS__) || defined(__BORLANDC__)
67 // We need this for VC++ < 8 or DigitalMars and expressions like
69 bool operator&&(bool v
) const { return (bool)*this && v
; }
72 // Assignment operators:
73 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
74 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
75 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
76 #ifndef wxWINT_T_IS_TYPEDEF
77 wxUniChar
& operator=(wint_t c
) { m_value
= c
; return *this; }
80 // Comparision operators:
81 bool operator==(const wxUniChar
& c
) const { return m_value
== c
.m_value
; }
82 bool operator==(char c
) const { return m_value
== From8bit(c
); }
83 bool operator==(wchar_t c
) const { return m_value
== (value_type
)c
; }
84 #ifndef wxWINT_T_IS_TYPEDEF
85 bool operator==(wint_t c
) const { return m_value
== (value_type
)c
; }
88 bool operator!=(const wxUniChar
& c
) const { return m_value
!= c
.m_value
; }
89 bool operator!=(char c
) const { return m_value
!= From8bit(c
); }
90 bool operator!=(wchar_t c
) const { return m_value
!= (value_type
)c
; }
91 #ifndef wxWINT_T_IS_TYPEDEF
92 bool operator!=(wint_t c
) const { return m_value
!= (value_type
)c
; }
95 bool operator>(const wxUniChar
& c
) const { return m_value
> c
.m_value
; }
96 bool operator>(char c
) const { return m_value
> (value_type
)c
; }
97 bool operator>(wchar_t c
) const { return m_value
> (value_type
)c
; }
98 #ifndef wxWINT_T_IS_TYPEDEF
99 bool operator>(wint_t c
) const { return m_value
> (value_type
)c
; }
102 bool operator<(const wxUniChar
& c
) const { return m_value
< c
.m_value
; }
103 bool operator<(char c
) const { return m_value
< From8bit(c
); }
104 bool operator<(wchar_t c
) const { return m_value
< (value_type
)c
; }
105 #ifndef wxWINT_T_IS_TYPEDEF
106 bool operator<(wint_t c
) const { return m_value
< (value_type
)c
; }
109 bool operator>=(const wxUniChar
& c
) const { return m_value
>= c
.m_value
; }
110 bool operator>=(char c
) const { return m_value
>= From8bit(c
); }
111 bool operator>=(wchar_t c
) const { return m_value
>= (value_type
)c
; }
112 #ifndef wxWINT_T_IS_TYPEDEF
113 bool operator>=(wint_t c
) const { return m_value
>= (value_type
)c
; }
116 bool operator<=(const wxUniChar
& c
) const { return m_value
<= c
.m_value
; }
117 bool operator<=(char c
) const { return m_value
<= From8bit(c
); }
118 bool operator<=(wchar_t c
) const { return m_value
<= (value_type
)c
; }
119 #ifndef wxWINT_T_IS_TYPEDEF
120 bool operator<=(wint_t c
) const { return m_value
<= (value_type
)c
; }
123 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
124 int operator-(char c
) const { return m_value
- From8bit(c
); }
125 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
126 #ifndef wxWINT_T_IS_TYPEDEF
127 int operator-(wint_t c
) const { return m_value
- (value_type
)c
; }
131 static value_type
From8bit(char c
);
132 static char To8bit(value_type c
);
139 // Writeable reference to a character in wxString.
141 // This class can be used in the same way wxChar is used, except that changing
142 // its value updates the underlying string object.
143 class WXDLLIMPEXP_BASE wxUniCharRef
146 // create the reference
147 // FIXME-UTF8: the interface will need changes for UTF-8 build
148 wxUniCharRef(wxChar
*pos
) : m_pos(pos
) {}
151 // NB: we have to make this public, because we don't have wxString
152 // declaration available here and so can't declare wxString::iterator
153 // as friend; so at least don't use a ctor but a static function
154 // that must be used explicitly (this is more than using 'explicit'
155 // keyword on ctor!):
157 // FIXME-UTF8: the interface will need changes for UTF-8 build
158 static wxUniCharRef
CreateForString(wxChar
*pos
)
159 { return wxUniCharRef(pos
); }
161 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
163 // Assignment operators:
164 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
170 wxUniCharRef
& operator=(const wxUniChar
& c
)
176 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
177 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
179 // Casts to wxUniChar type:
180 operator char() const { return UniChar(); }
181 operator wchar_t() const { return UniChar(); }
182 #ifndef wxWINT_T_IS_TYPEDEF
183 operator wint_t() const { return UniChar(); }
185 operator int() const { return UniChar(); }
187 // see wxUniChar::operator bool etc. for explanation
188 operator bool() const { return (bool)UniChar(); }
189 bool operator!() const { return !UniChar(); }
190 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
191 defined(__DIGITALMARS__) || defined(__BORLANDC__)
192 bool operator&&(bool v
) const { return UniChar() && v
; }
195 // Comparision operators:
196 bool operator==(const wxUniCharRef
& c
) const { return m_pos
== c
.m_pos
; }
197 bool operator==(const wxUniChar
& c
) const { return UniChar() == c
; }
198 bool operator==(char c
) const { return UniChar() == c
; }
199 bool operator==(wchar_t c
) const { return UniChar() == c
; }
200 #ifndef wxWINT_T_IS_TYPEDEF
201 bool operator==(wint_t c
) const { return UniChar() == c
; }
204 bool operator!=(const wxUniCharRef
& c
) const { return m_pos
!= c
.m_pos
; }
205 bool operator!=(const wxUniChar
& c
) const { return UniChar() != c
; }
206 bool operator!=(char c
) const { return UniChar() != c
; }
207 bool operator!=(wchar_t c
) const { return UniChar() != c
; }
208 #ifndef wxWINT_T_IS_TYPEDEF
209 bool operator!=(wint_t c
) const { return UniChar() != c
; }
212 bool operator>(const wxUniCharRef
& c
) const { return UniChar() > c
.UniChar(); }
213 bool operator>(const wxUniChar
& c
) const { return UniChar() > c
; }
214 bool operator>(char c
) const { return UniChar() > c
; }
215 bool operator>(wchar_t c
) const { return UniChar() > c
; }
216 #ifndef wxWINT_T_IS_TYPEDEF
217 bool operator>(wint_t c
) const { return UniChar() > c
; }
220 bool operator<(const wxUniCharRef
& c
) const { return UniChar() < c
.UniChar(); }
221 bool operator<(const wxUniChar
& c
) const { return UniChar() < c
; }
222 bool operator<(char c
) const { return UniChar() < c
; }
223 bool operator<(wchar_t c
) const { return UniChar() < c
; }
224 #ifndef wxWINT_T_IS_TYPEDEF
225 bool operator<(wint_t c
) const { return UniChar() < c
; }
228 bool operator>=(const wxUniCharRef
& c
) const { return UniChar() >= c
.UniChar(); }
229 bool operator>=(const wxUniChar
& c
) const { return UniChar() >= c
; }
230 bool operator>=(char c
) const { return UniChar() >= c
; }
231 bool operator>=(wchar_t c
) const { return UniChar() >= c
; }
232 #ifndef wxWINT_T_IS_TYPEDEF
233 bool operator>=(wint_t c
) const { return UniChar() >= c
; }
236 bool operator<=(const wxUniCharRef
& c
) const { return UniChar() <= c
.UniChar(); }
237 bool operator<=(const wxUniChar
& c
) const { return UniChar() <= c
; }
238 bool operator<=(char c
) const { return UniChar() <= c
; }
239 bool operator<=(wchar_t c
) const { return UniChar() <= c
; }
240 #ifndef wxWINT_T_IS_TYPEDEF
241 bool operator<=(wint_t c
) const { return UniChar() <= c
; }
244 // for expressions like c-'A':
245 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
246 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
247 int operator-(char c
) const { return UniChar() - c
; }
248 int operator-(wchar_t c
) const { return UniChar() - c
; }
249 #ifndef wxWINT_T_IS_TYPEDEF
250 int operator-(wint_t c
) const { return UniChar() - c
; }
254 wxUniChar
UniChar() const { return *m_pos
; }
255 friend class WXDLLIMPEXP_BASE wxUniChar
;
258 // pointer to the character in string
262 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
264 m_value
= c
.UniChar().m_value
;
267 // Comparision operators for the case when wxUniChar(Ref) is the second operand:
268 inline bool operator==(char c1
, const wxUniChar
& c2
) { return c2
== c1
; }
269 inline bool operator==(wchar_t c1
, const wxUniChar
& c2
) { return c2
== c1
; }
270 #ifndef wxWINT_T_IS_TYPEDEF
271 inline bool operator==(wint_t c1
, const wxUniChar
& c2
) { return c2
== c1
; }
274 inline bool operator!=(char c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
275 inline bool operator!=(wchar_t c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
276 #ifndef wxWINT_T_IS_TYPEDEF
277 inline bool operator!=(wint_t c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
280 inline bool operator>(char c1
, const wxUniChar
& c2
) { return c2
< c1
; }
281 inline bool operator>(wchar_t c1
, const wxUniChar
& c2
) { return c2
< c1
; }
282 #ifndef wxWINT_T_IS_TYPEDEF
283 inline bool operator>(wint_t c1
, const wxUniChar
& c2
) { return c2
< c1
; }
286 inline bool operator<(char c1
, const wxUniChar
& c2
) { return c2
> c1
; }
287 inline bool operator<(wchar_t c1
, const wxUniChar
& c2
) { return c2
> c1
; }
288 #ifndef wxWINT_T_IS_TYPEDEF
289 inline bool operator<(wint_t c1
, const wxUniChar
& c2
) { return c2
> c1
; }
292 inline bool operator>=(char c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
293 inline bool operator>=(wchar_t c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
294 #ifndef wxWINT_T_IS_TYPEDEF
295 inline bool operator>=(wint_t c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
298 inline bool operator<=(char c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
299 inline bool operator<=(wchar_t c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
300 #ifndef wxWINT_T_IS_TYPEDEF
301 inline bool operator<=(wint_t c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
305 inline bool operator==(char c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
306 inline bool operator==(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
307 #ifndef wxWINT_T_IS_TYPEDEF
308 inline bool operator==(wint_t c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
310 inline bool operator==(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
312 inline bool operator!=(char c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
313 inline bool operator!=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
314 #ifndef wxWINT_T_IS_TYPEDEF
315 inline bool operator!=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
317 inline bool operator!=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
319 inline bool operator>(char c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
320 inline bool operator>(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
321 #ifndef wxWINT_T_IS_TYPEDEF
322 inline bool operator>(wint_t c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
324 inline bool operator>(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
326 inline bool operator<(char c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
327 inline bool operator<(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
328 #ifndef wxWINT_T_IS_TYPEDEF
329 inline bool operator<(wint_t c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
331 inline bool operator<(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
333 inline bool operator>=(char c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
334 inline bool operator>=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
335 #ifndef wxWINT_T_IS_TYPEDEF
336 inline bool operator>=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
338 inline bool operator>=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
340 inline bool operator<=(char c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
341 inline bool operator<=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
342 #ifndef wxWINT_T_IS_TYPEDEF
343 inline bool operator<=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
345 inline bool operator<=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
347 // for expressions like c-'A':
348 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
349 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
350 #ifndef wxWINT_T_IS_TYPEDEF
351 inline int operator-(wint_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
353 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
355 #endif /* _WX_UNICHAR_H_ */