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