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