]>
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 VS |
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 | |
436bb9f5 VZ |
25 | #endif |
26 | ||
21f0762a | 27 | // helper macro for doing something dependent on whether wchar_t is or isn't a |
436bb9f5 | 28 | // typedef inside another macro |
21f0762a VS |
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 | |
153bc803 | 34 | |
e3f6cbd9 | 35 | class WXDLLIMPEXP_BASE wxUniCharRef; |
b0c4d5d7 | 36 | class WXDLLIMPEXP_BASE wxStringIteratorNode; |
e3f6cbd9 VS |
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) | |
dbecee02 | 46 | typedef wxUint32 value_type; |
e3f6cbd9 VS |
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); } | |
9f1b1b78 | 53 | wxUniChar(unsigned char c) { m_value = From8bit((char)c); } |
e3f6cbd9 VS |
54 | |
55 | // Create the character from a wchar_t character value. | |
21f0762a | 56 | #ifdef wxWCHAR_T_IS_SEPARATE_TYPE |
e3f6cbd9 | 57 | wxUniChar(wchar_t c) { m_value = c; } |
436bb9f5 | 58 | #endif |
e3f6cbd9 | 59 | |
e3f6cbd9 | 60 | wxUniChar(int c) { m_value = c; } |
21f0762a | 61 | wxUniChar(unsigned int c) { m_value = c; } |
4e5b3a4e | 62 | wxUniChar(long int c) { m_value = c; } |
21f0762a VS |
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; } | |
e3f6cbd9 VS |
66 | |
67 | wxUniChar(const wxUniCharRef& c); | |
68 | ||
69 | // Returns Unicode code point value of the character | |
d1b7ed67 | 70 | value_type GetValue() const { return m_value; } |
e3f6cbd9 | 71 | |
1fc10687 VS |
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 | ||
81727065 VS |
85 | // Returns true if the character is an ASCII character: |
86 | bool IsAscii() const { return m_value < 0x80; } | |
87 | ||
2a686bd3 VZ |
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 | |
e3f6cbd9 | 91 | operator char() const { return To8bit(m_value); } |
21f0762a VS |
92 | operator unsigned char() const { return (unsigned char)To8bit(m_value); } |
93 | #ifdef wxWCHAR_T_IS_SEPARATE_TYPE | |
e3f6cbd9 | 94 | operator wchar_t() const { return m_value; } |
21f0762a | 95 | #endif |
e3f6cbd9 | 96 | operator int() const { return m_value; } |
153bc803 | 97 | operator unsigned int() const { return m_value; } |
21f0762a VS |
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; } | |
e3f6cbd9 VS |
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); } | |
8019a673 VZ |
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 | |
e3f6cbd9 | 113 | bool operator&&(bool v) const { return (bool)*this && v; } |
e3f6cbd9 VS |
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; } | |
db18be57 | 118 | wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; } |
21f0762a | 119 | #ifdef wxWCHAR_T_IS_SEPARATE_TYPE |
e3f6cbd9 | 120 | wxUniChar& operator=(wchar_t c) { m_value = c; return *this; } |
21f0762a | 121 | #endif |
171d6132 | 122 | wxUniChar& operator=(int c) { m_value = c; return *this; } |
21f0762a | 123 | wxUniChar& operator=(unsigned int c) { m_value = c; return *this; } |
4e5b3a4e | 124 | wxUniChar& operator=(long int c) { m_value = c; return *this; } |
21f0762a VS |
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; } | |
e3f6cbd9 | 128 | |
fb52c2b6 | 129 | // Comparison operators: |
e3f6cbd9 | 130 | |
fb52c2b6 | 131 | // define the given comparison operator for all the types |
98c4eb39 | 132 | #define wxDEFINE_UNICHAR_OPERATOR(op) \ |
fb52c2b6 VZ |
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); } \ | |
db18be57 | 135 | bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \ |
21f0762a | 136 | wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \ |
db18be57 | 137 | bool operator op(int c) const { return m_value op (value_type)c; } \ |
21f0762a VS |
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; } \ | |
4e5b3a4e | 141 | bool operator op(long int c) const { return m_value op (value_type)c; } \ |
21f0762a | 142 | bool operator op(unsigned long int c) const { return m_value op (value_type)c; } |
e3f6cbd9 | 143 | |
fb52c2b6 | 144 | wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR) |
e3f6cbd9 | 145 | |
fb52c2b6 | 146 | #undef wxDEFINE_UNICHAR_OPERATOR |
e3f6cbd9 | 147 | |
fb52c2b6 | 148 | // this is needed for expressions like 'Z'-c |
e3f6cbd9 VS |
149 | int operator-(const wxUniChar& c) const { return m_value - c.m_value; } |
150 | int operator-(char c) const { return m_value - From8bit(c); } | |
db18be57 | 151 | int operator-(unsigned char c) const { return m_value - From8bit((char)c); } |
d1b7ed67 | 152 | int operator-(wchar_t c) const { return m_value - (value_type)c; } |
21f0762a | 153 | |
e3f6cbd9 VS |
154 | |
155 | private: | |
d1b7ed67 VS |
156 | static value_type From8bit(char c); |
157 | static char To8bit(value_type c); | |
e3f6cbd9 VS |
158 | |
159 | private: | |
d1b7ed67 | 160 | value_type m_value; |
e3f6cbd9 VS |
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: | |
a962cdf4 VS |
171 | typedef wxStringImpl::iterator iterator; |
172 | ||
e3f6cbd9 | 173 | // create the reference |
81727065 | 174 | #if wxUSE_UNICODE_UTF8 |
b0c4d5d7 | 175 | wxUniCharRef(wxStringIteratorNode& node, iterator pos) : m_node(node), m_pos(pos) {} |
81727065 | 176 | #else |
a962cdf4 | 177 | wxUniCharRef(iterator pos) : m_pos(pos) {} |
81727065 | 178 | #endif |
e3f6cbd9 VS |
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!): | |
81727065 | 186 | #if wxUSE_UNICODE_UTF8 |
b0c4d5d7 VS |
187 | static wxUniCharRef CreateForString(wxStringIteratorNode& node, iterator pos) |
188 | { return wxUniCharRef(node, pos); } | |
81727065 | 189 | #else |
a962cdf4 | 190 | static wxUniCharRef CreateForString(iterator pos) |
e3f6cbd9 | 191 | { return wxUniCharRef(pos); } |
81727065 | 192 | #endif |
e3f6cbd9 | 193 | |
d1b7ed67 | 194 | wxUniChar::value_type GetValue() const { return UniChar().GetValue(); } |
1fc10687 VS |
195 | |
196 | #if wxUSE_UNICODE_UTF8 | |
197 | wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); } | |
198 | #endif // wxUSE_UNICODE_UTF8 | |
199 | ||
bc5443ba | 200 | bool IsAscii() const { return UniChar().IsAscii(); } |
e3f6cbd9 VS |
201 | |
202 | // Assignment operators: | |
81727065 VS |
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 | |
e3f6cbd9 | 208 | |
81727065 VS |
209 | wxUniCharRef& operator=(const wxUniCharRef& c) |
210 | { return *this = c.UniChar(); } | |
e3f6cbd9 VS |
211 | |
212 | wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); } | |
db18be57 | 213 | wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); } |
21f0762a | 214 | #ifdef wxWCHAR_T_IS_SEPARATE_TYPE |
e3f6cbd9 | 215 | wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); } |
21f0762a | 216 | #endif |
171d6132 | 217 | wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); } |
21f0762a VS |
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); } | |
4e5b3a4e | 221 | wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); } |
21f0762a | 222 | wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); } |
e3f6cbd9 | 223 | |
2a686bd3 | 224 | // Conversions to the same types as wxUniChar is convertible too: |
e3f6cbd9 | 225 | operator char() const { return UniChar(); } |
2a686bd3 | 226 | operator unsigned char() const { return UniChar(); } |
21f0762a | 227 | #ifdef wxWCHAR_T_IS_SEPARATE_TYPE |
436bb9f5 | 228 | operator wchar_t() const { return UniChar(); } |
c961c0cf | 229 | #endif |
21f0762a | 230 | operator int() const { return UniChar(); } |
153bc803 | 231 | operator unsigned int() const { return UniChar(); } |
21f0762a VS |
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(); } | |
e3f6cbd9 VS |
236 | |
237 | // see wxUniChar::operator bool etc. for explanation | |
238 | operator bool() const { return (bool)UniChar(); } | |
239 | bool operator!() const { return !UniChar(); } | |
e3f6cbd9 | 240 | bool operator&&(bool v) const { return UniChar() && v; } |
e3f6cbd9 | 241 | |
fb52c2b6 | 242 | // Comparison operators: |
98c4eb39 | 243 | #define wxDEFINE_UNICHARREF_OPERATOR(op) \ |
fb52c2b6 VZ |
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; } \ | |
db18be57 | 247 | bool operator op(unsigned char c) const { return UniChar() op c; } \ |
21f0762a | 248 | wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \ |
db18be57 | 249 | bool operator op(int c) const { return UniChar() op c; } \ |
21f0762a VS |
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; } \ | |
4e5b3a4e | 253 | bool operator op(long int c) const { return UniChar() op c; } \ |
21f0762a | 254 | bool operator op(unsigned long int c) const { return UniChar() op c; } |
e3f6cbd9 | 255 | |
fb52c2b6 | 256 | wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR) |
e3f6cbd9 | 257 | |
fb52c2b6 | 258 | #undef wxDEFINE_UNICHARREF_OPERATOR |
e3f6cbd9 VS |
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; } | |
db18be57 | 264 | int operator-(unsigned char c) const { return UniChar() - c; } |
e3f6cbd9 | 265 | int operator-(wchar_t c) const { return UniChar() - c; } |
e3f6cbd9 VS |
266 | |
267 | private: | |
81727065 | 268 | #if wxUSE_UNICODE_UTF8 |
467175ab | 269 | wxUniChar UniChar() const; |
81727065 | 270 | #else |
467175ab | 271 | wxUniChar UniChar() const { return *m_pos; } |
81727065 VS |
272 | #endif |
273 | ||
e3f6cbd9 VS |
274 | friend class WXDLLIMPEXP_BASE wxUniChar; |
275 | ||
276 | private: | |
81727065 VS |
277 | // reference to the string and pointer to the character in string |
278 | #if wxUSE_UNICODE_UTF8 | |
b0c4d5d7 | 279 | wxStringIteratorNode& m_node; |
81727065 | 280 | #endif |
a962cdf4 | 281 | iterator m_pos; |
e3f6cbd9 VS |
282 | }; |
283 | ||
284 | inline wxUniChar::wxUniChar(const wxUniCharRef& c) | |
285 | { | |
286 | m_value = c.UniChar().m_value; | |
287 | } | |
288 | ||
fb52c2b6 VZ |
289 | // Comparison operators for the case when wxUniChar(Ref) is the second operand |
290 | // implemented in terms of member comparison functions | |
e3f6cbd9 | 291 | |
fb52c2b6 | 292 | #define wxCMP_REVERSE(c1, c2, op) c2 op c1 |
e3f6cbd9 | 293 | |
fb52c2b6 VZ |
294 | wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE) |
295 | wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE) | |
e3f6cbd9 | 296 | |
fb52c2b6 VZ |
297 | wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE) |
298 | wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE) | |
e3f6cbd9 | 299 | |
fb52c2b6 | 300 | wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE) |
e3f6cbd9 | 301 | |
fb52c2b6 | 302 | #undef wxCMP_REVERSE |
e3f6cbd9 VS |
303 | |
304 | // for expressions like c-'A': | |
305 | inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); } | |
e3f6cbd9 | 306 | inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); } |
436bb9f5 | 307 | inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); } |
e3f6cbd9 VS |
308 | |
309 | #endif /* _WX_UNICHAR_H_ */ |