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
); }
34 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
36 // Create the character from a wchar_t character value.
37 wxUniChar(wchar_t c
) { m_value
= c
; }
39 #ifndef wxWINT_T_IS_TYPEDEF
40 // Create the character from a wint_t character value.
41 wxUniChar(wint_t c
) { m_value
= c
; }
44 wxUniChar(int c
) { m_value
= c
; }
46 wxUniChar(const wxUniCharRef
& c
);
48 // Returns Unicode code point value of the character
49 value_type
GetValue() const { return m_value
; }
51 // Casts to char and wchar_t types:
52 operator char() const { return To8bit(m_value
); }
53 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
54 operator wchar_t() const { return m_value
; }
55 #ifndef wxWINT_T_IS_TYPEDEF
56 operator wint_t() const { return m_value
; }
58 operator int() const { return m_value
; }
60 // We need this operator for the "*p" part of expressions like "for (
61 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
62 // compilation would fail without it because the conversion to bool would
63 // be ambiguous (there are all these int types conversions...). (And adding
64 // operator unspecified_bool_type() would only makes the ambiguity worse.)
65 operator bool() const { return m_value
!= 0; }
66 bool operator!() const { return !((bool)*this); }
67 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
68 defined(__DIGITALMARS__) || defined(__BORLANDC__)
69 // We need this for VC++ < 8 or DigitalMars and expressions like
71 bool operator&&(bool v
) const { return (bool)*this && v
; }
74 // Assignment operators:
75 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
76 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
77 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
78 #ifndef wxWINT_T_IS_TYPEDEF
79 wxUniChar
& operator=(wint_t c
) { m_value
= c
; return *this; }
82 // Comparision operators:
83 bool operator==(const wxUniChar
& c
) const { return m_value
== c
.m_value
; }
84 bool operator==(char c
) const { return m_value
== From8bit(c
); }
85 bool operator==(wchar_t c
) const { return m_value
== (value_type
)c
; }
86 #ifndef wxWINT_T_IS_TYPEDEF
87 bool operator==(wint_t c
) const { return m_value
== (value_type
)c
; }
90 bool operator!=(const wxUniChar
& c
) const { return m_value
!= c
.m_value
; }
91 bool operator!=(char c
) const { return m_value
!= From8bit(c
); }
92 bool operator!=(wchar_t c
) const { return m_value
!= (value_type
)c
; }
93 #ifndef wxWINT_T_IS_TYPEDEF
94 bool operator!=(wint_t c
) const { return m_value
!= (value_type
)c
; }
97 bool operator>(const wxUniChar
& c
) const { return m_value
> c
.m_value
; }
98 bool operator>(char c
) const { return m_value
> (value_type
)c
; }
99 bool operator>(wchar_t c
) const { return m_value
> (value_type
)c
; }
100 #ifndef wxWINT_T_IS_TYPEDEF
101 bool operator>(wint_t c
) const { return m_value
> (value_type
)c
; }
104 bool operator<(const wxUniChar
& c
) const { return m_value
< c
.m_value
; }
105 bool operator<(char c
) const { return m_value
< From8bit(c
); }
106 bool operator<(wchar_t c
) const { return m_value
< (value_type
)c
; }
107 #ifndef wxWINT_T_IS_TYPEDEF
108 bool operator<(wint_t c
) const { return m_value
< (value_type
)c
; }
111 bool operator>=(const wxUniChar
& c
) const { return m_value
>= c
.m_value
; }
112 bool operator>=(char c
) const { return m_value
>= From8bit(c
); }
113 bool operator>=(wchar_t c
) const { return m_value
>= (value_type
)c
; }
114 #ifndef wxWINT_T_IS_TYPEDEF
115 bool operator>=(wint_t c
) const { return m_value
>= (value_type
)c
; }
118 bool operator<=(const wxUniChar
& c
) const { return m_value
<= c
.m_value
; }
119 bool operator<=(char c
) const { return m_value
<= From8bit(c
); }
120 bool operator<=(wchar_t c
) const { return m_value
<= (value_type
)c
; }
121 #ifndef wxWINT_T_IS_TYPEDEF
122 bool operator<=(wint_t c
) const { return m_value
<= (value_type
)c
; }
125 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
126 int operator-(char c
) const { return m_value
- From8bit(c
); }
127 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
128 #ifndef wxWINT_T_IS_TYPEDEF
129 int operator-(wint_t c
) const { return m_value
- (value_type
)c
; }
133 static value_type
From8bit(char c
);
134 static char To8bit(value_type c
);
141 // Writeable reference to a character in wxString.
143 // This class can be used in the same way wxChar is used, except that changing
144 // its value updates the underlying string object.
145 class WXDLLIMPEXP_BASE wxUniCharRef
148 // create the reference
149 // FIXME-UTF8: the interface will need changes for UTF-8 build
150 wxUniCharRef(wxChar
*pos
) : m_pos(pos
) {}
153 // NB: we have to make this public, because we don't have wxString
154 // declaration available here and so can't declare wxString::iterator
155 // as friend; so at least don't use a ctor but a static function
156 // that must be used explicitly (this is more than using 'explicit'
157 // keyword on ctor!):
159 // FIXME-UTF8: the interface will need changes for UTF-8 build
160 static wxUniCharRef
CreateForString(wxChar
*pos
)
161 { return wxUniCharRef(pos
); }
163 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
165 // Assignment operators:
166 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
172 wxUniCharRef
& operator=(const wxUniChar
& c
)
178 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
179 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
181 // Casts to wxUniChar type:
182 operator char() const { return UniChar(); }
183 operator unsigned char() const { return UniChar(); }
184 operator wchar_t() const { return UniChar(); }
185 #ifndef wxWINT_T_IS_TYPEDEF
186 operator wint_t() const { return UniChar(); }
188 operator int() const { return UniChar(); }
190 // see wxUniChar::operator bool etc. for explanation
191 operator bool() const { return (bool)UniChar(); }
192 bool operator!() const { return !UniChar(); }
193 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
194 defined(__DIGITALMARS__) || defined(__BORLANDC__)
195 bool operator&&(bool v
) const { return UniChar() && v
; }
198 // Comparision operators:
199 bool operator==(const wxUniCharRef
& c
) const { return m_pos
== c
.m_pos
; }
200 bool operator==(const wxUniChar
& c
) const { return UniChar() == c
; }
201 bool operator==(char c
) const { return UniChar() == c
; }
202 bool operator==(wchar_t c
) const { return UniChar() == c
; }
203 #ifndef wxWINT_T_IS_TYPEDEF
204 bool operator==(wint_t c
) const { return UniChar() == c
; }
207 bool operator!=(const wxUniCharRef
& c
) const { return m_pos
!= c
.m_pos
; }
208 bool operator!=(const wxUniChar
& c
) const { return UniChar() != c
; }
209 bool operator!=(char c
) const { return UniChar() != c
; }
210 bool operator!=(wchar_t c
) const { return UniChar() != c
; }
211 #ifndef wxWINT_T_IS_TYPEDEF
212 bool operator!=(wint_t c
) const { return UniChar() != c
; }
215 bool operator>(const wxUniCharRef
& c
) const { return UniChar() > c
.UniChar(); }
216 bool operator>(const wxUniChar
& c
) const { return UniChar() > c
; }
217 bool operator>(char c
) const { return UniChar() > c
; }
218 bool operator>(wchar_t c
) const { return UniChar() > c
; }
219 #ifndef wxWINT_T_IS_TYPEDEF
220 bool operator>(wint_t c
) const { return UniChar() > c
; }
223 bool operator<(const wxUniCharRef
& c
) const { return UniChar() < c
.UniChar(); }
224 bool operator<(const wxUniChar
& c
) const { return UniChar() < c
; }
225 bool operator<(char c
) const { return UniChar() < c
; }
226 bool operator<(wchar_t c
) const { return UniChar() < c
; }
227 #ifndef wxWINT_T_IS_TYPEDEF
228 bool operator<(wint_t c
) const { return UniChar() < c
; }
231 bool operator>=(const wxUniCharRef
& c
) const { return UniChar() >= c
.UniChar(); }
232 bool operator>=(const wxUniChar
& c
) const { return UniChar() >= c
; }
233 bool operator>=(char c
) const { return UniChar() >= c
; }
234 bool operator>=(wchar_t c
) const { return UniChar() >= c
; }
235 #ifndef wxWINT_T_IS_TYPEDEF
236 bool operator>=(wint_t c
) const { return UniChar() >= c
; }
239 bool operator<=(const wxUniCharRef
& c
) const { return UniChar() <= c
.UniChar(); }
240 bool operator<=(const wxUniChar
& c
) const { return UniChar() <= c
; }
241 bool operator<=(char c
) const { return UniChar() <= c
; }
242 bool operator<=(wchar_t c
) const { return UniChar() <= c
; }
243 #ifndef wxWINT_T_IS_TYPEDEF
244 bool operator<=(wint_t c
) const { return UniChar() <= c
; }
247 // for expressions like c-'A':
248 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
249 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
250 int operator-(char c
) const { return UniChar() - c
; }
251 int operator-(wchar_t c
) const { return UniChar() - c
; }
252 #ifndef wxWINT_T_IS_TYPEDEF
253 int operator-(wint_t c
) const { return UniChar() - c
; }
257 wxUniChar
UniChar() const { return *m_pos
; }
258 friend class WXDLLIMPEXP_BASE wxUniChar
;
261 // pointer to the character in string
265 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
267 m_value
= c
.UniChar().m_value
;
270 // Comparision operators for the case when wxUniChar(Ref) is the second operand:
271 inline bool operator==(char c1
, const wxUniChar
& c2
) { return c2
== c1
; }
272 inline bool operator==(wchar_t c1
, const wxUniChar
& c2
) { return c2
== c1
; }
273 #ifndef wxWINT_T_IS_TYPEDEF
274 inline bool operator==(wint_t c1
, const wxUniChar
& c2
) { return c2
== c1
; }
277 inline bool operator!=(char c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
278 inline bool operator!=(wchar_t c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
279 #ifndef wxWINT_T_IS_TYPEDEF
280 inline bool operator!=(wint_t c1
, const wxUniChar
& c2
) { return c2
!= c1
; }
283 inline bool operator>(char c1
, const wxUniChar
& c2
) { return c2
< c1
; }
284 inline bool operator>(wchar_t c1
, const wxUniChar
& c2
) { return c2
< c1
; }
285 #ifndef wxWINT_T_IS_TYPEDEF
286 inline bool operator>(wint_t c1
, const wxUniChar
& c2
) { return c2
< c1
; }
289 inline bool operator<(char c1
, const wxUniChar
& c2
) { return c2
> c1
; }
290 inline bool operator<(wchar_t c1
, const wxUniChar
& c2
) { return c2
> c1
; }
291 #ifndef wxWINT_T_IS_TYPEDEF
292 inline bool operator<(wint_t c1
, const wxUniChar
& c2
) { return c2
> c1
; }
295 inline bool operator>=(char c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
296 inline bool operator>=(wchar_t c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
297 #ifndef wxWINT_T_IS_TYPEDEF
298 inline bool operator>=(wint_t c1
, const wxUniChar
& c2
) { return c2
<= c1
; }
301 inline bool operator<=(char c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
302 inline bool operator<=(wchar_t c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
303 #ifndef wxWINT_T_IS_TYPEDEF
304 inline bool operator<=(wint_t c1
, const wxUniChar
& c2
) { return c2
>= c1
; }
308 inline bool operator==(char c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
309 inline bool operator==(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
310 #ifndef wxWINT_T_IS_TYPEDEF
311 inline bool operator==(wint_t c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
313 inline bool operator==(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
== c1
; }
315 inline bool operator!=(char c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
316 inline bool operator!=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
317 #ifndef wxWINT_T_IS_TYPEDEF
318 inline bool operator!=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
320 inline bool operator!=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
!= c1
; }
322 inline bool operator>(char c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
323 inline bool operator>(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
324 #ifndef wxWINT_T_IS_TYPEDEF
325 inline bool operator>(wint_t c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
327 inline bool operator>(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
< c1
; }
329 inline bool operator<(char c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
330 inline bool operator<(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
331 #ifndef wxWINT_T_IS_TYPEDEF
332 inline bool operator<(wint_t c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
334 inline bool operator<(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
> c1
; }
336 inline bool operator>=(char c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
337 inline bool operator>=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
338 #ifndef wxWINT_T_IS_TYPEDEF
339 inline bool operator>=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
341 inline bool operator>=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
<= c1
; }
343 inline bool operator<=(char c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
344 inline bool operator<=(wchar_t c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
345 #ifndef wxWINT_T_IS_TYPEDEF
346 inline bool operator<=(wint_t c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
348 inline bool operator<=(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return c2
>= c1
; }
350 // for expressions like c-'A':
351 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
352 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
353 #ifndef wxWINT_T_IS_TYPEDEF
354 inline int operator-(wint_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
356 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
358 #endif /* _WX_UNICHAR_H_ */