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