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