]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
removed extra semicolons (patch #1700459; fixes compilation with gcc's -pedantic...
[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 WXDLLIMPEXP_BASE 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 inline ~wxCStrData();
186
187 // methods defined inline below must be declared inline or mingw32 3.4.5
188 // warns about "<symbol> defined locally after being referenced with
189 // dllimport linkage"
190 #if wxUSE_UNICODE_WCHAR
191 inline
192 #endif
193 const wchar_t* AsWChar() const;
194 operator const wchar_t*() const { return AsWChar(); }
195
196 inline operator bool() const;
197
198 #if !wxUSE_UNICODE
199 inline
200 #endif
201 const char* AsChar() const;
202 const unsigned char* AsUnsignedChar() const
203 { return (const unsigned char *) AsChar(); }
204 operator const char*() const { return AsChar(); }
205 operator const unsigned char*() const { return AsUnsignedChar(); }
206
207 operator const void*() const { return AsChar(); }
208
209 inline wxString AsString() const;
210
211 // allow expressions like "c_str()[0]":
212 inline wxUniChar operator[](size_t n) const;
213 wxUniChar operator[](int n) const { return operator[](size_t(n)); }
214 wxUniChar operator[](long n) const { return operator[](size_t(n)); }
215 #ifndef wxSIZE_T_IS_UINT
216 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
217 #endif // size_t != unsigned int
218
219 // these operators are needed to emulate the pointer semantics of c_str():
220 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
221 // (we need both versions to resolve ambiguities):
222 wxCStrData operator+(int n) const
223 { return wxCStrData(m_str, m_offset + n, m_owned); }
224 wxCStrData operator+(long n) const
225 { return wxCStrData(m_str, m_offset + n, m_owned); }
226 wxCStrData operator+(size_t n) const
227 { return wxCStrData(m_str, m_offset + n, m_owned); }
228
229 // and these for "str.c_str() + n - 2":
230 wxCStrData operator-(int 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-(long n) const
237 {
238 wxASSERT_MSG( n <= (int)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 wxCStrData operator-(size_t n) const
243 {
244 wxASSERT_MSG( n <= m_offset,
245 _T("attempt to construct address before the beginning of the string") );
246 return wxCStrData(m_str, m_offset - n, m_owned);
247 }
248
249 // this operator is needed to make expressions like "*c_str()" or
250 // "*(c_str() + 2)" work
251 inline wxUniChar operator*() const;
252
253 private:
254 const wxString *m_str;
255 size_t m_offset;
256 bool m_owned;
257
258 friend class WXDLLIMPEXP_BASE wxString;
259 };
260
261 // ----------------------------------------------------------------------------
262 // wxStringPrintfMixin
263 // ---------------------------------------------------------------------------
264
265 // NB: VC6 has a bug that causes linker errors if you have template methods
266 // in a class using __declspec(dllimport). The solution is to split such
267 // class into two classes, one that contains the template methods and does
268 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
269 // (with DLL linkage).
270 //
271 // We only do this for VC6 here, because the code is less efficient
272 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
273 // cannot compile this code.
274
275 #if defined(__VISUALC__) && __VISUALC__ < 1300
276 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
277 #endif
278
279 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
280 // this class contains implementation of wxString's vararg methods, it's
281 // exported from wxBase DLL
282 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
283 {
284 protected:
285 wxStringPrintfMixinBase() {}
286
287 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
288 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
289 };
290
291 // this class contains template wrappers for wxString's vararg methods, it's
292 // intentionally *not* exported from the DLL in order to fix the VC6 bug
293 // described above
294 class wxStringPrintfMixin : public wxStringPrintfMixinBase
295 {
296 private:
297 // to further complicate things, we can't return wxString from
298 // wxStringPrintfMixin::Format() because wxString is not yet declared at
299 // this point; the solution is to use this fake type trait template - this
300 // way the compiler won't know the return type until Format() is used
301 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
302 template<typename T> struct StringReturnType
303 {
304 typedef wxString type;
305 };
306
307 public:
308 // these are duplicated wxString methods, they're also declared below
309 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
310
311 // int Printf(const wxChar *pszFormat, ...);
312 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
313 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
314 WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type,
315 Format, DoFormat)
316 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
317 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
318
319 protected:
320 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
321 };
322 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
323
324
325 // ----------------------------------------------------------------------------
326 // wxString: string class trying to be compatible with std::string, MFC
327 // CString and wxWindows 1.x wxString all at once
328 // ---------------------------------------------------------------------------
329
330 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
331 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
332 // for dll-interface class 'wxString'" -- this is OK in our case
333 #pragma warning (disable:4275)
334 #endif
335
336 class WXDLLIMPEXP_BASE wxString
337 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
338 : public wxStringPrintfMixin
339 #endif
340 {
341 // NB: special care was taken in arranging the member functions in such order
342 // that all inline functions can be effectively inlined, verify that all
343 // performance critical functions are still inlined if you change order!
344 public:
345 // an 'invalid' value for string index, moved to this place due to a CW bug
346 static const size_t npos;
347
348 private:
349 // if we hadn't made these operators private, it would be possible to
350 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
351 // converted to char in C and we do have operator=(char)
352 //
353 // NB: we don't need other versions (short/long and unsigned) as attempt
354 // to assign another numeric type to wxString will now result in
355 // ambiguity between operator=(char) and operator=(int)
356 wxString& operator=(int);
357
358 // these methods are not implemented - there is _no_ conversion from int to
359 // string, you're doing something wrong if the compiler wants to call it!
360 //
361 // try `s << i' or `s.Printf("%d", i)' instead
362 wxString(int);
363
364
365 // buffer for holding temporary substring when using any of the methods
366 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
367 // FIXME-UTF8: This will need changes when UTF8 build is introduced
368 template<typename T>
369 struct SubstrBufFromType
370 {
371 T data;
372 size_t len;
373
374 SubstrBufFromType(const T& data_, size_t len_)
375 : data(data_), len(len_) {}
376 };
377
378 #if wxUSE_UNICODE_UTF8
379 // even char* -> char* needs conversion, from locale charset to UTF-8
380 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
381 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB;
382 #elif wxUSE_UNICODE_WCHAR
383 typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
384 typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB;
385 #else
386 typedef SubstrBufFromType<const char*> SubstrBufFromMB;
387 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
388 #endif
389
390
391 // Functions implementing primitive operations on string data; wxString
392 // methods and iterators are implemented in terms of it. The differences
393 // between UTF-8 and wchar_t* representations of the string are mostly
394 // contained here.
395
396 #if wxUSE_UNICODE_UTF8
397 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
398 const wxMBConv& conv);
399 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
400 const wxMBConv& conv);
401 #elif wxUSE_UNICODE_WCHAR
402 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
403 const wxMBConv& conv);
404 #else
405 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
406 const wxMBConv& conv);
407 #endif
408
409 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
410 // returns C string encoded as the implementation expects:
411 #if wxUSE_UNICODE
412 static const wchar_t* ImplStr(const wchar_t* str)
413 { return str ? str : wxT(""); }
414 static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
415 { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); }
416 static wxWCharBuffer ImplStr(const char* str,
417 const wxMBConv& conv = wxConvLibc)
418 { return ConvertStr(str, npos, conv).data; }
419 static SubstrBufFromMB ImplStr(const char* str, size_t n,
420 const wxMBConv& conv = wxConvLibc)
421 { return ConvertStr(str, n, conv); }
422 #else
423 static const char* ImplStr(const char* str,
424 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
425 { return str ? str : ""; }
426 static const SubstrBufFromMB ImplStr(const char* str, size_t n,
427 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
428 { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); }
429 static wxCharBuffer ImplStr(const wchar_t* str)
430 { return ConvertStr(str, npos, wxConvLibc).data; }
431 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
432 { return ConvertStr(str, n, wxConvLibc); }
433 #endif
434
435 // moves the iterator to the next Unicode character
436 static void IncIter(wxStringImpl::iterator& i) { ++i; }
437 static void IncIter(wxStringImpl::const_iterator& i) { ++i; }
438 // moves the iterator to the previous Unicode character
439 static void DecIter(wxStringImpl::iterator& i) { --i; }
440 static void DecIter(wxStringImpl::const_iterator& i) { --i; }
441 // moves the iterator by n Unicode characters
442 static wxStringImpl::iterator AddToIter(wxStringImpl::iterator i, int n)
443 { return i + n; }
444 static wxStringImpl::const_iterator AddToIter(wxStringImpl::const_iterator i, int n)
445 { return i + n; }
446 // returns distance of the two iterators in Unicode characters
447 static int DiffIters(wxStringImpl::iterator i1, wxStringImpl::iterator i2)
448 { return i1 - i2; }
449 static int DiffIters(wxStringImpl::const_iterator i1, wxStringImpl::const_iterator i2)
450 { return i1 - i2; }
451
452 // encodes the character to a form used to represent it in internal
453 // representation (returns a string in UTF8 version)
454 static wxChar EncodeChar(wxUniChar ch) { return (wxChar)ch; }
455 static wxUniChar DecodeChar(wxStringImpl::const_iterator i) { return *i; }
456
457 // translates position index in wxString to/from index in underlying
458 // wxStringImpl:
459 static size_t PosToImpl(size_t pos) { return pos; }
460 static void PosLenToImpl(size_t pos, size_t len,
461 size_t *implPos, size_t *implLen)
462 { *implPos = pos; *implLen = len; }
463 static size_t LenToImpl(size_t len) { return len; }
464 static size_t PosFromImpl(size_t pos) { return pos; }
465
466 #else // wxUSE_UNICODE_UTF8
467
468 // checks correctness of UTF-8 sequence
469 static bool IsValidUtf8String(const char *c);
470 #ifdef __WXDEBUG__
471 static bool IsValidUtf8LeadByte(unsigned char c);
472 #endif
473
474 // table of offsets to skip forward when iterating
475 static unsigned char ms_utf8IterTable[256];
476
477 static void IncIter(wxStringImpl::iterator& i)
478 {
479 wxASSERT( IsValidUtf8LeadByte(*i) );
480 i += ms_utf8IterTable[(unsigned char)*i];
481 }
482 static void IncIter(wxStringImpl::const_iterator& i)
483 {
484 wxASSERT( IsValidUtf8LeadByte(*i) );
485 i += ms_utf8IterTable[(unsigned char)*i];
486 }
487
488 static void DecIter(wxStringImpl::iterator& i);
489 static void DecIter(wxStringImpl::const_iterator& i);
490 static wxStringImpl::iterator AddToIter(wxStringImpl::iterator i, int n);
491 static wxStringImpl::const_iterator AddToIter(wxStringImpl::const_iterator i, int n);
492 static int DiffIters(wxStringImpl::const_iterator i1, wxStringImpl::const_iterator i2);
493 static int DiffIters(wxStringImpl::iterator i1, wxStringImpl::iterator i2);
494
495 struct Utf8CharBuffer
496 {
497 char data[5];
498 operator const char*() const { return data; }
499 };
500 static Utf8CharBuffer EncodeChar(wxUniChar ch);
501 // returns n copies of ch encoded in UTF-8 string
502 static wxCharBuffer EncodeNChars(size_t n, wxUniChar ch);
503
504 // returns the length of UTF-8 encoding of the character with lead byte 'c'
505 static size_t GetUtf8CharLength(char c)
506 {
507 wxASSERT( IsValidUtf8LeadByte(c) );
508 return ms_utf8IterTable[(unsigned char)c];
509 }
510
511 // decodes single UTF-8 character from UTF-8 string
512 // FIXME-UTF8: move EncodeChar/DecodeChar and other operations to separate
513 // class
514 static wxUniChar DecodeChar(wxStringImpl::const_iterator i)
515 { return wxUniCharRef::DecodeChar(i); }
516 friend class WXDLLIMPEXP_BASE wxUniCharRef;
517
518 size_t PosToImpl(size_t pos) const
519 {
520 if ( pos == 0 || pos == npos )
521 return pos;
522 else
523 return wxStringImpl::const_iterator(begin() + pos) - m_impl.begin();
524 }
525
526 void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const;
527
528 size_t LenToImpl(size_t len) const
529 {
530 size_t pos, len2;
531 PosLenToImpl(0, len, &pos, &len2);
532 return len2;
533 }
534
535 size_t PosFromImpl(size_t pos) const
536 {
537 if ( pos == 0 || pos == npos )
538 return pos;
539 else
540 return const_iterator(m_impl.begin() + pos) - begin();
541 }
542
543 size_t IterToImplPos(wxStringImpl::iterator i) const
544 { return wxStringImpl::const_iterator(i) - m_impl.begin(); }
545
546 // FIXME-UTF8: return as-is without copying under UTF8 locale, return
547 // converted string under other locales - needs wxCharBuffer
548 // changes
549 static wxCharBuffer ImplStr(const char* str,
550 const wxMBConv& conv = wxConvLibc)
551 { return ConvertStr(str, npos, conv).data; }
552 static SubstrBufFromMB ImplStr(const char* str, size_t n,
553 const wxMBConv& conv = wxConvLibc)
554 { return ConvertStr(str, n, conv); }
555
556 static wxCharBuffer ImplStr(const wchar_t* str)
557 { return ConvertStr(str, npos, wxConvUTF8).data; }
558 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
559 { return ConvertStr(str, n, wxConvUTF8); }
560 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
561
562
563 public:
564 // constructors and destructor
565 // ctor for an empty string
566 wxString() {}
567
568 // copy ctor
569 // FIXME-UTF8: this one needs to do UTF-8 conversion in UTF-8 build!
570 wxString(const wxStringImpl& stringSrc) : m_impl(stringSrc) { }
571
572 wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
573
574 // string containing nRepeat copies of ch
575 wxString(wxUniChar ch, size_t nRepeat = 1)
576 { assign(nRepeat, ch); }
577 wxString(size_t nRepeat, wxUniChar ch)
578 { assign(nRepeat, ch); }
579 wxString(wxUniCharRef ch, size_t nRepeat = 1)
580 { assign(nRepeat, ch); }
581 wxString(size_t nRepeat, wxUniCharRef ch)
582 { assign(nRepeat, ch); }
583 wxString(char ch, size_t nRepeat = 1)
584 { assign(nRepeat, ch); }
585 wxString(size_t nRepeat, char ch)
586 { assign(nRepeat, ch); }
587 wxString(wchar_t ch, size_t nRepeat = 1)
588 { assign(nRepeat, ch); }
589 wxString(size_t nRepeat, wchar_t ch)
590 { assign(nRepeat, ch); }
591
592 // ctors from char* strings:
593 wxString(const char *psz)
594 : m_impl(ImplStr(psz)) {}
595 wxString(const char *psz, const wxMBConv& conv)
596 : m_impl(ImplStr(psz, conv)) {}
597 wxString(const char *psz, size_t nLength)
598 { assign(psz, nLength); }
599 wxString(const char *psz, const wxMBConv& conv, size_t nLength)
600 {
601 SubstrBufFromMB str(ImplStr(psz, nLength, conv));
602 m_impl.assign(str.data, str.len);
603 }
604
605 // and unsigned char*:
606 wxString(const unsigned char *psz)
607 : m_impl(ImplStr((const char*)psz)) {}
608 wxString(const unsigned char *psz, const wxMBConv& conv)
609 : m_impl(ImplStr((const char*)psz, conv)) {}
610 wxString(const unsigned char *psz, size_t nLength)
611 { assign((const char*)psz, nLength); }
612 wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength)
613 {
614 SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv));
615 m_impl.assign(str.data, str.len);
616 }
617
618 // ctors from wchar_t* strings:
619 wxString(const wchar_t *pwz)
620 : m_impl(ImplStr(pwz)) {}
621 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv))
622 : m_impl(ImplStr(pwz)) {}
623 wxString(const wchar_t *pwz, size_t nLength)
624 { assign(pwz, nLength); }
625 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength)
626 { assign(pwz, nLength); }
627
628 wxString(const wxCharBuffer& buf)
629 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
630 wxString(const wxWCharBuffer& buf)
631 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
632
633 wxString(const wxCStrData& cstr)
634 : m_impl(cstr.AsString().m_impl) { }
635
636 // as we provide both ctors with this signature for both char and unsigned
637 // char string, we need to provide one for wxCStrData to resolve ambiguity
638 wxString(const wxCStrData& cstr, size_t nLength)
639 : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {}
640
641 // and because wxString is convertible to wxCStrData and const wxChar *
642 // we also need to provide this one
643 wxString(const wxString& str, size_t nLength)
644 : m_impl(str.Mid(0, nLength).m_impl) {}
645
646 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
647 // implicit conversions from std::string to wxString as this allows to use
648 // the same strings in non-GUI and GUI code, however we don't want to
649 // unconditionally add this ctor as it would make wx lib dependent on
650 // libstdc++ on some Linux versions which is bad, so instead we ask the
651 // client code to define this wxUSE_STD_STRING symbol if they need it
652 #if wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
653 wxString(const wxStdString& s)
654 // FIXME-UTF8: this one needs to do UTF-8 conversion in UTF-8 build!
655 : m_impl(s.c_str()) { } // FIXME-UTF8: this is broken for embedded 0s
656 #endif // wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
657
658 public:
659 // standard types
660 typedef wxUniChar value_type;
661 typedef wxUniChar char_type;
662 typedef wxUniCharRef reference;
663 typedef wxChar* pointer;
664 typedef const wxChar* const_pointer;
665
666 typedef size_t size_type;
667 typedef wxUniChar const_reference;
668
669 #if wxUSE_STL
670 #if wxUSE_UNICODE_UTF8
671 // random access is not O(1), as required by Random Access Iterator
672 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
673 #else
674 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
675 #endif
676 #else
677 #define WX_STR_ITERATOR_TAG void /* dummy type */
678 #endif
679
680 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
681 reference_type, reference_ctor) \
682 private: \
683 typedef wxStringImpl::iterator_name underlying_iterator; \
684 public: \
685 typedef WX_STR_ITERATOR_TAG iterator_category; \
686 typedef wxUniChar value_type; \
687 typedef int difference_type; \
688 typedef reference_type reference; \
689 typedef pointer_type pointer; \
690 \
691 reference operator*() const { return reference_ctor; } \
692 reference operator[](size_t n) const { return *(*this + n); } \
693 \
694 iterator_name& operator++() \
695 { wxString::IncIter(m_cur); return *this; } \
696 iterator_name& operator--() \
697 { wxString::DecIter(m_cur); return *this; } \
698 iterator_name operator++(int) \
699 { \
700 iterator_name tmp = *this; \
701 wxString::IncIter(m_cur); \
702 return tmp; \
703 } \
704 iterator_name operator--(int) \
705 { \
706 iterator_name tmp = *this; \
707 wxString::DecIter(m_cur); \
708 return tmp; \
709 } \
710 \
711 iterator_name& operator+=(int n) \
712 { m_cur = wxString::AddToIter(m_cur, n); return *this; } \
713 iterator_name& operator+=(size_t n) \
714 { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \
715 iterator_name& operator-=(int n) \
716 { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \
717 iterator_name& operator-=(size_t n) \
718 { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \
719 \
720 difference_type operator-(const iterator_name& i) const \
721 { return wxString::DiffIters(m_cur, i.m_cur); } \
722 \
723 bool operator==(const iterator_name& i) const \
724 { return m_cur == i.m_cur; } \
725 bool operator!=(const iterator_name& i) const \
726 { return m_cur != i.m_cur; } \
727 \
728 bool operator<(const iterator_name& i) const \
729 { return m_cur < i.m_cur; } \
730 bool operator>(const iterator_name& i) const \
731 { return m_cur > i.m_cur; } \
732 bool operator<=(const iterator_name& i) const \
733 { return m_cur <= i.m_cur; } \
734 bool operator>=(const iterator_name& i) const \
735 { return m_cur >= i.m_cur; } \
736 \
737 private: \
738 /* for internal wxString use only: */ \
739 operator underlying_iterator() const { return m_cur; } \
740 \
741 friend class WXDLLIMPEXP_BASE wxString; \
742 friend class WXDLLIMPEXP_BASE wxCStrData; \
743 \
744 private: \
745 underlying_iterator m_cur
746
747 class const_iterator;
748
749 #if wxUSE_UNICODE_UTF8
750 class iterator
751 {
752 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
753 // to the underlying wxStringImpl, because UTF-8 is variable-length
754 // encoding and changing the value pointer to by an iterator using
755 // its operator* requires calling wxStringImpl::replace() if the old
756 // and new values differ in their encoding's length.
757
758 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef,
759 wxUniCharRef::CreateForString(m_str, m_cur));
760
761 public:
762 iterator(const iterator& i) : m_cur(i.m_cur), m_str(i.m_str) {}
763
764 iterator operator+(int n) const
765 { return iterator(m_str, wxString::AddToIter(m_cur, n)); }
766 iterator operator+(size_t n) const
767 { return iterator(m_str, wxString::AddToIter(m_cur, (int)n)); }
768 iterator operator-(int n) const
769 { return iterator(m_str, wxString::AddToIter(m_cur, -n)); }
770 iterator operator-(size_t n) const
771 { return iterator(m_str, wxString::AddToIter(m_cur, -(int)n)); }
772
773 private:
774 iterator(wxString *str, underlying_iterator ptr)
775 : m_cur(ptr), m_str(str->m_impl) {}
776 iterator(wxStringImpl& str, underlying_iterator ptr)
777 : m_cur(ptr), m_str(str) {}
778
779 wxStringImpl& m_str;
780
781 friend class const_iterator;
782 };
783 #else // !wxUSE_UNICODE_UTF8
784 class iterator
785 {
786 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef,
787 wxUniCharRef::CreateForString(m_cur));
788
789 public:
790 iterator(const iterator& i) : m_cur(i.m_cur) {}
791
792 iterator operator+(int n) const
793 { return iterator(wxString::AddToIter(m_cur, n)); }
794 iterator operator+(size_t n) const
795 { return iterator(wxString::AddToIter(m_cur, (int)n)); }
796 iterator operator-(int n) const
797 { return iterator(wxString::AddToIter(m_cur, -n)); }
798 iterator operator-(size_t n) const
799 { return iterator(wxString::AddToIter(m_cur, -(int)n)); }
800
801 private:
802 // for internal wxString use only:
803 iterator(underlying_iterator ptr) : m_cur(ptr) {}
804 iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {}
805
806 friend class const_iterator;
807 };
808 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
809
810 class const_iterator
811 {
812 // NB: reference_type is intentionally value, not reference, the character
813 // may be encoded differently in wxString data:
814 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar,
815 wxString::DecodeChar(m_cur));
816
817 public:
818 const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
819 const_iterator(const iterator& i) : m_cur(i.m_cur) {}
820
821 const_iterator operator+(int n) const
822 { return const_iterator(wxString::AddToIter(m_cur, n)); }
823 const_iterator operator+(size_t n) const
824 { return const_iterator(wxString::AddToIter(m_cur, (int)n)); }
825 const_iterator operator-(int n) const
826 { return const_iterator(wxString::AddToIter(m_cur, -n)); }
827 const_iterator operator-(size_t n) const
828 { return const_iterator(wxString::AddToIter(m_cur, -(int)n)); }
829
830 private:
831 // for internal wxString use only:
832 const_iterator(underlying_iterator ptr) : m_cur(ptr) {}
833 };
834
835 #undef WX_STR_ITERATOR_TAG
836 #undef WX_STR_ITERATOR_IMPL
837
838 friend class iterator;
839 friend class const_iterator;
840
841 template <typename T>
842 class reverse_iterator_impl
843 {
844 public:
845 typedef T iterator_type;
846
847 typedef typename T::iterator_category iterator_category;
848 typedef typename T::value_type value_type;
849 typedef typename T::difference_type difference_type;
850 typedef typename T::reference reference;
851 typedef typename T::pointer *pointer;
852
853 reverse_iterator_impl(iterator_type i) : m_cur(i) {}
854 reverse_iterator_impl(const reverse_iterator_impl& ri)
855 : m_cur(ri.m_cur) {}
856
857 iterator_type base() const { return m_cur; }
858
859 reference operator*() const { return *(m_cur-1); }
860 reference operator[](size_t n) const { return *(*this + n); }
861
862 reverse_iterator_impl& operator++()
863 { --m_cur; return *this; }
864 reverse_iterator_impl operator++(int)
865 { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
866 reverse_iterator_impl& operator--()
867 { ++m_cur; return *this; }
868 reverse_iterator_impl operator--(int)
869 { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
870
871 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
872 reverse_iterator_impl operator+(int n) const
873 { return reverse_iterator_impl<T>(m_cur - n); }
874 reverse_iterator_impl operator+(size_t n) const
875 { return reverse_iterator_impl<T>(m_cur - n); }
876 reverse_iterator_impl operator-(int n) const
877 { return reverse_iterator_impl<T>(m_cur + n); }
878 reverse_iterator_impl operator-(size_t n) const
879 { return reverse_iterator_impl<T>(m_cur + n); }
880 reverse_iterator_impl operator+=(int n)
881 { m_cur -= n; return *this; }
882 reverse_iterator_impl operator+=(size_t n)
883 { m_cur -= n; return *this; }
884 reverse_iterator_impl operator-=(int n)
885 { m_cur += n; return *this; }
886 reverse_iterator_impl operator-=(size_t n)
887 { m_cur += n; return *this; }
888
889 unsigned operator-(const reverse_iterator_impl& i) const
890 { return i.m_cur - m_cur; }
891
892 bool operator==(const reverse_iterator_impl& ri) const
893 { return m_cur == ri.m_cur; }
894 bool operator!=(const reverse_iterator_impl& ri) const
895 { return !(*this == ri); }
896
897 bool operator<(const reverse_iterator_impl& i) const
898 { return m_cur > i.m_cur; }
899 bool operator>(const reverse_iterator_impl& i) const
900 { return m_cur < i.m_cur; }
901 bool operator<=(const reverse_iterator_impl& i) const
902 { return m_cur >= i.m_cur; }
903 bool operator>=(const reverse_iterator_impl& i) const
904 { return m_cur <= i.m_cur; }
905
906 private:
907 iterator_type m_cur;
908 };
909
910 typedef reverse_iterator_impl<iterator> reverse_iterator;
911 typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
912
913 // first valid index position
914 const_iterator begin() const { return const_iterator(m_impl.begin()); }
915 iterator begin() { return iterator(this, m_impl.begin()); }
916 // position one after the last valid one
917 const_iterator end() const { return const_iterator(m_impl.end()); }
918 iterator end() { return iterator(this, m_impl.end()); }
919
920 // first element of the reversed string
921 const_reverse_iterator rbegin() const
922 { return const_reverse_iterator(end()); }
923 reverse_iterator rbegin()
924 { return reverse_iterator(end()); }
925 // one beyond the end of the reversed string
926 const_reverse_iterator rend() const
927 { return const_reverse_iterator(begin()); }
928 reverse_iterator rend()
929 { return reverse_iterator(begin()); }
930
931 // std::string methods:
932 #if wxUSE_UNICODE_UTF8
933 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
934 #else
935 size_t length() const { return m_impl.length(); }
936 #endif
937
938 size_type size() const { return length(); }
939 size_type max_size() const { return npos; }
940
941 bool empty() const { return m_impl.empty(); }
942
943 size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8
944 void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8
945
946 void resize(size_t nSize, wxUniChar ch = wxT('\0'))
947 {
948 #if wxUSE_UNICODE_UTF8
949 if ( !ch.IsAscii() )
950 {
951 size_t len = length();
952 if ( nSize == len)
953 return;
954 else if ( nSize < len )
955 erase(nSize);
956 else
957 append(nSize - len, ch);
958 }
959 else
960 #endif
961 m_impl.resize(nSize, (wxStringCharType)ch);
962 }
963
964 wxString substr(size_t nStart = 0, size_t nLen = npos) const
965 {
966 size_t pos, len;
967 PosLenToImpl(nStart, nLen, &pos, &len);
968 return m_impl.substr(pos, len);
969 }
970
971 // generic attributes & operations
972 // as standard strlen()
973 size_t Len() const { return length(); }
974 // string contains any characters?
975 bool IsEmpty() const { return empty(); }
976 // empty string is "false", so !str will return true
977 bool operator!() const { return empty(); }
978 // truncate the string to given length
979 wxString& Truncate(size_t uiLen);
980 // empty string contents
981 void Empty()
982 {
983 Truncate(0);
984
985 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
986 }
987 // empty the string and free memory
988 void Clear()
989 {
990 wxString tmp(wxEmptyString);
991 swap(tmp);
992 }
993
994 // contents test
995 // Is an ascii value
996 bool IsAscii() const;
997 // Is a number
998 bool IsNumber() const;
999 // Is a word
1000 bool IsWord() const;
1001
1002 // data access (all indexes are 0 based)
1003 // read access
1004 wxUniChar at(size_t n) const
1005 { return *(begin() + n); } // FIXME-UTF8: optimize?
1006 wxUniChar GetChar(size_t n) const
1007 { return at(n); }
1008 // read/write access
1009 wxUniCharRef at(size_t n)
1010 { return *(begin() + n); } // FIXME-UTF8: optimize?
1011 wxUniCharRef GetWritableChar(size_t n)
1012 { return at(n); }
1013 // write access
1014 void SetChar(size_t n, wxUniChar ch)
1015 { at(n) = ch; }
1016
1017 // get last character
1018 wxUniChar Last() const
1019 {
1020 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1021
1022 return at(length() - 1);
1023 }
1024
1025 // get writable last character
1026 wxUniCharRef Last()
1027 {
1028 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1029 return at(length() - 1);
1030 }
1031
1032 /*
1033 Note that we we must define all of the overloads below to avoid
1034 ambiguity when using str[0].
1035 */
1036 wxUniChar operator[](int n) const
1037 { return at(n); }
1038 wxUniChar operator[](long n) const
1039 { return at(n); }
1040 wxUniChar operator[](size_t n) const
1041 { return at(n); }
1042 #ifndef wxSIZE_T_IS_UINT
1043 wxUniChar operator[](unsigned int n) const
1044 { return at(n); }
1045 #endif // size_t != unsigned int
1046
1047 // operator versions of GetWriteableChar()
1048 wxUniCharRef operator[](int n)
1049 { return at(n); }
1050 wxUniCharRef operator[](long n)
1051 { return at(n); }
1052 wxUniCharRef operator[](size_t n)
1053 { return at(n); }
1054 #ifndef wxSIZE_T_IS_UINT
1055 wxUniCharRef operator[](unsigned int n)
1056 { return at(n); }
1057 #endif // size_t != unsigned int
1058
1059 // explicit conversion to C string (use this with printf()!)
1060 wxCStrData c_str() const { return wxCStrData(this); }
1061 wxCStrData data() const { return c_str(); }
1062
1063 // implicit conversion to C string
1064 operator wxCStrData() const { return c_str(); }
1065 operator const char*() const { return c_str(); }
1066 operator const wchar_t*() const { return c_str(); }
1067
1068 // identical to c_str(), for MFC compatibility
1069 const wxCStrData GetData() const { return c_str(); }
1070
1071 // explicit conversion to C string in internal representation (char*,
1072 // wchar_t*, UTF-8-encoded char*, depending on the build):
1073 const wxStringCharType *wx_str() const { return m_impl.c_str(); }
1074
1075 // conversion to *non-const* multibyte or widestring buffer; modifying
1076 // returned buffer won't affect the string, these methods are only useful
1077 // for passing values to const-incorrect functions
1078 wxWritableCharBuffer char_str() const { return mb_str(); }
1079 wxWritableWCharBuffer wchar_str() const { return wc_str(); }
1080
1081 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1082 // converting numbers or strings which are certain not to contain special
1083 // chars (typically system functions, X atoms, environment variables etc.)
1084 //
1085 // the behaviour of these functions with the strings containing anything
1086 // else than 7 bit ASCII characters is undefined, use at your own risk.
1087 #if wxUSE_UNICODE
1088 static wxString FromAscii(const char *ascii); // string
1089 static wxString FromAscii(const char ascii); // char
1090 const wxCharBuffer ToAscii() const;
1091 #else // ANSI
1092 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1093 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
1094 const char *ToAscii() const { return c_str(); }
1095 #endif // Unicode/!Unicode
1096
1097 // conversions with (possible) format conversions: have to return a
1098 // buffer with temporary data
1099 //
1100 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1101 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1102 // fn_str() to return a string which should be used with the OS APIs
1103 // accepting the file names. The return value is always the same, but the
1104 // type differs because a function may either return pointer to the buffer
1105 // directly or have to use intermediate buffer for translation.
1106 #if wxUSE_UNICODE
1107 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1108
1109 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1110
1111 #if wxUSE_UNICODE_WCHAR
1112 const wxChar* wc_str() const { return wx_str(); }
1113 #elif wxUSE_UNICODE_UTF8
1114 const wxWCharBuffer wc_str() const;
1115 #endif
1116 // for compatibility with !wxUSE_UNICODE version
1117 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1118 { return wc_str(); }
1119
1120 #if wxMBFILES
1121 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1122 #else // !wxMBFILES
1123 const wxWX2WCbuf fn_str() const { return wc_str(); }
1124 #endif // wxMBFILES/!wxMBFILES
1125
1126 #else // ANSI
1127 const wxChar* mb_str() const { return wx_str(); }
1128
1129 // for compatibility with wxUSE_UNICODE version
1130 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
1131
1132 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1133
1134 #if wxUSE_WCHAR_T
1135 const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
1136 #endif // wxUSE_WCHAR_T
1137 #ifdef __WXOSX__
1138 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1139 #else
1140 const wxChar* fn_str() const { return c_str(); }
1141 #endif
1142 #endif // Unicode/ANSI
1143
1144 // overloaded assignment
1145 // from another wxString
1146 wxString& operator=(const wxString& stringSrc)
1147 { m_impl = stringSrc.m_impl; return *this; }
1148 wxString& operator=(const wxCStrData& cstr)
1149 { return *this = cstr.AsString(); }
1150 // from a character
1151 wxString& operator=(wxUniChar ch)
1152 { m_impl = EncodeChar(ch); return *this; }
1153 wxString& operator=(wxUniCharRef ch)
1154 { return operator=((wxUniChar)ch); }
1155 wxString& operator=(char ch)
1156 { return operator=(wxUniChar(ch)); }
1157 wxString& operator=(unsigned char ch)
1158 { return operator=(wxUniChar(ch)); }
1159 wxString& operator=(wchar_t ch)
1160 { return operator=(wxUniChar(ch)); }
1161 // from a C string - STL probably will crash on NULL,
1162 // so we need to compensate in that case
1163 #if wxUSE_STL_BASED_WXSTRING
1164 wxString& operator=(const char *psz)
1165 { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; }
1166 wxString& operator=(const wchar_t *pwz)
1167 { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; }
1168 #else
1169 wxString& operator=(const char *psz)
1170 { m_impl = ImplStr(psz); return *this; }
1171 wxString& operator=(const wchar_t *pwz)
1172 { m_impl = ImplStr(pwz); return *this; }
1173 #endif
1174 wxString& operator=(const unsigned char *psz)
1175 { return operator=((const char*)psz); }
1176
1177 // from wxWCharBuffer
1178 wxString& operator=(const wxWCharBuffer& s)
1179 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1180 // from wxCharBuffer
1181 wxString& operator=(const wxCharBuffer& s)
1182 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1183
1184 // string concatenation
1185 // in place concatenation
1186 /*
1187 Concatenate and return the result. Note that the left to right
1188 associativity of << allows to write things like "str << str1 << str2
1189 << ..." (unlike with +=)
1190 */
1191 // string += string
1192 wxString& operator<<(const wxString& s)
1193 {
1194 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1195 wxASSERT_MSG( s.IsValid(),
1196 _T("did you forget to call UngetWriteBuf()?") );
1197 #endif
1198
1199 append(s);
1200 return *this;
1201 }
1202 // string += C string
1203 wxString& operator<<(const char *psz)
1204 { append(psz); return *this; }
1205 wxString& operator<<(const wchar_t *pwz)
1206 { append(pwz); return *this; }
1207 wxString& operator<<(const wxCStrData& psz)
1208 { append(psz.AsString()); return *this; }
1209 // string += char
1210 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1211 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1212 wxString& operator<<(char ch) { append(1, ch); return *this; }
1213 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
1214 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1215
1216 // string += buffer (i.e. from wxGetString)
1217 wxString& operator<<(const wxWCharBuffer& s)
1218 { return operator<<((const wchar_t *)s); }
1219 wxString& operator+=(const wxWCharBuffer& s)
1220 { return operator<<((const wchar_t *)s); }
1221
1222 wxString& operator<<(const wxCharBuffer& s)
1223 { return operator<<((const char *)s); }
1224 wxString& operator+=(const wxCharBuffer& s)
1225 { return operator<<((const char *)s); }
1226
1227 // string += C string
1228 wxString& Append(const wxString& s)
1229 {
1230 // test for empty() to share the string if possible
1231 if ( empty() )
1232 *this = s;
1233 else
1234 append(s);
1235 return *this;
1236 }
1237 wxString& Append(const wxCStrData& psz)
1238 { append(psz); return *this; }
1239 wxString& Append(const char* psz)
1240 { append(psz); return *this; }
1241 wxString& Append(const wchar_t* pwz)
1242 { append(pwz); return *this; }
1243 // append count copies of given character
1244 wxString& Append(wxUniChar ch, size_t count = 1u)
1245 { append(count, ch); return *this; }
1246 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1247 { append(count, ch); return *this; }
1248 wxString& Append(char ch, size_t count = 1u)
1249 { append(count, ch); return *this; }
1250 wxString& Append(unsigned char ch, size_t count = 1u)
1251 { append(count, ch); return *this; }
1252 wxString& Append(wchar_t ch, size_t count = 1u)
1253 { append(count, ch); return *this; }
1254 wxString& Append(const char* psz, size_t nLen)
1255 { append(psz, nLen); return *this; }
1256 wxString& Append(const wchar_t* pwz, size_t nLen)
1257 { append(pwz, nLen); return *this; }
1258
1259 // prepend a string, return the string itself
1260 wxString& Prepend(const wxString& str)
1261 { *this = str + *this; return *this; }
1262
1263 // non-destructive concatenation
1264 // two strings
1265 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1266 const wxString& string2);
1267 // string with a single char
1268 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1269 // char with a string
1270 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1271 // string with C string
1272 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1273 const char *psz);
1274 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1275 const wchar_t *pwz);
1276 // C string with string
1277 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1278 const wxString& string);
1279 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1280 const wxString& string);
1281
1282 // stream-like functions
1283 // insert an int into string
1284 wxString& operator<<(int i)
1285 { return (*this) << Format(_T("%d"), i); }
1286 // insert an unsigned int into string
1287 wxString& operator<<(unsigned int ui)
1288 { return (*this) << Format(_T("%u"), ui); }
1289 // insert a long into string
1290 wxString& operator<<(long l)
1291 { return (*this) << Format(_T("%ld"), l); }
1292 // insert an unsigned long into string
1293 wxString& operator<<(unsigned long ul)
1294 { return (*this) << Format(_T("%lu"), ul); }
1295 #if defined wxLongLong_t && !defined wxLongLongIsLong
1296 // insert a long long if they exist and aren't longs
1297 wxString& operator<<(wxLongLong_t ll)
1298 {
1299 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1300 return (*this) << Format(fmt, ll);
1301 }
1302 // insert an unsigned long long
1303 wxString& operator<<(wxULongLong_t ull)
1304 {
1305 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1306 return (*this) << Format(fmt , ull);
1307 }
1308 #endif
1309 // insert a float into string
1310 wxString& operator<<(float f)
1311 { return (*this) << Format(_T("%f"), f); }
1312 // insert a double into string
1313 wxString& operator<<(double d)
1314 { return (*this) << Format(_T("%g"), d); }
1315
1316 // string comparison
1317 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1318 int Cmp(const char *psz) const
1319 { return compare(psz); }
1320 int Cmp(const wchar_t *pwz) const
1321 { return compare(pwz); }
1322 int Cmp(const wxString& s) const
1323 { return compare(s); }
1324 // same as Cmp() but not case-sensitive
1325 int CmpNoCase(const wxString& s) const;
1326 int CmpNoCase(const char *psz) const
1327 { return CmpNoCase(wxString(psz)); }
1328 int CmpNoCase(const wchar_t *pwz) const
1329 { return CmpNoCase(wxString(pwz)); }
1330 // test for the string equality, either considering case or not
1331 // (if compareWithCase then the case matters)
1332 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
1333 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1334 bool IsSameAs(const char *str, bool compareWithCase = true) const
1335 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1336 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
1337 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1338 // comparison with a single character: returns true if equal
1339 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const
1340 {
1341 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
1342 : wxToupper(GetChar(0u)) == wxToupper(c));
1343 }
1344 // FIXME-UTF8: remove these overloads
1345 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
1346 { return IsSameAs(wxUniChar(c), compareWithCase); }
1347 bool IsSameAs(char c, bool compareWithCase = true) const
1348 { return IsSameAs(wxUniChar(c), compareWithCase); }
1349 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
1350 { return IsSameAs(wxUniChar(c), compareWithCase); }
1351 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
1352 { return IsSameAs(wxUniChar(c), compareWithCase); }
1353 bool IsSameAs(int c, bool compareWithCase = true) const
1354 { return IsSameAs(wxUniChar(c), compareWithCase); }
1355
1356 // simple sub-string extraction
1357 // return substring starting at nFirst of length nCount (or till the end
1358 // if nCount = default value)
1359 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1360
1361 // operator version of Mid()
1362 wxString operator()(size_t start, size_t len) const
1363 { return Mid(start, len); }
1364
1365 // check if the string starts with the given prefix and return the rest
1366 // of the string in the provided pointer if it is not NULL; otherwise
1367 // return false
1368 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
1369 // check if the string ends with the given suffix and return the
1370 // beginning of the string before the suffix in the provided pointer if
1371 // it is not NULL; otherwise return false
1372 bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const;
1373
1374 // get first nCount characters
1375 wxString Left(size_t nCount) const;
1376 // get last nCount characters
1377 wxString Right(size_t nCount) const;
1378 // get all characters before the first occurance of ch
1379 // (returns the whole string if ch not found)
1380 wxString BeforeFirst(wxUniChar ch) const;
1381 // get all characters before the last occurence of ch
1382 // (returns empty string if ch not found)
1383 wxString BeforeLast(wxUniChar ch) const;
1384 // get all characters after the first occurence of ch
1385 // (returns empty string if ch not found)
1386 wxString AfterFirst(wxUniChar ch) const;
1387 // get all characters after the last occurence of ch
1388 // (returns the whole string if ch not found)
1389 wxString AfterLast(wxUniChar ch) const;
1390
1391 // for compatibility only, use more explicitly named functions above
1392 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1393 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1394
1395 // case conversion
1396 // convert to upper case in place, return the string itself
1397 wxString& MakeUpper();
1398 // convert to upper case, return the copy of the string
1399 // Here's something to remember: BC++ doesn't like returns in inlines.
1400 wxString Upper() const ;
1401 // convert to lower case in place, return the string itself
1402 wxString& MakeLower();
1403 // convert to lower case, return the copy of the string
1404 wxString Lower() const ;
1405
1406 // trimming/padding whitespace (either side) and truncating
1407 // remove spaces from left or from right (default) side
1408 wxString& Trim(bool bFromRight = true);
1409 // add nCount copies chPad in the beginning or at the end (default)
1410 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1411
1412 // searching and replacing
1413 // searching (return starting index, or -1 if not found)
1414 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1415 int Find(wxUniCharRef ch, bool bFromEnd = false) const
1416 { return Find(wxUniChar(ch), bFromEnd); }
1417 int Find(char ch, bool bFromEnd = false) const
1418 { return Find(wxUniChar(ch), bFromEnd); }
1419 int Find(unsigned char ch, bool bFromEnd = false) const
1420 { return Find(wxUniChar(ch), bFromEnd); }
1421 int Find(wchar_t ch, bool bFromEnd = false) const
1422 { return Find(wxUniChar(ch), bFromEnd); }
1423 // searching (return starting index, or -1 if not found)
1424 // FIXME-UTF8: keep wxString overload only
1425 int Find(const wxString& sub) const // like strstr
1426 {
1427 size_type idx = find(sub);
1428 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1429 }
1430 int Find(const char *sub) const // like strstr
1431 {
1432 size_type idx = find(sub);
1433 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1434 }
1435 int Find(const wchar_t *sub) const // like strstr
1436 {
1437 size_type idx = find(sub);
1438 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1439 }
1440
1441 // replace first (or all of bReplaceAll) occurences of substring with
1442 // another string, returns the number of replacements made
1443 size_t Replace(const wxString& strOld,
1444 const wxString& strNew,
1445 bool bReplaceAll = true);
1446
1447 // check if the string contents matches a mask containing '*' and '?'
1448 bool Matches(const wxString& mask) const;
1449
1450 // conversion to numbers: all functions return true only if the whole
1451 // string is a number and put the value of this number into the pointer
1452 // provided, the base is the numeric base in which the conversion should be
1453 // done and must be comprised between 2 and 36 or be 0 in which case the
1454 // standard C rules apply (leading '0' => octal, "0x" => hex)
1455 // convert to a signed integer
1456 bool ToLong(long *val, int base = 10) const;
1457 // convert to an unsigned integer
1458 bool ToULong(unsigned long *val, int base = 10) const;
1459 // convert to wxLongLong
1460 #if defined(wxLongLong_t)
1461 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1462 // convert to wxULongLong
1463 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1464 #endif // wxLongLong_t
1465 // convert to a double
1466 bool ToDouble(double *val) const;
1467
1468
1469 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1470 // formatted input/output
1471 // as sprintf(), returns the number of characters written or < 0 on error
1472 // (take 'this' into account in attribute parameter count)
1473 // int Printf(const wxChar *pszFormat, ...);
1474 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
1475 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1476 // as vprintf(), returns the number of characters written or < 0 on error
1477 int PrintfV(const wxString& format, va_list argptr);
1478
1479 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1480 // returns the string containing the result of Printf() to it
1481 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1482 WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat)
1483 #endif
1484 // the same as above, but takes a va_list
1485 static wxString FormatV(const wxString& format, va_list argptr);
1486
1487 // raw access to string memory
1488 // ensure that string has space for at least nLen characters
1489 // only works if the data of this string is not shared
1490 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1491 // minimize the string's memory
1492 // only works if the data of this string is not shared
1493 bool Shrink();
1494 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1495 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1496 //
1497 // get writable buffer of at least nLen bytes. Unget() *must* be called
1498 // a.s.a.p. to put string back in a reasonable state!
1499 wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) );
1500 // call this immediately after GetWriteBuf() has been used
1501 wxDEPRECATED( void UngetWriteBuf() );
1502 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1503 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1504
1505 // wxWidgets version 1 compatibility functions
1506
1507 // use Mid()
1508 wxString SubString(size_t from, size_t to) const
1509 { return Mid(from, (to - from + 1)); }
1510 // values for second parameter of CompareTo function
1511 enum caseCompare {exact, ignoreCase};
1512 // values for first parameter of Strip function
1513 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1514
1515 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1516 // use Printf()
1517 // (take 'this' into account in attribute parameter count)
1518 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1519 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
1520 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1521
1522 // use Cmp()
1523 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1524 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1525
1526 // use length()
1527 size_t Length() const { return length(); }
1528 // Count the number of characters
1529 int Freq(wxUniChar ch) const;
1530 // use MakeLower
1531 void LowerCase() { MakeLower(); }
1532 // use MakeUpper
1533 void UpperCase() { MakeUpper(); }
1534 // use Trim except that it doesn't change this string
1535 wxString Strip(stripType w = trailing) const;
1536
1537 // use Find (more general variants not yet supported)
1538 size_t Index(const wxChar* psz) const { return Find(psz); }
1539 size_t Index(wxUniChar ch) const { return Find(ch); }
1540 // use Truncate
1541 wxString& Remove(size_t pos) { return Truncate(pos); }
1542 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1543
1544 wxString& Remove(size_t nStart, size_t nLen)
1545 { return (wxString&)erase( nStart, nLen ); }
1546
1547 // use Find()
1548 int First( wxUniChar ch ) const { return Find(ch); }
1549 int First( wxUniCharRef ch ) const { return Find(ch); }
1550 int First( char ch ) const { return Find(ch); }
1551 int First( unsigned char ch ) const { return Find(ch); }
1552 int First( wchar_t ch ) const { return Find(ch); }
1553 int First( const wxChar* psz ) const { return Find(psz); }
1554 int First( const wxString& str ) const { return Find(str); }
1555 int Last( wxUniChar ch ) const { return Find(ch, true); }
1556 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1557
1558 // use empty()
1559 bool IsNull() const { return empty(); }
1560
1561 // std::string compatibility functions
1562
1563 // take nLen chars starting at nPos
1564 wxString(const wxString& str, size_t nPos, size_t nLen)
1565 : m_impl(str.m_impl, nPos, nLen) { }
1566 // take all characters from first to last
1567 wxString(const_iterator first, const_iterator last)
1568 : m_impl(first, last) { }
1569 wxString(const char *first, const char *last)
1570 {
1571 SubstrBufFromMB str(ImplStr(first, last - first));
1572 m_impl.assign(str.data, str.len);
1573 }
1574 wxString(const wchar_t *first, const wchar_t *last)
1575 {
1576 SubstrBufFromWC str(ImplStr(first, last - first));
1577 m_impl.assign(str.data, str.len);
1578 }
1579
1580 // lib.string.modifiers
1581 // append elements str[pos], ..., str[pos+n]
1582 wxString& append(const wxString& str, size_t pos, size_t n)
1583 {
1584 size_t from, len;
1585 str.PosLenToImpl(pos, n, &from, &len);
1586 m_impl.append(str.m_impl, from, len);
1587 return *this;
1588 }
1589 // append a string
1590 wxString& append(const wxString& str)
1591 { m_impl.append(str.m_impl); return *this; }
1592 wxString& append(const wxCStrData& str)
1593 { m_impl.append(str.AsString().m_impl); return *this; }
1594 // append first n (or all if n == npos) characters of sz
1595 wxString& append(const char *sz)
1596 { m_impl.append(ImplStr(sz)); return *this; }
1597 wxString& append(const wchar_t *sz)
1598 { m_impl.append(ImplStr(sz)); return *this; }
1599 wxString& append(const char *sz, size_t n)
1600 {
1601 SubstrBufFromMB str(ImplStr(sz, n));
1602 m_impl.append(str.data, str.len);
1603 return *this;
1604 }
1605 wxString& append(const wchar_t *sz, size_t n)
1606 {
1607 SubstrBufFromWC str(ImplStr(sz, n));
1608 m_impl.append(str.data, str.len);
1609 return *this;
1610 }
1611 // append n copies of ch
1612 wxString& append(size_t n, wxUniChar ch)
1613 {
1614 #if wxUSE_UNICODE_UTF8
1615 if ( !ch.IsAscii() )
1616 m_impl.append(EncodeNChars(n, ch));
1617 else
1618 #endif
1619 m_impl.append(n, (wxStringCharType)ch);
1620 return *this;
1621 }
1622 // append from first to last
1623 wxString& append(const_iterator first, const_iterator last)
1624 { m_impl.append(first, last); return *this; }
1625 wxString& append(const char *first, const char *last)
1626 { return append(first, last - first); }
1627 wxString& append(const wchar_t *first, const wchar_t *last)
1628 { return append(first, last - first); }
1629
1630 // same as `this_string = str'
1631 wxString& assign(const wxString& str)
1632 { m_impl = str.m_impl; return *this; }
1633 wxString& assign(const wxString& str, size_t len)
1634 {
1635 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
1636 return *this;
1637 }
1638 // same as ` = str[pos..pos + n]
1639 wxString& assign(const wxString& str, size_t pos, size_t n)
1640 {
1641 size_t from, len;
1642 str.PosLenToImpl(pos, n, &from, &len);
1643 m_impl.assign(str.m_impl, from, len);
1644 return *this;
1645 }
1646 // same as `= first n (or all if n == npos) characters of sz'
1647 wxString& assign(const char *sz)
1648 { m_impl.assign(ImplStr(sz)); return *this; }
1649 wxString& assign(const wchar_t *sz)
1650 { m_impl.assign(ImplStr(sz)); return *this; }
1651 wxString& assign(const char *sz, size_t n)
1652 {
1653 SubstrBufFromMB str(ImplStr(sz, n));
1654 m_impl.assign(str.data, str.len);
1655 return *this;
1656 }
1657 wxString& assign(const wchar_t *sz, size_t n)
1658 {
1659 SubstrBufFromWC str(ImplStr(sz, n));
1660 m_impl.assign(str.data, str.len);
1661 return *this;
1662 }
1663 // same as `= n copies of ch'
1664 wxString& assign(size_t n, wxUniChar ch)
1665 {
1666 #if wxUSE_UNICODE_UTF8
1667 if ( !ch.IsAscii() )
1668 m_impl.assign(EncodeNChars(n, ch));
1669 else
1670 #endif
1671 m_impl.assign(n, (wxStringCharType)ch);
1672 return *this;
1673 }
1674
1675 wxString& assign(size_t n, wxUniCharRef ch)
1676 { return assign(n, wxUniChar(ch)); }
1677 wxString& assign(size_t n, char ch)
1678 { return assign(n, wxUniChar(ch)); }
1679 wxString& assign(size_t n, unsigned char ch)
1680 { return assign(n, wxUniChar(ch)); }
1681 wxString& assign(size_t n, wchar_t ch)
1682 { return assign(n, wxUniChar(ch)); }
1683
1684 // assign from first to last
1685 wxString& assign(const_iterator first, const_iterator last)
1686 { m_impl.assign(first, last); return *this; }
1687 wxString& assign(const char *first, const char *last)
1688 { return assign(first, last - first); }
1689 wxString& assign(const wchar_t *first, const wchar_t *last)
1690 { return assign(first, last - first); }
1691
1692 // string comparison
1693 int compare(const wxString& str) const;
1694 // comparison with a substring
1695 int compare(size_t nStart, size_t nLen, const wxString& str) const;
1696 // comparison of 2 substrings
1697 int compare(size_t nStart, size_t nLen,
1698 const wxString& str, size_t nStart2, size_t nLen2) const;
1699 // just like strcmp()
1700 int compare(const char* sz) const;
1701 int compare(const wchar_t* sz) const;
1702 // substring comparison with first nCount characters of sz
1703 int compare(size_t nStart, size_t nLen,
1704 const char* sz, size_t nCount = npos) const;
1705 int compare(size_t nStart, size_t nLen,
1706 const wchar_t* sz, size_t nCount = npos) const;
1707
1708 // insert another string
1709 wxString& insert(size_t nPos, const wxString& str)
1710 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
1711 // insert n chars of str starting at nStart (in str)
1712 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1713 {
1714 size_t from, len;
1715 str.PosLenToImpl(nStart, n, &from, &len);
1716 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
1717 return *this;
1718 }
1719 // insert first n (or all if n == npos) characters of sz
1720 wxString& insert(size_t nPos, const char *sz)
1721 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1722 wxString& insert(size_t nPos, const wchar_t *sz)
1723 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1724 wxString& insert(size_t nPos, const char *sz, size_t n)
1725 {
1726 SubstrBufFromMB str(ImplStr(sz, n));
1727 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1728 return *this;
1729 }
1730 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
1731 {
1732 SubstrBufFromWC str(ImplStr(sz, n));
1733 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1734 return *this;
1735 }
1736 // insert n copies of ch
1737 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
1738 {
1739 #if wxUSE_UNICODE_UTF8
1740 if ( !ch.IsAscii() )
1741 m_impl.insert(PosToImpl(nPos), EncodeNChars(n, ch));
1742 else
1743 #endif
1744 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
1745 return *this;
1746 }
1747 iterator insert(iterator it, wxUniChar ch)
1748 {
1749 #if wxUSE_UNICODE_UTF8
1750 if ( !ch.IsAscii() )
1751 {
1752 size_t pos = IterToImplPos(it);
1753 m_impl.insert(pos, EncodeChar(ch));
1754 return iterator(this, m_impl.begin() + pos);
1755 }
1756 else
1757 #endif
1758 return iterator(this, m_impl.insert(it, (wxStringCharType)ch));
1759 }
1760 void insert(iterator it, const_iterator first, const_iterator last)
1761 { m_impl.insert(it, first, last); }
1762 void insert(iterator it, const char *first, const char *last)
1763 { insert(it - begin(), first, last - first); }
1764 void append(iterator it, const wchar_t *first, const wchar_t *last)
1765 { insert(it - begin(), first, last - first); }
1766 void insert(iterator it, size_type n, wxUniChar ch)
1767 {
1768 #if wxUSE_UNICODE_UTF8
1769 if ( !ch.IsAscii() )
1770 m_impl.insert(IterToImplPos(it), EncodeNChars(n, ch));
1771 else
1772 #endif
1773 m_impl.insert(it, n, (wxStringCharType)ch);
1774 }
1775
1776 // delete characters from nStart to nStart + nLen
1777 wxString& erase(size_type pos = 0, size_type n = npos)
1778 {
1779 size_t from, len;
1780 PosLenToImpl(pos, n, &from, &len);
1781 m_impl.erase(from, len);
1782 return *this;
1783 }
1784 // delete characters from first up to last
1785 iterator erase(iterator first, iterator last)
1786 { return iterator(this, m_impl.erase(first, last)); }
1787 iterator erase(iterator first)
1788 { return iterator(this, m_impl.erase(first)); }
1789
1790 #ifdef wxSTRING_BASE_HASNT_CLEAR
1791 void clear() { erase(); }
1792 #else
1793 void clear() { m_impl.clear(); }
1794 #endif
1795
1796 // replaces the substring of length nLen starting at nStart
1797 wxString& replace(size_t nStart, size_t nLen, const char* sz)
1798 {
1799 size_t from, len;
1800 PosLenToImpl(nStart, nLen, &from, &len);
1801 m_impl.replace(from, len, ImplStr(sz));
1802 return *this;
1803 }
1804 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
1805 {
1806 size_t from, len;
1807 PosLenToImpl(nStart, nLen, &from, &len);
1808 m_impl.replace(from, len, ImplStr(sz));
1809 return *this;
1810 }
1811 // replaces the substring of length nLen starting at nStart
1812 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1813 {
1814 size_t from, len;
1815 PosLenToImpl(nStart, nLen, &from, &len);
1816 m_impl.replace(from, len, str.m_impl);
1817 return *this;
1818 }
1819 // replaces the substring with nCount copies of ch
1820 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
1821 {
1822 size_t from, len;
1823 PosLenToImpl(nStart, nLen, &from, &len);
1824 #if wxUSE_UNICODE_UTF8
1825 if ( !ch.IsAscii() )
1826 m_impl.replace(from, len, EncodeNChars(nCount, ch));
1827 else
1828 #endif
1829 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
1830 return *this;
1831 }
1832 // replaces a substring with another substring
1833 wxString& replace(size_t nStart, size_t nLen,
1834 const wxString& str, size_t nStart2, size_t nLen2)
1835 {
1836 size_t from, len;
1837 PosLenToImpl(nStart, nLen, &from, &len);
1838
1839 size_t from2, len2;
1840 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
1841
1842 m_impl.replace(from, len, str.m_impl, from2, len2);
1843 return *this;
1844 }
1845 // replaces the substring with first nCount chars of sz
1846 wxString& replace(size_t nStart, size_t nLen,
1847 const char* sz, size_t nCount)
1848 {
1849 size_t from, len;
1850 PosLenToImpl(nStart, nLen, &from, &len);
1851
1852 SubstrBufFromMB str(ImplStr(sz, nCount));
1853
1854 m_impl.replace(from, len, str.data, str.len);
1855 return *this;
1856 }
1857 wxString& replace(size_t nStart, size_t nLen,
1858 const wchar_t* sz, size_t nCount)
1859 {
1860 size_t from, len;
1861 PosLenToImpl(nStart, nLen, &from, &len);
1862
1863 SubstrBufFromWC str(ImplStr(sz, nCount));
1864
1865 m_impl.replace(from, len, str.data, str.len);
1866 return *this;
1867 }
1868 wxString& replace(size_t nStart, size_t nLen,
1869 const wxString& s, size_t nCount)
1870 {
1871 size_t from, len;
1872 PosLenToImpl(nStart, nLen, &from, &len);
1873 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
1874 return *this;
1875 }
1876
1877 wxString& replace(iterator first, iterator last, const char* s)
1878 { m_impl.replace(first, last, ImplStr(s)); return *this; }
1879 wxString& replace(iterator first, iterator last, const wchar_t* s)
1880 { m_impl.replace(first, last, ImplStr(s)); return *this; }
1881 wxString& replace(iterator first, iterator last, const char* s, size_type n)
1882 {
1883 SubstrBufFromMB str(ImplStr(s, n));
1884 m_impl.replace(first, last, str.data, str.len);
1885 return *this;
1886 }
1887 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
1888 {
1889 SubstrBufFromWC str(ImplStr(s, n));
1890 m_impl.replace(first, last, str.data, str.len);
1891 return *this;
1892 }
1893 wxString& replace(iterator first, iterator last, const wxString& s)
1894 { m_impl.replace(first, last, s.m_impl); return *this; }
1895 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
1896 {
1897 #if wxUSE_UNICODE_UTF8
1898 if ( !ch.IsAscii() )
1899 m_impl.replace(first, last, EncodeNChars(n, ch));
1900 else
1901 #endif
1902 m_impl.replace(first, last, n, (wxStringCharType)ch);
1903 return *this;
1904 }
1905 wxString& replace(iterator first, iterator last,
1906 const_iterator first1, const_iterator last1)
1907 { m_impl.replace(first, last, first1, last1); return *this; }
1908 wxString& replace(iterator first, iterator last,
1909 const char *first1, const char *last1)
1910 { replace(first, last, first1, last1 - first1); return *this; }
1911 wxString& replace(iterator first, iterator last,
1912 const wchar_t *first1, const wchar_t *last1)
1913 { replace(first, last, first1, last1 - first1); return *this; }
1914
1915 // swap two strings
1916 void swap(wxString& str)
1917 { m_impl.swap(str.m_impl); }
1918
1919 // find a substring
1920 size_t find(const wxString& str, size_t nStart = 0) const
1921 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
1922
1923 // find first n characters of sz
1924 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
1925 {
1926 SubstrBufFromMB str(ImplStr(sz, n));
1927 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
1928 }
1929 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
1930 {
1931 SubstrBufFromWC str(ImplStr(sz, n));
1932 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
1933 }
1934
1935 // find the first occurence of character ch after nStart
1936 size_t find(wxUniChar ch, size_t nStart = 0) const
1937 { return PosFromImpl(m_impl.find(EncodeChar(ch), PosToImpl(nStart))); }
1938 size_t find(wxUniCharRef ch, size_t nStart = 0) const
1939 { return find(wxUniChar(ch), nStart); }
1940 size_t find(char ch, size_t nStart = 0) const
1941 { return find(wxUniChar(ch), nStart); }
1942 size_t find(unsigned char ch, size_t nStart = 0) const
1943 { return find(wxUniChar(ch), nStart); }
1944 size_t find(wchar_t ch, size_t nStart = 0) const
1945 { return find(wxUniChar(ch), nStart); }
1946
1947 // rfind() family is exactly like find() but works right to left
1948
1949 // as find, but from the end
1950 size_t rfind(const wxString& str, size_t nStart = npos) const
1951 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
1952
1953 // as find, but from the end
1954 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
1955 {
1956 SubstrBufFromMB str(ImplStr(sz, n));
1957 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
1958 }
1959 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
1960 {
1961 SubstrBufFromWC str(ImplStr(sz, n));
1962 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
1963 }
1964 // as find, but from the end
1965 size_t rfind(wxUniChar ch, size_t nStart = npos) const
1966 { return PosFromImpl(m_impl.rfind(EncodeChar(ch), PosToImpl(nStart))); }
1967 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
1968 { return rfind(wxUniChar(ch), nStart); }
1969 size_t rfind(char ch, size_t nStart = npos) const
1970 { return rfind(wxUniChar(ch), nStart); }
1971 size_t rfind(unsigned char ch, size_t nStart = npos) const
1972 { return rfind(wxUniChar(ch), nStart); }
1973 size_t rfind(wchar_t ch, size_t nStart = npos) const
1974 { return rfind(wxUniChar(ch), nStart); }
1975
1976 // find first/last occurence of any character (not) in the set:
1977 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1978 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
1979 // sizeof(wchar_t)==2 and surrogates are present in the string;
1980 // should we care? Probably not.
1981 size_t find_first_of(const wxString& str, size_t nStart = 0) const
1982 { return m_impl.find_first_of(str.m_impl, nStart); }
1983 size_t find_first_of(const char* sz, size_t nStart = 0) const
1984 { return m_impl.find_first_of(ImplStr(sz), nStart); }
1985 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
1986 { return m_impl.find_first_of(ImplStr(sz), nStart); }
1987 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
1988 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
1989 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
1990 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
1991 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
1992 { return m_impl.find_first_of((wxChar)c, nStart); }
1993
1994 size_t find_last_of(const wxString& str, size_t nStart = npos) const
1995 { return m_impl.find_last_of(str.m_impl, nStart); }
1996 size_t find_last_of(const char* sz, size_t nStart = npos) const
1997 { return m_impl.find_last_of(ImplStr(sz), nStart); }
1998 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
1999 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2000 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2001 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2002 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2003 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2004 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2005 { return m_impl.find_last_of((wxChar)c, nStart); }
2006
2007 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2008 { return m_impl.find_first_not_of(str.m_impl, nStart); }
2009 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
2010 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2011 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
2012 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2013 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
2014 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2015 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2016 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2017 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
2018 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2019
2020 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2021 { return m_impl.find_last_not_of(str.m_impl, nStart); }
2022 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
2023 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2024 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
2025 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2026 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
2027 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2028 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2029 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2030 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
2031 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2032 #else
2033 // we can't use std::string implementation in UTF-8 build, because the
2034 // character sets would be interpreted wrongly:
2035
2036 // as strpbrk() but starts at nStart, returns npos if not found
2037 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2038 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2039 { return find_first_of(str.mb_str().data(), nStart); }
2040 #else
2041 { return find_first_of((const wxChar*)str.c_str(), nStart); }
2042 #endif
2043 // same as above
2044 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2045 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2046 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2047 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2048 // same as find(char, size_t)
2049 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2050 { return find(c, nStart); }
2051 // find the last (starting from nStart) char from str in this string
2052 size_t find_last_of (const wxString& str, size_t nStart = npos) const
2053 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2054 { return find_last_of(str.mb_str().data(), nStart); }
2055 #else
2056 { return find_last_of((const wxChar*)str.c_str(), nStart); }
2057 #endif
2058 // same as above
2059 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2060 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2061 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2062 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2063 // same as above
2064 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2065 { return rfind(c, nStart); }
2066
2067 // find first/last occurence of any character not in the set
2068
2069 // as strspn() (starting from nStart), returns npos on failure
2070 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2071 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2072 { return find_first_not_of(str.mb_str().data(), nStart); }
2073 #else
2074 { return find_first_not_of((const wxChar*)str.c_str(), nStart); }
2075 #endif
2076 // same as above
2077 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2078 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2079 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2080 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2081 // same as above
2082 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2083 // as strcspn()
2084 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2085 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2086 { return find_last_not_of(str.mb_str().data(), nStart); }
2087 #else
2088 { return find_last_not_of((const wxChar*)str.c_str(), nStart); }
2089 #endif
2090 // same as above
2091 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2092 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2093 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2094 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2095 // same as above
2096 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2097 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2098
2099 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2100 // above to resolve ambiguities:
2101 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2102 { return find_first_of(wxUniChar(ch), nStart); }
2103 size_t find_first_of(char ch, size_t nStart = 0) const
2104 { return find_first_of(wxUniChar(ch), nStart); }
2105 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
2106 { return find_first_of(wxUniChar(ch), nStart); }
2107 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2108 { return find_first_of(wxUniChar(ch), nStart); }
2109 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2110 { return find_last_of(wxUniChar(ch), nStart); }
2111 size_t find_last_of(char ch, size_t nStart = npos) const
2112 { return find_last_of(wxUniChar(ch), nStart); }
2113 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
2114 { return find_last_of(wxUniChar(ch), nStart); }
2115 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2116 { return find_last_of(wxUniChar(ch), nStart); }
2117 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2118 { return find_first_not_of(wxUniChar(ch), nStart); }
2119 size_t find_first_not_of(char ch, size_t nStart = 0) const
2120 { return find_first_not_of(wxUniChar(ch), nStart); }
2121 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
2122 { return find_first_not_of(wxUniChar(ch), nStart); }
2123 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2124 { return find_first_not_of(wxUniChar(ch), nStart); }
2125 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2126 { return find_last_not_of(wxUniChar(ch), nStart); }
2127 size_t find_last_not_of(char ch, size_t nStart = npos) const
2128 { return find_last_not_of(wxUniChar(ch), nStart); }
2129 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
2130 { return find_last_not_of(wxUniChar(ch), nStart); }
2131 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2132 { return find_last_not_of(wxUniChar(ch), nStart); }
2133
2134 // string += string
2135 wxString& operator+=(const wxString& s)
2136 { m_impl += s.m_impl; return *this; }
2137 // string += C string
2138 wxString& operator+=(const char *psz)
2139 { m_impl += ImplStr(psz); return *this; }
2140 wxString& operator+=(const wchar_t *pwz)
2141 { m_impl += ImplStr(pwz); return *this; }
2142 wxString& operator+=(const wxCStrData& s)
2143 { m_impl += s.AsString().m_impl; return *this; }
2144 // string += char
2145 wxString& operator+=(wxUniChar ch)
2146 { m_impl += EncodeChar(ch); return *this; }
2147 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
2148 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
2149 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
2150 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
2151 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
2152
2153 private:
2154 #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2155 // helpers for wxStringBuffer and wxStringBufferLength
2156 wxStringCharType *DoGetWriteBuf(size_t nLen)
2157 { return m_impl.DoGetWriteBuf(nLen); }
2158 void DoUngetWriteBuf()
2159 { m_impl.DoUngetWriteBuf(); }
2160 void DoUngetWriteBuf(size_t nLen)
2161 { m_impl.DoUngetWriteBuf(nLen); }
2162
2163 friend class WXDLLIMPEXP_BASE wxStringBuffer;
2164 friend class WXDLLIMPEXP_BASE wxStringBufferLength;
2165 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2166
2167 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2168 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
2169 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
2170 #endif
2171
2172 #if !wxUSE_STL_BASED_WXSTRING
2173 // check string's data validity
2174 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2175 #endif
2176
2177 private:
2178 wxStringImpl m_impl;
2179
2180 // buffers for compatibility conversion from (char*)c_str() and
2181 // (wchar_t*)c_str():
2182 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2183 template<typename T>
2184 struct ConvertedBuffer
2185 {
2186 ConvertedBuffer() : m_buf(NULL) {}
2187 ~ConvertedBuffer()
2188 { free(m_buf); }
2189
2190 operator T*() const { return m_buf; }
2191
2192 ConvertedBuffer& operator=(T *str)
2193 {
2194 free(m_buf);
2195 m_buf = str;
2196 return *this;
2197 }
2198
2199 T *m_buf;
2200 };
2201 #if wxUSE_UNICODE
2202 ConvertedBuffer<char> m_convertedToChar;
2203 #endif
2204 #if !wxUSE_UNICODE_WCHAR
2205 ConvertedBuffer<wchar_t> m_convertedToWChar;
2206 #endif
2207 friend class WXDLLIMPEXP_BASE wxCStrData;
2208 };
2209
2210 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2211 #pragma warning (default:4275)
2212 #endif
2213
2214 // string iterator operators that satisfy STL Random Access Iterator
2215 // requirements:
2216 inline wxString::iterator operator+(int n, wxString::iterator i)
2217 { return i + n; }
2218 inline wxString::iterator operator+(size_t n, wxString::iterator i)
2219 { return i + n; }
2220 inline wxString::const_iterator operator+(int n, wxString::const_iterator i)
2221 { return i + n; }
2222 inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i)
2223 { return i + n; }
2224 inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
2225 { return i + n; }
2226 inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
2227 { return i + n; }
2228 inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
2229 { return i + n; }
2230 inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
2231 { return i + n; }
2232
2233 // notice that even though for many compilers the friend declarations above are
2234 // enough, from the point of view of C++ standard we must have the declarations
2235 // here as friend ones are not injected in the enclosing namespace and without
2236 // them the code fails to compile with conforming compilers such as xlC or g++4
2237 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
2238 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2239 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2240 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2241 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
2242
2243 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2244 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2245
2246 inline wxString operator+(const wxString& string, wxUniCharRef ch)
2247 { return string + (wxUniChar)ch; }
2248 inline wxString operator+(const wxString& string, char ch)
2249 { return string + wxUniChar(ch); }
2250 inline wxString operator+(const wxString& string, wchar_t ch)
2251 { return string + wxUniChar(ch); }
2252 inline wxString operator+(wxUniCharRef ch, const wxString& string)
2253 { return (wxUniChar)ch + string; }
2254 inline wxString operator+(char ch, const wxString& string)
2255 { return wxUniChar(ch) + string; }
2256 inline wxString operator+(wchar_t ch, const wxString& string)
2257 { return wxUniChar(ch) + string; }
2258
2259
2260 #if wxUSE_STL_BASED_WXSTRING
2261 // return an empty wxString (not very useful with wxUSE_STL == 1)
2262 inline const wxString wxGetEmptyString() { return wxString(); }
2263 #else // !wxUSE_STL_BASED_WXSTRING
2264 // return an empty wxString (more efficient than wxString() here)
2265 inline const wxString& wxGetEmptyString()
2266 {
2267 return *(wxString *)&wxEmptyString;
2268 }
2269 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2270
2271 // ----------------------------------------------------------------------------
2272 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2273 // ----------------------------------------------------------------------------
2274
2275 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2276
2277 class WXDLLIMPEXP_BASE wxStringBuffer
2278 {
2279 public:
2280 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
2281 : m_str(str), m_buf(lenWanted)
2282 { }
2283
2284 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
2285
2286 operator wxChar*() { return m_buf.data(); }
2287
2288 private:
2289 wxString& m_str;
2290 #if wxUSE_UNICODE
2291 wxWCharBuffer m_buf;
2292 #else
2293 wxCharBuffer m_buf;
2294 #endif
2295
2296 DECLARE_NO_COPY_CLASS(wxStringBuffer)
2297 };
2298
2299 class WXDLLIMPEXP_BASE wxStringBufferLength
2300 {
2301 public:
2302 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
2303 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
2304 { }
2305
2306 ~wxStringBufferLength()
2307 {
2308 wxASSERT(m_lenSet);
2309 m_str.assign(m_buf.data(), m_len);
2310 }
2311
2312 operator wxChar*() { return m_buf.data(); }
2313 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2314
2315 private:
2316 wxString& m_str;
2317 #if wxUSE_UNICODE
2318 wxWCharBuffer m_buf;
2319 #else
2320 wxCharBuffer m_buf;
2321 #endif
2322 size_t m_len;
2323 bool m_lenSet;
2324
2325 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
2326 };
2327
2328 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2329
2330 class WXDLLIMPEXP_BASE wxStringBuffer
2331 {
2332 public:
2333 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
2334 : m_str(str), m_buf(NULL)
2335 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2336
2337 ~wxStringBuffer() { m_str.DoUngetWriteBuf(); }
2338
2339 operator wxChar*() const { return m_buf; }
2340
2341 private:
2342 wxString& m_str;
2343 wxChar *m_buf;
2344
2345 DECLARE_NO_COPY_CLASS(wxStringBuffer)
2346 };
2347
2348 class WXDLLIMPEXP_BASE wxStringBufferLength
2349 {
2350 public:
2351 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
2352 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2353 {
2354 m_buf = m_str.DoGetWriteBuf(lenWanted);
2355 wxASSERT(m_buf != NULL);
2356 }
2357
2358 ~wxStringBufferLength()
2359 {
2360 wxASSERT(m_lenSet);
2361 m_str.DoUngetWriteBuf(m_len);
2362 }
2363
2364 operator wxChar*() const { return m_buf; }
2365 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2366
2367 private:
2368 wxString& m_str;
2369 wxChar *m_buf;
2370 size_t m_len;
2371 bool m_lenSet;
2372
2373 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
2374 };
2375
2376 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2377
2378 // ---------------------------------------------------------------------------
2379 // wxString comparison functions: operator versions are always case sensitive
2380 // ---------------------------------------------------------------------------
2381
2382 #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0
2383
2384 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2385
2386 #undef wxCMP_WXCHAR_STRING
2387
2388 // note that there is an optimization in operator==() and !=(): we (quickly)
2389 // checks the strings length first, before comparing their data
2390 inline bool operator==(const wxString& s1, const wxString& s2)
2391 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
2392 inline bool operator!=(const wxString& s1, const wxString& s2)
2393 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
2394 inline bool operator< (const wxString& s1, const wxString& s2)
2395 { return s1.Cmp(s2) < 0; }
2396 inline bool operator> (const wxString& s1, const wxString& s2)
2397 { return s1.Cmp(s2) > 0; }
2398 inline bool operator<=(const wxString& s1, const wxString& s2)
2399 { return s1.Cmp(s2) <= 0; }
2400 inline bool operator>=(const wxString& s1, const wxString& s2)
2401 { return s1.Cmp(s2) >= 0; }
2402
2403 #if wxUSE_UNICODE
2404 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
2405 { return (s1.Cmp((const wchar_t *)s2) == 0); }
2406 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
2407 { return (s2.Cmp((const wchar_t *)s1) == 0); }
2408 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2409 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2410 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2411 { return (s2.Cmp((const wchar_t *)s1) != 0); }
2412 #else // !wxUSE_UNICODE
2413 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
2414 { return (s1.Cmp((const char *)s2) == 0); }
2415 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
2416 { return (s2.Cmp((const char *)s1) == 0); }
2417 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2418 { return (s1.Cmp((const char *)s2) != 0); }
2419 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2420 { return (s2.Cmp((const char *)s1) != 0); }
2421 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2422
2423 #if wxUSE_UNICODE
2424 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
2425 { return string + (const wchar_t *)buf; }
2426 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
2427 { return (const wchar_t *)buf + string; }
2428 #else // !wxUSE_UNICODE
2429 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
2430 { return string + (const char *)buf; }
2431 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
2432 { return (const char *)buf + string; }
2433 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2434
2435 // comparison with char
2436 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2437 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2438 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2439 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2440 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2441 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2442 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2443 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2444 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2445 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2446 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2447 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2448 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2449 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2450 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2451 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2452 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2453 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
2454
2455 // comparison with C string in Unicode build
2456 #if wxUSE_UNICODE
2457
2458 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2459
2460 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
2461
2462 #undef wxCMP_CHAR_STRING
2463
2464 #endif // wxUSE_UNICODE
2465
2466 // we also need to provide the operators for comparison with wxCStrData to
2467 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2468 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2469 //
2470 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2471 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2472 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2473
2474 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
2475 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
2476
2477 #undef wxCMP_CHAR_CSTRDATA
2478 #undef wxCMP_WCHAR_CSTRDATA
2479
2480 // ---------------------------------------------------------------------------
2481 // Implementation only from here until the end of file
2482 // ---------------------------------------------------------------------------
2483
2484 #if wxUSE_STD_IOSTREAM
2485
2486 #include "wx/iosfwrap.h"
2487
2488 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
2489 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
2490 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
2491 #ifndef __BORLANDC__
2492 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
2493 #endif
2494
2495 #endif // wxSTD_STRING_COMPATIBILITY
2496
2497 // ---------------------------------------------------------------------------
2498 // wxCStrData implementation
2499 // ---------------------------------------------------------------------------
2500
2501 inline wxCStrData::wxCStrData(char *buf)
2502 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2503 inline wxCStrData::wxCStrData(wchar_t *buf)
2504 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2505
2506 inline wxCStrData::~wxCStrData()
2507 {
2508 if ( m_owned )
2509 delete m_str;
2510 }
2511
2512 inline wxCStrData::operator bool() const
2513 {
2514 return !m_str->empty();
2515 }
2516
2517 // simple cases for AsChar() and AsWChar(), the complicated ones are
2518 // in string.cpp
2519 #if wxUSE_UNICODE_WCHAR
2520 inline const wchar_t* wxCStrData::AsWChar() const
2521 {
2522 return m_str->wx_str() + m_offset;
2523 }
2524 #endif // wxUSE_UNICODE_WCHAR
2525
2526 #if !wxUSE_UNICODE
2527 inline const char* wxCStrData::AsChar() const
2528 {
2529 return m_str->wx_str() + m_offset;
2530 }
2531 #endif // !wxUSE_UNICODE
2532
2533 inline wxString wxCStrData::AsString() const
2534 {
2535 if ( m_offset == 0 )
2536 return *m_str;
2537 else
2538 return m_str->Mid(m_offset);
2539 }
2540
2541 inline wxUniChar wxCStrData::operator*() const
2542 {
2543 if ( m_str->empty() )
2544 return wxUniChar(_T('\0'));
2545 else
2546 return (*m_str)[m_offset];
2547 }
2548
2549 inline wxUniChar wxCStrData::operator[](size_t n) const
2550 {
2551 return m_str->at(m_offset + n);
2552 }
2553
2554 // ----------------------------------------------------------------------------
2555 // more wxCStrData operators
2556 // ----------------------------------------------------------------------------
2557
2558 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
2559 // some pointer into the string
2560 inline size_t operator-(const char *p, const wxCStrData& cs)
2561 {
2562 return p - cs.AsChar();
2563 }
2564
2565 inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
2566 {
2567 return p - cs.AsWChar();
2568 }
2569
2570 // ----------------------------------------------------------------------------
2571 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2572 // ----------------------------------------------------------------------------
2573
2574 // FIXME-UTF8: move this to buffer.h
2575 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
2576 : wxCharTypeBufferBase(cstr.AsChar())
2577 {
2578 }
2579
2580 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
2581 : wxCharTypeBufferBase(cstr.AsWChar())
2582 {
2583 }
2584
2585 #endif // _WX_WXSTRING_H_