1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_UNICHAR_H_
12 #define _WX_UNICHAR_H_
15 #include "wx/chartype.h"
16 #include "wx/stringimpl.h"
18 class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
19 class WXDLLIMPEXP_FWD_BASE wxString
;
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
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)
29 typedef wxUint32 value_type
;
31 wxUniChar() : m_value(0) {}
33 // Create the character from 8bit character value encoded in the current
35 wxUniChar(char c
) { m_value
= From8bit(c
); }
36 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
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
43 wxUniChar(const wxUniCharRef
& c
);
45 // Returns Unicode code point value of the character
46 value_type
GetValue() const { return m_value
; }
48 #if wxUSE_UNICODE_UTF8
49 // buffer for single UTF-8 character
53 operator const char*() const { return data
; }
56 // returns the character encoded as UTF-8
57 // (NB: implemented in stringops.cpp)
58 Utf8CharBuffer
AsUTF8() const;
59 #endif // wxUSE_UNICODE_UTF8
61 // Returns true if the character is an ASCII character:
62 bool IsAscii() const { return m_value
< 0x80; }
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
67 bool GetAsChar(char *c
) const
72 #if !wxUSE_UTF8_LOCALE_ONLY
73 if ( GetAsHi8bit(m_value
, c
) )
75 #endif // !wxUSE_UTF8_LOCALE_ONLY
79 #endif // wxUSE_UNICODE
81 *c
= wx_truncate_cast(char, m_value
);
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
88 operator char() const { return To8bit(m_value
); }
89 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
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
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); }
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
106 bool operator&&(bool v
) const { return (bool)*this && v
; }
108 // Assignment operators:
109 wxUniChar
& operator=(const wxUniChar
& c
) { if (&c
!= this) m_value
= c
.m_value
; return *this; }
110 wxUniChar
& operator=(const wxUniCharRef
& c
);
111 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
112 wxUniChar
& operator=(unsigned char c
) { m_value
= From8bit((char)c
); return *this; }
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
119 // Comparison operators:
120 #define wxDEFINE_UNICHAR_CMP_WITH_INT(T, op) \
121 bool operator op(T c) const { return m_value op (value_type)c; }
123 // define the given comparison operator for all the types
124 #define wxDEFINE_UNICHAR_OPERATOR(op) \
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); } \
127 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
128 wxDO_FOR_INT_TYPES_1(wxDEFINE_UNICHAR_CMP_WITH_INT, op)
130 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
132 #undef wxDEFINE_UNICHAR_OPERATOR
133 #undef wxDEFINE_UNCHAR_CMP_WITH_INT
135 // this is needed for expressions like 'Z'-c
136 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
137 int operator-(char c
) const { return m_value
- From8bit(c
); }
138 int operator-(unsigned char c
) const { return m_value
- From8bit((char)c
); }
139 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
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
)
148 if ( (unsigned char)c
< 0x80 )
151 return FromHi8bit(c
);
157 static char To8bit(value_type c
)
161 return wx_truncate_cast(char, c
);
169 // helpers of the functions above called to deal with non-ASCII chars
170 static value_type
FromHi8bit(char c
);
171 static char ToHi8bit(value_type v
);
172 static bool GetAsHi8bit(value_type v
, char *c
);
179 // Writeable reference to a character in wxString.
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
186 typedef wxStringImpl::iterator iterator
;
188 // create the reference
189 #if wxUSE_UNICODE_UTF8
190 wxUniCharRef(wxString
& str
, iterator pos
) : m_str(str
), m_pos(pos
) {}
192 wxUniCharRef(iterator pos
) : m_pos(pos
) {}
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!):
201 #if wxUSE_UNICODE_UTF8
202 static wxUniCharRef
CreateForString(wxString
& str
, iterator pos
)
203 { return wxUniCharRef(str
, pos
); }
205 static wxUniCharRef
CreateForString(iterator pos
)
206 { return wxUniCharRef(pos
); }
209 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
211 #if wxUSE_UNICODE_UTF8
212 wxUniChar::Utf8CharBuffer
AsUTF8() const { return UniChar().AsUTF8(); }
213 #endif // wxUSE_UNICODE_UTF8
215 bool IsAscii() const { return UniChar().IsAscii(); }
216 bool GetAsChar(char *c
) const { return UniChar().GetAsChar(c
); }
218 // Assignment operators:
219 #if wxUSE_UNICODE_UTF8
220 wxUniCharRef
& operator=(const wxUniChar
& c
);
222 wxUniCharRef
& operator=(const wxUniChar
& c
) { *m_pos
= c
; return *this; }
225 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
226 { if (&c
!= this) *this = c
.UniChar(); return *this; }
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
233 // Conversions to the same types as wxUniChar is convertible too:
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
239 // see wxUniChar::operator bool etc. for explanation
240 operator bool() const { return (bool)UniChar(); }
241 bool operator!() const { return !UniChar(); }
242 bool operator&&(bool v
) const { return UniChar() && v
; }
244 #define wxDEFINE_UNICHARREF_CMP_WITH_INT(T, op) \
245 bool operator op(T c) const { return UniChar() op c; }
247 // Comparison operators:
248 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
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; } \
251 wxDO_FOR_CHAR_INT_TYPES_1(wxDEFINE_UNICHARREF_CMP_WITH_INT, op)
253 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
255 #undef wxDEFINE_UNICHARREF_OPERATOR
256 #undef wxDEFINE_UNICHARREF_CMP_WITH_INT
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
; }
262 int operator-(unsigned char c
) const { return UniChar() - c
; }
263 int operator-(wchar_t c
) const { return UniChar() - c
; }
266 #if wxUSE_UNICODE_UTF8
267 wxUniChar
UniChar() const;
269 wxUniChar
UniChar() const { return *m_pos
; }
272 friend class WXDLLIMPEXP_FWD_BASE wxUniChar
;
275 // reference to the string and pointer to the character in string
276 #if wxUSE_UNICODE_UTF8
282 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
284 m_value
= c
.UniChar().m_value
;
287 inline wxUniChar
& wxUniChar::operator=(const wxUniCharRef
& c
)
289 m_value
= c
.UniChar().m_value
;
293 // Comparison operators for the case when wxUniChar(Ref) is the second operand
294 // implemented in terms of member comparison functions
296 wxDEFINE_COMPARISONS_BY_REV(char, const wxUniChar
&)
297 wxDEFINE_COMPARISONS_BY_REV(char, const wxUniCharRef
&)
299 wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniChar
&)
300 wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniCharRef
&)
302 wxDEFINE_COMPARISONS_BY_REV(const wxUniChar
&, const wxUniCharRef
&)
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
); }
309 #endif /* _WX_UNICHAR_H_ */