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