split wxchar.h into several smaller headers
[wxWidgets.git] / include / wx / unichar.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unichar.h
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
5 // Created: 2007-03-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNICHAR_H_
12 #define _WX_UNICHAR_H_
13
14 #include "wx/chartype.h"
15
16 class WXDLLIMPEXP_BASE wxUniCharRef;
17
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
21 {
22 public:
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;
27
28 wxUniChar() : m_value(0) {}
29
30 // Create the character from 8bit character value encoded in the current
31 // locale's charset.
32 wxUniChar(char c) { m_value = From8bit(c); }
33
34 // Create the character from a wchar_t character value.
35 wxUniChar(wchar_t c) { m_value = c; }
36
37 #ifndef wxWINT_T_IS_TYPEDEF
38 // Create the character from a wint_t character value.
39 wxUniChar(wint_t c) { m_value = c; }
40 #endif
41
42 wxUniChar(int c) { m_value = c; }
43
44 wxUniChar(const wxUniCharRef& c);
45
46 // Returns Unicode code point value of the character
47 unicode_type GetValue() const { return m_value; }
48
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; }
54 #endif
55 operator int() const { return m_value; }
56
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
67 // "str[0] && *p":
68 bool operator&&(bool v) const { return (bool)*this && v; }
69 #endif
70
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; }
77 #endif
78
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; }
85 #endif
86
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; }
92 #endif
93
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; }
99 #endif
100
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; }
106 #endif
107
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; }
113 #endif
114
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; }
120 #endif
121
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; }
127 #endif
128
129 private:
130 static unicode_type From8bit(char c);
131 static char To8bit(unicode_type c);
132
133 private:
134 unicode_type m_value;
135 };
136
137
138 // Writeable reference to a character in wxString.
139 //
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
143 {
144 private:
145 // create the reference
146 // FIXME-UTF8: the interface will need changes for UTF-8 build
147 wxUniCharRef(wxChar *pos) : m_pos(pos) {}
148
149 public:
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!):
155 //
156 // FIXME-UTF8: the interface will need changes for UTF-8 build
157 static wxUniCharRef CreateForString(wxChar *pos)
158 { return wxUniCharRef(pos); }
159
160 wxUniChar::unicode_type GetValue() const { return UniChar().GetValue(); }
161
162 // Assignment operators:
163 wxUniCharRef& operator=(const wxUniCharRef& c)
164 {
165 *m_pos = *c.m_pos;
166 return *this;
167 };
168
169 wxUniCharRef& operator=(const wxUniChar& c)
170 {
171 *m_pos = c;
172 return *this;
173 };
174
175 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
176 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
177
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(); }
183 #endif
184 operator int() const { return UniChar(); }
185
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; }
192 #endif
193
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; }
201 #endif
202
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; }
209 #endif
210
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; }
217 #endif
218
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; }
225 #endif
226
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; }
233 #endif
234
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; }
241 #endif
242
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; }
250 #endif
251
252 private:
253 wxUniChar UniChar() const { return *m_pos; }
254 friend class WXDLLIMPEXP_BASE wxUniChar;
255
256 private:
257 // pointer to the character in string
258 wxChar *m_pos;
259 };
260
261 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
262 {
263 m_value = c.UniChar().m_value;
264 }
265
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; }
271 #endif
272
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; }
277 #endif
278
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; }
283 #endif
284
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; }
289 #endif
290
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; }
295 #endif
296
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; }
301 #endif
302
303
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; }
308 #endif
309 inline bool operator==(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 == c1; }
310
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; }
315 #endif
316 inline bool operator!=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 != c1; }
317
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; }
322 #endif
323 inline bool operator>(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 < c1; }
324
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; }
329 #endif
330 inline bool operator<(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 > c1; }
331
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; }
336 #endif
337 inline bool operator>=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 <= c1; }
338
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; }
343 #endif
344 inline bool operator<=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 >= c1; }
345
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); }
351 #endif
352 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
353
354 #endif /* _WX_UNICHAR_H_ */