]>
Commit | Line | Data |
---|---|---|
3c67202d | 1 | /////////////////////////////////////////////////////////////////////////////// |
1c2d1459 | 2 | // Name: wx/string.h |
a7ea63e2 | 3 | // Purpose: wxString class |
c801d85f KB |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
3c67202d | 10 | /////////////////////////////////////////////////////////////////////////////// |
c801d85f | 11 | |
e90c1d2a VZ |
12 | /* |
13 | Efficient string class [more or less] compatible with MFC CString, | |
77ffb593 | 14 | wxWidgets version 1 wxString and std::string and some handy functions |
e90c1d2a VZ |
15 | missing from string.h. |
16 | */ | |
17 | ||
a7ea63e2 VS |
18 | #ifndef _WX_WXSTRING_H__ |
19 | #define _WX_WXSTRING_H__ | |
c801d85f | 20 | |
e90c1d2a VZ |
21 | // ---------------------------------------------------------------------------- |
22 | // headers | |
23 | // ---------------------------------------------------------------------------- | |
24 | ||
5737d05f VZ |
25 | #include "wx/defs.h" // everybody should include this |
26 | ||
9dea36ef | 27 | #if defined(__WXMAC__) || defined(__VISAGECPP__) |
3f4a0c5b | 28 | #include <ctype.h> |
17dff81c | 29 | #endif |
3f4a0c5b | 30 | |
9dea36ef DW |
31 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
32 | // problem in VACPP V4 with including stdlib.h multiple times | |
33 | // strconv includes it anyway | |
34 | # include <stdio.h> | |
35 | # include <string.h> | |
36 | # include <stdarg.h> | |
37 | # include <limits.h> | |
38 | #else | |
39 | # include <string.h> | |
40 | # include <stdio.h> | |
41 | # include <stdarg.h> | |
42 | # include <limits.h> | |
43 | # include <stdlib.h> | |
44 | #endif | |
c801d85f | 45 | |
1c2d1459 | 46 | #ifdef HAVE_STRCASECMP_IN_STRINGS_H |
1bfcb0b6 | 47 | #include <strings.h> // for strcasecmp() |
1c2d1459 | 48 | #endif // HAVE_STRCASECMP_IN_STRINGS_H |
1bfcb0b6 | 49 | |
4055ed82 | 50 | #ifdef __WXPALMOS__ |
ffecfa5a JS |
51 | #include <StringMgr.h> |
52 | #endif | |
53 | ||
e3f6cbd9 | 54 | #include "wx/wxchar.h" // for wxChar, wxStrlen() etc. |
c9f78968 | 55 | #include "wx/strvararg.h" |
e90c1d2a VZ |
56 | #include "wx/buffer.h" // for wxCharBuffer |
57 | #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes | |
a7ea63e2 VS |
58 | #include "wx/stringimpl.h" |
59 | #include "wx/unichar.h" | |
3f4a0c5b | 60 | |
fb3c9042 VZ |
61 | class WXDLLIMPEXP_BASE wxString; |
62 | ||
c801d85f KB |
63 | // --------------------------------------------------------------------------- |
64 | // macros | |
65 | // --------------------------------------------------------------------------- | |
66 | ||
5737d05f VZ |
67 | // casts [unfortunately!] needed to call some broken functions which require |
68 | // "char *" instead of "const char *" | |
2bb67b80 | 69 | #define WXSTRINGCAST (wxChar *)(const wxChar *) |
e90c1d2a VZ |
70 | #define wxCSTRINGCAST (wxChar *)(const wxChar *) |
71 | #define wxMBSTRINGCAST (char *)(const char *) | |
72 | #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) | |
c801d85f | 73 | |
e90c1d2a VZ |
74 | // ---------------------------------------------------------------------------- |
75 | // constants | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
13874b5c VZ |
78 | #if WXWIN_COMPATIBILITY_2_6 |
79 | ||
80 | // deprecated in favour of wxString::npos, don't use in new code | |
81 | // | |
e90c1d2a | 82 | // maximum possible length for a string means "take all string" everywhere |
8f93a29f | 83 | #define wxSTRING_MAXLEN wxString::npos |
66b6b045 | 84 | |
13874b5c VZ |
85 | #endif // WXWIN_COMPATIBILITY_2_6 |
86 | ||
c801d85f | 87 | // --------------------------------------------------------------------------- |
e90c1d2a | 88 | // global functions complementing standard C string library replacements for |
3c67202d VZ |
89 | // strlen() and portable strcasecmp() |
90 | //--------------------------------------------------------------------------- | |
e90c1d2a | 91 | |
c7554da8 | 92 | #if WXWIN_COMPATIBILITY_2_8 |
e3f6cbd9 | 93 | // Use wxXXX() functions from wxcrt.h instead! These functions are for |
e90c1d2a | 94 | // backwards compatibility only. |
88150e60 | 95 | |
3c67202d | 96 | // checks whether the passed in pointer is NULL and if the string is empty |
c7554da8 | 97 | wxDEPRECATED( inline bool IsEmpty(const char *p) ); |
6d56eb5c | 98 | inline bool IsEmpty(const char *p) { return (!p || !*p); } |
c801d85f | 99 | |
3c67202d | 100 | // safe version of strlen() (returns 0 if passed NULL pointer) |
c7554da8 | 101 | wxDEPRECATED( inline size_t Strlen(const char *psz) ); |
6d56eb5c | 102 | inline size_t Strlen(const char *psz) |
c801d85f KB |
103 | { return psz ? strlen(psz) : 0; } |
104 | ||
3c67202d | 105 | // portable strcasecmp/_stricmp |
c7554da8 | 106 | wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) ); |
6d56eb5c | 107 | inline int Stricmp(const char *psz1, const char *psz2) |
dd1eaa89 | 108 | { |
7f0586ef JS |
109 | #if defined(__VISUALC__) && defined(__WXWINCE__) |
110 | register char c1, c2; | |
111 | do { | |
112 | c1 = tolower(*psz1++); | |
113 | c2 = tolower(*psz2++); | |
114 | } while ( c1 && (c1 == c2) ); | |
115 | ||
116 | return c1 - c2; | |
117 | #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) ) | |
dd1eaa89 | 118 | return _stricmp(psz1, psz2); |
91b8de8d | 119 | #elif defined(__SC__) |
2432b92d | 120 | return _stricmp(psz1, psz2); |
91b8de8d | 121 | #elif defined(__SALFORDC__) |
a3ef5bf5 | 122 | return stricmp(psz1, psz2); |
dd1eaa89 VZ |
123 | #elif defined(__BORLANDC__) |
124 | return stricmp(psz1, psz2); | |
7be1f0d9 JS |
125 | #elif defined(__WATCOMC__) |
126 | return stricmp(psz1, psz2); | |
14704334 VS |
127 | #elif defined(__DJGPP__) |
128 | return stricmp(psz1, psz2); | |
91b8de8d RR |
129 | #elif defined(__EMX__) |
130 | return stricmp(psz1, psz2); | |
e2c87f4c | 131 | #elif defined(__WXPM__) |
1777b9bb | 132 | return stricmp(psz1, psz2); |
a0be6908 WS |
133 | #elif defined(__WXPALMOS__) || \ |
134 | defined(HAVE_STRCASECMP_IN_STRING_H) || \ | |
1c2d1459 VZ |
135 | defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ |
136 | defined(__GNUWIN32__) | |
dd1eaa89 | 137 | return strcasecmp(psz1, psz2); |
8be97d65 | 138 | #elif defined(__MWERKS__) && !defined(__INTEL__) |
17dff81c SC |
139 | register char c1, c2; |
140 | do { | |
141 | c1 = tolower(*psz1++); | |
142 | c2 = tolower(*psz2++); | |
143 | } while ( c1 && (c1 == c2) ); | |
144 | ||
145 | return c1 - c2; | |
dd1eaa89 VZ |
146 | #else |
147 | // almost all compilers/libraries provide this function (unfortunately under | |
148 | // different names), that's why we don't implement our own which will surely | |
149 | // be more efficient than this code (uncomment to use): | |
150 | /* | |
151 | register char c1, c2; | |
152 | do { | |
153 | c1 = tolower(*psz1++); | |
154 | c2 = tolower(*psz2++); | |
155 | } while ( c1 && (c1 == c2) ); | |
156 | ||
157 | return c1 - c2; | |
158 | */ | |
159 | ||
160 | #error "Please define string case-insensitive compare for your OS/compiler" | |
161 | #endif // OS/compiler | |
162 | } | |
c801d85f | 163 | |
c7554da8 VS |
164 | #endif // WXWIN_COMPATIBILITY_2_8 |
165 | ||
c9f78968 VS |
166 | // ---------------------------------------------------------------------------- |
167 | // wxCStrData | |
168 | // ---------------------------------------------------------------------------- | |
169 | ||
170 | // Lightweight object returned by wxString::c_str() and implicitly convertible | |
171 | // to either const char* or const wchar_t*. | |
bb650010 | 172 | class wxCStrData |
c9f78968 VS |
173 | { |
174 | private: | |
175 | // Ctors; for internal use by wxString and wxCStrData only | |
176 | wxCStrData(const wxString *str, size_t offset = 0, bool owned = false) | |
177 | : m_str(str), m_offset(offset), m_owned(owned) {} | |
178 | ||
179 | public: | |
180 | // Ctor constructs the object from char literal; they are needed to make | |
181 | // operator?: compile and they intentionally take char*, not const char* | |
182 | wxCStrData(char *buf); | |
183 | wxCStrData(wchar_t *buf); | |
184 | ||
185 | ~wxCStrData(); | |
186 | ||
187 | // FIXME: we'll need convertors for both char* and wchar_t* and NONE | |
188 | // for wxChar*, but that's after completing the transition to | |
189 | // "smart" wxUniChar class. For now, just have conversion to | |
190 | // char* in ANSI build and wchar_t in Unicode build. | |
191 | #if wxUSE_UNICODE | |
192 | const wchar_t* AsWChar() const; | |
193 | operator const wchar_t*() const { return AsWChar(); } | |
194 | #else | |
195 | const char* AsChar() const; | |
dbea442a VZ |
196 | const unsigned char* AsUnsignedChar() const |
197 | { return (const unsigned char *) AsChar(); } | |
17709275 | 198 | operator const void*() const { return AsChar(); } |
c9f78968 | 199 | operator const char*() const { return AsChar(); } |
dbea442a | 200 | operator const unsigned char*() const { return AsUnsignedChar(); } |
c9f78968 VS |
201 | #endif |
202 | ||
203 | wxString AsString() const; | |
c9f78968 VS |
204 | |
205 | // allow expressions like "c_str()[0]": | |
206 | wxUniChar operator[](int n) const { return operator[](size_t(n)); } | |
207 | wxUniChar operator[](size_t n) const; | |
c1df0a3b | 208 | wxUniChar operator[](long n) const { return operator[](size_t(n)); } |
c9f78968 VS |
209 | #ifndef wxSIZE_T_IS_UINT |
210 | wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); } | |
211 | #endif // size_t != unsigned int | |
212 | ||
acf0c9ac | 213 | // these operators are needed to emulate the pointer semantics of c_str(): |
c9f78968 VS |
214 | // expressions like "wxChar *p = str.c_str() + 1;" should continue to work |
215 | // (we need both versions to resolve ambiguities): | |
216 | wxCStrData operator+(int n) const | |
217 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
acf0c9ac VZ |
218 | wxCStrData operator+(long n) const |
219 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
c9f78968 VS |
220 | wxCStrData operator+(size_t n) const |
221 | { return wxCStrData(m_str, m_offset + n, m_owned); } | |
222 | ||
f4c90fdf VS |
223 | // and these for "str.c_str() + n - 2": |
224 | wxCStrData operator-(int n) const | |
225 | { | |
226 | wxASSERT_MSG( n <= (int)m_offset, | |
227 | _T("attempt to construct address before the beginning of the string") ); | |
228 | return wxCStrData(m_str, m_offset - n, m_owned); | |
229 | } | |
230 | wxCStrData operator-(long n) const | |
231 | { | |
232 | wxASSERT_MSG( n <= (int)m_offset, | |
233 | _T("attempt to construct address before the beginning of the string") ); | |
234 | return wxCStrData(m_str, m_offset - n, m_owned); | |
235 | } | |
236 | wxCStrData operator-(size_t n) const | |
237 | { | |
759d51f2 | 238 | wxASSERT_MSG( n <= m_offset, |
f4c90fdf VS |
239 | _T("attempt to construct address before the beginning of the string") ); |
240 | return wxCStrData(m_str, m_offset - n, m_owned); | |
241 | } | |
242 | ||
b77afb41 | 243 | // this operator is needed to make expressions like "*c_str()" or |
c9f78968 VS |
244 | // "*(c_str() + 2)" work |
245 | wxUniChar operator*() const; | |
246 | ||
247 | private: | |
248 | const wxString *m_str; | |
249 | size_t m_offset; | |
250 | bool m_owned; | |
251 | ||
252 | friend class WXDLLIMPEXP_BASE wxString; | |
253 | }; | |
254 | ||
255 | // ---------------------------------------------------------------------------- | |
256 | // wxStringPrintfMixin | |
257 | // --------------------------------------------------------------------------- | |
258 | ||
259 | // NB: VC6 has a bug that causes linker errors if you have template methods | |
260 | // in a class using __declspec(dllimport). The solution is to split such | |
261 | // class into two classes, one that contains the template methods and does | |
262 | // *not* use WXDLLIMPEXP_BASE and another class that contains the rest | |
263 | // (with DLL linkage). | |
264 | // | |
265 | // We only do this for VC6 here, because the code is less efficient | |
266 | // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler | |
267 | // cannot compile this code. | |
268 | ||
269 | #if defined(__VISUALC__) && __VISUALC__ < 1300 | |
270 | #define wxNEEDS_WXSTRING_PRINTF_MIXIN | |
271 | #endif | |
272 | ||
273 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
274 | // this class contains implementation of wxString's vararg methods, it's | |
275 | // exported from wxBase DLL | |
276 | class WXDLLIMPEXP_BASE wxStringPrintfMixinBase | |
277 | { | |
278 | protected: | |
279 | wxStringPrintfMixinBase() {} | |
280 | ||
281 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
282 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
283 | }; | |
284 | ||
285 | // this class contains template wrappers for wxString's vararg methods, it's | |
286 | // intentionally *not* exported from the DLL in order to fix the VC6 bug | |
287 | // described above | |
288 | class wxStringPrintfMixin : public wxStringPrintfMixinBase | |
289 | { | |
290 | private: | |
291 | // to further complicate things, we can't return wxString from | |
292 | // wxStringPrintfMixin::Format() because wxString is not yet declared at | |
293 | // this point; the solution is to use this fake type trait template - this | |
294 | // way the compiler won't know the return type until Format() is used | |
295 | // (this doesn't compile with Watcom, but VC6 compiles it just fine): | |
296 | template<typename T> struct StringReturnType | |
297 | { | |
298 | typedef wxString type; | |
299 | }; | |
300 | ||
301 | public: | |
302 | // these are duplicated wxString methods, they're also declared below | |
303 | // if !wxNEEDS_WXSTRING_PRINTF_MIXIN: | |
304 | ||
305 | // int Printf(const wxChar *pszFormat, ...); | |
306 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
307 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; | |
308 | WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type, | |
309 | Format, DoFormat) | |
310 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; | |
311 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
312 | ||
313 | protected: | |
314 | wxStringPrintfMixin() : wxStringPrintfMixinBase() {} | |
315 | }; | |
316 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
317 | ||
318 | ||
b6339dde VZ |
319 | // ---------------------------------------------------------------------------- |
320 | // wxString: string class trying to be compatible with std::string, MFC | |
321 | // CString and wxWindows 1.x wxString all at once | |
e87b7833 MB |
322 | // --------------------------------------------------------------------------- |
323 | ||
c9f78968 VS |
324 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
325 | // "non dll-interface class 'wxStringPrintfMixin' used as base interface | |
326 | // for dll-interface class 'wxString'" -- this is OK in our case | |
327 | #pragma warning (disable:4275) | |
e87b7833 MB |
328 | #endif |
329 | ||
8f93a29f | 330 | class WXDLLIMPEXP_BASE wxString |
c9f78968 | 331 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
8f93a29f | 332 | : public wxStringPrintfMixin |
c9f78968 VS |
333 | #endif |
334 | { | |
e87b7833 MB |
335 | // NB: special care was taken in arranging the member functions in such order |
336 | // that all inline functions can be effectively inlined, verify that all | |
20aa779e | 337 | // performance critical functions are still inlined if you change order! |
8f93a29f | 338 | public: |
8f93a29f VS |
339 | // an 'invalid' value for string index, moved to this place due to a CW bug |
340 | static const size_t npos; | |
8f93a29f | 341 | |
e87b7833 MB |
342 | private: |
343 | // if we hadn't made these operators private, it would be possible to | |
344 | // compile "wxString s; s = 17;" without any warnings as 17 is implicitly | |
345 | // converted to char in C and we do have operator=(char) | |
346 | // | |
347 | // NB: we don't need other versions (short/long and unsigned) as attempt | |
348 | // to assign another numeric type to wxString will now result in | |
349 | // ambiguity between operator=(char) and operator=(int) | |
350 | wxString& operator=(int); | |
351 | ||
8f93a29f VS |
352 | // these methods are not implemented - there is _no_ conversion from int to |
353 | // string, you're doing something wrong if the compiler wants to call it! | |
354 | // | |
355 | // try `s << i' or `s.Printf("%d", i)' instead | |
356 | wxString(int); | |
357 | ||
358 | ||
359 | // buffer for holding temporary substring when using any of the methods | |
360 | // that take (char*,size_t) or (wchar_t*,size_t) arguments: | |
361 | // FIXME-UTF8: This will need changes when UTF8 build is introduced | |
362 | template<typename T> | |
363 | struct SubstrBufFromType | |
364 | { | |
365 | T data; | |
366 | size_t len; | |
367 | ||
368 | SubstrBufFromType() {} | |
369 | SubstrBufFromType(const T& data_, size_t len_) | |
370 | : data(data_), len(len_) {} | |
371 | }; | |
372 | ||
373 | #if wxUSE_UNICODE_UTF8 | |
374 | // FIXME-UTF8: this will have to use slightly different type | |
375 | #elif wxUSE_UNICODE_WCHAR | |
376 | typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC; | |
377 | typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB; | |
8f93a29f VS |
378 | #else |
379 | typedef SubstrBufFromType<const char*> SubstrBufFromMB; | |
380 | typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC; | |
8f93a29f VS |
381 | #endif |
382 | ||
383 | ||
384 | // Functions implementing primitive operations on string data; wxString | |
385 | // methods and iterators are implemented in terms of it. The differences | |
386 | // between UTF-8 and wchar_t* representations of the string are mostly | |
387 | // contained here. | |
388 | ||
389 | #if wxUSE_UNICODE | |
390 | // FIXME-UTF8: This will need changes when UTF8 build is introduced | |
391 | static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength, | |
a962cdf4 | 392 | const wxMBConv& conv); |
8f93a29f VS |
393 | #else |
394 | static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength, | |
a962cdf4 | 395 | const wxMBConv& conv); |
8f93a29f VS |
396 | #endif |
397 | ||
398 | #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE | |
a962cdf4 | 399 | // returns C string encoded as the implementation expects: |
8f93a29f | 400 | #if wxUSE_UNICODE |
a962cdf4 VS |
401 | static const wchar_t* ImplStr(const wchar_t* str) |
402 | { return str; } | |
403 | static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
404 | { return SubstrBufFromWC(str, n == npos ? wxWcslen(str) : n); } | |
8f93a29f VS |
405 | static wxWCharBuffer ImplStr(const char* str) |
406 | { return ConvertStr(str, npos, wxConvLibc).data; } | |
407 | static SubstrBufFromMB ImplStr(const char* str, size_t n) | |
408 | { return ConvertStr(str, n, wxConvLibc); } | |
409 | #else | |
a962cdf4 VS |
410 | static const char* ImplStr(const char* str) |
411 | { return str; } | |
412 | static const SubstrBufFromMB ImplStr(const char* str, size_t n) | |
413 | { return SubstrBufFromMB(str, n == npos ? wxStrlen(str) : n); } | |
8f93a29f VS |
414 | static wxCharBuffer ImplStr(const wchar_t* str) |
415 | { return ConvertStr(str, npos, wxConvLibc).data; } | |
416 | static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) | |
417 | { return ConvertStr(str, n, wxConvLibc); } | |
418 | #endif | |
419 | ||
420 | // moves the iterator to the next Unicode character | |
421 | static void IncIter(wxStringImpl::iterator& i) { ++i; } | |
422 | static void IncIter(wxStringImpl::const_iterator& i) { ++i; } | |
423 | // moves the iterator to the previous Unicode character | |
424 | static void DecIter(wxStringImpl::iterator& i) { --i; } | |
425 | static void DecIter(wxStringImpl::const_iterator& i) { --i; } | |
426 | // moves the iterator by n Unicode characters | |
427 | static wxStringImpl::iterator AddToIter(wxStringImpl::iterator i, int n) | |
428 | { return i + n; } | |
429 | static wxStringImpl::const_iterator AddToIter(wxStringImpl::const_iterator i, int n) | |
430 | { return i + n; } | |
431 | // returns distance of the two iterators in Unicode characters | |
432 | static int DiffIters(wxStringImpl::iterator i1, wxStringImpl::iterator i2) | |
433 | { return i1 - i2; } | |
434 | static int DiffIters(wxStringImpl::const_iterator i1, wxStringImpl::const_iterator i2) | |
435 | { return i1 - i2; } | |
436 | ||
437 | // encodes the character to a form used to represent it in internal | |
438 | // representation (returns a string in UTF8 version) | |
439 | static wxChar EncodeChar(wxUniChar ch) { return (wxChar)ch; } | |
440 | ||
441 | // translates position index in wxString to/from index in underlying | |
442 | // wxStringImpl: | |
443 | static size_t PosToImpl(size_t pos) { return pos; } | |
444 | static void PosLenToImpl(size_t pos, size_t len, | |
445 | size_t *implPos, size_t *implLen) | |
446 | { *implPos = pos; *implLen = len; } | |
447 | static size_t PosFromImpl(size_t pos) { return pos; } | |
448 | ||
449 | #else // wxUSE_UNICODE_UTF8 | |
450 | ||
451 | typedef char Utf8CharBuffer[5]; | |
452 | static Utf8CharBuffer EncodeChar(wxUniChar ch); | |
453 | // returns n copies of ch encoded in UTF-8 string | |
454 | static wxCharBuffer EncodeNChars(size_t n, wxUniChar ch); | |
455 | ||
456 | size_t PosToImpl(size_t pos) const | |
457 | { | |
458 | if ( pos == 0 || pos == npos ) | |
459 | return pos; | |
460 | else | |
461 | return wxStringImpl::const_iterator(begin() + pos) - m_impl.begin(); | |
462 | } | |
463 | ||
464 | size_t PosFromImpl(size_t pos) const | |
465 | { | |
466 | if ( pos == 0 || pos == npos ) | |
467 | return pos; | |
468 | else | |
469 | return const_iterator(m_impl.begin() + pos) - begin(); | |
470 | } | |
471 | ||
472 | // FIXME: return as-is without copying under UTF8 locale, return | |
473 | // converted string under other locales - needs wxCharBuffer | |
474 | // changes | |
475 | static wxCharBuffer ImplStr(const char* str); | |
476 | ||
477 | static wxCharBuffer ImplStr(const wchar_t* str) | |
478 | { return wxConvUTF8.cWC2MB(str); } | |
479 | #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8 | |
480 | ||
481 | ||
482 | public: | |
483 | // constructors and destructor | |
484 | // ctor for an empty string | |
485 | wxString() {} | |
486 | // copy ctor | |
487 | wxString(const wxStringImpl& stringSrc) : m_impl(stringSrc) { } | |
488 | wxString(const wxString& stringSrc) : m_impl(stringSrc) { } | |
489 | // string containing nRepeat copies of ch | |
490 | wxString(wxUniChar ch, size_t nRepeat = 1) | |
491 | : m_impl(nRepeat, ch) { } | |
492 | wxString(size_t nRepeat, wxUniChar ch) | |
493 | : m_impl(nRepeat, ch) { } | |
494 | wxString(wxUniCharRef ch, size_t nRepeat = 1) | |
495 | : m_impl(nRepeat, ch) { } | |
496 | wxString(size_t nRepeat, wxUniCharRef ch) | |
497 | : m_impl(nRepeat, ch) { } | |
498 | wxString(char ch, size_t nRepeat = 1) | |
499 | : m_impl(nRepeat, ch) { } | |
500 | wxString(size_t nRepeat, char ch) | |
501 | : m_impl(nRepeat, ch) { } | |
502 | wxString(wchar_t ch, size_t nRepeat = 1) | |
503 | : m_impl(nRepeat, ch) { } | |
504 | wxString(size_t nRepeat, wchar_t ch) | |
505 | : m_impl(nRepeat, ch) { } | |
506 | // ctor takes first nLength characters from C string | |
507 | // (default value of npos means take all the string) | |
508 | wxString(const wxChar *psz) | |
509 | : m_impl(psz ? psz : wxT("")) { } | |
510 | wxString(const wxChar *psz, size_t nLength) | |
511 | : m_impl(psz, nLength) { } | |
512 | wxString(const wxChar *psz, | |
513 | const wxMBConv& WXUNUSED(conv), | |
514 | size_t nLength = npos) | |
515 | : m_impl(psz, nLength == npos ? wxStrlen(psz) : nLength) { } | |
516 | ||
517 | // even if we're not built with wxUSE_STL == 1 it is very convenient to allow | |
518 | // implicit conversions from std::string to wxString as this allows to use | |
519 | // the same strings in non-GUI and GUI code, however we don't want to | |
520 | // unconditionally add this ctor as it would make wx lib dependent on | |
521 | // libstdc++ on some Linux versions which is bad, so instead we ask the | |
522 | // client code to define this wxUSE_STD_STRING symbol if they need it | |
a962cdf4 | 523 | #if wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING |
8f93a29f | 524 | wxString(const wxStdString& s) |
a962cdf4 VS |
525 | : m_impl(s.c_str()) { } // FIXME-UTF8: this is broken for embedded 0s |
526 | #endif // wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING | |
8f93a29f VS |
527 | |
528 | #if wxUSE_UNICODE | |
529 | // from multibyte string | |
530 | wxString(const char *psz, | |
531 | const wxMBConv& conv = wxConvLibc, | |
532 | size_t nLength = npos); | |
533 | // from multibyte string for ANSI compatibility, with wxConvLibc | |
534 | wxString(const char *psz, size_t nLength); | |
535 | // from wxWCharBuffer (i.e. return from wxGetString) | |
536 | wxString(const wxWCharBuffer& psz) : m_impl(psz.data()) { } | |
537 | #else // ANSI | |
538 | // from C string (for compilers using unsigned char) | |
539 | wxString(const unsigned char* psz) | |
540 | : m_impl((const char*)psz) { } | |
541 | // from part of C string (for compilers using unsigned char) | |
542 | wxString(const unsigned char* psz, size_t nLength) | |
543 | : m_impl((const char*)psz, nLength) { } | |
544 | ||
545 | #if wxUSE_WCHAR_T | |
546 | // from wide (Unicode) string | |
547 | wxString(const wchar_t *pwz, | |
548 | const wxMBConv& conv = wxConvLibc, | |
549 | size_t nLength = npos); | |
550 | // from wide string for Unicode compatibility, with wxConvLibc | |
551 | wxString(const wchar_t *pwz, size_t nLength); | |
552 | #endif // !wxUSE_WCHAR_T | |
553 | ||
554 | // from wxCharBuffer | |
555 | wxString(const wxCharBuffer& psz) | |
556 | : m_impl(psz) { } | |
557 | #endif // Unicode/ANSI | |
558 | ||
a962cdf4 VS |
559 | wxString(const wxCStrData& cstr) |
560 | : m_impl(cstr.AsString().m_impl) { } | |
561 | ||
8f93a29f VS |
562 | // as we provide both ctors with this signature for both char and unsigned |
563 | // char string, we need to provide one for wxCStrData to resolve ambiguity | |
564 | wxString(const wxCStrData& cstr, size_t nLength) | |
565 | { assign(cstr.AsString(), nLength); } | |
566 | ||
567 | // and because wxString is convertible to wxCStrData and const wxChar * | |
568 | // we also need to provide this one | |
569 | wxString(const wxString& str, size_t nLength) | |
570 | { assign(str, nLength); } | |
571 | ||
572 | public: | |
573 | // standard types | |
574 | typedef wxUniChar value_type; | |
575 | typedef wxUniChar char_type; | |
576 | typedef wxUniCharRef reference; | |
577 | typedef wxChar* pointer; | |
578 | typedef const wxChar* const_pointer; | |
579 | ||
580 | typedef size_t size_type; | |
581 | typedef wxUniChar const_reference; | |
582 | ||
a962cdf4 VS |
583 | #if wxUSE_STL |
584 | #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag | |
585 | #else | |
586 | #define WX_STR_ITERATOR_TAG void /* dummy type */ | |
587 | #endif | |
588 | ||
8f93a29f VS |
589 | #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \ |
590 | reference_type, reference_ctor) \ | |
591 | private: \ | |
592 | typedef wxStringImpl::iterator_name underlying_iterator; \ | |
593 | public: \ | |
a962cdf4 | 594 | typedef WX_STR_ITERATOR_TAG iterator_category; \ |
8f93a29f | 595 | typedef wxUniChar value_type; \ |
a962cdf4 | 596 | typedef int difference_type; \ |
8f93a29f VS |
597 | typedef reference_type reference; \ |
598 | typedef pointer_type pointer; \ | |
599 | \ | |
600 | iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \ | |
601 | \ | |
602 | reference operator*() const { return reference_ctor; } \ | |
a962cdf4 | 603 | reference operator[](size_t n) const { return *(*this + n); } \ |
8f93a29f VS |
604 | \ |
605 | iterator_name& operator++() \ | |
606 | { wxString::IncIter(m_cur); return *this; } \ | |
607 | iterator_name& operator--() \ | |
608 | { wxString::DecIter(m_cur); return *this; } \ | |
609 | iterator_name operator++(int) \ | |
610 | { \ | |
611 | iterator_name tmp = *this; \ | |
612 | wxString::IncIter(m_cur); \ | |
613 | return tmp; \ | |
614 | } \ | |
615 | iterator_name operator--(int) \ | |
616 | { \ | |
617 | iterator_name tmp = *this; \ | |
618 | wxString::DecIter(m_cur); \ | |
619 | return tmp; \ | |
620 | } \ | |
621 | \ | |
622 | iterator_name operator+(int n) const \ | |
623 | { return iterator_name(wxString::AddToIter(m_cur, n)); } \ | |
624 | iterator_name operator+(size_t n) const \ | |
625 | { return iterator_name(wxString::AddToIter(m_cur, (int)n)); } \ | |
626 | iterator_name operator-(int n) const \ | |
627 | { return iterator_name(wxString::AddToIter(m_cur, -n)); } \ | |
628 | iterator_name operator-(size_t n) const \ | |
629 | { return iterator_name(wxString::AddToIter(m_cur, -(int)n)); } \ | |
630 | iterator_name operator+=(int n) \ | |
631 | { m_cur = wxString::AddToIter(m_cur, n); return *this; } \ | |
632 | iterator_name operator+=(size_t n) \ | |
633 | { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \ | |
634 | iterator_name operator-=(int n) \ | |
635 | { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \ | |
636 | iterator_name operator-=(size_t n) \ | |
637 | { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \ | |
638 | \ | |
639 | unsigned operator-(const iterator_name& i) const \ | |
640 | { return wxString::DiffIters(m_cur, i.m_cur); } \ | |
641 | \ | |
642 | bool operator==(const iterator_name&i) const \ | |
643 | { return m_cur == i.m_cur; } \ | |
644 | bool operator!=(const iterator_name& i) const \ | |
645 | { return m_cur != i.m_cur; } \ | |
646 | \ | |
647 | bool operator<(const iterator_name& i) const \ | |
648 | { return m_cur < i.m_cur; } \ | |
649 | bool operator>(const iterator_name& i) const \ | |
650 | { return m_cur > i.m_cur; } \ | |
651 | bool operator<=(const iterator_name& i) const \ | |
652 | { return m_cur <= i.m_cur; } \ | |
653 | bool operator>=(const iterator_name& i) const \ | |
654 | { return m_cur >= i.m_cur; } \ | |
655 | \ | |
656 | private: \ | |
657 | /* for internal wxString use only: */ \ | |
658 | iterator_name(underlying_iterator ptr) : m_cur(ptr) {} \ | |
659 | operator underlying_iterator() const { return m_cur; } \ | |
660 | \ | |
661 | friend class WXDLLIMPEXP_BASE wxString; \ | |
8f93a29f VS |
662 | friend class WXDLLIMPEXP_BASE wxCStrData; \ |
663 | \ | |
664 | private: \ | |
665 | underlying_iterator m_cur; | |
666 | ||
667 | class const_iterator; | |
668 | ||
669 | class iterator | |
670 | { | |
671 | WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, | |
672 | wxUniCharRef::CreateForString(m_cur)) | |
673 | ||
674 | friend class const_iterator; | |
675 | }; | |
676 | ||
677 | class const_iterator | |
678 | { | |
679 | // NB: reference_type is intentionally value, not reference, the character | |
680 | // may be encoded differently in wxString data: | |
681 | WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar, | |
682 | wxUniChar(*m_cur)) | |
683 | ||
684 | public: | |
685 | const_iterator(const iterator& i) : m_cur(i.m_cur) {} | |
686 | }; | |
687 | ||
a962cdf4 | 688 | #undef WX_STR_ITERATOR_TAG |
8f93a29f VS |
689 | #undef WX_STR_ITERATOR_IMPL |
690 | ||
691 | friend class iterator; | |
692 | friend class const_iterator; | |
693 | ||
694 | template <typename T> | |
695 | class reverse_iterator_impl | |
696 | { | |
697 | public: | |
698 | typedef T iterator_type; | |
a962cdf4 VS |
699 | |
700 | typedef typename T::iterator_category iterator_category; | |
8f93a29f | 701 | typedef typename T::value_type value_type; |
a962cdf4 | 702 | typedef typename T::difference_type difference_type; |
8f93a29f VS |
703 | typedef typename T::reference reference; |
704 | typedef typename T::pointer *pointer; | |
705 | ||
706 | reverse_iterator_impl(iterator_type i) : m_cur(i) {} | |
707 | reverse_iterator_impl(const reverse_iterator_impl& ri) | |
708 | : m_cur(ri.m_cur) {} | |
709 | ||
710 | iterator_type base() const { return m_cur; } | |
711 | ||
712 | reference operator*() const { return *(m_cur-1); } | |
a962cdf4 | 713 | reference operator[](size_t n) const { return *(*this + n); } |
8f93a29f VS |
714 | |
715 | reverse_iterator_impl& operator++() | |
716 | { --m_cur; return *this; } | |
717 | reverse_iterator_impl operator++(int) | |
718 | { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } | |
719 | reverse_iterator_impl& operator--() | |
720 | { ++m_cur; return *this; } | |
721 | reverse_iterator_impl operator--(int) | |
722 | { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } | |
723 | ||
724 | reverse_iterator_impl operator+(int n) const | |
725 | { return reverse_iterator_impl(m_cur - n); } | |
726 | reverse_iterator_impl operator+(size_t n) const | |
727 | { return reverse_iterator_impl(m_cur - n); } | |
728 | reverse_iterator_impl operator-(int n) const | |
729 | { return reverse_iterator_impl(m_cur + n); } | |
730 | reverse_iterator_impl operator-(size_t n) const | |
731 | { return reverse_iterator_impl(m_cur + n); } | |
732 | reverse_iterator_impl operator+=(int n) | |
733 | { m_cur -= n; return *this; } | |
734 | reverse_iterator_impl operator+=(size_t n) | |
735 | { m_cur -= n; return *this; } | |
736 | reverse_iterator_impl operator-=(int n) | |
737 | { m_cur += n; return *this; } | |
738 | reverse_iterator_impl operator-=(size_t n) | |
739 | { m_cur += n; return *this; } | |
740 | ||
741 | unsigned operator-(const reverse_iterator_impl& i) const | |
742 | { return i.m_cur - m_cur; } | |
743 | ||
744 | bool operator==(const reverse_iterator_impl& ri) const | |
745 | { return m_cur == ri.m_cur; } | |
746 | bool operator!=(const reverse_iterator_impl& ri) const | |
747 | { return !(*this == ri); } | |
748 | ||
749 | bool operator<(const reverse_iterator_impl& i) const | |
750 | { return m_cur > i.m_cur; } | |
751 | bool operator>(const reverse_iterator_impl& i) const | |
752 | { return m_cur < i.m_cur; } | |
753 | bool operator<=(const reverse_iterator_impl& i) const | |
754 | { return m_cur >= i.m_cur; } | |
755 | bool operator>=(const reverse_iterator_impl& i) const | |
756 | { return m_cur <= i.m_cur; } | |
757 | ||
758 | private: | |
759 | iterator_type m_cur; | |
760 | }; | |
e87b7833 | 761 | |
8f93a29f VS |
762 | typedef reverse_iterator_impl<iterator> reverse_iterator; |
763 | typedef reverse_iterator_impl<const_iterator> const_reverse_iterator; | |
e90c1d2a | 764 | |
8f93a29f VS |
765 | // first valid index position |
766 | const_iterator begin() const { return const_iterator(m_impl.begin()); } | |
767 | iterator begin() { return iterator(m_impl.begin()); } | |
768 | // position one after the last valid one | |
769 | const_iterator end() const { return const_iterator(m_impl.end()); } | |
770 | iterator end() { return iterator(m_impl.end()); } | |
771 | ||
772 | // first element of the reversed string | |
773 | const_reverse_iterator rbegin() const | |
774 | { return const_reverse_iterator(end()); } | |
775 | reverse_iterator rbegin() | |
776 | { return reverse_iterator(end()); } | |
777 | // one beyond the end of the reversed string | |
778 | const_reverse_iterator rend() const | |
779 | { return const_reverse_iterator(begin()); } | |
780 | reverse_iterator rend() | |
781 | { return reverse_iterator(begin()); } | |
782 | ||
783 | // std::string methods: | |
784 | #if wxUSE_UNICODE_UTF8 | |
785 | size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize! | |
786 | #else | |
787 | size_t length() const { return m_impl.length(); } | |
788 | #endif | |
ce76f779 | 789 | |
8f93a29f VS |
790 | size_type size() const { return length(); } |
791 | size_type max_size() const { return npos; } | |
e90c1d2a | 792 | |
8f93a29f | 793 | bool empty() const { return m_impl.empty(); } |
b77afb41 | 794 | |
8f93a29f VS |
795 | size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8 |
796 | void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8 | |
b77afb41 | 797 | |
8f93a29f VS |
798 | void resize(size_t nSize, wxUniChar ch = wxT('\0')) |
799 | { | |
800 | #if wxUSE_UNICODE_UTF8 | |
801 | if ( !ch.IsAscii() ) | |
802 | { | |
803 | size_t len = length(); | |
804 | if ( nSize == len) | |
805 | return; | |
806 | else if ( nSize < len ) | |
807 | erase(nSize); | |
808 | else | |
809 | append(nSize - len, ch); | |
810 | } | |
811 | else | |
812 | #endif | |
813 | m_impl.resize(nSize, (wxStringCharType)ch); | |
814 | } | |
e90c1d2a | 815 | |
8f93a29f VS |
816 | wxString substr(size_t nStart = 0, size_t nLen = npos) const |
817 | { | |
818 | size_t pos, len; | |
819 | PosLenToImpl(nStart, nLen, &pos, &len); | |
820 | return m_impl.substr(pos, len); | |
821 | } | |
e90c1d2a | 822 | |
3c67202d VZ |
823 | // generic attributes & operations |
824 | // as standard strlen() | |
e87b7833 | 825 | size_t Len() const { return length(); } |
3c67202d | 826 | // string contains any characters? |
e87b7833 | 827 | bool IsEmpty() const { return empty(); } |
d775fa82 | 828 | // empty string is "false", so !str will return true |
20aa779e | 829 | bool operator!() const { return empty(); } |
735d1db6 VZ |
830 | // truncate the string to given length |
831 | wxString& Truncate(size_t uiLen); | |
3c67202d | 832 | // empty string contents |
dd1eaa89 VZ |
833 | void Empty() |
834 | { | |
735d1db6 | 835 | Truncate(0); |
dd1eaa89 | 836 | |
5a8231ef | 837 | wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); |
7be07660 | 838 | } |
3c67202d | 839 | // empty the string and free memory |
7be07660 VZ |
840 | void Clear() |
841 | { | |
e87b7833 MB |
842 | wxString tmp(wxEmptyString); |
843 | swap(tmp); | |
dd1eaa89 VZ |
844 | } |
845 | ||
3c67202d VZ |
846 | // contents test |
847 | // Is an ascii value | |
c801d85f | 848 | bool IsAscii() const; |
3c67202d | 849 | // Is a number |
c801d85f | 850 | bool IsNumber() const; |
3c67202d | 851 | // Is a word |
c801d85f | 852 | bool IsWord() const; |
c801d85f | 853 | |
3c67202d VZ |
854 | // data access (all indexes are 0 based) |
855 | // read access | |
8f93a29f VS |
856 | wxUniChar at(size_t n) const |
857 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
c9f78968 | 858 | wxUniChar GetChar(size_t n) const |
9d9ad673 | 859 | { return at(n); } |
3c67202d | 860 | // read/write access |
8f93a29f VS |
861 | wxUniCharRef at(size_t n) |
862 | { return *(begin() + n); } // FIXME-UTF8: optimize? | |
c9f78968 | 863 | wxUniCharRef GetWritableChar(size_t n) |
9d9ad673 | 864 | { return at(n); } |
3c67202d | 865 | // write access |
c9f78968 | 866 | void SetChar(size_t n, wxUniChar ch) |
9d9ad673 | 867 | { at(n) = ch; } |
c801d85f | 868 | |
3c67202d | 869 | // get last character |
8f93a29f | 870 | wxUniChar Last() const |
09443a26 | 871 | { |
5a8231ef | 872 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
09443a26 | 873 | |
9d9ad673 | 874 | return at(length() - 1); |
09443a26 VZ |
875 | } |
876 | ||
3c67202d | 877 | // get writable last character |
c9f78968 | 878 | wxUniCharRef Last() |
09443a26 | 879 | { |
5a8231ef | 880 | wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); |
9d9ad673 | 881 | return at(length() - 1); |
09443a26 | 882 | } |
c801d85f | 883 | |
d836ee96 | 884 | /* |
9d9ad673 | 885 | Note that we we must define all of the overloads below to avoid |
c9f78968 | 886 | ambiguity when using str[0]. |
d836ee96 | 887 | */ |
c9f78968 | 888 | wxUniChar operator[](int n) const |
8f93a29f | 889 | { return at(n); } |
c1df0a3b | 890 | wxUniChar operator[](long n) const |
8f93a29f | 891 | { return at(n); } |
c9f78968 | 892 | wxUniChar operator[](size_t n) const |
8f93a29f | 893 | { return at(n); } |
01398433 | 894 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 895 | wxUniChar operator[](unsigned int n) const |
8f93a29f | 896 | { return at(n); } |
01398433 | 897 | #endif // size_t != unsigned int |
01398433 | 898 | |
9d9ad673 | 899 | // operator versions of GetWriteableChar() |
c9f78968 | 900 | wxUniCharRef operator[](int n) |
8f93a29f | 901 | { return at(n); } |
c1df0a3b | 902 | wxUniCharRef operator[](long n) |
8f93a29f | 903 | { return at(n); } |
c9f78968 | 904 | wxUniCharRef operator[](size_t n) |
8f93a29f | 905 | { return at(n); } |
d836ee96 | 906 | #ifndef wxSIZE_T_IS_UINT |
c9f78968 | 907 | wxUniCharRef operator[](unsigned int n) |
8f93a29f | 908 | { return at(n); } |
d836ee96 | 909 | #endif // size_t != unsigned int |
c801d85f | 910 | |
c9f78968 VS |
911 | // explicit conversion to C string (use this with printf()!) |
912 | wxCStrData c_str() const { return wxCStrData(this); } | |
8f93a29f | 913 | wxCStrData data() const { return c_str(); } |
c9f78968 | 914 | |
3c67202d | 915 | // implicit conversion to C string |
c9f78968 | 916 | operator wxCStrData() const { return c_str(); } |
e87b7833 | 917 | operator const wxChar*() const { return c_str(); } |
e015c2a3 | 918 | |
e015c2a3 | 919 | // identical to c_str(), for MFC compatibility |
c9f78968 VS |
920 | const wxCStrData GetData() const { return c_str(); } |
921 | ||
922 | // explicit conversion to C string in internal representation (char*, | |
923 | // wchar_t*, UTF-8-encoded char*, depending on the build): | |
8f93a29f | 924 | const_pointer wx_str() const { return m_impl.c_str(); } |
e90c1d2a | 925 | |
ef0f1387 VS |
926 | // conversion to *non-const* multibyte or widestring buffer; modifying |
927 | // returned buffer won't affect the string, these methods are only useful | |
928 | // for passing values to const-incorrect functions | |
929 | wxWritableCharBuffer char_str() const { return mb_str(); } | |
930 | wxWritableWCharBuffer wchar_str() const { return wc_str(); } | |
931 | ||
e015c2a3 VZ |
932 | // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for |
933 | // converting numbers or strings which are certain not to contain special | |
934 | // chars (typically system functions, X atoms, environment variables etc.) | |
935 | // | |
936 | // the behaviour of these functions with the strings containing anything | |
937 | // else than 7 bit ASCII characters is undefined, use at your own risk. | |
b1ac3b56 | 938 | #if wxUSE_UNICODE |
2b5f62a0 VZ |
939 | static wxString FromAscii(const char *ascii); // string |
940 | static wxString FromAscii(const char ascii); // char | |
b1ac3b56 | 941 | const wxCharBuffer ToAscii() const; |
e015c2a3 VZ |
942 | #else // ANSI |
943 | static wxString FromAscii(const char *ascii) { return wxString( ascii ); } | |
2b5f62a0 | 944 | static wxString FromAscii(const char ascii) { return wxString( ascii ); } |
e015c2a3 VZ |
945 | const char *ToAscii() const { return c_str(); } |
946 | #endif // Unicode/!Unicode | |
b1ac3b56 | 947 | |
2b5f62a0 | 948 | // conversions with (possible) format conversions: have to return a |
e90c1d2a | 949 | // buffer with temporary data |
f6bcfd97 BP |
950 | // |
951 | // the functions defined (in either Unicode or ANSI) mode are mb_str() to | |
952 | // return an ANSI (multibyte) string, wc_str() to return a wide string and | |
953 | // fn_str() to return a string which should be used with the OS APIs | |
954 | // accepting the file names. The return value is always the same, but the | |
955 | // type differs because a function may either return pointer to the buffer | |
956 | // directly or have to use intermediate buffer for translation. | |
2bb67b80 | 957 | #if wxUSE_UNICODE |
830f8f11 | 958 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; |
f6bcfd97 | 959 | |
e90c1d2a VZ |
960 | const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } |
961 | ||
e87b7833 | 962 | const wxChar* wc_str() const { return c_str(); } |
f6bcfd97 BP |
963 | |
964 | // for compatibility with !wxUSE_UNICODE version | |
830f8f11 | 965 | const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
e90c1d2a | 966 | |
2bb67b80 | 967 | #if wxMBFILES |
5f709e67 | 968 | const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } |
e90c1d2a | 969 | #else // !wxMBFILES |
e87b7833 | 970 | const wxChar* fn_str() const { return c_str(); } |
e90c1d2a VZ |
971 | #endif // wxMBFILES/!wxMBFILES |
972 | #else // ANSI | |
e87b7833 | 973 | const wxChar* mb_str() const { return c_str(); } |
f6bcfd97 BP |
974 | |
975 | // for compatibility with wxUSE_UNICODE version | |
830f8f11 | 976 | const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } |
f6bcfd97 | 977 | |
e90c1d2a | 978 | const wxWX2MBbuf mbc_str() const { return mb_str(); } |
f6bcfd97 | 979 | |
6f841509 | 980 | #if wxUSE_WCHAR_T |
ef0f1387 | 981 | const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const; |
e90c1d2a | 982 | #endif // wxUSE_WCHAR_T |
d5c8817c SC |
983 | #ifdef __WXOSX__ |
984 | const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } | |
985 | #else | |
e87b7833 | 986 | const wxChar* fn_str() const { return c_str(); } |
d5c8817c | 987 | #endif |
e90c1d2a | 988 | #endif // Unicode/ANSI |
c801d85f | 989 | |
3c67202d VZ |
990 | // overloaded assignment |
991 | // from another wxString | |
8f93a29f VS |
992 | wxString& operator=(const wxStringImpl& stringSrc) |
993 | { m_impl = stringSrc; return *this; } | |
994 | wxString& operator=(const wxCStrData& cstr) | |
995 | { return *this = cstr.AsString(); } | |
3c67202d | 996 | // from a character |
c9f78968 | 997 | wxString& operator=(wxUniChar ch) |
8f93a29f | 998 | { m_impl = EncodeChar(ch); return *this; } |
c9f78968 | 999 | wxString& operator=(wxUniCharRef ch) |
8f93a29f | 1000 | { return operator=((wxUniChar)ch); } |
c9f78968 | 1001 | wxString& operator=(char ch) |
8f93a29f | 1002 | { return operator=(wxUniChar(ch)); } |
50e02008 VS |
1003 | wxString& operator=(unsigned char ch) |
1004 | { return operator=(wxUniChar(ch)); } | |
c9f78968 | 1005 | wxString& operator=(wchar_t ch) |
8f93a29f | 1006 | { return operator=(wxUniChar(ch)); } |
c57e3bd5 RN |
1007 | // from a C string - STL probably will crash on NULL, |
1008 | // so we need to compensate in that case | |
992527a5 | 1009 | #if wxUSE_STL_BASED_WXSTRING |
c57e3bd5 | 1010 | wxString& operator=(const wxChar *psz) |
8f93a29f | 1011 | { if(psz) m_impl = psz; else Clear(); return *this; } |
c57e3bd5 | 1012 | #else |
e87b7833 | 1013 | wxString& operator=(const wxChar *psz) |
8f93a29f | 1014 | { m_impl = psz; return *this; } |
c57e3bd5 RN |
1015 | #endif |
1016 | ||
111bb7f2 OK |
1017 | #if wxUSE_UNICODE |
1018 | // from wxWCharBuffer | |
8f93a29f VS |
1019 | wxString& operator=(const wxWCharBuffer& s) |
1020 | { (void) operator=((const wchar_t *)s); return *this; } | |
d7330233 VS |
1021 | // from C string |
1022 | wxString& operator=(const char* psz) | |
1023 | { return operator=(wxString(psz)); } | |
e90c1d2a | 1024 | #else // ANSI |
3c67202d | 1025 | // from another kind of C string |
c801d85f | 1026 | wxString& operator=(const unsigned char* psz); |
6f841509 | 1027 | #if wxUSE_WCHAR_T |
3c67202d | 1028 | // from a wide string |
c801d85f | 1029 | wxString& operator=(const wchar_t *pwz); |
6f841509 | 1030 | #endif |
111bb7f2 | 1031 | // from wxCharBuffer |
b1801e0e | 1032 | wxString& operator=(const wxCharBuffer& psz) |
e87b7833 | 1033 | { (void) operator=((const char *)psz); return *this; } |
e90c1d2a | 1034 | #endif // Unicode/ANSI |
3c67202d VZ |
1035 | |
1036 | // string concatenation | |
1037 | // in place concatenation | |
1038 | /* | |
1039 | Concatenate and return the result. Note that the left to right | |
1040 | associativity of << allows to write things like "str << str1 << str2 | |
1041 | << ..." (unlike with +=) | |
1042 | */ | |
1043 | // string += string | |
dd1eaa89 VZ |
1044 | wxString& operator<<(const wxString& s) |
1045 | { | |
8f93a29f VS |
1046 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
1047 | wxASSERT_MSG( s.IsValid(), | |
09443a26 | 1048 | _T("did you forget to call UngetWriteBuf()?") ); |
e87b7833 | 1049 | #endif |
dd1eaa89 | 1050 | |
e87b7833 | 1051 | append(s); |
dd1eaa89 VZ |
1052 | return *this; |
1053 | } | |
3c67202d | 1054 | // string += C string |
8f93a29f | 1055 | wxString& operator<<(const char *psz) |
c9f78968 | 1056 | { append(psz); return *this; } |
8f93a29f VS |
1057 | wxString& operator<<(const wchar_t *pwz) |
1058 | { append(pwz); return *this; } | |
c9f78968 | 1059 | wxString& operator<<(const wxCStrData& psz) |
8f93a29f | 1060 | { append(psz.AsString()); return *this; } |
3c67202d | 1061 | // string += char |
c9f78968 VS |
1062 | wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } |
1063 | wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } | |
1064 | wxString& operator<<(char ch) { append(1, ch); return *this; } | |
50e02008 | 1065 | wxString& operator<<(unsigned char ch) { append(1, ch); return *this; } |
c9f78968 | 1066 | wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } |
2bb67b80 OK |
1067 | |
1068 | // string += buffer (i.e. from wxGetString) | |
09443a26 | 1069 | wxString& operator<<(const wxWCharBuffer& s) |
d7330233 VS |
1070 | { return operator<<((const wchar_t *)s); } |
1071 | wxString& operator+=(const wxWCharBuffer& s) | |
1072 | { return operator<<((const wchar_t *)s); } | |
8f93a29f | 1073 | |
09443a26 | 1074 | wxString& operator<<(const wxCharBuffer& s) |
d7330233 VS |
1075 | { return operator<<((const char *)s); } |
1076 | wxString& operator+=(const wxCharBuffer& s) | |
1077 | { return operator<<((const char *)s); } | |
d7330233 | 1078 | |
3c67202d | 1079 | // string += C string |
09443a26 VZ |
1080 | wxString& Append(const wxString& s) |
1081 | { | |
5a8231ef WS |
1082 | // test for empty() to share the string if possible |
1083 | if ( empty() ) | |
09443a26 VZ |
1084 | *this = s; |
1085 | else | |
e87b7833 | 1086 | append(s); |
09443a26 VZ |
1087 | return *this; |
1088 | } | |
c9f78968 VS |
1089 | wxString& Append(const wxCStrData& psz) |
1090 | { append(psz); return *this; } | |
8f93a29f | 1091 | wxString& Append(const char* psz) |
e87b7833 | 1092 | { append(psz); return *this; } |
8f93a29f VS |
1093 | wxString& Append(const wchar_t* pwz) |
1094 | { append(pwz); return *this; } | |
3c67202d | 1095 | // append count copies of given character |
c9f78968 VS |
1096 | wxString& Append(wxUniChar ch, size_t count = 1u) |
1097 | { append(count, ch); return *this; } | |
1098 | wxString& Append(wxUniCharRef ch, size_t count = 1u) | |
1099 | { append(count, ch); return *this; } | |
1100 | wxString& Append(char ch, size_t count = 1u) | |
1101 | { append(count, ch); return *this; } | |
50e02008 VS |
1102 | wxString& Append(unsigned char ch, size_t count = 1u) |
1103 | { append(count, ch); return *this; } | |
c9f78968 | 1104 | wxString& Append(wchar_t ch, size_t count = 1u) |
e87b7833 | 1105 | { append(count, ch); return *this; } |
8f93a29f | 1106 | wxString& Append(const char* psz, size_t nLen) |
e87b7833 | 1107 | { append(psz, nLen); return *this; } |
8f93a29f VS |
1108 | wxString& Append(const wchar_t* pwz, size_t nLen) |
1109 | { append(pwz, nLen); return *this; } | |
3c67202d VZ |
1110 | |
1111 | // prepend a string, return the string itself | |
1112 | wxString& Prepend(const wxString& str) | |
1113 | { *this = str + *this; return *this; } | |
1114 | ||
1115 | // non-destructive concatenation | |
1fc8cfbd VZ |
1116 | // two strings |
1117 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, | |
1118 | const wxString& string2); | |
1119 | // string with a single char | |
c9f78968 | 1120 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
1fc8cfbd | 1121 | // char with a string |
c9f78968 | 1122 | friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); |
1fc8cfbd VZ |
1123 | // string with C string |
1124 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
8f93a29f VS |
1125 | const char *psz); |
1126 | friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, | |
1127 | const wchar_t *pwz); | |
1fc8cfbd | 1128 | // C string with string |
8f93a29f VS |
1129 | friend wxString WXDLLIMPEXP_BASE operator+(const char *psz, |
1130 | const wxString& string); | |
1131 | friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, | |
1fc8cfbd | 1132 | const wxString& string); |
3c67202d VZ |
1133 | |
1134 | // stream-like functions | |
1135 | // insert an int into string | |
3ce65f6c VZ |
1136 | wxString& operator<<(int i) |
1137 | { return (*this) << Format(_T("%d"), i); } | |
1138 | // insert an unsigned int into string | |
1139 | wxString& operator<<(unsigned int ui) | |
1140 | { return (*this) << Format(_T("%u"), ui); } | |
1141 | // insert a long into string | |
1142 | wxString& operator<<(long l) | |
1143 | { return (*this) << Format(_T("%ld"), l); } | |
1144 | // insert an unsigned long into string | |
1145 | wxString& operator<<(unsigned long ul) | |
1146 | { return (*this) << Format(_T("%lu"), ul); } | |
3fc1adbd MW |
1147 | #if defined wxLongLong_t && !defined wxLongLongIsLong |
1148 | // insert a long long if they exist and aren't longs | |
1149 | wxString& operator<<(wxLongLong_t ll) | |
b7859132 MW |
1150 | { |
1151 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); | |
1152 | return (*this) << Format(fmt, ll); | |
1153 | } | |
3fc1adbd MW |
1154 | // insert an unsigned long long |
1155 | wxString& operator<<(wxULongLong_t ull) | |
b7859132 MW |
1156 | { |
1157 | const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); | |
1158 | return (*this) << Format(fmt , ull); | |
1159 | } | |
3fc1adbd | 1160 | #endif |
3c67202d | 1161 | // insert a float into string |
3ce65f6c VZ |
1162 | wxString& operator<<(float f) |
1163 | { return (*this) << Format(_T("%f"), f); } | |
3c67202d | 1164 | // insert a double into string |
3ce65f6c VZ |
1165 | wxString& operator<<(double d) |
1166 | { return (*this) << Format(_T("%g"), d); } | |
c84c52de | 1167 | |
3c67202d | 1168 | // string comparison |
30b21f9a | 1169 | // case-sensitive comparison (returns a value < 0, = 0 or > 0) |
8f93a29f VS |
1170 | int Cmp(const char *psz) const |
1171 | { return compare(psz); } | |
1172 | int Cmp(const wchar_t *pwz) const | |
1173 | { return compare(pwz); } | |
1174 | int Cmp(const wxString& s) const | |
1175 | { return compare(s); } | |
3c67202d | 1176 | // same as Cmp() but not case-sensitive |
dcb68102 | 1177 | int CmpNoCase(const wxString& s) const; |
8f93a29f VS |
1178 | int CmpNoCase(const char *psz) const |
1179 | { return CmpNoCase(wxString(psz)); } | |
1180 | int CmpNoCase(const wchar_t *pwz) const | |
1181 | { return CmpNoCase(wxString(pwz)); } | |
3c67202d VZ |
1182 | // test for the string equality, either considering case or not |
1183 | // (if compareWithCase then the case matters) | |
8f93a29f | 1184 | bool IsSameAs(const char *psz, bool compareWithCase = true) const |
3c67202d | 1185 | { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } |
8f93a29f VS |
1186 | bool IsSameAs(const wchar_t *pwz, bool compareWithCase = true) const |
1187 | { return (compareWithCase ? Cmp(pwz) : CmpNoCase(pwz)) == 0; } | |
20aa779e | 1188 | // comparison with a single character: returns true if equal |
c9f78968 | 1189 | bool IsSameAs(wxUniChar c, bool compareWithCase = true) const |
f33fee2a | 1190 | { |
e87b7833 | 1191 | return (length() == 1) && (compareWithCase ? GetChar(0u) == c |
f33fee2a VZ |
1192 | : wxToupper(GetChar(0u)) == wxToupper(c)); |
1193 | } | |
3c67202d VZ |
1194 | |
1195 | // simple sub-string extraction | |
1196 | // return substring starting at nFirst of length nCount (or till the end | |
1197 | // if nCount = default value) | |
e87b7833 | 1198 | wxString Mid(size_t nFirst, size_t nCount = npos) const; |
3c67202d | 1199 | |
f6bcfd97 | 1200 | // operator version of Mid() |
3c67202d VZ |
1201 | wxString operator()(size_t start, size_t len) const |
1202 | { return Mid(start, len); } | |
1203 | ||
3affcd07 VZ |
1204 | // check if the string starts with the given prefix and return the rest |
1205 | // of the string in the provided pointer if it is not NULL; otherwise | |
1206 | // return false | |
f6bcfd97 | 1207 | bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; |
3affcd07 VZ |
1208 | // check if the string ends with the given suffix and return the |
1209 | // beginning of the string before the suffix in the provided pointer if | |
1210 | // it is not NULL; otherwise return false | |
1211 | bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const; | |
f6bcfd97 | 1212 | |
3c67202d | 1213 | // get first nCount characters |
c801d85f | 1214 | wxString Left(size_t nCount) const; |
3c67202d | 1215 | // get last nCount characters |
c801d85f | 1216 | wxString Right(size_t nCount) const; |
c0881dc3 | 1217 | // get all characters before the first occurance of ch |
3c67202d | 1218 | // (returns the whole string if ch not found) |
c9f78968 | 1219 | wxString BeforeFirst(wxUniChar ch) const; |
3c67202d VZ |
1220 | // get all characters before the last occurence of ch |
1221 | // (returns empty string if ch not found) | |
c9f78968 | 1222 | wxString BeforeLast(wxUniChar ch) const; |
3c67202d VZ |
1223 | // get all characters after the first occurence of ch |
1224 | // (returns empty string if ch not found) | |
c9f78968 | 1225 | wxString AfterFirst(wxUniChar ch) const; |
3c67202d VZ |
1226 | // get all characters after the last occurence of ch |
1227 | // (returns the whole string if ch not found) | |
c9f78968 | 1228 | wxString AfterLast(wxUniChar ch) const; |
3c67202d VZ |
1229 | |
1230 | // for compatibility only, use more explicitly named functions above | |
c9f78968 VS |
1231 | wxString Before(wxUniChar ch) const { return BeforeLast(ch); } |
1232 | wxString After(wxUniChar ch) const { return AfterFirst(ch); } | |
3c67202d VZ |
1233 | |
1234 | // case conversion | |
c84c52de | 1235 | // convert to upper case in place, return the string itself |
c801d85f | 1236 | wxString& MakeUpper(); |
c84c52de | 1237 | // convert to upper case, return the copy of the string |
03ab016d JS |
1238 | // Here's something to remember: BC++ doesn't like returns in inlines. |
1239 | wxString Upper() const ; | |
c84c52de | 1240 | // convert to lower case in place, return the string itself |
c801d85f | 1241 | wxString& MakeLower(); |
c84c52de | 1242 | // convert to lower case, return the copy of the string |
03ab016d | 1243 | wxString Lower() const ; |
c801d85f | 1244 | |
3c67202d VZ |
1245 | // trimming/padding whitespace (either side) and truncating |
1246 | // remove spaces from left or from right (default) side | |
d775fa82 | 1247 | wxString& Trim(bool bFromRight = true); |
3c67202d | 1248 | // add nCount copies chPad in the beginning or at the end (default) |
c9f78968 | 1249 | wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); |
dd1eaa89 | 1250 | |
3c67202d VZ |
1251 | // searching and replacing |
1252 | // searching (return starting index, or -1 if not found) | |
c9f78968 | 1253 | int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr |
3c67202d | 1254 | // searching (return starting index, or -1 if not found) |
2bb67b80 | 1255 | int Find(const wxChar *pszSub) const; // like strstr |
3c67202d VZ |
1256 | // replace first (or all of bReplaceAll) occurences of substring with |
1257 | // another string, returns the number of replacements made | |
2bb67b80 OK |
1258 | size_t Replace(const wxChar *szOld, |
1259 | const wxChar *szNew, | |
d775fa82 | 1260 | bool bReplaceAll = true); |
3c67202d VZ |
1261 | |
1262 | // check if the string contents matches a mask containing '*' and '?' | |
2bb67b80 | 1263 | bool Matches(const wxChar *szMask) const; |
c801d85f | 1264 | |
d775fa82 | 1265 | // conversion to numbers: all functions return true only if the whole |
538f35cc VZ |
1266 | // string is a number and put the value of this number into the pointer |
1267 | // provided, the base is the numeric base in which the conversion should be | |
1268 | // done and must be comprised between 2 and 36 or be 0 in which case the | |
1269 | // standard C rules apply (leading '0' => octal, "0x" => hex) | |
cd0b1709 | 1270 | // convert to a signed integer |
538f35cc | 1271 | bool ToLong(long *val, int base = 10) const; |
cd0b1709 | 1272 | // convert to an unsigned integer |
538f35cc | 1273 | bool ToULong(unsigned long *val, int base = 10) const; |
d6718dd1 VZ |
1274 | // convert to wxLongLong |
1275 | #if defined(wxLongLong_t) | |
1276 | bool ToLongLong(wxLongLong_t *val, int base = 10) const; | |
1277 | // convert to wxULongLong | |
1278 | bool ToULongLong(wxULongLong_t *val, int base = 10) const; | |
1279 | #endif // wxLongLong_t | |
cd0b1709 VZ |
1280 | // convert to a double |
1281 | bool ToDouble(double *val) const; | |
1282 | ||
d6718dd1 | 1283 | |
c9f78968 | 1284 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
90e572f1 | 1285 | // formatted input/output |
3c67202d | 1286 | // as sprintf(), returns the number of characters written or < 0 on error |
7357f981 | 1287 | // (take 'this' into account in attribute parameter count) |
c9f78968 VS |
1288 | // int Printf(const wxChar *pszFormat, ...); |
1289 | WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) | |
1290 | #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN | |
3c67202d | 1291 | // as vprintf(), returns the number of characters written or < 0 on error |
c9f78968 | 1292 | int PrintfV(const wxString& format, va_list argptr); |
dd1eaa89 | 1293 | |
c9f78968 | 1294 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
341e7d28 | 1295 | // returns the string containing the result of Printf() to it |
c9f78968 VS |
1296 | // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; |
1297 | WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat) | |
1298 | #endif | |
341e7d28 | 1299 | // the same as above, but takes a va_list |
c9f78968 | 1300 | static wxString FormatV(const wxString& format, va_list argptr); |
341e7d28 | 1301 | |
3c67202d VZ |
1302 | // raw access to string memory |
1303 | // ensure that string has space for at least nLen characters | |
dd1eaa89 | 1304 | // only works if the data of this string is not shared |
e87b7833 | 1305 | bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; } |
3c67202d | 1306 | // minimize the string's memory |
dd1eaa89 | 1307 | // only works if the data of this string is not shared |
b1801e0e | 1308 | bool Shrink(); |
8f93a29f | 1309 | #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
d8a4b666 VS |
1310 | // These are deprecated, use wxStringBuffer or wxStringBufferLength instead |
1311 | // | |
3c67202d VZ |
1312 | // get writable buffer of at least nLen bytes. Unget() *must* be called |
1313 | // a.s.a.p. to put string back in a reasonable state! | |
d8a4b666 | 1314 | wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) ); |
3c67202d | 1315 | // call this immediately after GetWriteBuf() has been used |
d8a4b666 VS |
1316 | wxDEPRECATED( void UngetWriteBuf() ); |
1317 | wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); | |
8f93a29f | 1318 | #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8 |
c801d85f | 1319 | |
77ffb593 | 1320 | // wxWidgets version 1 compatibility functions |
3c67202d VZ |
1321 | |
1322 | // use Mid() | |
1323 | wxString SubString(size_t from, size_t to) const | |
1324 | { return Mid(from, (to - from + 1)); } | |
1325 | // values for second parameter of CompareTo function | |
c801d85f | 1326 | enum caseCompare {exact, ignoreCase}; |
3c67202d | 1327 | // values for first parameter of Strip function |
c801d85f | 1328 | enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; |
8870c26e | 1329 | |
c9f78968 | 1330 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN |
7357f981 GD |
1331 | // use Printf() |
1332 | // (take 'this' into account in attribute parameter count) | |
c9f78968 VS |
1333 | // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; |
1334 | WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) | |
1335 | #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN | |
c801d85f | 1336 | |
3c67202d | 1337 | // use Cmp() |
2bb67b80 | 1338 | inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const |
6b95b20d | 1339 | { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); } |
c801d85f | 1340 | |
8f93a29f | 1341 | // use length() |
e87b7833 | 1342 | size_t Length() const { return length(); } |
3c67202d | 1343 | // Count the number of characters |
c9f78968 | 1344 | int Freq(wxUniChar ch) const; |
3c67202d | 1345 | // use MakeLower |
c801d85f | 1346 | void LowerCase() { MakeLower(); } |
3c67202d | 1347 | // use MakeUpper |
c801d85f | 1348 | void UpperCase() { MakeUpper(); } |
3c67202d | 1349 | // use Trim except that it doesn't change this string |
c801d85f KB |
1350 | wxString Strip(stripType w = trailing) const; |
1351 | ||
3c67202d | 1352 | // use Find (more general variants not yet supported) |
2bb67b80 | 1353 | size_t Index(const wxChar* psz) const { return Find(psz); } |
c9f78968 | 1354 | size_t Index(wxUniChar ch) const { return Find(ch); } |
3c67202d | 1355 | // use Truncate |
c801d85f | 1356 | wxString& Remove(size_t pos) { return Truncate(pos); } |
e87b7833 | 1357 | wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } |
c801d85f | 1358 | |
e87b7833 MB |
1359 | wxString& Remove(size_t nStart, size_t nLen) |
1360 | { return (wxString&)erase( nStart, nLen ); } | |
dd1eaa89 | 1361 | |
3c67202d | 1362 | // use Find() |
8f93a29f | 1363 | int First( wxUniChar ch ) const { return Find(ch); } |
c9f78968 | 1364 | int First( char ch ) const { return Find(ch); } |
50e02008 | 1365 | int First( unsigned char ch ) const { return Find(ch); } |
c9f78968 | 1366 | int First( wchar_t ch ) const { return Find(ch); } |
2bb67b80 | 1367 | int First( const wxChar* psz ) const { return Find(psz); } |
3ed358cb | 1368 | int First( const wxString &str ) const { return Find(str); } |
8f93a29f | 1369 | int Last( wxUniChar ch ) const { return Find(ch, true); } |
d775fa82 | 1370 | bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } |
c801d85f | 1371 | |
5a8231ef WS |
1372 | // use empty() |
1373 | bool IsNull() const { return empty(); } | |
c801d85f | 1374 | |
3c67202d | 1375 | // std::string compatibility functions |
dd1eaa89 | 1376 | |
3c67202d VZ |
1377 | // take nLen chars starting at nPos |
1378 | wxString(const wxString& str, size_t nPos, size_t nLen) | |
8f93a29f | 1379 | : m_impl(str.m_impl, nPos, nLen) { } |
3c67202d | 1380 | // take all characters from pStart to pEnd |
e87b7833 | 1381 | wxString(const void *pStart, const void *pEnd) |
8f93a29f | 1382 | : m_impl((const wxChar*)pStart, (const wxChar*)pEnd) { } |
e87b7833 | 1383 | wxString(const_iterator first, const_iterator last) |
8f93a29f | 1384 | : m_impl(first, last) { } |
c9f78968 | 1385 | wxString(iterator first, iterator last) |
8f93a29f | 1386 | : m_impl(first, last) { } |
5460b9e3 | 1387 | |
3c67202d | 1388 | // lib.string.modifiers |
3c67202d VZ |
1389 | // append elements str[pos], ..., str[pos+n] |
1390 | wxString& append(const wxString& str, size_t pos, size_t n) | |
8f93a29f VS |
1391 | { |
1392 | size_t from, len; | |
1393 | str.PosLenToImpl(pos, n, &from, &len); | |
1394 | m_impl.append(str.m_impl, from, len); | |
1395 | return *this; | |
1396 | } | |
e87b7833 MB |
1397 | // append a string |
1398 | wxString& append(const wxString& str) | |
8f93a29f | 1399 | { m_impl.append(str.m_impl); return *this; } |
c9f78968 | 1400 | wxString& append(const wxCStrData& str) |
8f93a29f | 1401 | { m_impl.append(str.AsString().m_impl); return *this; } |
3c67202d | 1402 | // append first n (or all if n == npos) characters of sz |
8f93a29f VS |
1403 | wxString& append(const char *sz) |
1404 | { m_impl.append(ImplStr(sz)); return *this; } | |
1405 | wxString& append(const wchar_t *sz) | |
1406 | { m_impl.append(ImplStr(sz)); return *this; } | |
1407 | wxString& append(const char *sz, size_t n) | |
1408 | { | |
1409 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1410 | m_impl.append(str.data, str.len); | |
1411 | return *this; | |
1412 | } | |
1413 | wxString& append(const wchar_t *sz, size_t n) | |
1414 | { | |
1415 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1416 | m_impl.append(str.data, str.len); | |
1417 | return *this; | |
1418 | } | |
3c67202d | 1419 | // append n copies of ch |
c9f78968 | 1420 | wxString& append(size_t n, wxUniChar ch) |
8f93a29f VS |
1421 | { |
1422 | #if wxUSE_UNICODE_UTF8 | |
1423 | if ( !ch.IsAscii() ) | |
1424 | m_impl.append(EncodeNChars(n, ch)); | |
1425 | else | |
1426 | #endif | |
1427 | m_impl.append(n, (wxStringCharType)ch); | |
1428 | return *this; | |
1429 | } | |
e87b7833 MB |
1430 | // append from first to last |
1431 | wxString& append(const_iterator first, const_iterator last) | |
8f93a29f | 1432 | { m_impl.append(first, last); return *this; } |
3c67202d VZ |
1433 | |
1434 | // same as `this_string = str' | |
735d1db6 | 1435 | wxString& assign(const wxString& str) |
8f93a29f | 1436 | { m_impl = str.m_impl; return *this; } |
3c67202d VZ |
1437 | // same as ` = str[pos..pos + n] |
1438 | wxString& assign(const wxString& str, size_t pos, size_t n) | |
8f93a29f VS |
1439 | { |
1440 | size_t from, len; | |
1441 | str.PosLenToImpl(pos, n, &from, &len); | |
1442 | m_impl.assign(str.m_impl, from, len); | |
1443 | return *this; | |
1444 | } | |
3c67202d | 1445 | // same as `= first n (or all if n == npos) characters of sz' |
8f93a29f VS |
1446 | wxString& assign(const char *sz) |
1447 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1448 | wxString& assign(const wchar_t *sz) | |
1449 | { m_impl.assign(ImplStr(sz)); return *this; } | |
1450 | wxString& assign(const char *sz, size_t n) | |
1451 | { | |
1452 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1453 | m_impl.assign(str.data, str.len); | |
1454 | return *this; | |
1455 | } | |
1456 | wxString& assign(const wchar_t *sz, size_t n) | |
1457 | { | |
1458 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1459 | m_impl.assign(str.data, str.len); | |
1460 | return *this; | |
1461 | } | |
3c67202d | 1462 | // same as `= n copies of ch' |
c9f78968 | 1463 | wxString& assign(size_t n, wxUniChar ch) |
8f93a29f VS |
1464 | { |
1465 | #if wxUSE_UNICODE_UTF8 | |
1466 | if ( !ch.IsAscii() ) | |
1467 | m_impl.assign(EncodeNChars(n, ch)); | |
1468 | else | |
1469 | #endif | |
1470 | m_impl.assign(n, (wxStringCharType)ch); | |
1471 | return *this; | |
1472 | } | |
e87b7833 MB |
1473 | // assign from first to last |
1474 | wxString& assign(const_iterator first, const_iterator last) | |
8f93a29f | 1475 | { m_impl.assign(first, last); return *this; } |
e87b7833 MB |
1476 | |
1477 | // string comparison | |
8f93a29f | 1478 | int compare(const wxString& str) const; |
e87b7833 | 1479 | // comparison with a substring |
8f93a29f | 1480 | int compare(size_t nStart, size_t nLen, const wxString& str) const; |
e87b7833 MB |
1481 | // comparison of 2 substrings |
1482 | int compare(size_t nStart, size_t nLen, | |
8f93a29f | 1483 | const wxString& str, size_t nStart2, size_t nLen2) const; |
e87b7833 | 1484 | // just like strcmp() |
8f93a29f VS |
1485 | int compare(const char* sz) const; |
1486 | int compare(const wchar_t* sz) const; | |
e87b7833 MB |
1487 | // substring comparison with first nCount characters of sz |
1488 | int compare(size_t nStart, size_t nLen, | |
8f93a29f VS |
1489 | const char* sz, size_t nCount = npos) const; |
1490 | int compare(size_t nStart, size_t nLen, | |
1491 | const wchar_t* sz, size_t nCount = npos) const; | |
3c67202d VZ |
1492 | |
1493 | // insert another string | |
e87b7833 | 1494 | wxString& insert(size_t nPos, const wxString& str) |
8f93a29f | 1495 | { insert(begin() + nPos, str.begin(), str.end()); return *this; } |
3c67202d VZ |
1496 | // insert n chars of str starting at nStart (in str) |
1497 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n) | |
8f93a29f VS |
1498 | { |
1499 | size_t from, len; | |
1500 | str.PosLenToImpl(nStart, n, &from, &len); | |
1501 | m_impl.insert(PosToImpl(nPos), str.m_impl, from, len); | |
1502 | return *this; | |
1503 | } | |
3c67202d | 1504 | // insert first n (or all if n == npos) characters of sz |
8f93a29f VS |
1505 | wxString& insert(size_t nPos, const char *sz) |
1506 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1507 | wxString& insert(size_t nPos, const wchar_t *sz) | |
1508 | { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; } | |
1509 | wxString& insert(size_t nPos, const char *sz, size_t n) | |
1510 | { | |
1511 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1512 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1513 | return *this; | |
1514 | } | |
1515 | wxString& insert(size_t nPos, const wchar_t *sz, size_t n) | |
1516 | { | |
1517 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1518 | m_impl.insert(PosToImpl(nPos), str.data, str.len); | |
1519 | return *this; | |
1520 | } | |
3c67202d | 1521 | // insert n copies of ch |
c9f78968 | 1522 | wxString& insert(size_t nPos, size_t n, wxUniChar ch) |
8f93a29f VS |
1523 | { |
1524 | #if wxUSE_UNICODE_UTF8 | |
1525 | if ( !ch.IsAscii() ) | |
1526 | m_impl.insert(begin() + nPos, EncodeNChars(n, ch)); | |
1527 | else | |
1528 | #endif | |
1529 | m_impl.insert(begin() + nPos, n, (wxStringCharType)ch); | |
1530 | return *this; | |
1531 | } | |
c9f78968 | 1532 | iterator insert(iterator it, wxUniChar ch) |
8f93a29f | 1533 | { return iterator(m_impl.insert(it, EncodeChar(ch))); } |
e87b7833 | 1534 | void insert(iterator it, const_iterator first, const_iterator last) |
8f93a29f | 1535 | { m_impl.insert(it, first, last); } |
c9f78968 | 1536 | void insert(iterator it, size_type n, wxUniChar ch) |
8f93a29f VS |
1537 | { |
1538 | #if wxUSE_UNICODE_UTF8 | |
1539 | if ( !ch.IsAscii() ) | |
1540 | m_impl.insert(it, EncodeNChars(n, ch)); | |
1541 | else | |
1542 | #endif | |
1543 | m_impl.insert(it, n, (wxStringCharType)ch); | |
1544 | } | |
3c67202d VZ |
1545 | |
1546 | // delete characters from nStart to nStart + nLen | |
e87b7833 | 1547 | wxString& erase(size_type pos = 0, size_type n = npos) |
8f93a29f VS |
1548 | { |
1549 | size_t from, len; | |
1550 | PosLenToImpl(pos, n, &from, &len); | |
1551 | m_impl.erase(from, len); | |
1552 | return *this; | |
1553 | } | |
e87b7833 | 1554 | iterator erase(iterator first, iterator last) |
8f93a29f | 1555 | { return iterator(m_impl.erase(first, last)); } |
e87b7833 | 1556 | iterator erase(iterator first) |
8f93a29f | 1557 | { return iterator(m_impl.erase(first)); } |
e87b7833 MB |
1558 | |
1559 | #ifdef wxSTRING_BASE_HASNT_CLEAR | |
1560 | void clear() { erase(); } | |
8f93a29f VS |
1561 | #else |
1562 | void clear() { m_impl.clear(); } | |
e87b7833 | 1563 | #endif |
3c67202d VZ |
1564 | |
1565 | // replaces the substring of length nLen starting at nStart | |
8f93a29f VS |
1566 | wxString& replace(size_t nStart, size_t nLen, const char* sz) |
1567 | { | |
1568 | size_t from, len; | |
1569 | PosLenToImpl(nStart, nLen, &from, &len); | |
1570 | m_impl.replace(from, len, ImplStr(sz)); | |
1571 | return *this; | |
1572 | } | |
1573 | wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz) | |
1574 | { | |
1575 | size_t from, len; | |
1576 | PosLenToImpl(nStart, nLen, &from, &len); | |
1577 | m_impl.replace(from, len, ImplStr(sz)); | |
1578 | return *this; | |
1579 | } | |
e87b7833 MB |
1580 | // replaces the substring of length nLen starting at nStart |
1581 | wxString& replace(size_t nStart, size_t nLen, const wxString& str) | |
8f93a29f VS |
1582 | { |
1583 | size_t from, len; | |
1584 | PosLenToImpl(nStart, nLen, &from, &len); | |
1585 | m_impl.replace(from, len, str.m_impl); | |
1586 | return *this; | |
1587 | } | |
3c67202d | 1588 | // replaces the substring with nCount copies of ch |
c9f78968 | 1589 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) |
8f93a29f VS |
1590 | { |
1591 | size_t from, len; | |
1592 | PosLenToImpl(nStart, nLen, &from, &len); | |
1593 | #if wxUSE_UNICODE_UTF8 | |
1594 | if ( !ch.IsAscii() ) | |
1595 | m_impl.replace(from, len, EncodeNChars(nCount, ch)); | |
1596 | else | |
1597 | #endif | |
1598 | m_impl.replace(from, len, nCount, (wxStringCharType)ch); | |
1599 | return *this; | |
1600 | } | |
3c67202d VZ |
1601 | // replaces a substring with another substring |
1602 | wxString& replace(size_t nStart, size_t nLen, | |
e87b7833 | 1603 | const wxString& str, size_t nStart2, size_t nLen2) |
8f93a29f VS |
1604 | { |
1605 | size_t from, len; | |
1606 | PosLenToImpl(nStart, nLen, &from, &len); | |
1607 | ||
1608 | size_t from2, len2; | |
1609 | str.PosLenToImpl(nStart2, nLen2, &from2, &len2); | |
1610 | ||
1611 | m_impl.replace(from, len, str.m_impl, from2, len2); | |
1612 | return *this; | |
1613 | } | |
e87b7833 | 1614 | // replaces the substring with first nCount chars of sz |
fb20fa43 | 1615 | wxString& replace(size_t nStart, size_t nLen, |
8f93a29f VS |
1616 | const char* sz, size_t nCount) |
1617 | { | |
1618 | size_t from, len; | |
1619 | PosLenToImpl(nStart, nLen, &from, &len); | |
1620 | ||
1621 | SubstrBufFromMB str(ImplStr(sz, nCount)); | |
1622 | ||
1623 | m_impl.replace(from, len, str.data, str.len); | |
1624 | return *this; | |
1625 | } | |
1626 | wxString& replace(size_t nStart, size_t nLen, | |
1627 | const wchar_t* sz, size_t nCount) | |
1628 | { | |
1629 | size_t from, len; | |
1630 | PosLenToImpl(nStart, nLen, &from, &len); | |
1631 | ||
1632 | SubstrBufFromWC str(ImplStr(sz, nCount)); | |
1633 | ||
1634 | m_impl.replace(from, len, str.data, str.len); | |
1635 | return *this; | |
1636 | } | |
1637 | wxString& replace(iterator first, iterator last, const char* s) | |
1638 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
1639 | wxString& replace(iterator first, iterator last, const wchar_t* s) | |
1640 | { m_impl.replace(first, last, ImplStr(s)); return *this; } | |
1641 | wxString& replace(iterator first, iterator last, const char* s, size_type n) | |
1642 | { | |
1643 | SubstrBufFromMB str(ImplStr(s, n)); | |
1644 | m_impl.replace(first, last, str.data, str.len); | |
1645 | return *this; | |
1646 | } | |
1647 | wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n) | |
1648 | { | |
1649 | SubstrBufFromWC str(ImplStr(s, n)); | |
1650 | m_impl.replace(first, last, str.data, str.len); | |
1651 | return *this; | |
1652 | } | |
e87b7833 | 1653 | wxString& replace(iterator first, iterator last, const wxString& s) |
8f93a29f VS |
1654 | { m_impl.replace(first, last, s.m_impl); return *this; } |
1655 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch) | |
1656 | { | |
1657 | #if wxUSE_UNICODE_UTF8 | |
1658 | if ( !ch.IsAscii() ) | |
1659 | m_impl.replace(first, last, EncodeNChars(n, ch)); | |
1660 | else | |
1661 | #endif | |
1662 | m_impl.replace(first, last, n, (wxStringCharType)ch); | |
1663 | return *this; | |
1664 | } | |
e87b7833 MB |
1665 | wxString& replace(iterator first, iterator last, |
1666 | const_iterator first1, const_iterator last1) | |
8f93a29f VS |
1667 | { m_impl.replace(first, last, first1, last1); return *this; } |
1668 | ||
1669 | // swap two strings | |
1670 | void swap(wxString& str) | |
1671 | { m_impl.swap(str.m_impl); } | |
1672 | ||
1673 | // find a substring | |
1674 | size_t find(const wxString& str, size_t nStart = 0) const | |
1675 | { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); } | |
1676 | ||
1677 | // find first n characters of sz | |
1678 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const | |
1679 | { | |
1680 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1681 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
1682 | } | |
1683 | size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const | |
1684 | { | |
1685 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1686 | return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); | |
1687 | } | |
1688 | ||
1689 | // find the first occurence of character ch after nStart | |
1690 | size_t find(wxUniChar ch, size_t nStart = 0) const | |
1691 | { return PosFromImpl(m_impl.find(EncodeChar(ch), PosToImpl(nStart))); } | |
1692 | size_t find(wxUniCharRef ch, size_t nStart = 0) const | |
1693 | { return find(wxUniChar(ch), nStart); } | |
1694 | size_t find(char ch, size_t nStart = 0) const | |
1695 | { return find(wxUniChar(ch), nStart); } | |
50e02008 VS |
1696 | size_t find(unsigned char ch, size_t nStart = 0) const |
1697 | { return find(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1698 | size_t find(wchar_t ch, size_t nStart = 0) const |
1699 | { return find(wxUniChar(ch), nStart); } | |
1700 | ||
1701 | // rfind() family is exactly like find() but works right to left | |
1702 | ||
1703 | // as find, but from the end | |
1704 | size_t rfind(const wxString& str, size_t nStart = npos) const | |
1705 | { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); } | |
1706 | ||
1707 | // as find, but from the end | |
1708 | size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const | |
1709 | { | |
1710 | SubstrBufFromMB str(ImplStr(sz, n)); | |
1711 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
1712 | } | |
1713 | size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const | |
1714 | { | |
1715 | SubstrBufFromWC str(ImplStr(sz, n)); | |
1716 | return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); | |
1717 | } | |
1718 | // as find, but from the end | |
1719 | size_t rfind(wxUniChar ch, size_t nStart = npos) const | |
1720 | { return PosFromImpl(m_impl.rfind(EncodeChar(ch), PosToImpl(nStart))); } | |
1721 | size_t rfind(wxUniCharRef ch, size_t nStart = npos) const | |
1722 | { return rfind(wxUniChar(ch), nStart); } | |
1723 | size_t rfind(char ch, size_t nStart = npos) const | |
1724 | { return rfind(wxUniChar(ch), nStart); } | |
50e02008 VS |
1725 | size_t rfind(unsigned char ch, size_t nStart = npos) const |
1726 | { return rfind(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1727 | size_t rfind(wchar_t ch, size_t nStart = npos) const |
1728 | { return rfind(wxUniChar(ch), nStart); } | |
1729 | ||
1730 | // find first/last occurence of any character (not) in the set: | |
1731 | #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 | |
1732 | // FIXME-UTF8: this is not entirely correct, because it doesn't work if | |
1733 | // sizeof(wchar_t)==2 and surrogates are present in the string; | |
1734 | // should we care? Probably not. | |
1735 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
a962cdf4 | 1736 | { return m_impl.find_first_of(str.m_impl, nStart); } |
8f93a29f VS |
1737 | size_t find_first_of(const char* sz, size_t nStart = 0) const |
1738 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
1739 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const | |
1740 | { return m_impl.find_first_of(ImplStr(sz), nStart); } | |
a962cdf4 | 1741 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 1742 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1743 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f VS |
1744 | { return m_impl.find_first_of(ImplStr(sz), nStart, n); } |
1745 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
1746 | { return m_impl.find_first_of((wxChar)c, nStart); } | |
1747 | ||
a962cdf4 VS |
1748 | size_t find_last_of(const wxString& str, size_t nStart = npos) const |
1749 | { return m_impl.find_last_of(str.m_impl, nStart); } | |
8f93a29f VS |
1750 | size_t find_last_of(const char* sz, size_t nStart = npos) const |
1751 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
1752 | size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const | |
1753 | { return m_impl.find_last_of(ImplStr(sz), nStart); } | |
1754 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const | |
1755 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
1756 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const | |
1757 | { return m_impl.find_last_of(ImplStr(sz), nStart, n); } | |
1758 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
1759 | { return m_impl.find_last_of((wxChar)c, nStart); } | |
1760 | ||
a962cdf4 | 1761 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const |
8f93a29f | 1762 | { return m_impl.find_first_not_of(str.m_impl, nStart); } |
a962cdf4 | 1763 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const |
8f93a29f | 1764 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1765 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const |
8f93a29f | 1766 | { return m_impl.find_first_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1767 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 1768 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1769 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 1770 | { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1771 | size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const |
8f93a29f VS |
1772 | { return m_impl.find_first_not_of((wxChar)c, nStart); } |
1773 | ||
a962cdf4 | 1774 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const |
8f93a29f | 1775 | { return m_impl.find_last_not_of(str.m_impl, nStart); } |
a962cdf4 | 1776 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const |
8f93a29f | 1777 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1778 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const |
8f93a29f | 1779 | { return m_impl.find_last_not_of(ImplStr(sz), nStart); } |
a962cdf4 | 1780 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const |
8f93a29f | 1781 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1782 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const |
8f93a29f | 1783 | { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); } |
a962cdf4 | 1784 | size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const |
8f93a29f VS |
1785 | { return m_impl.find_last_not_of((wxChar)c, nStart); } |
1786 | #else | |
1787 | // we can't use std::string implementation in UTF-8 build, because the | |
1788 | // character sets would be interpreted wrongly: | |
1789 | ||
1790 | // as strpbrk() but starts at nStart, returns npos if not found | |
1791 | size_t find_first_of(const wxString& str, size_t nStart = 0) const | |
1792 | { return find_first_of((const wxChar*)str.c_str(), nStart); } | |
1793 | // same as above | |
1794 | size_t find_first_of(const char* sz, size_t nStart = 0) const; | |
1795 | size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const; | |
1796 | size_t find_first_of(const char* sz, size_t nStart, size_t n) const; | |
1797 | size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1798 | // same as find(char, size_t) | |
1799 | size_t find_first_of(wxUniChar c, size_t nStart = 0) const | |
1800 | { return find(c, nStart); } | |
1801 | // find the last (starting from nStart) char from str in this string | |
1802 | size_t find_last_of (const wxString& str, size_t nStart = npos) const | |
1803 | { return find_last_of((const wxChar*)str.c_str(), nStart); } | |
1804 | // same as above | |
1805 | size_t find_last_of (const char* sz, size_t nStart = npos) const; | |
1806 | size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const; | |
1807 | size_t find_last_of(const char* sz, size_t nStart, size_t n) const; | |
1808 | size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1809 | // same as above | |
1810 | size_t find_last_of(wxUniChar c, size_t nStart = npos) const | |
1811 | { return rfind(c, nStart); } | |
1812 | ||
1813 | // find first/last occurence of any character not in the set | |
1814 | ||
1815 | // as strspn() (starting from nStart), returns npos on failure | |
1816 | size_t find_first_not_of(const wxString& str, size_t nStart = 0) const | |
1817 | { return find_first_not_of((const wxChar*)str.c_str(), nStart); } | |
1818 | // same as above | |
1819 | size_t find_first_not_of(const char* sz, size_t nStart = 0) const; | |
1820 | size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const; | |
1821 | size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const; | |
1822 | size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1823 | // same as above | |
1824 | size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; | |
1825 | // as strcspn() | |
1826 | size_t find_last_not_of(const wxString& str, size_t nStart = npos) const | |
1827 | { return find_last_not_of((const wxChar*)str.c_str(), nStart); } | |
1828 | // same as above | |
1829 | size_t find_last_not_of(const char* sz, size_t nStart = npos) const; | |
1830 | size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const; | |
1831 | size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const; | |
1832 | size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const; | |
1833 | // same as above | |
1834 | size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; | |
1835 | #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not | |
1836 | ||
1837 | // provide char/wchar_t/wxUniCharRef overloads for char-finding functions | |
1838 | // above to resolve ambiguities: | |
1839 | size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const | |
1840 | { return find_first_of(wxUniChar(ch), nStart); } | |
1841 | size_t find_first_of(char ch, size_t nStart = 0) const | |
1842 | { return find_first_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1843 | size_t find_first_of(unsigned char ch, size_t nStart = 0) const |
1844 | { return find_first_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1845 | size_t find_first_of(wchar_t ch, size_t nStart = 0) const |
1846 | { return find_first_of(wxUniChar(ch), nStart); } | |
1847 | size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const | |
1848 | { return find_last_of(wxUniChar(ch), nStart); } | |
1849 | size_t find_last_of(char ch, size_t nStart = npos) const | |
1850 | { return find_last_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1851 | size_t find_last_of(unsigned char ch, size_t nStart = npos) const |
1852 | { return find_last_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1853 | size_t find_last_of(wchar_t ch, size_t nStart = npos) const |
1854 | { return find_last_of(wxUniChar(ch), nStart); } | |
1855 | size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const | |
1856 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
1857 | size_t find_first_not_of(char ch, size_t nStart = 0) const | |
1858 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1859 | size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const |
1860 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1861 | size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const |
1862 | { return find_first_not_of(wxUniChar(ch), nStart); } | |
1863 | size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const | |
1864 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
1865 | size_t find_last_not_of(char ch, size_t nStart = npos) const | |
1866 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
50e02008 VS |
1867 | size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const |
1868 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
8f93a29f VS |
1869 | size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const |
1870 | { return find_last_not_of(wxUniChar(ch), nStart); } | |
3c67202d | 1871 | |
e87b7833 MB |
1872 | // string += string |
1873 | wxString& operator+=(const wxString& s) | |
8f93a29f | 1874 | { m_impl += s.m_impl; return *this; } |
e87b7833 | 1875 | // string += C string |
8f93a29f VS |
1876 | wxString& operator+=(const char *psz) |
1877 | { m_impl += ImplStr(psz); return *this; } | |
1878 | wxString& operator+=(const wchar_t *pwz) | |
1879 | { m_impl += ImplStr(pwz); return *this; } | |
c9f78968 | 1880 | wxString& operator+=(const wxCStrData& s) |
8f93a29f | 1881 | { m_impl += s.AsString().m_impl; return *this; } |
e87b7833 | 1882 | // string += char |
c9f78968 | 1883 | wxString& operator+=(wxUniChar ch) |
8f93a29f | 1884 | { m_impl += EncodeChar(ch); return *this; } |
c9f78968 | 1885 | wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } |
b7d40341 | 1886 | wxString& operator+=(int ch) { return *this += wxUniChar(ch); } |
c9f78968 | 1887 | wxString& operator+=(char ch) { return *this += wxUniChar(ch); } |
b77afb41 | 1888 | wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); } |
c9f78968 | 1889 | wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } |
d8a4b666 VS |
1890 | |
1891 | private: | |
8f93a29f | 1892 | #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
d8a4b666 | 1893 | // helpers for wxStringBuffer and wxStringBufferLength |
8f93a29f VS |
1894 | wxStringCharType *DoGetWriteBuf(size_t nLen) |
1895 | { return m_impl.DoGetWriteBuf(nLen); } | |
1896 | void DoUngetWriteBuf() | |
1897 | { m_impl.DoUngetWriteBuf(); } | |
1898 | void DoUngetWriteBuf(size_t nLen) | |
1899 | { m_impl.DoUngetWriteBuf(nLen); } | |
d8a4b666 VS |
1900 | |
1901 | friend class WXDLLIMPEXP_BASE wxStringBuffer; | |
1902 | friend class WXDLLIMPEXP_BASE wxStringBufferLength; | |
8f93a29f | 1903 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
c9f78968 VS |
1904 | |
1905 | #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN | |
1906 | int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; | |
1907 | static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; | |
1908 | #endif | |
8f93a29f VS |
1909 | |
1910 | #if !wxUSE_STL_BASED_WXSTRING | |
1911 | // check string's data validity | |
1912 | bool IsValid() const { return m_impl.GetStringData()->IsValid(); } | |
1913 | #endif | |
1914 | ||
1915 | private: | |
1916 | wxStringImpl m_impl; | |
c801d85f KB |
1917 | }; |
1918 | ||
c9f78968 VS |
1919 | #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN |
1920 | #pragma warning (default:4275) | |
1921 | #endif | |
1922 | ||
a962cdf4 VS |
1923 | // string iterator operators that satisfy STL Random Access Iterator |
1924 | // requirements: | |
1925 | inline wxString::iterator operator+(int n, wxString::iterator i) | |
1926 | { return i + n; } | |
1927 | inline wxString::iterator operator+(size_t n, wxString::iterator i) | |
1928 | { return i + n; } | |
1929 | inline wxString::const_iterator operator+(int n, wxString::const_iterator i) | |
1930 | { return i + n; } | |
1931 | inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i) | |
1932 | { return i + n; } | |
1933 | inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i) | |
1934 | { return i + n; } | |
1935 | inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i) | |
1936 | { return i + n; } | |
1937 | inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i) | |
1938 | { return i + n; } | |
1939 | inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i) | |
1940 | { return i + n; } | |
1941 | ||
1fc8cfbd VZ |
1942 | // notice that even though for many compilers the friend declarations above are |
1943 | // enough, from the point of view of C++ standard we must have the declarations | |
1944 | // here as friend ones are not injected in the enclosing namespace and without | |
1945 | // them the code fails to compile with conforming compilers such as xlC or g++4 | |
c9f78968 | 1946 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); |
8f93a29f VS |
1947 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz); |
1948 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz); | |
1949 | wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string); | |
1950 | wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string); | |
1fc8cfbd | 1951 | |
c9f78968 VS |
1952 | wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); |
1953 | wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); | |
1954 | ||
1955 | inline wxString operator+(const wxString& string, wxUniCharRef ch) | |
1956 | { return string + (wxUniChar)ch; } | |
1957 | inline wxString operator+(const wxString& string, char ch) | |
1958 | { return string + wxUniChar(ch); } | |
1959 | inline wxString operator+(const wxString& string, wchar_t ch) | |
1960 | { return string + wxUniChar(ch); } | |
1961 | inline wxString operator+(wxUniCharRef ch, const wxString& string) | |
1962 | { return (wxUniChar)ch + string; } | |
1963 | inline wxString operator+(char ch, const wxString& string) | |
1964 | { return wxUniChar(ch) + string; } | |
1965 | inline wxString operator+(wchar_t ch, const wxString& string) | |
1966 | { return wxUniChar(ch) + string; } | |
1967 | ||
b7146cbe | 1968 | |
992527a5 | 1969 | #if wxUSE_STL_BASED_WXSTRING |
07170120 | 1970 | // return an empty wxString (not very useful with wxUSE_STL == 1) |
12f99210 | 1971 | inline const wxString wxGetEmptyString() { return wxString(); } |
992527a5 | 1972 | #else // !wxUSE_STL_BASED_WXSTRING |
07170120 VZ |
1973 | // return an empty wxString (more efficient than wxString() here) |
1974 | inline const wxString& wxGetEmptyString() | |
1975 | { | |
1976 | return *(wxString *)&wxEmptyString; | |
1977 | } | |
992527a5 | 1978 | #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING |
07170120 | 1979 | |
1d218550 VZ |
1980 | // ---------------------------------------------------------------------------- |
1981 | // wxStringBuffer: a tiny class allowing to get a writable pointer into string | |
1982 | // ---------------------------------------------------------------------------- | |
1983 | ||
8f93a29f | 1984 | #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8 |
941b78cc MB |
1985 | |
1986 | class WXDLLIMPEXP_BASE wxStringBuffer | |
1987 | { | |
1988 | public: | |
1989 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
e87b7833 | 1990 | : m_str(str), m_buf(lenWanted) |
941b78cc MB |
1991 | { } |
1992 | ||
fe5fc682 | 1993 | ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); } |
941b78cc MB |
1994 | |
1995 | operator wxChar*() { return m_buf.data(); } | |
1996 | ||
1997 | private: | |
1998 | wxString& m_str; | |
1999 | #if wxUSE_UNICODE | |
2000 | wxWCharBuffer m_buf; | |
2001 | #else | |
2002 | wxCharBuffer m_buf; | |
2003 | #endif | |
941b78cc MB |
2004 | |
2005 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
2006 | }; | |
2007 | ||
2008 | class WXDLLIMPEXP_BASE wxStringBufferLength | |
2009 | { | |
2010 | public: | |
2011 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2012 | : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false) | |
2013 | { } | |
2014 | ||
2015 | ~wxStringBufferLength() | |
2016 | { | |
2017 | wxASSERT(m_lenSet); | |
2018 | m_str.assign(m_buf.data(), m_len); | |
2019 | } | |
2020 | ||
2021 | operator wxChar*() { return m_buf.data(); } | |
2022 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2023 | ||
2024 | private: | |
2025 | wxString& m_str; | |
2026 | #if wxUSE_UNICODE | |
2027 | wxWCharBuffer m_buf; | |
2028 | #else | |
2029 | wxCharBuffer m_buf; | |
2030 | #endif | |
2031 | size_t m_len; | |
2032 | bool m_lenSet; | |
2033 | ||
2034 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2035 | }; | |
2036 | ||
8f93a29f | 2037 | #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
941b78cc | 2038 | |
bddd7a8d | 2039 | class WXDLLIMPEXP_BASE wxStringBuffer |
1d218550 VZ |
2040 | { |
2041 | public: | |
2042 | wxStringBuffer(wxString& str, size_t lenWanted = 1024) | |
b1801e0e | 2043 | : m_str(str), m_buf(NULL) |
d8a4b666 | 2044 | { m_buf = m_str.DoGetWriteBuf(lenWanted); } |
e015c2a3 | 2045 | |
d8a4b666 | 2046 | ~wxStringBuffer() { m_str.DoUngetWriteBuf(); } |
e015c2a3 | 2047 | |
1d218550 | 2048 | operator wxChar*() const { return m_buf; } |
e015c2a3 | 2049 | |
1d218550 VZ |
2050 | private: |
2051 | wxString& m_str; | |
2052 | wxChar *m_buf; | |
e015c2a3 VZ |
2053 | |
2054 | DECLARE_NO_COPY_CLASS(wxStringBuffer) | |
1d218550 VZ |
2055 | }; |
2056 | ||
941b78cc MB |
2057 | class WXDLLIMPEXP_BASE wxStringBufferLength |
2058 | { | |
2059 | public: | |
2060 | wxStringBufferLength(wxString& str, size_t lenWanted = 1024) | |
2061 | : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) | |
4055ed82 | 2062 | { |
d8a4b666 | 2063 | m_buf = m_str.DoGetWriteBuf(lenWanted); |
494b978f RN |
2064 | wxASSERT(m_buf != NULL); |
2065 | } | |
941b78cc MB |
2066 | |
2067 | ~wxStringBufferLength() | |
2068 | { | |
2069 | wxASSERT(m_lenSet); | |
d8a4b666 | 2070 | m_str.DoUngetWriteBuf(m_len); |
941b78cc MB |
2071 | } |
2072 | ||
2073 | operator wxChar*() const { return m_buf; } | |
2074 | void SetLength(size_t length) { m_len = length; m_lenSet = true; } | |
2075 | ||
2076 | private: | |
2077 | wxString& m_str; | |
2078 | wxChar *m_buf; | |
2079 | size_t m_len; | |
2080 | bool m_lenSet; | |
2081 | ||
2082 | DECLARE_NO_COPY_CLASS(wxStringBufferLength) | |
2083 | }; | |
2084 | ||
8f93a29f | 2085 | #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 |
941b78cc | 2086 | |
c801d85f | 2087 | // --------------------------------------------------------------------------- |
3c67202d | 2088 | // wxString comparison functions: operator versions are always case sensitive |
c801d85f | 2089 | // --------------------------------------------------------------------------- |
f33fee2a | 2090 | |
fb52c2b6 VZ |
2091 | #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0 |
2092 | ||
2093 | wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) | |
2094 | ||
2095 | #undef wxCMP_WXCHAR_STRING | |
2096 | ||
2097 | // note that there is an optimization in operator==() and !=(): we (quickly) | |
2098 | // checks the strings length first, before comparing their data | |
0cbe5ee3 VZ |
2099 | inline bool operator==(const wxString& s1, const wxString& s2) |
2100 | { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } | |
0cbe5ee3 VZ |
2101 | inline bool operator!=(const wxString& s1, const wxString& s2) |
2102 | { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } | |
0cbe5ee3 VZ |
2103 | inline bool operator< (const wxString& s1, const wxString& s2) |
2104 | { return s1.Cmp(s2) < 0; } | |
0cbe5ee3 VZ |
2105 | inline bool operator> (const wxString& s1, const wxString& s2) |
2106 | { return s1.Cmp(s2) > 0; } | |
0cbe5ee3 VZ |
2107 | inline bool operator<=(const wxString& s1, const wxString& s2) |
2108 | { return s1.Cmp(s2) <= 0; } | |
0cbe5ee3 VZ |
2109 | inline bool operator>=(const wxString& s1, const wxString& s2) |
2110 | { return s1.Cmp(s2) >= 0; } | |
3c67202d | 2111 | |
5ca07d0f OK |
2112 | #if wxUSE_UNICODE |
2113 | inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) | |
f33fee2a | 2114 | { return (s1.Cmp((const wchar_t *)s2) == 0); } |
5ca07d0f | 2115 | inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) |
f33fee2a | 2116 | { return (s2.Cmp((const wchar_t *)s1) == 0); } |
f6bcfd97 BP |
2117 | inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) |
2118 | { return (s1.Cmp((const wchar_t *)s2) != 0); } | |
2119 | inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) | |
2120 | { return (s2.Cmp((const wchar_t *)s1) != 0); } | |
0cbe5ee3 | 2121 | #else // !wxUSE_UNICODE |
5ca07d0f | 2122 | inline bool operator==(const wxString& s1, const wxCharBuffer& s2) |
f33fee2a | 2123 | { return (s1.Cmp((const char *)s2) == 0); } |
5ca07d0f | 2124 | inline bool operator==(const wxCharBuffer& s1, const wxString& s2) |
f33fee2a | 2125 | { return (s2.Cmp((const char *)s1) == 0); } |
f6bcfd97 BP |
2126 | inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) |
2127 | { return (s1.Cmp((const char *)s2) != 0); } | |
2128 | inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) | |
2129 | { return (s2.Cmp((const char *)s1) != 0); } | |
0cbe5ee3 | 2130 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
5ca07d0f | 2131 | |
028a2b5d | 2132 | #if wxUSE_UNICODE |
6d56eb5c | 2133 | inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) |
e90c1d2a | 2134 | { return string + (const wchar_t *)buf; } |
6d56eb5c | 2135 | inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) |
e90c1d2a | 2136 | { return (const wchar_t *)buf + string; } |
0cbe5ee3 | 2137 | #else // !wxUSE_UNICODE |
6d56eb5c | 2138 | inline wxString operator+(const wxString& string, const wxCharBuffer& buf) |
e90c1d2a | 2139 | { return string + (const char *)buf; } |
6d56eb5c | 2140 | inline wxString operator+(const wxCharBuffer& buf, const wxString& string) |
e90c1d2a | 2141 | { return (const char *)buf + string; } |
0cbe5ee3 | 2142 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
f04f3991 | 2143 | |
a962cdf4 | 2144 | // comparison with char |
c9f78968 VS |
2145 | inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } |
2146 | inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } | |
2147 | inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } | |
2148 | inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } | |
2149 | inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } | |
2150 | inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } | |
2151 | inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } | |
2152 | inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } | |
2153 | inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } | |
2154 | inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } | |
2155 | inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } | |
2156 | inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } | |
2157 | inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } | |
2158 | inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } | |
2159 | inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } | |
2160 | inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } | |
2161 | inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } | |
2162 | inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } | |
e51b17a1 | 2163 | |
d7330233 VS |
2164 | // comparison with C string in Unicode build |
2165 | #if wxUSE_UNICODE | |
fb52c2b6 VZ |
2166 | |
2167 | #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s | |
2168 | ||
2169 | wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) | |
2170 | ||
2171 | #undef wxCMP_CHAR_STRING | |
2172 | ||
d7330233 VS |
2173 | #endif // wxUSE_UNICODE |
2174 | ||
fb52c2b6 VZ |
2175 | // we also need to provide the operators for comparison with wxCStrData to |
2176 | // resolve ambiguity between operator(const wxChar *,const wxString &) and | |
2177 | // operator(const wxChar *, const wxChar *) for "p == s.c_str()" | |
2178 | // | |
2179 | // notice that these are (shallow) pointer comparisons, not (deep) string ones | |
2180 | #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() | |
2181 | #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() | |
2182 | ||
2183 | // FIXME: these ifdefs must be removed when wxCStrData has both conversions | |
2184 | #if wxUSE_UNICODE | |
2185 | wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) | |
2186 | #else | |
2187 | wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) | |
2188 | #endif | |
2189 | ||
2190 | #undef wxCMP_CHAR_CSTRDATA | |
2191 | #undef wxCMP_WCHAR_CSTRDATA | |
2192 | ||
c801d85f | 2193 | // --------------------------------------------------------------------------- |
3c67202d | 2194 | // Implementation only from here until the end of file |
c801d85f KB |
2195 | // --------------------------------------------------------------------------- |
2196 | ||
e87b7833 | 2197 | #if wxUSE_STD_IOSTREAM |
c801d85f | 2198 | |
65f19af1 | 2199 | #include "wx/iosfwrap.h" |
c801d85f | 2200 | |
bddd7a8d | 2201 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); |
c9f78968 | 2202 | WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); |
c801d85f | 2203 | |
3c67202d | 2204 | #endif // wxSTD_STRING_COMPATIBILITY |
c801d85f | 2205 | |
c9f78968 VS |
2206 | // --------------------------------------------------------------------------- |
2207 | // wxCStrData implementation | |
2208 | // --------------------------------------------------------------------------- | |
2209 | ||
2210 | inline wxCStrData::wxCStrData(char *buf) | |
2211 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2212 | inline wxCStrData::wxCStrData(wchar_t *buf) | |
2213 | : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} | |
2214 | ||
2215 | inline wxCStrData::~wxCStrData() | |
2216 | { | |
2217 | if ( m_owned ) | |
2218 | delete m_str; | |
2219 | } | |
2220 | ||
2221 | #if wxUSE_UNICODE | |
2222 | inline const wchar_t* wxCStrData::AsWChar() const | |
2223 | #else | |
2224 | inline const char* wxCStrData::AsChar() const | |
2225 | #endif | |
2226 | { | |
a962cdf4 VS |
2227 | // FIXME-UTF8: incorrect position, incorrect charset |
2228 | return m_str->wx_str() + m_offset; | |
c9f78968 VS |
2229 | } |
2230 | ||
2231 | inline wxString wxCStrData::AsString() const | |
2232 | { | |
2233 | if ( m_offset == 0 ) | |
2234 | return *m_str; | |
2235 | else | |
2236 | return m_str->Mid(m_offset); | |
2237 | } | |
2238 | ||
c9f78968 VS |
2239 | inline wxUniChar wxCStrData::operator*() const |
2240 | { | |
2241 | if ( m_str->empty() ) | |
2242 | return wxUniChar(_T('\0')); | |
2243 | else | |
2244 | return (*m_str)[m_offset]; | |
2245 | } | |
2246 | ||
2247 | inline wxUniChar wxCStrData::operator[](size_t n) const | |
2248 | { | |
2249 | return m_str->at(m_offset + n); | |
2250 | } | |
2251 | ||
414b7b40 VZ |
2252 | // ---------------------------------------------------------------------------- |
2253 | // implementation of wx[W]CharBuffer inline methods using wxCStrData | |
2254 | // ---------------------------------------------------------------------------- | |
2255 | ||
d18c8d3d | 2256 | // FIXME-UTF8: move this to buffer.h; provide versions for both variants |
ccd4deab VZ |
2257 | inline wxWxCharBuffer::wxWxCharBuffer(const wxCStrData& cstr) |
2258 | : wxCharTypeBufferBase((const wxChar *)cstr) | |
414b7b40 VZ |
2259 | { |
2260 | } | |
2261 | ||
a7ea63e2 | 2262 | #endif // _WX_WXSTRING_H__ |