]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
corrected WX_DEFINE_VARARG_FUNC_NOP usage
[wxWidgets.git] / include / wx / string.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/string.h
3 // Purpose: wxString and wxArrayString classes
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_WXSTRINGH__
19 #define _WX_WXSTRINGH__
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
55 #include "wx/strvararg.h"
56 #include "wx/buffer.h" // for wxCharBuffer
57 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58
59 class WXDLLIMPEXP_BASE wxString;
60
61 // ---------------------------------------------------------------------------
62 // macros
63 // ---------------------------------------------------------------------------
64
65 // casts [unfortunately!] needed to call some broken functions which require
66 // "char *" instead of "const char *"
67 #define WXSTRINGCAST (wxChar *)(const wxChar *)
68 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
69 #define wxMBSTRINGCAST (char *)(const char *)
70 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
71
72 // implementation only
73 #define wxASSERT_VALID_INDEX(i) \
74 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
75
76 // ----------------------------------------------------------------------------
77 // constants
78 // ----------------------------------------------------------------------------
79
80 #if WXWIN_COMPATIBILITY_2_6
81
82 // deprecated in favour of wxString::npos, don't use in new code
83 //
84 // maximum possible length for a string means "take all string" everywhere
85 #define wxSTRING_MAXLEN wxStringBase::npos
86
87 #endif // WXWIN_COMPATIBILITY_2_6
88
89 // ----------------------------------------------------------------------------
90 // global data
91 // ----------------------------------------------------------------------------
92
93 // global pointer to empty string
94 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
95
96 // ---------------------------------------------------------------------------
97 // global functions complementing standard C string library replacements for
98 // strlen() and portable strcasecmp()
99 //---------------------------------------------------------------------------
100
101 // Use wxXXX() functions from wxchar.h instead! These functions are for
102 // backwards compatibility only.
103
104 // checks whether the passed in pointer is NULL and if the string is empty
105 inline bool IsEmpty(const char *p) { return (!p || !*p); }
106
107 // safe version of strlen() (returns 0 if passed NULL pointer)
108 inline size_t Strlen(const char *psz)
109 { return psz ? strlen(psz) : 0; }
110
111 // portable strcasecmp/_stricmp
112 inline int Stricmp(const char *psz1, const char *psz2)
113 {
114 #if defined(__VISUALC__) && defined(__WXWINCE__)
115 register char c1, c2;
116 do {
117 c1 = tolower(*psz1++);
118 c2 = tolower(*psz2++);
119 } while ( c1 && (c1 == c2) );
120
121 return c1 - c2;
122 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
123 return _stricmp(psz1, psz2);
124 #elif defined(__SC__)
125 return _stricmp(psz1, psz2);
126 #elif defined(__SALFORDC__)
127 return stricmp(psz1, psz2);
128 #elif defined(__BORLANDC__)
129 return stricmp(psz1, psz2);
130 #elif defined(__WATCOMC__)
131 return stricmp(psz1, psz2);
132 #elif defined(__DJGPP__)
133 return stricmp(psz1, psz2);
134 #elif defined(__EMX__)
135 return stricmp(psz1, psz2);
136 #elif defined(__WXPM__)
137 return stricmp(psz1, psz2);
138 #elif defined(__WXPALMOS__) || \
139 defined(HAVE_STRCASECMP_IN_STRING_H) || \
140 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
141 defined(__GNUWIN32__)
142 return strcasecmp(psz1, psz2);
143 #elif defined(__MWERKS__) && !defined(__INTEL__)
144 register char c1, c2;
145 do {
146 c1 = tolower(*psz1++);
147 c2 = tolower(*psz2++);
148 } while ( c1 && (c1 == c2) );
149
150 return c1 - c2;
151 #else
152 // almost all compilers/libraries provide this function (unfortunately under
153 // different names), that's why we don't implement our own which will surely
154 // be more efficient than this code (uncomment to use):
155 /*
156 register char c1, c2;
157 do {
158 c1 = tolower(*psz1++);
159 c2 = tolower(*psz2++);
160 } while ( c1 && (c1 == c2) );
161
162 return c1 - c2;
163 */
164
165 #error "Please define string case-insensitive compare for your OS/compiler"
166 #endif // OS/compiler
167 }
168
169 // ----------------------------------------------------------------------------
170 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
171 // ----------------------------------------------------------------------------
172
173 // in both cases we need to define wxStdString
174 #if wxUSE_STL || wxUSE_STD_STRING
175
176 #include "wx/beforestd.h"
177 #include <string>
178 #include "wx/afterstd.h"
179
180 #if wxUSE_UNICODE
181 #ifdef HAVE_STD_WSTRING
182 typedef std::wstring wxStdString;
183 #else
184 typedef std::basic_string<wxChar> wxStdString;
185 #endif
186 #else
187 typedef std::string wxStdString;
188 #endif
189
190 #endif // need <string>
191
192 #if wxUSE_STL
193
194 // we don't need an extra ctor from std::string when copy ctor already does
195 // the work
196 #undef wxUSE_STD_STRING
197 #define wxUSE_STD_STRING 0
198
199 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
200 (defined(_MSC_VER) && (_MSC_VER <= 1200))
201 #define wxSTRING_BASE_HASNT_CLEAR
202 #endif
203
204 typedef wxStdString wxStringBase;
205 #else // if !wxUSE_STL
206
207 #if !defined(HAVE_STD_STRING_COMPARE) && \
208 (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
209 #define HAVE_STD_STRING_COMPARE
210 #endif
211
212 // ---------------------------------------------------------------------------
213 // string data prepended with some housekeeping info (used by wxString class),
214 // is never used directly (but had to be put here to allow inlining)
215 // ---------------------------------------------------------------------------
216
217 struct WXDLLIMPEXP_BASE wxStringData
218 {
219 int nRefs; // reference count
220 size_t nDataLength, // actual string length
221 nAllocLength; // allocated memory size
222
223 // mimics declaration 'wxChar data[nAllocLength]'
224 wxChar* data() const { return (wxChar*)(this + 1); }
225
226 // empty string has a special ref count so it's never deleted
227 bool IsEmpty() const { return (nRefs == -1); }
228 bool IsShared() const { return (nRefs > 1); }
229
230 // lock/unlock
231 void Lock() { if ( !IsEmpty() ) nRefs++; }
232
233 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
234 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
235 __forceinline
236 #endif
237 // VC++ free must take place in same DLL as allocation when using non dll
238 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
239 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
240 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
241 // we must not inline deallocation since allocation is not inlined
242 void Free();
243 #else
244 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
245 #endif
246
247 // if we had taken control over string memory (GetWriteBuf), it's
248 // intentionally put in invalid state
249 void Validate(bool b) { nRefs = (b ? 1 : 0); }
250 bool IsValid() const { return (nRefs != 0); }
251 };
252
253 class WXDLLIMPEXP_BASE wxStringBase
254 {
255 public :
256 // an 'invalid' value for string index, moved to this place due to a CW bug
257 static const size_t npos;
258 protected:
259 // points to data preceded by wxStringData structure with ref count info
260 wxChar *m_pchData;
261
262 // accessor to string data
263 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
264
265 // string (re)initialization functions
266 // initializes the string to the empty value (must be called only from
267 // ctors, use Reinit() otherwise)
268 void Init() { m_pchData = (wxChar *)wxEmptyString; }
269 // initializes the string with (a part of) C-string
270 void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = npos);
271 // as Init, but also frees old data
272 void Reinit() { GetStringData()->Unlock(); Init(); }
273
274 // memory allocation
275 // allocates memory for string of length nLen
276 bool AllocBuffer(size_t nLen);
277 // copies data to another string
278 bool AllocCopy(wxString&, int, int) const;
279 // effectively copies data to string
280 bool AssignCopy(size_t, const wxChar *);
281
282 // append a (sub)string
283 bool ConcatSelf(size_t nLen, const wxChar *src, size_t nMaxLen);
284 bool ConcatSelf(size_t nLen, const wxChar *src)
285 { return ConcatSelf(nLen, src, nLen); }
286
287 // functions called before writing to the string: they copy it if there
288 // are other references to our data (should be the only owner when writing)
289 bool CopyBeforeWrite();
290 bool AllocBeforeWrite(size_t);
291
292 // compatibility with wxString
293 bool Alloc(size_t nLen);
294 public:
295 // standard types
296 typedef wxUniChar value_type;
297 typedef wxUniChar char_type;
298 typedef wxUniCharRef reference;
299 typedef wxChar* pointer;
300 typedef const wxChar* const_pointer;
301
302 typedef size_t size_type;
303 typedef wxUniChar const_reference;
304
305 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
306 reference_type, reference_ctor) \
307 public: \
308 typedef wxUniChar value_type; \
309 typedef reference_type reference; \
310 typedef pointer_type pointer; \
311 \
312 iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \
313 \
314 reference operator*() const { return reference_ctor; } \
315 \
316 iterator_name& operator++() \
317 { ++m_cur; return *this; } \
318 iterator_name operator++(int) \
319 { iterator_name tmp = *this; ++m_cur; return tmp; } \
320 iterator_name& operator--() \
321 { --m_cur; return *this; } \
322 iterator_name operator--(int) \
323 { iterator_name tmp = *this; --m_cur; return tmp; } \
324 \
325 iterator_name operator+(int n) const \
326 { return iterator_name(m_cur + n); } \
327 iterator_name operator+(size_t n) const \
328 { return iterator_name(m_cur + n); } \
329 iterator_name operator-(int n) const \
330 { return iterator_name(m_cur - n); } \
331 iterator_name operator-(size_t n) const \
332 { return iterator_name(m_cur - n); } \
333 iterator_name operator+=(int n) \
334 { m_cur += n; return *this; } \
335 iterator_name operator+=(size_t n) \
336 { m_cur += n; return *this; } \
337 iterator_name operator-=(int n) \
338 { m_cur -= n; return *this; } \
339 iterator_name operator-=(size_t n) \
340 { m_cur -= n; return *this; } \
341 \
342 unsigned operator-(const iterator_name& i) const \
343 { return m_cur - i.m_cur; } \
344 \
345 bool operator==(const iterator_name&i) const \
346 { return m_cur == i.m_cur; } \
347 bool operator!=(const iterator_name& i) const \
348 { return m_cur != i.m_cur; } \
349 \
350 bool operator<(const iterator_name& i) const \
351 { return m_cur < i.m_cur; } \
352 bool operator>(const iterator_name& i) const \
353 { return m_cur > i.m_cur; } \
354 bool operator<=(const iterator_name& i) const \
355 { return m_cur <= i.m_cur; } \
356 bool operator>=(const iterator_name& i) const \
357 { return m_cur >= i.m_cur; } \
358 \
359 protected: \
360 /* for internal wxString use only: */ \
361 iterator_name(pointer ptr) : m_cur(ptr) {} \
362 operator pointer() const { return m_cur; } \
363 \
364 friend class WXDLLIMPEXP_BASE wxString; \
365 friend class WXDLLIMPEXP_BASE wxStringBase; \
366 friend class WXDLLIMPEXP_BASE wxCStrData; \
367 \
368 protected: \
369 pointer m_cur;
370
371 class const_iterator;
372
373 class iterator
374 {
375 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef,
376 wxUniCharRef::CreateForString(m_cur))
377
378 friend class const_iterator;
379 };
380
381 class const_iterator
382 {
383 // NB: reference_type is intentionally value, not reference, the character
384 // may be encoded differently in wxString data:
385 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar,
386 wxUniChar(*m_cur))
387
388 public:
389 const_iterator(const iterator& i) : m_cur(i.m_cur) {}
390 };
391
392 #undef WX_STR_ITERATOR
393
394 template <typename T>
395 class reverse_iterator_impl
396 {
397 public:
398 typedef T iterator_type;
399 typedef typename T::value_type value_type;
400 typedef typename T::reference reference;
401 typedef typename T::pointer *pointer;
402
403 reverse_iterator_impl(iterator_type i) : m_cur(i) {}
404 reverse_iterator_impl(const reverse_iterator_impl& ri)
405 : m_cur(ri.m_cur) {}
406
407 iterator_type base() const { return m_cur; }
408
409 reference operator*() const { return *(m_cur-1); }
410
411 reverse_iterator_impl& operator++()
412 { --m_cur; return *this; }
413 reverse_iterator_impl operator++(int)
414 { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
415 reverse_iterator_impl& operator--()
416 { ++m_cur; return *this; }
417 reverse_iterator_impl operator--(int)
418 { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
419
420 bool operator==(const reverse_iterator_impl& ri) const
421 { return m_cur == ri.m_cur; }
422 bool operator!=(const reverse_iterator_impl& ri) const
423 { return !(*this == ri); }
424
425 private:
426 iterator_type m_cur;
427 };
428
429 typedef reverse_iterator_impl<iterator> reverse_iterator;
430 typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
431
432
433 // constructors and destructor
434 // ctor for an empty string
435 wxStringBase() { Init(); }
436 // copy ctor
437 wxStringBase(const wxStringBase& stringSrc)
438 {
439 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
440 _T("did you forget to call UngetWriteBuf()?") );
441
442 if ( stringSrc.empty() ) {
443 // nothing to do for an empty string
444 Init();
445 }
446 else {
447 m_pchData = stringSrc.m_pchData; // share same data
448 GetStringData()->Lock(); // => one more copy
449 }
450 }
451 // string containing nRepeat copies of ch
452 wxStringBase(size_type nRepeat, wxUniChar ch);
453 // ctor takes first nLength characters from C string
454 // (default value of npos means take all the string)
455 wxStringBase(const wxChar *psz)
456 { InitWith(psz, 0, npos); }
457 wxStringBase(const wxChar *psz, size_t nLength)
458 { InitWith(psz, 0, nLength); }
459 wxStringBase(const wxChar *psz,
460 const wxMBConv& WXUNUSED(conv),
461 size_t nLength = npos)
462 { InitWith(psz, 0, nLength); }
463 // take nLen chars starting at nPos
464 wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen)
465 {
466 wxASSERT_MSG( str.GetStringData()->IsValid(),
467 _T("did you forget to call UngetWriteBuf()?") );
468 Init();
469 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
470 InitWith(str.c_str(), nPos, nLen);
471 }
472 // take all characters from pStart to pEnd
473 wxStringBase(const void *pStart, const void *pEnd);
474
475 // dtor is not virtual, this class must not be inherited from!
476 ~wxStringBase()
477 {
478 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
479 //RN - according to the above VC++ does indeed inline this,
480 //even though it spits out two warnings
481 #pragma warning (disable:4714)
482 #endif
483
484 GetStringData()->Unlock();
485 }
486
487 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
488 //re-enable inlining warning
489 #pragma warning (default:4714)
490 #endif
491 // overloaded assignment
492 // from another wxString
493 wxStringBase& operator=(const wxStringBase& stringSrc);
494 // from a character
495 wxStringBase& operator=(wxUniChar ch);
496 // from a C string
497 wxStringBase& operator=(const wxChar *psz);
498
499 // return the length of the string
500 size_type length() const { return GetStringData()->nDataLength; }
501 // return the length of the string
502 size_type size() const { return length(); }
503 // return the maximum size of the string
504 size_type max_size() const { return npos; }
505 // resize the string, filling the space with c if c != 0
506 void resize(size_t nSize, wxUniChar ch = wxT('\0'));
507 // delete the contents of the string
508 void clear() { erase(0, npos); }
509 // returns true if the string is empty
510 bool empty() const { return length() == 0; }
511 // inform string about planned change in size
512 void reserve(size_t sz) { Alloc(sz); }
513 size_type capacity() const { return GetStringData()->nAllocLength; }
514
515 // lib.string.access
516 // return the character at position n
517 value_type at(size_type n) const
518 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
519 // returns the writable character at position n
520 reference at(size_type n)
521 {
522 wxASSERT_VALID_INDEX( n );
523 CopyBeforeWrite();
524 return wxUniCharRef::CreateForString(&m_pchData[n]);
525 }
526
527 // lib.string.modifiers
528 // append elements str[pos], ..., str[pos+n]
529 wxStringBase& append(const wxStringBase& str, size_t pos, size_t n)
530 {
531 wxASSERT(pos <= str.length());
532 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
533 return *this;
534 }
535 // append a string
536 wxStringBase& append(const wxStringBase& str)
537 { ConcatSelf(str.length(), str.c_str()); return *this; }
538 // append first n (or all if n == npos) characters of sz
539 wxStringBase& append(const wxChar *sz)
540 { ConcatSelf(wxStrlen(sz), sz); return *this; }
541 wxStringBase& append(const wxChar *sz, size_t n)
542 { ConcatSelf(n, sz); return *this; }
543 // append n copies of ch
544 wxStringBase& append(size_t n, wxUniChar ch);
545 // append from first to last
546 wxStringBase& append(const_iterator first, const_iterator last)
547 { ConcatSelf(last - first, first); return *this; }
548
549 // same as `this_string = str'
550 wxStringBase& assign(const wxStringBase& str)
551 { return *this = str; }
552 // same as ` = str[pos..pos + n]
553 wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n)
554 { clear(); return append(str, pos, n); }
555 // same as `= first n (or all if n == npos) characters of sz'
556 wxStringBase& assign(const wxChar *sz)
557 { clear(); return append(sz, wxStrlen(sz)); }
558 wxStringBase& assign(const wxChar *sz, size_t n)
559 { clear(); return append(sz, n); }
560 // same as `= n copies of ch'
561 wxStringBase& assign(size_t n, wxUniChar ch)
562 { clear(); return append(n, ch); }
563 // assign from first to last
564 wxStringBase& assign(const_iterator first, const_iterator last)
565 { clear(); return append(first, last); }
566
567 // first valid index position
568 const_iterator begin() const { return m_pchData; }
569 iterator begin();
570 // position one after the last valid one
571 const_iterator end() const { return m_pchData + length(); }
572 iterator end();
573
574 // first element of the reversed string
575 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
576 reverse_iterator rbegin() { return reverse_iterator(end()); }
577 // one beyond the end of the reversed string
578 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
579 reverse_iterator rend() { return reverse_iterator(begin()); }
580
581 // insert another string
582 wxStringBase& insert(size_t nPos, const wxStringBase& str)
583 {
584 wxASSERT( str.GetStringData()->IsValid() );
585 return insert(nPos, str.c_str(), str.length());
586 }
587 // insert n chars of str starting at nStart (in str)
588 wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n)
589 {
590 wxASSERT( str.GetStringData()->IsValid() );
591 wxASSERT( nStart < str.length() );
592 size_t strLen = str.length() - nStart;
593 n = strLen < n ? strLen : n;
594 return insert(nPos, str.c_str() + nStart, n);
595 }
596 // insert first n (or all if n == npos) characters of sz
597 wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos);
598 // insert n copies of ch
599 wxStringBase& insert(size_t nPos, size_t n, wxUniChar ch)
600 { return insert(nPos, wxStringBase(n, ch)); }
601 iterator insert(iterator it, wxUniChar ch)
602 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
603 void insert(iterator it, const_iterator first, const_iterator last)
604 { insert(it - begin(), first, last - first); }
605 void insert(iterator it, size_type n, wxUniChar ch)
606 { insert(it - begin(), n, ch); }
607
608 // delete characters from nStart to nStart + nLen
609 wxStringBase& erase(size_type pos = 0, size_type n = npos);
610 iterator erase(iterator first, iterator last)
611 {
612 size_t idx = first - begin();
613 erase(idx, last - first);
614 return begin() + idx;
615 }
616 iterator erase(iterator first);
617
618 // explicit conversion to C string (use this with printf()!)
619 const wxChar* c_str() const { return m_pchData; }
620 const wxChar* data() const { return m_pchData; }
621
622 // replaces the substring of length nLen starting at nStart
623 wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz);
624 // replaces the substring of length nLen starting at nStart
625 wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str)
626 { return replace(nStart, nLen, str.c_str()); }
627 // replaces the substring with nCount copies of ch
628 wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch);
629 // replaces a substring with another substring
630 wxStringBase& replace(size_t nStart, size_t nLen,
631 const wxStringBase& str, size_t nStart2, size_t nLen2);
632 // replaces the substring with first nCount chars of sz
633 wxStringBase& replace(size_t nStart, size_t nLen,
634 const wxChar* sz, size_t nCount);
635 wxStringBase& replace(iterator first, iterator last, const_pointer s)
636 { return replace(first - begin(), last - first, s); }
637 wxStringBase& replace(iterator first, iterator last, const_pointer s,
638 size_type n)
639 { return replace(first - begin(), last - first, s, n); }
640 wxStringBase& replace(iterator first, iterator last, const wxStringBase& s)
641 { return replace(first - begin(), last - first, s); }
642 wxStringBase& replace(iterator first, iterator last, size_type n, wxUniChar c)
643 { return replace(first - begin(), last - first, n, c); }
644 wxStringBase& replace(iterator first, iterator last,
645 const_iterator first1, const_iterator last1)
646 { return replace(first - begin(), last - first, first1, last1 - first1); }
647
648 // swap two strings
649 void swap(wxStringBase& str);
650
651 // All find() functions take the nStart argument which specifies the
652 // position to start the search on, the default value is 0. All functions
653 // return npos if there were no match.
654
655 // find a substring
656 size_t find(const wxStringBase& str, size_t nStart = 0) const;
657
658 // find first n characters of sz
659 size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;
660
661 // find the first occurence of character ch after nStart
662 size_t find(wxUniChar ch, size_t nStart = 0) const;
663
664 // rfind() family is exactly like find() but works right to left
665
666 // as find, but from the end
667 size_t rfind(const wxStringBase& str, size_t nStart = npos) const;
668
669 // as find, but from the end
670 size_t rfind(const wxChar* sz, size_t nStart = npos,
671 size_t n = npos) const;
672 // as find, but from the end
673 size_t rfind(wxUniChar ch, size_t nStart = npos) const;
674
675 // find first/last occurence of any character in the set
676
677 // as strpbrk() but starts at nStart, returns npos if not found
678 size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const
679 { return find_first_of(str.c_str(), nStart); }
680 // same as above
681 size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
682 size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const;
683 // same as find(char, size_t)
684 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
685 { return find(c, nStart); }
686 // find the last (starting from nStart) char from str in this string
687 size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const
688 { return find_last_of(str.c_str(), nStart); }
689 // same as above
690 size_t find_last_of (const wxChar* sz, size_t nStart = npos) const;
691 size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const;
692 // same as above
693 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
694 { return rfind(c, nStart); }
695
696 // find first/last occurence of any character not in the set
697
698 // as strspn() (starting from nStart), returns npos on failure
699 size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const
700 { return find_first_not_of(str.c_str(), nStart); }
701 // same as above
702 size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const;
703 size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const;
704 // same as above
705 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
706 // as strcspn()
707 size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const
708 { return find_last_not_of(str.c_str(), nStart); }
709 // same as above
710 size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const;
711 size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const;
712 // same as above
713 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
714
715 // All compare functions return -1, 0 or 1 if the [sub]string is less,
716 // equal or greater than the compare() argument.
717
718 // comparison with another string
719 int compare(const wxStringBase& str) const;
720 // comparison with a substring
721 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
722 // comparison of 2 substrings
723 int compare(size_t nStart, size_t nLen,
724 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
725 // comparison with a c string
726 int compare(const wxChar* sz) const;
727 // substring comparison with first nCount characters of sz
728 int compare(size_t nStart, size_t nLen,
729 const wxChar* sz, size_t nCount = npos) const;
730
731 size_type copy(wxChar* s, size_type n, size_type pos = 0);
732
733 // substring extraction
734 wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const;
735
736 // string += string
737 wxStringBase& operator+=(const wxStringBase& s) { return append(s); }
738 // string += C string
739 wxStringBase& operator+=(const wxChar *psz) { return append(psz); }
740 // string += char
741 wxStringBase& operator+=(wxUniChar ch) { return append(1, ch); }
742 wxStringBase& operator+=(wxUniCharRef ch) { return append(1, ch); }
743 wxStringBase& operator+=(char ch) { return append(1, ch); }
744 wxStringBase& operator+=(wchar_t ch) { return append(1, ch); }
745 };
746
747 #endif // !wxUSE_STL
748
749 // ----------------------------------------------------------------------------
750 // wxCStrData
751 // ----------------------------------------------------------------------------
752
753 // Lightweight object returned by wxString::c_str() and implicitly convertible
754 // to either const char* or const wchar_t*.
755 class WXDLLIMPEXP_BASE wxCStrData
756 {
757 private:
758 // Ctors; for internal use by wxString and wxCStrData only
759 wxCStrData(const wxString *str, size_t offset = 0, bool owned = false)
760 : m_str(str), m_offset(offset), m_owned(owned) {}
761
762 public:
763 // Ctor constructs the object from char literal; they are needed to make
764 // operator?: compile and they intentionally take char*, not const char*
765 wxCStrData(char *buf);
766 wxCStrData(wchar_t *buf);
767
768 ~wxCStrData();
769
770 // FIXME: we'll need convertors for both char* and wchar_t* and NONE
771 // for wxChar*, but that's after completing the transition to
772 // "smart" wxUniChar class. For now, just have conversion to
773 // char* in ANSI build and wchar_t in Unicode build.
774 #if wxUSE_UNICODE
775 const wchar_t* AsWChar() const;
776 operator const wchar_t*() const { return AsWChar(); }
777 #else
778 const char* AsChar() const;
779 operator const char*() const { return AsChar(); }
780 #endif
781
782 wxString AsString() const;
783 operator wxString() const;
784
785 // allow expressions like "c_str()[0]":
786 wxUniChar operator[](int n) const { return operator[](size_t(n)); }
787 wxUniChar operator[](size_t n) const;
788 #ifndef wxSIZE_T_IS_UINT
789 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
790 #endif // size_t != unsigned int
791
792 // this operator is needed to emulate the pointer semantics of c_str():
793 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
794 // (we need both versions to resolve ambiguities):
795 wxCStrData operator+(int n) const
796 { return wxCStrData(m_str, m_offset + n, m_owned); }
797 wxCStrData operator+(size_t n) const
798 { return wxCStrData(m_str, m_offset + n, m_owned); }
799
800 // this operator is need to make expressions like "*c_str()" or
801 // "*(c_str() + 2)" work
802 wxUniChar operator*() const;
803
804 private:
805 const wxString *m_str;
806 size_t m_offset;
807 bool m_owned;
808
809 friend class WXDLLIMPEXP_BASE wxString;
810 };
811
812 // ----------------------------------------------------------------------------
813 // wxStringPrintfMixin
814 // ---------------------------------------------------------------------------
815
816 // NB: VC6 has a bug that causes linker errors if you have template methods
817 // in a class using __declspec(dllimport). The solution is to split such
818 // class into two classes, one that contains the template methods and does
819 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
820 // (with DLL linkage).
821 //
822 // We only do this for VC6 here, because the code is less efficient
823 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
824 // cannot compile this code.
825
826 #if defined(__VISUALC__) && __VISUALC__ < 1300
827 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
828 #endif
829
830 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
831 // this class contains implementation of wxString's vararg methods, it's
832 // exported from wxBase DLL
833 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
834 {
835 protected:
836 wxStringPrintfMixinBase() {}
837
838 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
839 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
840 };
841
842 // this class contains template wrappers for wxString's vararg methods, it's
843 // intentionally *not* exported from the DLL in order to fix the VC6 bug
844 // described above
845 class wxStringPrintfMixin : public wxStringPrintfMixinBase
846 {
847 private:
848 // to further complicate things, we can't return wxString from
849 // wxStringPrintfMixin::Format() because wxString is not yet declared at
850 // this point; the solution is to use this fake type trait template - this
851 // way the compiler won't know the return type until Format() is used
852 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
853 template<typename T> struct StringReturnType
854 {
855 typedef wxString type;
856 };
857
858 public:
859 // these are duplicated wxString methods, they're also declared below
860 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
861
862 // int Printf(const wxChar *pszFormat, ...);
863 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
864 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
865 WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type,
866 Format, DoFormat)
867 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
868 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
869
870 protected:
871 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
872 };
873 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
874
875
876 // ----------------------------------------------------------------------------
877 // wxString: string class trying to be compatible with std::string, MFC
878 // CString and wxWindows 1.x wxString all at once
879 // ---------------------------------------------------------------------------
880
881 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
882 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
883 // for dll-interface class 'wxString'" -- this is OK in our case
884 #pragma warning (disable:4275)
885 #endif
886
887 class WXDLLIMPEXP_BASE wxString : public wxStringBase
888 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
889 ,public wxStringPrintfMixin
890 #endif
891 {
892 // NB: special care was taken in arranging the member functions in such order
893 // that all inline functions can be effectively inlined, verify that all
894 // performance critical functions are still inlined if you change order!
895 private:
896 // if we hadn't made these operators private, it would be possible to
897 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
898 // converted to char in C and we do have operator=(char)
899 //
900 // NB: we don't need other versions (short/long and unsigned) as attempt
901 // to assign another numeric type to wxString will now result in
902 // ambiguity between operator=(char) and operator=(int)
903 wxString& operator=(int);
904
905 // these methods are not implemented - there is _no_ conversion from int to
906 // string, you're doing something wrong if the compiler wants to call it!
907 //
908 // try `s << i' or `s.Printf("%d", i)' instead
909 wxString(int);
910
911 public:
912 // constructors and destructor
913 // ctor for an empty string
914 wxString() : wxStringBase() { }
915 // copy ctor
916 wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { }
917 wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { }
918 // string containing nRepeat copies of ch
919 wxString(wxUniChar ch, size_t nRepeat = 1)
920 : wxStringBase(nRepeat, ch) { }
921 wxString(size_t nRepeat, wxUniChar ch)
922 : wxStringBase(nRepeat, ch) { }
923 wxString(wxUniCharRef ch, size_t nRepeat = 1)
924 : wxStringBase(nRepeat, ch) { }
925 wxString(size_t nRepeat, wxUniCharRef ch)
926 : wxStringBase(nRepeat, ch) { }
927 wxString(char ch, size_t nRepeat = 1)
928 : wxStringBase(nRepeat, ch) { }
929 wxString(size_t nRepeat, char ch)
930 : wxStringBase(nRepeat, ch) { }
931 wxString(wchar_t ch, size_t nRepeat = 1)
932 : wxStringBase(nRepeat, ch) { }
933 wxString(size_t nRepeat, wchar_t ch)
934 : wxStringBase(nRepeat, ch) { }
935 // ctor takes first nLength characters from C string
936 // (default value of npos means take all the string)
937 wxString(const wxChar *psz)
938 : wxStringBase(psz ? psz : wxT("")) { }
939 wxString(const wxChar *psz, size_t nLength)
940 : wxStringBase(psz, nLength) { }
941 wxString(const wxChar *psz,
942 const wxMBConv& WXUNUSED(conv),
943 size_t nLength = npos)
944 : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { }
945
946 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
947 // implicit conversions from std::string to wxString as this allows to use
948 // the same strings in non-GUI and GUI code, however we don't want to
949 // unconditionally add this ctor as it would make wx lib dependent on
950 // libstdc++ on some Linux versions which is bad, so instead we ask the
951 // client code to define this wxUSE_STD_STRING symbol if they need it
952 #if wxUSE_STD_STRING
953 wxString(const wxStdString& s)
954 : wxStringBase(s.c_str()) { }
955 #endif // wxUSE_STD_STRING
956
957 #if wxUSE_UNICODE
958 // from multibyte string
959 wxString(const char *psz,
960 const wxMBConv& conv = wxConvLibc,
961 size_t nLength = npos);
962 // from wxWCharBuffer (i.e. return from wxGetString)
963 wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { }
964 #else // ANSI
965 // from C string (for compilers using unsigned char)
966 wxString(const unsigned char* psz)
967 : wxStringBase((const char*)psz) { }
968 // from part of C string (for compilers using unsigned char)
969 wxString(const unsigned char* psz, size_t nLength)
970 : wxStringBase((const char*)psz, nLength) { }
971
972 #if wxUSE_WCHAR_T
973 // from wide (Unicode) string
974 wxString(const wchar_t *pwz,
975 const wxMBConv& conv = wxConvLibc,
976 size_t nLength = npos);
977 #endif // !wxUSE_WCHAR_T
978
979 // from wxCharBuffer
980 wxString(const wxCharBuffer& psz)
981 : wxStringBase(psz) { }
982 #endif // Unicode/ANSI
983
984 // generic attributes & operations
985 // as standard strlen()
986 size_t Len() const { return length(); }
987 // string contains any characters?
988 bool IsEmpty() const { return empty(); }
989 // empty string is "false", so !str will return true
990 bool operator!() const { return empty(); }
991 // truncate the string to given length
992 wxString& Truncate(size_t uiLen);
993 // empty string contents
994 void Empty()
995 {
996 Truncate(0);
997
998 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
999 }
1000 // empty the string and free memory
1001 void Clear()
1002 {
1003 wxString tmp(wxEmptyString);
1004 swap(tmp);
1005 }
1006
1007 // contents test
1008 // Is an ascii value
1009 bool IsAscii() const;
1010 // Is a number
1011 bool IsNumber() const;
1012 // Is a word
1013 bool IsWord() const;
1014
1015 // data access (all indexes are 0 based)
1016 // read access
1017 wxUniChar GetChar(size_t n) const
1018 { return at(n); }
1019 // read/write access
1020 wxUniCharRef GetWritableChar(size_t n)
1021 { return at(n); }
1022 // write access
1023 void SetChar(size_t n, wxUniChar ch)
1024 { at(n) = ch; }
1025
1026 // get last character
1027 wxUniChar Last() const
1028 {
1029 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1030
1031 return at(length() - 1);
1032 }
1033
1034 // get writable last character
1035 wxUniCharRef Last()
1036 {
1037 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1038 return at(length() - 1);
1039 }
1040
1041 /*
1042 Note that we we must define all of the overloads below to avoid
1043 ambiguity when using str[0].
1044 */
1045 wxUniChar operator[](int n) const
1046 { return wxStringBase::at(n); }
1047 wxUniChar operator[](size_t n) const
1048 { return wxStringBase::at(n); }
1049 #ifndef wxSIZE_T_IS_UINT
1050 wxUniChar operator[](unsigned int n) const
1051 { return wxStringBase::at(n); }
1052 #endif // size_t != unsigned int
1053
1054 // operator versions of GetWriteableChar()
1055 wxUniCharRef operator[](int n)
1056 { return wxStringBase::at(n); }
1057 wxUniCharRef operator[](size_t n)
1058 { return wxStringBase::at(n); }
1059 #ifndef wxSIZE_T_IS_UINT
1060 wxUniCharRef operator[](unsigned int n)
1061 { return wxStringBase::at(n); }
1062 #endif // size_t != unsigned int
1063
1064 // explicit conversion to C string (use this with printf()!)
1065 wxCStrData c_str() const { return wxCStrData(this); }
1066
1067 // implicit conversion to C string
1068 operator wxCStrData() const { return c_str(); }
1069 operator const wxChar*() const { return c_str(); }
1070
1071 // identical to c_str(), for MFC compatibility
1072 const wxCStrData GetData() const { return c_str(); }
1073
1074 // explicit conversion to C string in internal representation (char*,
1075 // wchar_t*, UTF-8-encoded char*, depending on the build):
1076 const_pointer wx_str() const { return data(); }
1077
1078 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1079 // converting numbers or strings which are certain not to contain special
1080 // chars (typically system functions, X atoms, environment variables etc.)
1081 //
1082 // the behaviour of these functions with the strings containing anything
1083 // else than 7 bit ASCII characters is undefined, use at your own risk.
1084 #if wxUSE_UNICODE
1085 static wxString FromAscii(const char *ascii); // string
1086 static wxString FromAscii(const char ascii); // char
1087 const wxCharBuffer ToAscii() const;
1088 #else // ANSI
1089 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1090 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
1091 const char *ToAscii() const { return c_str(); }
1092 #endif // Unicode/!Unicode
1093
1094 // conversions with (possible) format conversions: have to return a
1095 // buffer with temporary data
1096 //
1097 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1098 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1099 // fn_str() to return a string which should be used with the OS APIs
1100 // accepting the file names. The return value is always the same, but the
1101 // type differs because a function may either return pointer to the buffer
1102 // directly or have to use intermediate buffer for translation.
1103 #if wxUSE_UNICODE
1104 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1105
1106 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1107
1108 const wxChar* wc_str() const { return c_str(); }
1109
1110 // for compatibility with !wxUSE_UNICODE version
1111 const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
1112
1113 #if wxMBFILES
1114 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1115 #else // !wxMBFILES
1116 const wxChar* fn_str() const { return c_str(); }
1117 #endif // wxMBFILES/!wxMBFILES
1118 #else // ANSI
1119 const wxChar* mb_str() const { return c_str(); }
1120
1121 // for compatibility with wxUSE_UNICODE version
1122 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
1123
1124 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1125
1126 #if wxUSE_WCHAR_T
1127 const wxWCharBuffer wc_str(const wxMBConv& conv) const;
1128 #endif // wxUSE_WCHAR_T
1129 #ifdef __WXOSX__
1130 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1131 #else
1132 const wxChar* fn_str() const { return c_str(); }
1133 #endif
1134 #endif // Unicode/ANSI
1135
1136 // overloaded assignment
1137 // from another wxString
1138 wxString& operator=(const wxStringBase& stringSrc)
1139 { return (wxString&)wxStringBase::operator=(stringSrc); }
1140 // from a character
1141 wxString& operator=(wxUniChar ch)
1142 { return (wxString&)wxStringBase::operator=(ch); }
1143 wxString& operator=(wxUniCharRef ch)
1144 { return (wxString&)wxStringBase::operator=((wxUniChar)ch); }
1145 wxString& operator=(char ch)
1146 { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); }
1147 wxString& operator=(wchar_t ch)
1148 { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); }
1149 // from a C string - STL probably will crash on NULL,
1150 // so we need to compensate in that case
1151 #if wxUSE_STL
1152 wxString& operator=(const wxChar *psz)
1153 { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; }
1154 #else
1155 wxString& operator=(const wxChar *psz)
1156 { return (wxString&)wxStringBase::operator=(psz); }
1157 #endif
1158
1159 #if wxUSE_UNICODE
1160 // from wxWCharBuffer
1161 wxString& operator=(const wxWCharBuffer& psz)
1162 { (void) operator=((const wchar_t *)psz); return *this; }
1163 // from C string
1164 wxString& operator=(const char* psz)
1165 { return operator=(wxString(psz)); }
1166 #else // ANSI
1167 // from another kind of C string
1168 wxString& operator=(const unsigned char* psz);
1169 #if wxUSE_WCHAR_T
1170 // from a wide string
1171 wxString& operator=(const wchar_t *pwz);
1172 #endif
1173 // from wxCharBuffer
1174 wxString& operator=(const wxCharBuffer& psz)
1175 { (void) operator=((const char *)psz); return *this; }
1176 #endif // Unicode/ANSI
1177
1178 // string concatenation
1179 // in place concatenation
1180 /*
1181 Concatenate and return the result. Note that the left to right
1182 associativity of << allows to write things like "str << str1 << str2
1183 << ..." (unlike with +=)
1184 */
1185 // string += string
1186 wxString& operator<<(const wxString& s)
1187 {
1188 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1189 wxASSERT_MSG( s.GetStringData()->IsValid(),
1190 _T("did you forget to call UngetWriteBuf()?") );
1191 #endif
1192
1193 append(s);
1194 return *this;
1195 }
1196 // string += C string
1197 wxString& operator<<(const wxChar *psz)
1198 { append(psz); return *this; }
1199 wxString& operator<<(const wxCStrData& psz)
1200 { append(psz); return *this; }
1201 // string += char
1202 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1203 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1204 wxString& operator<<(char ch) { append(1, ch); return *this; }
1205 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1206
1207 // string += buffer (i.e. from wxGetString)
1208 #if wxUSE_UNICODE
1209 wxString& operator<<(const wxWCharBuffer& s)
1210 { return operator<<((const wchar_t *)s); }
1211 wxString& operator+=(const wxWCharBuffer& s)
1212 { return operator<<((const wchar_t *)s); }
1213 #else // !wxUSE_UNICODE
1214 wxString& operator<<(const wxCharBuffer& s)
1215 { return operator<<((const char *)s); }
1216 wxString& operator+=(const wxCharBuffer& s)
1217 { return operator<<((const char *)s); }
1218 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1219
1220 #if wxUSE_UNICODE
1221 // string += C string in Unicode build (with conversion)
1222 wxString& operator<<(const char *s)
1223 { return operator<<(wxString(s)); }
1224 wxString& operator+=(const char *s)
1225 { return operator+=(wxString(s)); }
1226 #endif // wxUSE_UNICODE
1227
1228 // string += C string
1229 wxString& Append(const wxString& s)
1230 {
1231 // test for empty() to share the string if possible
1232 if ( empty() )
1233 *this = s;
1234 else
1235 append(s);
1236 return *this;
1237 }
1238 wxString& Append(const wxCStrData& psz)
1239 { append(psz); return *this; }
1240 wxString& Append(const wxChar* psz)
1241 { append(psz); return *this; }
1242 // append count copies of given character
1243 wxString& Append(wxUniChar ch, size_t count = 1u)
1244 { append(count, ch); return *this; }
1245 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1246 { append(count, ch); return *this; }
1247 wxString& Append(char ch, size_t count = 1u)
1248 { append(count, ch); return *this; }
1249 wxString& Append(wchar_t ch, size_t count = 1u)
1250 { append(count, ch); return *this; }
1251 wxString& Append(const wxChar* psz, size_t nLen)
1252 { append(psz, nLen); return *this; }
1253
1254 // prepend a string, return the string itself
1255 wxString& Prepend(const wxString& str)
1256 { *this = str + *this; return *this; }
1257
1258 // non-destructive concatenation
1259 // two strings
1260 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1261 const wxString& string2);
1262 // string with a single char
1263 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1264 // char with a string
1265 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1266 // string with C string
1267 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1268 const wxChar *psz);
1269 // C string with string
1270 friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz,
1271 const wxString& string);
1272
1273 // stream-like functions
1274 // insert an int into string
1275 wxString& operator<<(int i)
1276 { return (*this) << Format(_T("%d"), i); }
1277 // insert an unsigned int into string
1278 wxString& operator<<(unsigned int ui)
1279 { return (*this) << Format(_T("%u"), ui); }
1280 // insert a long into string
1281 wxString& operator<<(long l)
1282 { return (*this) << Format(_T("%ld"), l); }
1283 // insert an unsigned long into string
1284 wxString& operator<<(unsigned long ul)
1285 { return (*this) << Format(_T("%lu"), ul); }
1286 #if defined wxLongLong_t && !defined wxLongLongIsLong
1287 // insert a long long if they exist and aren't longs
1288 wxString& operator<<(wxLongLong_t ll)
1289 {
1290 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1291 return (*this) << Format(fmt, ll);
1292 }
1293 // insert an unsigned long long
1294 wxString& operator<<(wxULongLong_t ull)
1295 {
1296 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1297 return (*this) << Format(fmt , ull);
1298 }
1299 #endif
1300 // insert a float into string
1301 wxString& operator<<(float f)
1302 { return (*this) << Format(_T("%f"), f); }
1303 // insert a double into string
1304 wxString& operator<<(double d)
1305 { return (*this) << Format(_T("%g"), d); }
1306
1307 // string comparison
1308 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1309 int Cmp(const wxChar *psz) const;
1310 int Cmp(const wxString& s) const;
1311 // same as Cmp() but not case-sensitive
1312 int CmpNoCase(const wxChar *psz) const;
1313 int CmpNoCase(const wxString& s) const;
1314 // test for the string equality, either considering case or not
1315 // (if compareWithCase then the case matters)
1316 bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const
1317 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
1318 // comparison with a single character: returns true if equal
1319 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const
1320 {
1321 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
1322 : wxToupper(GetChar(0u)) == wxToupper(c));
1323 }
1324
1325 // simple sub-string extraction
1326 // return substring starting at nFirst of length nCount (or till the end
1327 // if nCount = default value)
1328 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1329
1330 // operator version of Mid()
1331 wxString operator()(size_t start, size_t len) const
1332 { return Mid(start, len); }
1333
1334 // check if the string starts with the given prefix and return the rest
1335 // of the string in the provided pointer if it is not NULL; otherwise
1336 // return false
1337 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
1338 // check if the string ends with the given suffix and return the
1339 // beginning of the string before the suffix in the provided pointer if
1340 // it is not NULL; otherwise return false
1341 bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const;
1342
1343 // get first nCount characters
1344 wxString Left(size_t nCount) const;
1345 // get last nCount characters
1346 wxString Right(size_t nCount) const;
1347 // get all characters before the first occurance of ch
1348 // (returns the whole string if ch not found)
1349 wxString BeforeFirst(wxUniChar ch) const;
1350 // get all characters before the last occurence of ch
1351 // (returns empty string if ch not found)
1352 wxString BeforeLast(wxUniChar ch) const;
1353 // get all characters after the first occurence of ch
1354 // (returns empty string if ch not found)
1355 wxString AfterFirst(wxUniChar ch) const;
1356 // get all characters after the last occurence of ch
1357 // (returns the whole string if ch not found)
1358 wxString AfterLast(wxUniChar ch) const;
1359
1360 // for compatibility only, use more explicitly named functions above
1361 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1362 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1363
1364 // case conversion
1365 // convert to upper case in place, return the string itself
1366 wxString& MakeUpper();
1367 // convert to upper case, return the copy of the string
1368 // Here's something to remember: BC++ doesn't like returns in inlines.
1369 wxString Upper() const ;
1370 // convert to lower case in place, return the string itself
1371 wxString& MakeLower();
1372 // convert to lower case, return the copy of the string
1373 wxString Lower() const ;
1374
1375 // trimming/padding whitespace (either side) and truncating
1376 // remove spaces from left or from right (default) side
1377 wxString& Trim(bool bFromRight = true);
1378 // add nCount copies chPad in the beginning or at the end (default)
1379 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1380
1381 // searching and replacing
1382 // searching (return starting index, or -1 if not found)
1383 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1384 // searching (return starting index, or -1 if not found)
1385 int Find(const wxChar *pszSub) const; // like strstr
1386 // replace first (or all of bReplaceAll) occurences of substring with
1387 // another string, returns the number of replacements made
1388 size_t Replace(const wxChar *szOld,
1389 const wxChar *szNew,
1390 bool bReplaceAll = true);
1391
1392 // check if the string contents matches a mask containing '*' and '?'
1393 bool Matches(const wxChar *szMask) const;
1394
1395 // conversion to numbers: all functions return true only if the whole
1396 // string is a number and put the value of this number into the pointer
1397 // provided, the base is the numeric base in which the conversion should be
1398 // done and must be comprised between 2 and 36 or be 0 in which case the
1399 // standard C rules apply (leading '0' => octal, "0x" => hex)
1400 // convert to a signed integer
1401 bool ToLong(long *val, int base = 10) const;
1402 // convert to an unsigned integer
1403 bool ToULong(unsigned long *val, int base = 10) const;
1404 // convert to wxLongLong
1405 #if defined(wxLongLong_t)
1406 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1407 // convert to wxULongLong
1408 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1409 #endif // wxLongLong_t
1410 // convert to a double
1411 bool ToDouble(double *val) const;
1412
1413
1414 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1415 // formatted input/output
1416 // as sprintf(), returns the number of characters written or < 0 on error
1417 // (take 'this' into account in attribute parameter count)
1418 // int Printf(const wxChar *pszFormat, ...);
1419 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
1420 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1421 // as vprintf(), returns the number of characters written or < 0 on error
1422 int PrintfV(const wxString& format, va_list argptr);
1423
1424 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1425 // returns the string containing the result of Printf() to it
1426 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1427 WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat)
1428 #endif
1429 // the same as above, but takes a va_list
1430 static wxString FormatV(const wxString& format, va_list argptr);
1431
1432 // raw access to string memory
1433 // ensure that string has space for at least nLen characters
1434 // only works if the data of this string is not shared
1435 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1436 // minimize the string's memory
1437 // only works if the data of this string is not shared
1438 bool Shrink();
1439 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1440 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1441 //
1442 // get writable buffer of at least nLen bytes. Unget() *must* be called
1443 // a.s.a.p. to put string back in a reasonable state!
1444 wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) );
1445 // call this immediately after GetWriteBuf() has been used
1446 wxDEPRECATED( void UngetWriteBuf() );
1447 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1448 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1449
1450 // wxWidgets version 1 compatibility functions
1451
1452 // use Mid()
1453 wxString SubString(size_t from, size_t to) const
1454 { return Mid(from, (to - from + 1)); }
1455 // values for second parameter of CompareTo function
1456 enum caseCompare {exact, ignoreCase};
1457 // values for first parameter of Strip function
1458 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1459
1460 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1461 // use Printf()
1462 // (take 'this' into account in attribute parameter count)
1463 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1464 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
1465 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1466
1467 // use Cmp()
1468 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1469 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1470
1471 // use Len
1472 size_t Length() const { return length(); }
1473 // Count the number of characters
1474 int Freq(wxUniChar ch) const;
1475 // use MakeLower
1476 void LowerCase() { MakeLower(); }
1477 // use MakeUpper
1478 void UpperCase() { MakeUpper(); }
1479 // use Trim except that it doesn't change this string
1480 wxString Strip(stripType w = trailing) const;
1481
1482 // use Find (more general variants not yet supported)
1483 size_t Index(const wxChar* psz) const { return Find(psz); }
1484 size_t Index(wxUniChar ch) const { return Find(ch); }
1485 // use Truncate
1486 wxString& Remove(size_t pos) { return Truncate(pos); }
1487 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1488
1489 wxString& Remove(size_t nStart, size_t nLen)
1490 { return (wxString&)erase( nStart, nLen ); }
1491
1492 // use Find()
1493 int First( const wxUniChar ch ) const { return Find(ch); }
1494 int First( char ch ) const { return Find(ch); }
1495 int First( wchar_t ch ) const { return Find(ch); }
1496 int First( const wxChar* psz ) const { return Find(psz); }
1497 int First( const wxString &str ) const { return Find(str); }
1498 int Last( const wxUniChar ch ) const { return Find(ch, true); }
1499 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1500
1501 // use empty()
1502 bool IsNull() const { return empty(); }
1503
1504 // std::string compatibility functions
1505
1506 // take nLen chars starting at nPos
1507 wxString(const wxString& str, size_t nPos, size_t nLen)
1508 : wxStringBase(str, nPos, nLen) { }
1509 // take all characters from pStart to pEnd
1510 wxString(const void *pStart, const void *pEnd)
1511 : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { }
1512 wxString(const_iterator first, const_iterator last)
1513 : wxStringBase(first, last) { }
1514 wxString(iterator first, iterator last)
1515 : wxStringBase(first, last) { }
1516
1517 // lib.string.modifiers
1518 // append elements str[pos], ..., str[pos+n]
1519 wxString& append(const wxString& str, size_t pos, size_t n)
1520 { return (wxString&)wxStringBase::append(str, pos, n); }
1521 // append a string
1522 wxString& append(const wxString& str)
1523 { return (wxString&)wxStringBase::append(str); }
1524 wxString& append(const wxCStrData& str)
1525 { return (wxString&)wxStringBase::append(str.AsString()); }
1526 // append first n (or all if n == npos) characters of sz
1527 wxString& append(const wxChar *sz)
1528 { return (wxString&)wxStringBase::append(sz); }
1529 wxString& append(const wxChar *sz, size_t n)
1530 { return (wxString&)wxStringBase::append(sz, n); }
1531 // append n copies of ch
1532 wxString& append(size_t n, wxUniChar ch)
1533 { return (wxString&)wxStringBase::append(n, ch); }
1534 // append from first to last
1535 wxString& append(const_iterator first, const_iterator last)
1536 { return (wxString&)wxStringBase::append(first, last); }
1537
1538 // same as `this_string = str'
1539 wxString& assign(const wxString& str)
1540 { return (wxString&)wxStringBase::assign(str); }
1541 // same as ` = str[pos..pos + n]
1542 wxString& assign(const wxString& str, size_t pos, size_t n)
1543 { return (wxString&)wxStringBase::assign(str, pos, n); }
1544 // same as `= first n (or all if n == npos) characters of sz'
1545 wxString& assign(const wxChar *sz)
1546 { return (wxString&)wxStringBase::assign(sz); }
1547 wxString& assign(const wxChar *sz, size_t n)
1548 { return (wxString&)wxStringBase::assign(sz, n); }
1549 // same as `= n copies of ch'
1550 wxString& assign(size_t n, wxUniChar ch)
1551 { return (wxString&)wxStringBase::assign(n, ch); }
1552 // assign from first to last
1553 wxString& assign(const_iterator first, const_iterator last)
1554 { return (wxString&)wxStringBase::assign(first, last); }
1555
1556 // string comparison
1557 #if !defined(HAVE_STD_STRING_COMPARE)
1558 int compare(const wxStringBase& str) const;
1559 // comparison with a substring
1560 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
1561 // comparison of 2 substrings
1562 int compare(size_t nStart, size_t nLen,
1563 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
1564 // just like strcmp()
1565 int compare(const wxChar* sz) const;
1566 // substring comparison with first nCount characters of sz
1567 int compare(size_t nStart, size_t nLen,
1568 const wxChar* sz, size_t nCount = npos) const;
1569 #endif // !defined HAVE_STD_STRING_COMPARE
1570
1571 // insert another string
1572 wxString& insert(size_t nPos, const wxString& str)
1573 { return (wxString&)wxStringBase::insert(nPos, str); }
1574 // insert n chars of str starting at nStart (in str)
1575 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1576 { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); }
1577 // insert first n (or all if n == npos) characters of sz
1578 wxString& insert(size_t nPos, const wxChar *sz)
1579 { return (wxString&)wxStringBase::insert(nPos, sz); }
1580 wxString& insert(size_t nPos, const wxChar *sz, size_t n)
1581 { return (wxString&)wxStringBase::insert(nPos, sz, n); }
1582 // insert n copies of ch
1583 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
1584 { return (wxString&)wxStringBase::insert(nPos, n, ch); }
1585 iterator insert(iterator it, wxUniChar ch)
1586 { return wxStringBase::insert(it, ch); }
1587 void insert(iterator it, const_iterator first, const_iterator last)
1588 { wxStringBase::insert(it, first, last); }
1589 void insert(iterator it, size_type n, wxUniChar ch)
1590 { wxStringBase::insert(it, n, ch); }
1591
1592 // delete characters from nStart to nStart + nLen
1593 wxString& erase(size_type pos = 0, size_type n = npos)
1594 { return (wxString&)wxStringBase::erase(pos, n); }
1595 iterator erase(iterator first, iterator last)
1596 { return wxStringBase::erase(first, last); }
1597 iterator erase(iterator first)
1598 { return wxStringBase::erase(first); }
1599
1600 #ifdef wxSTRING_BASE_HASNT_CLEAR
1601 void clear() { erase(); }
1602 #endif
1603
1604 // replaces the substring of length nLen starting at nStart
1605 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz)
1606 { return (wxString&)wxStringBase::replace(nStart, nLen, sz); }
1607 // replaces the substring of length nLen starting at nStart
1608 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1609 { return (wxString&)wxStringBase::replace(nStart, nLen, str); }
1610 // replaces the substring with nCount copies of ch
1611 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
1612 { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); }
1613 // replaces a substring with another substring
1614 wxString& replace(size_t nStart, size_t nLen,
1615 const wxString& str, size_t nStart2, size_t nLen2)
1616 { return (wxString&)wxStringBase::replace(nStart, nLen, str,
1617 nStart2, nLen2); }
1618 // replaces the substring with first nCount chars of sz
1619 wxString& replace(size_t nStart, size_t nLen,
1620 const wxChar* sz, size_t nCount)
1621 { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); }
1622 wxString& replace(iterator first, iterator last, const_pointer s)
1623 { return (wxString&)wxStringBase::replace(first, last, s); }
1624 wxString& replace(iterator first, iterator last, const_pointer s,
1625 size_type n)
1626 { return (wxString&)wxStringBase::replace(first, last, s, n); }
1627 wxString& replace(iterator first, iterator last, const wxString& s)
1628 { return (wxString&)wxStringBase::replace(first, last, s); }
1629 wxString& replace(iterator first, iterator last, size_type n, wxUniChar c)
1630 { return (wxString&)wxStringBase::replace(first, last, n, c); }
1631 wxString& replace(iterator first, iterator last,
1632 const_iterator first1, const_iterator last1)
1633 { return (wxString&)wxStringBase::replace(first, last, first1, last1); }
1634
1635 // string += string
1636 wxString& operator+=(const wxString& s)
1637 { return (wxString&)wxStringBase::operator+=(s); }
1638 // string += C string
1639 wxString& operator+=(const wxChar *psz)
1640 { return (wxString&)wxStringBase::operator+=(psz); }
1641 wxString& operator+=(const wxCStrData& s)
1642 { return (wxString&)wxStringBase::operator+=(s.AsString()); }
1643 // string += char
1644 wxString& operator+=(wxUniChar ch)
1645 { return (wxString&)wxStringBase::operator+=(ch); }
1646 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
1647 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
1648 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
1649
1650 private:
1651 #if !wxUSE_STL
1652 // helpers for wxStringBuffer and wxStringBufferLength
1653 wxChar *DoGetWriteBuf(size_t nLen);
1654 void DoUngetWriteBuf();
1655 void DoUngetWriteBuf(size_t nLen);
1656
1657 friend class WXDLLIMPEXP_BASE wxStringBuffer;
1658 friend class WXDLLIMPEXP_BASE wxStringBufferLength;
1659 #endif
1660
1661 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1662 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
1663 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
1664 #endif
1665 };
1666
1667 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1668 #pragma warning (default:4275)
1669 #endif
1670
1671 // notice that even though for many compilers the friend declarations above are
1672 // enough, from the point of view of C++ standard we must have the declarations
1673 // here as friend ones are not injected in the enclosing namespace and without
1674 // them the code fails to compile with conforming compilers such as xlC or g++4
1675 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
1676 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
1677 wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
1678
1679 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1680 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1681
1682 inline wxString operator+(const wxString& string, wxUniCharRef ch)
1683 { return string + (wxUniChar)ch; }
1684 inline wxString operator+(const wxString& string, char ch)
1685 { return string + wxUniChar(ch); }
1686 inline wxString operator+(const wxString& string, wchar_t ch)
1687 { return string + wxUniChar(ch); }
1688 inline wxString operator+(wxUniCharRef ch, const wxString& string)
1689 { return (wxUniChar)ch + string; }
1690 inline wxString operator+(char ch, const wxString& string)
1691 { return wxUniChar(ch) + string; }
1692 inline wxString operator+(wchar_t ch, const wxString& string)
1693 { return wxUniChar(ch) + string; }
1694
1695
1696 #if wxUSE_STL
1697 // return an empty wxString (not very useful with wxUSE_STL == 1)
1698 inline const wxString wxGetEmptyString() { return wxString(); }
1699 #else // !wxUSE_STL
1700 // return an empty wxString (more efficient than wxString() here)
1701 inline const wxString& wxGetEmptyString()
1702 {
1703 return *(wxString *)&wxEmptyString;
1704 }
1705 #endif // wxUSE_STL/!wxUSE_STL
1706
1707 // ----------------------------------------------------------------------------
1708 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1709 // ----------------------------------------------------------------------------
1710
1711 #if wxUSE_STL
1712
1713 class WXDLLIMPEXP_BASE wxStringBuffer
1714 {
1715 public:
1716 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1717 : m_str(str), m_buf(lenWanted)
1718 { }
1719
1720 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
1721
1722 operator wxChar*() { return m_buf.data(); }
1723
1724 private:
1725 wxString& m_str;
1726 #if wxUSE_UNICODE
1727 wxWCharBuffer m_buf;
1728 #else
1729 wxCharBuffer m_buf;
1730 #endif
1731
1732 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1733 };
1734
1735 class WXDLLIMPEXP_BASE wxStringBufferLength
1736 {
1737 public:
1738 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1739 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
1740 { }
1741
1742 ~wxStringBufferLength()
1743 {
1744 wxASSERT(m_lenSet);
1745 m_str.assign(m_buf.data(), m_len);
1746 }
1747
1748 operator wxChar*() { return m_buf.data(); }
1749 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1750
1751 private:
1752 wxString& m_str;
1753 #if wxUSE_UNICODE
1754 wxWCharBuffer m_buf;
1755 #else
1756 wxCharBuffer m_buf;
1757 #endif
1758 size_t m_len;
1759 bool m_lenSet;
1760
1761 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1762 };
1763
1764 #else // if !wxUSE_STL
1765
1766 class WXDLLIMPEXP_BASE wxStringBuffer
1767 {
1768 public:
1769 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1770 : m_str(str), m_buf(NULL)
1771 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
1772
1773 ~wxStringBuffer() { m_str.DoUngetWriteBuf(); }
1774
1775 operator wxChar*() const { return m_buf; }
1776
1777 private:
1778 wxString& m_str;
1779 wxChar *m_buf;
1780
1781 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1782 };
1783
1784 class WXDLLIMPEXP_BASE wxStringBufferLength
1785 {
1786 public:
1787 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1788 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
1789 {
1790 m_buf = m_str.DoGetWriteBuf(lenWanted);
1791 wxASSERT(m_buf != NULL);
1792 }
1793
1794 ~wxStringBufferLength()
1795 {
1796 wxASSERT(m_lenSet);
1797 m_str.DoUngetWriteBuf(m_len);
1798 }
1799
1800 operator wxChar*() const { return m_buf; }
1801 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1802
1803 private:
1804 wxString& m_str;
1805 wxChar *m_buf;
1806 size_t m_len;
1807 bool m_lenSet;
1808
1809 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1810 };
1811
1812 #endif // !wxUSE_STL
1813
1814 // ---------------------------------------------------------------------------
1815 // wxString comparison functions: operator versions are always case sensitive
1816 // ---------------------------------------------------------------------------
1817
1818 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1819 // are used and defining them also for wxString would only result in
1820 // compilation ambiguities when comparing std::string and wxString
1821 #if !wxUSE_STL
1822
1823 inline bool operator==(const wxString& s1, const wxString& s2)
1824 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
1825 inline bool operator==(const wxString& s1, const wxChar * s2)
1826 { return s1.Cmp(s2) == 0; }
1827 inline bool operator==(const wxChar * s1, const wxString& s2)
1828 { return s2.Cmp(s1) == 0; }
1829 inline bool operator!=(const wxString& s1, const wxString& s2)
1830 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
1831 inline bool operator!=(const wxString& s1, const wxChar * s2)
1832 { return s1.Cmp(s2) != 0; }
1833 inline bool operator!=(const wxChar * s1, const wxString& s2)
1834 { return s2.Cmp(s1) != 0; }
1835 inline bool operator< (const wxString& s1, const wxString& s2)
1836 { return s1.Cmp(s2) < 0; }
1837 inline bool operator< (const wxString& s1, const wxChar * s2)
1838 { return s1.Cmp(s2) < 0; }
1839 inline bool operator< (const wxChar * s1, const wxString& s2)
1840 { return s2.Cmp(s1) > 0; }
1841 inline bool operator> (const wxString& s1, const wxString& s2)
1842 { return s1.Cmp(s2) > 0; }
1843 inline bool operator> (const wxString& s1, const wxChar * s2)
1844 { return s1.Cmp(s2) > 0; }
1845 inline bool operator> (const wxChar * s1, const wxString& s2)
1846 { return s2.Cmp(s1) < 0; }
1847 inline bool operator<=(const wxString& s1, const wxString& s2)
1848 { return s1.Cmp(s2) <= 0; }
1849 inline bool operator<=(const wxString& s1, const wxChar * s2)
1850 { return s1.Cmp(s2) <= 0; }
1851 inline bool operator<=(const wxChar * s1, const wxString& s2)
1852 { return s2.Cmp(s1) >= 0; }
1853 inline bool operator>=(const wxString& s1, const wxString& s2)
1854 { return s1.Cmp(s2) >= 0; }
1855 inline bool operator>=(const wxString& s1, const wxChar * s2)
1856 { return s1.Cmp(s2) >= 0; }
1857 inline bool operator>=(const wxChar * s1, const wxString& s2)
1858 { return s2.Cmp(s1) <= 0; }
1859
1860 #if wxUSE_UNICODE
1861 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
1862 { return (s1.Cmp((const wchar_t *)s2) == 0); }
1863 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
1864 { return (s2.Cmp((const wchar_t *)s1) == 0); }
1865 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
1866 { return (s1.Cmp((const wchar_t *)s2) != 0); }
1867 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
1868 { return (s2.Cmp((const wchar_t *)s1) != 0); }
1869 #else // !wxUSE_UNICODE
1870 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
1871 { return (s1.Cmp((const char *)s2) == 0); }
1872 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
1873 { return (s2.Cmp((const char *)s1) == 0); }
1874 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
1875 { return (s1.Cmp((const char *)s2) != 0); }
1876 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
1877 { return (s2.Cmp((const char *)s1) != 0); }
1878 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1879
1880 #if wxUSE_UNICODE
1881 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
1882 { return string + (const wchar_t *)buf; }
1883 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
1884 { return (const wchar_t *)buf + string; }
1885 #else // !wxUSE_UNICODE
1886 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
1887 { return string + (const char *)buf; }
1888 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
1889 { return (const char *)buf + string; }
1890 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1891
1892 #endif // !wxUSE_STL
1893
1894 // comparison with char (those are not defined by std::[w]string and so should
1895 // be always available)
1896 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
1897 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
1898 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
1899 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
1900 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
1901 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
1902 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
1903 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
1904 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
1905 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
1906 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
1907 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
1908 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
1909 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
1910 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
1911 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
1912 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
1913 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
1914
1915 // comparison with C string in Unicode build
1916 #if wxUSE_UNICODE
1917 inline bool operator==(const wxString& s1, const char* s2)
1918 { return s1 == wxString(s2); }
1919 inline bool operator==(const char* s1, const wxString& s2)
1920 { return wxString(s1) == s2; }
1921 inline bool operator!=(const wxString& s1, const char* s2)
1922 { return s1 != wxString(s2); }
1923 inline bool operator!=(const char* s1, const wxString& s2)
1924 { return wxString(s1) != s2; }
1925 inline bool operator< (const wxString& s1, const char* s2)
1926 { return s1 < wxString(s2); }
1927 inline bool operator< (const char* s1, const wxString& s2)
1928 { return wxString(s1) < s2; }
1929 inline bool operator> (const wxString& s1, const char* s2)
1930 { return s1 > wxString(s2); }
1931 inline bool operator> (const char* s1, const wxString& s2)
1932 { return wxString(s1) > s2; }
1933 inline bool operator<=(const wxString& s1, const char* s2)
1934 { return s1 <= wxString(s2); }
1935 inline bool operator<=(const char* s1, const wxString& s2)
1936 { return wxString(s1) <= s2; }
1937 inline bool operator>=(const wxString& s1, const char* s2)
1938 { return s1 >= wxString(s2); }
1939 inline bool operator>=(const char* s1, const wxString& s2)
1940 { return wxString(s1) >= s2; }
1941 #endif // wxUSE_UNICODE
1942
1943 // ---------------------------------------------------------------------------
1944 // Implementation only from here until the end of file
1945 // ---------------------------------------------------------------------------
1946
1947 // don't pollute the library user's name space
1948 #undef wxASSERT_VALID_INDEX
1949
1950 #if wxUSE_STD_IOSTREAM
1951
1952 #include "wx/iosfwrap.h"
1953
1954 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
1955 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
1956
1957 #endif // wxSTD_STRING_COMPATIBILITY
1958
1959 // ---------------------------------------------------------------------------
1960 // wxCStrData implementation
1961 // ---------------------------------------------------------------------------
1962
1963 inline wxCStrData::wxCStrData(char *buf)
1964 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
1965 inline wxCStrData::wxCStrData(wchar_t *buf)
1966 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
1967
1968 inline wxCStrData::~wxCStrData()
1969 {
1970 if ( m_owned )
1971 delete m_str;
1972 }
1973
1974 #if wxUSE_UNICODE
1975 inline const wchar_t* wxCStrData::AsWChar() const
1976 #else
1977 inline const char* wxCStrData::AsChar() const
1978 #endif
1979 {
1980 if ( m_offset == 0 )
1981 return m_str->wx_str(); // FIXME
1982 else
1983 return (const wxChar*)(m_str->begin() + m_offset);
1984 }
1985
1986 inline wxString wxCStrData::AsString() const
1987 {
1988 if ( m_offset == 0 )
1989 return *m_str;
1990 else
1991 return m_str->Mid(m_offset);
1992 }
1993
1994 inline wxCStrData::operator wxString() const { return AsString(); }
1995
1996 inline wxUniChar wxCStrData::operator*() const
1997 {
1998 if ( m_str->empty() )
1999 return wxUniChar(_T('\0'));
2000 else
2001 return (*m_str)[m_offset];
2002 }
2003
2004 inline wxUniChar wxCStrData::operator[](size_t n) const
2005 {
2006 return m_str->at(m_offset + n);
2007 }
2008
2009 #endif // _WX_WXSTRINGH__