]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unichar.h
fixed typo in comment
[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/defs.h"
15 #include "wx/chartype.h"
16 #include "wx/stringimpl.h"
17
18 #ifndef wxWCHAR_T_IS_SEPARATE_TYPE
19 #ifdef __GNUG__
20 #define wxWCHAR_T_IS_SEPARATE_TYPE
21 #endif
22 #if defined(__VISUALC__) && defined(_NATIVE_WCHAR_T_DEFINED)
23 #define wxWCHAR_T_IS_SEPARATE_TYPE
24 #endif
25 #endif
26
27 // helper macro for doing something dependent on whether wchar_t is or isn't a
28 // typedef inside another macro
29 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
30 #define wxIF_WCHAR_T_TYPE(x) x
31 #else // !wxWCHAR_T_IS_SEPARATE_TYPE
32 #define wxIF_WCHAR_T_TYPE(x)
33 #endif // wxWCHAR_T_IS_SEPARATE_TYPE/!wxWCHAR_T_IS_SEPARATE_TYPE
34
35 class WXDLLIMPEXP_BASE wxUniCharRef;
36 class WXDLLIMPEXP_BASE wxStringIteratorNode;
37
38 // This class represents single Unicode character. It can be converted to
39 // and from char or wchar_t and implements commonly used character operations.
40 class WXDLLIMPEXP_BASE wxUniChar
41 {
42 public:
43 // NB: this is not wchar_t on purpose, it needs to represent the entire
44 // Unicode code points range and wchar_t may be too small for that
45 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
46 typedef wxUint32 value_type;
47
48 wxUniChar() : m_value(0) {}
49
50 // Create the character from 8bit character value encoded in the current
51 // locale's charset.
52 wxUniChar(char c) { m_value = From8bit(c); }
53 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
54
55 // Create the character from a wchar_t character value.
56 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
57 wxUniChar(wchar_t c) { m_value = c; }
58 #endif
59
60 wxUniChar(int c) { m_value = c; }
61 wxUniChar(unsigned int c) { m_value = c; }
62 wxUniChar(long int c) { m_value = c; }
63 wxUniChar(unsigned long int c) { m_value = c; }
64 wxUniChar(short int c) { m_value = c; }
65 wxUniChar(unsigned short int c) { m_value = c; }
66
67 wxUniChar(const wxUniCharRef& c);
68
69 // Returns Unicode code point value of the character
70 value_type GetValue() const { return m_value; }
71
72 #if wxUSE_UNICODE_UTF8
73 // buffer for single UTF-8 character
74 struct Utf8CharBuffer
75 {
76 char data[5];
77 operator const char*() const { return data; }
78 };
79
80 // returns the character encoded as UTF-8
81 // (NB: implemented in stringops.cpp)
82 Utf8CharBuffer AsUTF8() const;
83 #endif // wxUSE_UNICODE_UTF8
84
85 // Returns true if the character is an ASCII character:
86 bool IsAscii() const { return m_value < 0x80; }
87
88 // Conversions to char and wchar_t types: all of those are needed to be
89 // able to pass wxUniChars to verious standard narrow and wide character
90 // functions
91 operator char() const { return To8bit(m_value); }
92 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
93 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
94 operator wchar_t() const { return m_value; }
95 #endif
96 operator int() const { return m_value; }
97 operator unsigned int() const { return m_value; }
98 operator long int() const { return m_value; }
99 operator unsigned long int() const { return m_value; }
100 operator short int() const { return m_value; }
101 operator unsigned short int() const { return m_value; }
102
103 // We need this operator for the "*p" part of expressions like "for (
104 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
105 // compilation would fail without it because the conversion to bool would
106 // be ambiguous (there are all these int types conversions...). (And adding
107 // operator unspecified_bool_type() would only makes the ambiguity worse.)
108 operator bool() const { return m_value != 0; }
109 bool operator!() const { return !((bool)*this); }
110
111 // And this one is needed by some (not all, but not using ifdefs makes the
112 // code easier) compilers to parse "str[0] && *p" successfully
113 bool operator&&(bool v) const { return (bool)*this && v; }
114
115 // Assignment operators:
116 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
117 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
118 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
119 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
120 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
121 #endif
122 wxUniChar& operator=(int c) { m_value = c; return *this; }
123 wxUniChar& operator=(unsigned int c) { m_value = c; return *this; }
124 wxUniChar& operator=(long int c) { m_value = c; return *this; }
125 wxUniChar& operator=(unsigned long int c) { m_value = c; return *this; }
126 wxUniChar& operator=(short int c) { m_value = c; return *this; }
127 wxUniChar& operator=(unsigned short int c) { m_value = c; return *this; }
128
129 // Comparison operators:
130
131 // define the given comparison operator for all the types
132 #define wxDEFINE_UNICHAR_OPERATOR(op) \
133 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
134 bool operator op(char c) const { return m_value op From8bit(c); } \
135 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
136 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
137 bool operator op(int c) const { return m_value op (value_type)c; } \
138 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
139 bool operator op(short int c) const { return m_value op (value_type)c; } \
140 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
141 bool operator op(long int c) const { return m_value op (value_type)c; } \
142 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
143
144 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
145
146 #undef wxDEFINE_UNICHAR_OPERATOR
147
148 // this is needed for expressions like 'Z'-c
149 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
150 int operator-(char c) const { return m_value - From8bit(c); }
151 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
152 int operator-(wchar_t c) const { return m_value - (value_type)c; }
153
154
155 private:
156 static value_type From8bit(char c);
157 static char To8bit(value_type c);
158
159 private:
160 value_type m_value;
161 };
162
163
164 // Writeable reference to a character in wxString.
165 //
166 // This class can be used in the same way wxChar is used, except that changing
167 // its value updates the underlying string object.
168 class WXDLLIMPEXP_BASE wxUniCharRef
169 {
170 private:
171 typedef wxStringImpl::iterator iterator;
172
173 // create the reference
174 #if wxUSE_UNICODE_UTF8
175 wxUniCharRef(wxStringIteratorNode& node, iterator pos) : m_node(node), m_pos(pos) {}
176 #else
177 wxUniCharRef(iterator pos) : m_pos(pos) {}
178 #endif
179
180 public:
181 // NB: we have to make this public, because we don't have wxString
182 // declaration available here and so can't declare wxString::iterator
183 // as friend; so at least don't use a ctor but a static function
184 // that must be used explicitly (this is more than using 'explicit'
185 // keyword on ctor!):
186 #if wxUSE_UNICODE_UTF8
187 static wxUniCharRef CreateForString(wxStringIteratorNode& node, iterator pos)
188 { return wxUniCharRef(node, pos); }
189 #else
190 static wxUniCharRef CreateForString(iterator pos)
191 { return wxUniCharRef(pos); }
192 #endif
193
194 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
195
196 #if wxUSE_UNICODE_UTF8
197 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
198 #endif // wxUSE_UNICODE_UTF8
199
200 bool IsAscii() const { return UniChar().IsAscii(); }
201
202 // Assignment operators:
203 #if wxUSE_UNICODE_UTF8
204 wxUniCharRef& operator=(const wxUniChar& c);
205 #else
206 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
207 #endif
208
209 wxUniCharRef& operator=(const wxUniCharRef& c)
210 { return *this = c.UniChar(); }
211
212 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
213 wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
214 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
215 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
216 #endif
217 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
218 wxUniCharRef& operator=(unsigned int c) { return *this = wxUniChar(c); }
219 wxUniCharRef& operator=(short int c) { return *this = wxUniChar(c); }
220 wxUniCharRef& operator=(unsigned short int c) { return *this = wxUniChar(c); }
221 wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
222 wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); }
223
224 // Conversions to the same types as wxUniChar is convertible too:
225 operator char() const { return UniChar(); }
226 operator unsigned char() const { return UniChar(); }
227 #ifdef wxWCHAR_T_IS_SEPARATE_TYPE
228 operator wchar_t() const { return UniChar(); }
229 #endif
230 operator int() const { return UniChar(); }
231 operator unsigned int() const { return UniChar(); }
232 operator short int() const { return UniChar(); }
233 operator unsigned short int() const { return UniChar(); }
234 operator long int() const { return UniChar(); }
235 operator unsigned long int() const { return UniChar(); }
236
237 // see wxUniChar::operator bool etc. for explanation
238 operator bool() const { return (bool)UniChar(); }
239 bool operator!() const { return !UniChar(); }
240 bool operator&&(bool v) const { return UniChar() && v; }
241
242 // Comparison operators:
243 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
244 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
245 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
246 bool operator op(char c) const { return UniChar() op c; } \
247 bool operator op(unsigned char c) const { return UniChar() op c; } \
248 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
249 bool operator op(int c) const { return UniChar() op c; } \
250 bool operator op(unsigned int c) const { return UniChar() op c; } \
251 bool operator op(short int c) const { return UniChar() op c; } \
252 bool operator op(unsigned short int c) const { return UniChar() op c; } \
253 bool operator op(long int c) const { return UniChar() op c; } \
254 bool operator op(unsigned long int c) const { return UniChar() op c; }
255
256 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
257
258 #undef wxDEFINE_UNICHARREF_OPERATOR
259
260 // for expressions like c-'A':
261 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
262 int operator-(const wxUniChar& c) const { return UniChar() - c; }
263 int operator-(char c) const { return UniChar() - c; }
264 int operator-(unsigned char c) const { return UniChar() - c; }
265 int operator-(wchar_t c) const { return UniChar() - c; }
266
267 private:
268 #if wxUSE_UNICODE_UTF8
269 wxUniChar UniChar() const;
270 #else
271 wxUniChar UniChar() const { return *m_pos; }
272 #endif
273
274 friend class WXDLLIMPEXP_BASE wxUniChar;
275
276 private:
277 // reference to the string and pointer to the character in string
278 #if wxUSE_UNICODE_UTF8
279 wxStringIteratorNode& m_node;
280 #endif
281 iterator m_pos;
282 };
283
284 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
285 {
286 m_value = c.UniChar().m_value;
287 }
288
289 // Comparison operators for the case when wxUniChar(Ref) is the second operand
290 // implemented in terms of member comparison functions
291
292 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
293
294 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
295 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
296
297 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
298 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
299
300 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
301
302 #undef wxCMP_REVERSE
303
304 // for expressions like c-'A':
305 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
306 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
307 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
308
309 #endif /* _WX_UNICHAR_H_ */