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