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