]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
65edbb87e017ff092629312c29bf1f39a5a104ad
[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 wxUniChar operator[](long n) const { return operator[](size_t(n)); }
789 #ifndef wxSIZE_T_IS_UINT
790 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
791 #endif // size_t != unsigned int
792
793 // these operators are needed to emulate the pointer semantics of c_str():
794 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
795 // (we need both versions to resolve ambiguities):
796 wxCStrData operator+(int n) const
797 { return wxCStrData(m_str, m_offset + n, m_owned); }
798 wxCStrData operator+(long n) const
799 { return wxCStrData(m_str, m_offset + n, m_owned); }
800 wxCStrData operator+(size_t n) const
801 { return wxCStrData(m_str, m_offset + n, m_owned); }
802
803 // this operator is need to make expressions like "*c_str()" or
804 // "*(c_str() + 2)" work
805 wxUniChar operator*() const;
806
807 private:
808 const wxString *m_str;
809 size_t m_offset;
810 bool m_owned;
811
812 friend class WXDLLIMPEXP_BASE wxString;
813 };
814
815 // ----------------------------------------------------------------------------
816 // wxStringPrintfMixin
817 // ---------------------------------------------------------------------------
818
819 // NB: VC6 has a bug that causes linker errors if you have template methods
820 // in a class using __declspec(dllimport). The solution is to split such
821 // class into two classes, one that contains the template methods and does
822 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
823 // (with DLL linkage).
824 //
825 // We only do this for VC6 here, because the code is less efficient
826 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
827 // cannot compile this code.
828
829 #if defined(__VISUALC__) && __VISUALC__ < 1300
830 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
831 #endif
832
833 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
834 // this class contains implementation of wxString's vararg methods, it's
835 // exported from wxBase DLL
836 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
837 {
838 protected:
839 wxStringPrintfMixinBase() {}
840
841 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
842 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
843 };
844
845 // this class contains template wrappers for wxString's vararg methods, it's
846 // intentionally *not* exported from the DLL in order to fix the VC6 bug
847 // described above
848 class wxStringPrintfMixin : public wxStringPrintfMixinBase
849 {
850 private:
851 // to further complicate things, we can't return wxString from
852 // wxStringPrintfMixin::Format() because wxString is not yet declared at
853 // this point; the solution is to use this fake type trait template - this
854 // way the compiler won't know the return type until Format() is used
855 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
856 template<typename T> struct StringReturnType
857 {
858 typedef wxString type;
859 };
860
861 public:
862 // these are duplicated wxString methods, they're also declared below
863 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
864
865 // int Printf(const wxChar *pszFormat, ...);
866 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
867 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
868 WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type,
869 Format, DoFormat)
870 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
871 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
872
873 protected:
874 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
875 };
876 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
877
878
879 // ----------------------------------------------------------------------------
880 // wxString: string class trying to be compatible with std::string, MFC
881 // CString and wxWindows 1.x wxString all at once
882 // ---------------------------------------------------------------------------
883
884 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
885 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
886 // for dll-interface class 'wxString'" -- this is OK in our case
887 #pragma warning (disable:4275)
888 #endif
889
890 class WXDLLIMPEXP_BASE wxString : public wxStringBase
891 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
892 ,public wxStringPrintfMixin
893 #endif
894 {
895 // NB: special care was taken in arranging the member functions in such order
896 // that all inline functions can be effectively inlined, verify that all
897 // performance critical functions are still inlined if you change order!
898 private:
899 // if we hadn't made these operators private, it would be possible to
900 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
901 // converted to char in C and we do have operator=(char)
902 //
903 // NB: we don't need other versions (short/long and unsigned) as attempt
904 // to assign another numeric type to wxString will now result in
905 // ambiguity between operator=(char) and operator=(int)
906 wxString& operator=(int);
907
908 // these methods are not implemented - there is _no_ conversion from int to
909 // string, you're doing something wrong if the compiler wants to call it!
910 //
911 // try `s << i' or `s.Printf("%d", i)' instead
912 wxString(int);
913
914 public:
915 // constructors and destructor
916 // ctor for an empty string
917 wxString() : wxStringBase() { }
918 // copy ctor
919 wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { }
920 wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { }
921 // string containing nRepeat copies of ch
922 wxString(wxUniChar ch, size_t nRepeat = 1)
923 : wxStringBase(nRepeat, ch) { }
924 wxString(size_t nRepeat, wxUniChar ch)
925 : wxStringBase(nRepeat, ch) { }
926 wxString(wxUniCharRef ch, size_t nRepeat = 1)
927 : wxStringBase(nRepeat, ch) { }
928 wxString(size_t nRepeat, wxUniCharRef ch)
929 : wxStringBase(nRepeat, ch) { }
930 wxString(char ch, size_t nRepeat = 1)
931 : wxStringBase(nRepeat, ch) { }
932 wxString(size_t nRepeat, char ch)
933 : wxStringBase(nRepeat, ch) { }
934 wxString(wchar_t ch, size_t nRepeat = 1)
935 : wxStringBase(nRepeat, ch) { }
936 wxString(size_t nRepeat, wchar_t ch)
937 : wxStringBase(nRepeat, ch) { }
938 // ctor takes first nLength characters from C string
939 // (default value of npos means take all the string)
940 wxString(const wxChar *psz)
941 : wxStringBase(psz ? psz : wxT("")) { }
942 wxString(const wxChar *psz, size_t nLength)
943 : wxStringBase(psz, nLength) { }
944 wxString(const wxChar *psz,
945 const wxMBConv& WXUNUSED(conv),
946 size_t nLength = npos)
947 : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { }
948
949 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
950 // implicit conversions from std::string to wxString as this allows to use
951 // the same strings in non-GUI and GUI code, however we don't want to
952 // unconditionally add this ctor as it would make wx lib dependent on
953 // libstdc++ on some Linux versions which is bad, so instead we ask the
954 // client code to define this wxUSE_STD_STRING symbol if they need it
955 #if wxUSE_STD_STRING
956 wxString(const wxStdString& s)
957 : wxStringBase(s.c_str()) { }
958 #endif // wxUSE_STD_STRING
959
960 #if wxUSE_UNICODE
961 // from multibyte string
962 wxString(const char *psz,
963 const wxMBConv& conv = wxConvLibc,
964 size_t nLength = npos);
965 // from wxWCharBuffer (i.e. return from wxGetString)
966 wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { }
967 #else // ANSI
968 // from C string (for compilers using unsigned char)
969 wxString(const unsigned char* psz)
970 : wxStringBase((const char*)psz) { }
971 // from part of C string (for compilers using unsigned char)
972 wxString(const unsigned char* psz, size_t nLength)
973 : wxStringBase((const char*)psz, nLength) { }
974
975 #if wxUSE_WCHAR_T
976 // from wide (Unicode) string
977 wxString(const wchar_t *pwz,
978 const wxMBConv& conv = wxConvLibc,
979 size_t nLength = npos);
980 #endif // !wxUSE_WCHAR_T
981
982 // from wxCharBuffer
983 wxString(const wxCharBuffer& psz)
984 : wxStringBase(psz) { }
985 #endif // Unicode/ANSI
986
987 // generic attributes & operations
988 // as standard strlen()
989 size_t Len() const { return length(); }
990 // string contains any characters?
991 bool IsEmpty() const { return empty(); }
992 // empty string is "false", so !str will return true
993 bool operator!() const { return empty(); }
994 // truncate the string to given length
995 wxString& Truncate(size_t uiLen);
996 // empty string contents
997 void Empty()
998 {
999 Truncate(0);
1000
1001 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1002 }
1003 // empty the string and free memory
1004 void Clear()
1005 {
1006 wxString tmp(wxEmptyString);
1007 swap(tmp);
1008 }
1009
1010 // contents test
1011 // Is an ascii value
1012 bool IsAscii() const;
1013 // Is a number
1014 bool IsNumber() const;
1015 // Is a word
1016 bool IsWord() const;
1017
1018 // data access (all indexes are 0 based)
1019 // read access
1020 wxUniChar GetChar(size_t n) const
1021 { return at(n); }
1022 // read/write access
1023 wxUniCharRef GetWritableChar(size_t n)
1024 { return at(n); }
1025 // write access
1026 void SetChar(size_t n, wxUniChar ch)
1027 { at(n) = ch; }
1028
1029 // get last character
1030 wxUniChar Last() const
1031 {
1032 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1033
1034 return at(length() - 1);
1035 }
1036
1037 // get writable last character
1038 wxUniCharRef Last()
1039 {
1040 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1041 return at(length() - 1);
1042 }
1043
1044 /*
1045 Note that we we must define all of the overloads below to avoid
1046 ambiguity when using str[0].
1047 */
1048 wxUniChar operator[](int n) const
1049 { return wxStringBase::at(n); }
1050 wxUniChar operator[](long n) const
1051 { return wxStringBase::at(n); }
1052 wxUniChar operator[](size_t n) const
1053 { return wxStringBase::at(n); }
1054 #ifndef wxSIZE_T_IS_UINT
1055 wxUniChar operator[](unsigned int n) const
1056 { return wxStringBase::at(n); }
1057 #endif // size_t != unsigned int
1058
1059 // operator versions of GetWriteableChar()
1060 wxUniCharRef operator[](int n)
1061 { return wxStringBase::at(n); }
1062 wxUniCharRef operator[](long n)
1063 { return wxStringBase::at(n); }
1064 wxUniCharRef operator[](size_t n)
1065 { return wxStringBase::at(n); }
1066 #ifndef wxSIZE_T_IS_UINT
1067 wxUniCharRef operator[](unsigned int n)
1068 { return wxStringBase::at(n); }
1069 #endif // size_t != unsigned int
1070
1071 // explicit conversion to C string (use this with printf()!)
1072 wxCStrData c_str() const { return wxCStrData(this); }
1073
1074 // implicit conversion to C string
1075 operator wxCStrData() const { return c_str(); }
1076 operator const wxChar*() const { return c_str(); }
1077
1078 // identical to c_str(), for MFC compatibility
1079 const wxCStrData GetData() const { return c_str(); }
1080
1081 // explicit conversion to C string in internal representation (char*,
1082 // wchar_t*, UTF-8-encoded char*, depending on the build):
1083 const_pointer wx_str() const { return data(); }
1084
1085 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1086 // converting numbers or strings which are certain not to contain special
1087 // chars (typically system functions, X atoms, environment variables etc.)
1088 //
1089 // the behaviour of these functions with the strings containing anything
1090 // else than 7 bit ASCII characters is undefined, use at your own risk.
1091 #if wxUSE_UNICODE
1092 static wxString FromAscii(const char *ascii); // string
1093 static wxString FromAscii(const char ascii); // char
1094 const wxCharBuffer ToAscii() const;
1095 #else // ANSI
1096 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1097 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
1098 const char *ToAscii() const { return c_str(); }
1099 #endif // Unicode/!Unicode
1100
1101 // conversions with (possible) format conversions: have to return a
1102 // buffer with temporary data
1103 //
1104 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1105 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1106 // fn_str() to return a string which should be used with the OS APIs
1107 // accepting the file names. The return value is always the same, but the
1108 // type differs because a function may either return pointer to the buffer
1109 // directly or have to use intermediate buffer for translation.
1110 #if wxUSE_UNICODE
1111 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1112
1113 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1114
1115 const wxChar* wc_str() const { return c_str(); }
1116
1117 // for compatibility with !wxUSE_UNICODE version
1118 const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
1119
1120 #if wxMBFILES
1121 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1122 #else // !wxMBFILES
1123 const wxChar* fn_str() const { return c_str(); }
1124 #endif // wxMBFILES/!wxMBFILES
1125 #else // ANSI
1126 const wxChar* mb_str() const { return c_str(); }
1127
1128 // for compatibility with wxUSE_UNICODE version
1129 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
1130
1131 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1132
1133 #if wxUSE_WCHAR_T
1134 const wxWCharBuffer wc_str(const wxMBConv& conv) const;
1135 #endif // wxUSE_WCHAR_T
1136 #ifdef __WXOSX__
1137 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1138 #else
1139 const wxChar* fn_str() const { return c_str(); }
1140 #endif
1141 #endif // Unicode/ANSI
1142
1143 // overloaded assignment
1144 // from another wxString
1145 wxString& operator=(const wxStringBase& stringSrc)
1146 { return (wxString&)wxStringBase::operator=(stringSrc); }
1147 wxString& operator=(const wxCStrData& cstr);
1148 // from a character
1149 wxString& operator=(wxUniChar ch)
1150 { return (wxString&)wxStringBase::operator=(ch); }
1151 wxString& operator=(wxUniCharRef ch)
1152 { return (wxString&)wxStringBase::operator=((wxUniChar)ch); }
1153 wxString& operator=(char ch)
1154 { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); }
1155 wxString& operator=(wchar_t ch)
1156 { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); }
1157 // from a C string - STL probably will crash on NULL,
1158 // so we need to compensate in that case
1159 #if wxUSE_STL
1160 wxString& operator=(const wxChar *psz)
1161 { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; }
1162 #else
1163 wxString& operator=(const wxChar *psz)
1164 { return (wxString&)wxStringBase::operator=(psz); }
1165 #endif
1166
1167 #if wxUSE_UNICODE
1168 // from wxWCharBuffer
1169 wxString& operator=(const wxWCharBuffer& psz)
1170 { (void) operator=((const wchar_t *)psz); return *this; }
1171 // from C string
1172 wxString& operator=(const char* psz)
1173 { return operator=(wxString(psz)); }
1174 #else // ANSI
1175 // from another kind of C string
1176 wxString& operator=(const unsigned char* psz);
1177 #if wxUSE_WCHAR_T
1178 // from a wide string
1179 wxString& operator=(const wchar_t *pwz);
1180 #endif
1181 // from wxCharBuffer
1182 wxString& operator=(const wxCharBuffer& psz)
1183 { (void) operator=((const char *)psz); return *this; }
1184 #endif // Unicode/ANSI
1185
1186 // string concatenation
1187 // in place concatenation
1188 /*
1189 Concatenate and return the result. Note that the left to right
1190 associativity of << allows to write things like "str << str1 << str2
1191 << ..." (unlike with +=)
1192 */
1193 // string += string
1194 wxString& operator<<(const wxString& s)
1195 {
1196 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1197 wxASSERT_MSG( s.GetStringData()->IsValid(),
1198 _T("did you forget to call UngetWriteBuf()?") );
1199 #endif
1200
1201 append(s);
1202 return *this;
1203 }
1204 // string += C string
1205 wxString& operator<<(const wxChar *psz)
1206 { append(psz); return *this; }
1207 wxString& operator<<(const wxCStrData& psz)
1208 { append(psz); 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<<(wchar_t ch) { append(1, ch); return *this; }
1214
1215 // string += buffer (i.e. from wxGetString)
1216 #if wxUSE_UNICODE
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 #else // !wxUSE_UNICODE
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 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1227
1228 #if wxUSE_UNICODE
1229 // string += C string in Unicode build (with conversion)
1230 wxString& operator<<(const char *s)
1231 { return operator<<(wxString(s)); }
1232 wxString& operator+=(const char *s)
1233 { return operator+=(wxString(s)); }
1234 #endif // wxUSE_UNICODE
1235
1236 // string += C string
1237 wxString& Append(const wxString& s)
1238 {
1239 // test for empty() to share the string if possible
1240 if ( empty() )
1241 *this = s;
1242 else
1243 append(s);
1244 return *this;
1245 }
1246 wxString& Append(const wxCStrData& psz)
1247 { append(psz); return *this; }
1248 wxString& Append(const wxChar* psz)
1249 { append(psz); return *this; }
1250 // append count copies of given character
1251 wxString& Append(wxUniChar ch, size_t count = 1u)
1252 { append(count, ch); return *this; }
1253 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1254 { append(count, ch); return *this; }
1255 wxString& Append(char ch, size_t count = 1u)
1256 { append(count, ch); return *this; }
1257 wxString& Append(wchar_t ch, size_t count = 1u)
1258 { append(count, ch); return *this; }
1259 wxString& Append(const wxChar* psz, size_t nLen)
1260 { append(psz, nLen); return *this; }
1261
1262 // prepend a string, return the string itself
1263 wxString& Prepend(const wxString& str)
1264 { *this = str + *this; return *this; }
1265
1266 // non-destructive concatenation
1267 // two strings
1268 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1269 const wxString& string2);
1270 // string with a single char
1271 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1272 // char with a string
1273 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1274 // string with C string
1275 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1276 const wxChar *psz);
1277 // C string with string
1278 friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz,
1279 const wxString& string);
1280
1281 // stream-like functions
1282 // insert an int into string
1283 wxString& operator<<(int i)
1284 { return (*this) << Format(_T("%d"), i); }
1285 // insert an unsigned int into string
1286 wxString& operator<<(unsigned int ui)
1287 { return (*this) << Format(_T("%u"), ui); }
1288 // insert a long into string
1289 wxString& operator<<(long l)
1290 { return (*this) << Format(_T("%ld"), l); }
1291 // insert an unsigned long into string
1292 wxString& operator<<(unsigned long ul)
1293 { return (*this) << Format(_T("%lu"), ul); }
1294 #if defined wxLongLong_t && !defined wxLongLongIsLong
1295 // insert a long long if they exist and aren't longs
1296 wxString& operator<<(wxLongLong_t ll)
1297 {
1298 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1299 return (*this) << Format(fmt, ll);
1300 }
1301 // insert an unsigned long long
1302 wxString& operator<<(wxULongLong_t ull)
1303 {
1304 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1305 return (*this) << Format(fmt , ull);
1306 }
1307 #endif
1308 // insert a float into string
1309 wxString& operator<<(float f)
1310 { return (*this) << Format(_T("%f"), f); }
1311 // insert a double into string
1312 wxString& operator<<(double d)
1313 { return (*this) << Format(_T("%g"), d); }
1314
1315 // string comparison
1316 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1317 int Cmp(const wxChar *psz) const;
1318 int Cmp(const wxString& s) const;
1319 // same as Cmp() but not case-sensitive
1320 int CmpNoCase(const wxChar *psz) const;
1321 int CmpNoCase(const wxString& s) const;
1322 // test for the string equality, either considering case or not
1323 // (if compareWithCase then the case matters)
1324 bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const
1325 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
1326 // comparison with a single character: returns true if equal
1327 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const
1328 {
1329 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
1330 : wxToupper(GetChar(0u)) == wxToupper(c));
1331 }
1332
1333 // simple sub-string extraction
1334 // return substring starting at nFirst of length nCount (or till the end
1335 // if nCount = default value)
1336 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1337
1338 // operator version of Mid()
1339 wxString operator()(size_t start, size_t len) const
1340 { return Mid(start, len); }
1341
1342 // check if the string starts with the given prefix and return the rest
1343 // of the string in the provided pointer if it is not NULL; otherwise
1344 // return false
1345 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
1346 // check if the string ends with the given suffix and return the
1347 // beginning of the string before the suffix in the provided pointer if
1348 // it is not NULL; otherwise return false
1349 bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const;
1350
1351 // get first nCount characters
1352 wxString Left(size_t nCount) const;
1353 // get last nCount characters
1354 wxString Right(size_t nCount) const;
1355 // get all characters before the first occurance of ch
1356 // (returns the whole string if ch not found)
1357 wxString BeforeFirst(wxUniChar ch) const;
1358 // get all characters before the last occurence of ch
1359 // (returns empty string if ch not found)
1360 wxString BeforeLast(wxUniChar ch) const;
1361 // get all characters after the first occurence of ch
1362 // (returns empty string if ch not found)
1363 wxString AfterFirst(wxUniChar ch) const;
1364 // get all characters after the last occurence of ch
1365 // (returns the whole string if ch not found)
1366 wxString AfterLast(wxUniChar ch) const;
1367
1368 // for compatibility only, use more explicitly named functions above
1369 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1370 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1371
1372 // case conversion
1373 // convert to upper case in place, return the string itself
1374 wxString& MakeUpper();
1375 // convert to upper case, return the copy of the string
1376 // Here's something to remember: BC++ doesn't like returns in inlines.
1377 wxString Upper() const ;
1378 // convert to lower case in place, return the string itself
1379 wxString& MakeLower();
1380 // convert to lower case, return the copy of the string
1381 wxString Lower() const ;
1382
1383 // trimming/padding whitespace (either side) and truncating
1384 // remove spaces from left or from right (default) side
1385 wxString& Trim(bool bFromRight = true);
1386 // add nCount copies chPad in the beginning or at the end (default)
1387 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1388
1389 // searching and replacing
1390 // searching (return starting index, or -1 if not found)
1391 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1392 // searching (return starting index, or -1 if not found)
1393 int Find(const wxChar *pszSub) const; // like strstr
1394 // replace first (or all of bReplaceAll) occurences of substring with
1395 // another string, returns the number of replacements made
1396 size_t Replace(const wxChar *szOld,
1397 const wxChar *szNew,
1398 bool bReplaceAll = true);
1399
1400 // check if the string contents matches a mask containing '*' and '?'
1401 bool Matches(const wxChar *szMask) const;
1402
1403 // conversion to numbers: all functions return true only if the whole
1404 // string is a number and put the value of this number into the pointer
1405 // provided, the base is the numeric base in which the conversion should be
1406 // done and must be comprised between 2 and 36 or be 0 in which case the
1407 // standard C rules apply (leading '0' => octal, "0x" => hex)
1408 // convert to a signed integer
1409 bool ToLong(long *val, int base = 10) const;
1410 // convert to an unsigned integer
1411 bool ToULong(unsigned long *val, int base = 10) const;
1412 // convert to wxLongLong
1413 #if defined(wxLongLong_t)
1414 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1415 // convert to wxULongLong
1416 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1417 #endif // wxLongLong_t
1418 // convert to a double
1419 bool ToDouble(double *val) const;
1420
1421
1422 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1423 // formatted input/output
1424 // as sprintf(), returns the number of characters written or < 0 on error
1425 // (take 'this' into account in attribute parameter count)
1426 // int Printf(const wxChar *pszFormat, ...);
1427 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
1428 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1429 // as vprintf(), returns the number of characters written or < 0 on error
1430 int PrintfV(const wxString& format, va_list argptr);
1431
1432 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1433 // returns the string containing the result of Printf() to it
1434 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1435 WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat)
1436 #endif
1437 // the same as above, but takes a va_list
1438 static wxString FormatV(const wxString& format, va_list argptr);
1439
1440 // raw access to string memory
1441 // ensure that string has space for at least nLen characters
1442 // only works if the data of this string is not shared
1443 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1444 // minimize the string's memory
1445 // only works if the data of this string is not shared
1446 bool Shrink();
1447 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1448 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1449 //
1450 // get writable buffer of at least nLen bytes. Unget() *must* be called
1451 // a.s.a.p. to put string back in a reasonable state!
1452 wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) );
1453 // call this immediately after GetWriteBuf() has been used
1454 wxDEPRECATED( void UngetWriteBuf() );
1455 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1456 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1457
1458 // wxWidgets version 1 compatibility functions
1459
1460 // use Mid()
1461 wxString SubString(size_t from, size_t to) const
1462 { return Mid(from, (to - from + 1)); }
1463 // values for second parameter of CompareTo function
1464 enum caseCompare {exact, ignoreCase};
1465 // values for first parameter of Strip function
1466 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1467
1468 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1469 // use Printf()
1470 // (take 'this' into account in attribute parameter count)
1471 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1472 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
1473 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1474
1475 // use Cmp()
1476 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1477 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1478
1479 // use Len
1480 size_t Length() const { return length(); }
1481 // Count the number of characters
1482 int Freq(wxUniChar ch) const;
1483 // use MakeLower
1484 void LowerCase() { MakeLower(); }
1485 // use MakeUpper
1486 void UpperCase() { MakeUpper(); }
1487 // use Trim except that it doesn't change this string
1488 wxString Strip(stripType w = trailing) const;
1489
1490 // use Find (more general variants not yet supported)
1491 size_t Index(const wxChar* psz) const { return Find(psz); }
1492 size_t Index(wxUniChar ch) const { return Find(ch); }
1493 // use Truncate
1494 wxString& Remove(size_t pos) { return Truncate(pos); }
1495 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1496
1497 wxString& Remove(size_t nStart, size_t nLen)
1498 { return (wxString&)erase( nStart, nLen ); }
1499
1500 // use Find()
1501 int First( const wxUniChar ch ) const { return Find(ch); }
1502 int First( char ch ) const { return Find(ch); }
1503 int First( wchar_t ch ) const { return Find(ch); }
1504 int First( const wxChar* psz ) const { return Find(psz); }
1505 int First( const wxString &str ) const { return Find(str); }
1506 int Last( const wxUniChar ch ) const { return Find(ch, true); }
1507 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1508
1509 // use empty()
1510 bool IsNull() const { return empty(); }
1511
1512 // std::string compatibility functions
1513
1514 // take nLen chars starting at nPos
1515 wxString(const wxString& str, size_t nPos, size_t nLen)
1516 : wxStringBase(str, nPos, nLen) { }
1517 // take all characters from pStart to pEnd
1518 wxString(const void *pStart, const void *pEnd)
1519 : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { }
1520 wxString(const_iterator first, const_iterator last)
1521 : wxStringBase(first, last) { }
1522 wxString(iterator first, iterator last)
1523 : wxStringBase(first, last) { }
1524
1525 // lib.string.modifiers
1526 // append elements str[pos], ..., str[pos+n]
1527 wxString& append(const wxString& str, size_t pos, size_t n)
1528 { return (wxString&)wxStringBase::append(str, pos, n); }
1529 // append a string
1530 wxString& append(const wxString& str)
1531 { return (wxString&)wxStringBase::append(str); }
1532 wxString& append(const wxCStrData& str)
1533 { return (wxString&)wxStringBase::append(str.AsString()); }
1534 // append first n (or all if n == npos) characters of sz
1535 wxString& append(const wxChar *sz)
1536 { return (wxString&)wxStringBase::append(sz); }
1537 wxString& append(const wxChar *sz, size_t n)
1538 { return (wxString&)wxStringBase::append(sz, n); }
1539 // append n copies of ch
1540 wxString& append(size_t n, wxUniChar ch)
1541 { return (wxString&)wxStringBase::append(n, ch); }
1542 // append from first to last
1543 wxString& append(const_iterator first, const_iterator last)
1544 { return (wxString&)wxStringBase::append(first, last); }
1545
1546 // same as `this_string = str'
1547 wxString& assign(const wxString& str)
1548 { return (wxString&)wxStringBase::assign(str); }
1549 // same as ` = str[pos..pos + n]
1550 wxString& assign(const wxString& str, size_t pos, size_t n)
1551 { return (wxString&)wxStringBase::assign(str, pos, n); }
1552 // same as `= first n (or all if n == npos) characters of sz'
1553 wxString& assign(const wxChar *sz)
1554 { return (wxString&)wxStringBase::assign(sz); }
1555 wxString& assign(const wxChar *sz, size_t n)
1556 { return (wxString&)wxStringBase::assign(sz, n); }
1557 // same as `= n copies of ch'
1558 wxString& assign(size_t n, wxUniChar ch)
1559 { return (wxString&)wxStringBase::assign(n, ch); }
1560 // assign from first to last
1561 wxString& assign(const_iterator first, const_iterator last)
1562 { return (wxString&)wxStringBase::assign(first, last); }
1563
1564 // string comparison
1565 #if !defined(HAVE_STD_STRING_COMPARE)
1566 int compare(const wxStringBase& str) const;
1567 // comparison with a substring
1568 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
1569 // comparison of 2 substrings
1570 int compare(size_t nStart, size_t nLen,
1571 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
1572 // just like strcmp()
1573 int compare(const wxChar* sz) const;
1574 // substring comparison with first nCount characters of sz
1575 int compare(size_t nStart, size_t nLen,
1576 const wxChar* sz, size_t nCount = npos) const;
1577 #endif // !defined HAVE_STD_STRING_COMPARE
1578
1579 // insert another string
1580 wxString& insert(size_t nPos, const wxString& str)
1581 { return (wxString&)wxStringBase::insert(nPos, str); }
1582 // insert n chars of str starting at nStart (in str)
1583 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1584 { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); }
1585 // insert first n (or all if n == npos) characters of sz
1586 wxString& insert(size_t nPos, const wxChar *sz)
1587 { return (wxString&)wxStringBase::insert(nPos, sz); }
1588 wxString& insert(size_t nPos, const wxChar *sz, size_t n)
1589 { return (wxString&)wxStringBase::insert(nPos, sz, n); }
1590 // insert n copies of ch
1591 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
1592 { return (wxString&)wxStringBase::insert(nPos, n, ch); }
1593 iterator insert(iterator it, wxUniChar ch)
1594 { return wxStringBase::insert(it, ch); }
1595 void insert(iterator it, const_iterator first, const_iterator last)
1596 { wxStringBase::insert(it, first, last); }
1597 void insert(iterator it, size_type n, wxUniChar ch)
1598 { wxStringBase::insert(it, n, ch); }
1599
1600 // delete characters from nStart to nStart + nLen
1601 wxString& erase(size_type pos = 0, size_type n = npos)
1602 { return (wxString&)wxStringBase::erase(pos, n); }
1603 iterator erase(iterator first, iterator last)
1604 { return wxStringBase::erase(first, last); }
1605 iterator erase(iterator first)
1606 { return wxStringBase::erase(first); }
1607
1608 #ifdef wxSTRING_BASE_HASNT_CLEAR
1609 void clear() { erase(); }
1610 #endif
1611
1612 // replaces the substring of length nLen starting at nStart
1613 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz)
1614 { return (wxString&)wxStringBase::replace(nStart, nLen, sz); }
1615 // replaces the substring of length nLen starting at nStart
1616 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1617 { return (wxString&)wxStringBase::replace(nStart, nLen, str); }
1618 // replaces the substring with nCount copies of ch
1619 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
1620 { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); }
1621 // replaces a substring with another substring
1622 wxString& replace(size_t nStart, size_t nLen,
1623 const wxString& str, size_t nStart2, size_t nLen2)
1624 { return (wxString&)wxStringBase::replace(nStart, nLen, str,
1625 nStart2, nLen2); }
1626 // replaces the substring with first nCount chars of sz
1627 wxString& replace(size_t nStart, size_t nLen,
1628 const wxChar* sz, size_t nCount)
1629 { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); }
1630 wxString& replace(iterator first, iterator last, const_pointer s)
1631 { return (wxString&)wxStringBase::replace(first, last, s); }
1632 wxString& replace(iterator first, iterator last, const_pointer s,
1633 size_type n)
1634 { return (wxString&)wxStringBase::replace(first, last, s, n); }
1635 wxString& replace(iterator first, iterator last, const wxString& s)
1636 { return (wxString&)wxStringBase::replace(first, last, s); }
1637 wxString& replace(iterator first, iterator last, size_type n, wxUniChar c)
1638 { return (wxString&)wxStringBase::replace(first, last, n, c); }
1639 wxString& replace(iterator first, iterator last,
1640 const_iterator first1, const_iterator last1)
1641 { return (wxString&)wxStringBase::replace(first, last, first1, last1); }
1642
1643 // string += string
1644 wxString& operator+=(const wxString& s)
1645 { return (wxString&)wxStringBase::operator+=(s); }
1646 // string += C string
1647 wxString& operator+=(const wxChar *psz)
1648 { return (wxString&)wxStringBase::operator+=(psz); }
1649 wxString& operator+=(const wxCStrData& s)
1650 { return (wxString&)wxStringBase::operator+=(s.AsString()); }
1651 // string += char
1652 wxString& operator+=(wxUniChar ch)
1653 { return (wxString&)wxStringBase::operator+=(ch); }
1654 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
1655 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
1656 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
1657
1658 private:
1659 #if !wxUSE_STL
1660 // helpers for wxStringBuffer and wxStringBufferLength
1661 wxChar *DoGetWriteBuf(size_t nLen);
1662 void DoUngetWriteBuf();
1663 void DoUngetWriteBuf(size_t nLen);
1664
1665 friend class WXDLLIMPEXP_BASE wxStringBuffer;
1666 friend class WXDLLIMPEXP_BASE wxStringBufferLength;
1667 #endif
1668
1669 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1670 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
1671 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
1672 #endif
1673 };
1674
1675 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1676 #pragma warning (default:4275)
1677 #endif
1678
1679 // notice that even though for many compilers the friend declarations above are
1680 // enough, from the point of view of C++ standard we must have the declarations
1681 // here as friend ones are not injected in the enclosing namespace and without
1682 // them the code fails to compile with conforming compilers such as xlC or g++4
1683 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
1684 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
1685 wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
1686
1687 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1688 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1689
1690 inline wxString operator+(const wxString& string, wxUniCharRef ch)
1691 { return string + (wxUniChar)ch; }
1692 inline wxString operator+(const wxString& string, char ch)
1693 { return string + wxUniChar(ch); }
1694 inline wxString operator+(const wxString& string, wchar_t ch)
1695 { return string + wxUniChar(ch); }
1696 inline wxString operator+(wxUniCharRef ch, const wxString& string)
1697 { return (wxUniChar)ch + string; }
1698 inline wxString operator+(char ch, const wxString& string)
1699 { return wxUniChar(ch) + string; }
1700 inline wxString operator+(wchar_t ch, const wxString& string)
1701 { return wxUniChar(ch) + string; }
1702
1703
1704 #if wxUSE_STL
1705 // return an empty wxString (not very useful with wxUSE_STL == 1)
1706 inline const wxString wxGetEmptyString() { return wxString(); }
1707 #else // !wxUSE_STL
1708 // return an empty wxString (more efficient than wxString() here)
1709 inline const wxString& wxGetEmptyString()
1710 {
1711 return *(wxString *)&wxEmptyString;
1712 }
1713 #endif // wxUSE_STL/!wxUSE_STL
1714
1715 // ----------------------------------------------------------------------------
1716 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1717 // ----------------------------------------------------------------------------
1718
1719 #if wxUSE_STL
1720
1721 class WXDLLIMPEXP_BASE wxStringBuffer
1722 {
1723 public:
1724 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1725 : m_str(str), m_buf(lenWanted)
1726 { }
1727
1728 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
1729
1730 operator wxChar*() { return m_buf.data(); }
1731
1732 private:
1733 wxString& m_str;
1734 #if wxUSE_UNICODE
1735 wxWCharBuffer m_buf;
1736 #else
1737 wxCharBuffer m_buf;
1738 #endif
1739
1740 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1741 };
1742
1743 class WXDLLIMPEXP_BASE wxStringBufferLength
1744 {
1745 public:
1746 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1747 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
1748 { }
1749
1750 ~wxStringBufferLength()
1751 {
1752 wxASSERT(m_lenSet);
1753 m_str.assign(m_buf.data(), m_len);
1754 }
1755
1756 operator wxChar*() { return m_buf.data(); }
1757 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1758
1759 private:
1760 wxString& m_str;
1761 #if wxUSE_UNICODE
1762 wxWCharBuffer m_buf;
1763 #else
1764 wxCharBuffer m_buf;
1765 #endif
1766 size_t m_len;
1767 bool m_lenSet;
1768
1769 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1770 };
1771
1772 #else // if !wxUSE_STL
1773
1774 class WXDLLIMPEXP_BASE wxStringBuffer
1775 {
1776 public:
1777 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1778 : m_str(str), m_buf(NULL)
1779 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
1780
1781 ~wxStringBuffer() { m_str.DoUngetWriteBuf(); }
1782
1783 operator wxChar*() const { return m_buf; }
1784
1785 private:
1786 wxString& m_str;
1787 wxChar *m_buf;
1788
1789 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1790 };
1791
1792 class WXDLLIMPEXP_BASE wxStringBufferLength
1793 {
1794 public:
1795 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1796 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
1797 {
1798 m_buf = m_str.DoGetWriteBuf(lenWanted);
1799 wxASSERT(m_buf != NULL);
1800 }
1801
1802 ~wxStringBufferLength()
1803 {
1804 wxASSERT(m_lenSet);
1805 m_str.DoUngetWriteBuf(m_len);
1806 }
1807
1808 operator wxChar*() const { return m_buf; }
1809 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1810
1811 private:
1812 wxString& m_str;
1813 wxChar *m_buf;
1814 size_t m_len;
1815 bool m_lenSet;
1816
1817 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1818 };
1819
1820 #endif // !wxUSE_STL
1821
1822 // ---------------------------------------------------------------------------
1823 // wxString comparison functions: operator versions are always case sensitive
1824 // ---------------------------------------------------------------------------
1825
1826 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1827 // are used and defining them also for wxString would only result in
1828 // compilation ambiguities when comparing std::string and wxString
1829 #if !wxUSE_STL
1830
1831 inline bool operator==(const wxString& s1, const wxString& s2)
1832 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
1833 inline bool operator==(const wxString& s1, const wxChar * s2)
1834 { return s1.Cmp(s2) == 0; }
1835 inline bool operator==(const wxChar * s1, const wxString& s2)
1836 { return s2.Cmp(s1) == 0; }
1837 inline bool operator!=(const wxString& s1, const wxString& s2)
1838 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
1839 inline bool operator!=(const wxString& s1, const wxChar * s2)
1840 { return s1.Cmp(s2) != 0; }
1841 inline bool operator!=(const wxChar * s1, const wxString& s2)
1842 { return s2.Cmp(s1) != 0; }
1843 inline bool operator< (const wxString& s1, const wxString& s2)
1844 { return s1.Cmp(s2) < 0; }
1845 inline bool operator< (const wxString& s1, const wxChar * s2)
1846 { return s1.Cmp(s2) < 0; }
1847 inline bool operator< (const wxChar * s1, const wxString& s2)
1848 { return s2.Cmp(s1) > 0; }
1849 inline bool operator> (const wxString& s1, const wxString& s2)
1850 { return s1.Cmp(s2) > 0; }
1851 inline bool operator> (const wxString& s1, const wxChar * s2)
1852 { return s1.Cmp(s2) > 0; }
1853 inline bool operator> (const wxChar * s1, const wxString& s2)
1854 { return s2.Cmp(s1) < 0; }
1855 inline bool operator<=(const wxString& s1, const wxString& s2)
1856 { return s1.Cmp(s2) <= 0; }
1857 inline bool operator<=(const wxString& s1, const wxChar * s2)
1858 { return s1.Cmp(s2) <= 0; }
1859 inline bool operator<=(const wxChar * s1, const wxString& s2)
1860 { return s2.Cmp(s1) >= 0; }
1861 inline bool operator>=(const wxString& s1, const wxString& s2)
1862 { return s1.Cmp(s2) >= 0; }
1863 inline bool operator>=(const wxString& s1, const wxChar * s2)
1864 { return s1.Cmp(s2) >= 0; }
1865 inline bool operator>=(const wxChar * s1, const wxString& s2)
1866 { return s2.Cmp(s1) <= 0; }
1867
1868 #if wxUSE_UNICODE
1869 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
1870 { return (s1.Cmp((const wchar_t *)s2) == 0); }
1871 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
1872 { return (s2.Cmp((const wchar_t *)s1) == 0); }
1873 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
1874 { return (s1.Cmp((const wchar_t *)s2) != 0); }
1875 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
1876 { return (s2.Cmp((const wchar_t *)s1) != 0); }
1877 #else // !wxUSE_UNICODE
1878 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
1879 { return (s1.Cmp((const char *)s2) == 0); }
1880 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
1881 { return (s2.Cmp((const char *)s1) == 0); }
1882 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
1883 { return (s1.Cmp((const char *)s2) != 0); }
1884 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
1885 { return (s2.Cmp((const char *)s1) != 0); }
1886 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1887
1888 #if wxUSE_UNICODE
1889 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
1890 { return string + (const wchar_t *)buf; }
1891 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
1892 { return (const wchar_t *)buf + string; }
1893 #else // !wxUSE_UNICODE
1894 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
1895 { return string + (const char *)buf; }
1896 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
1897 { return (const char *)buf + string; }
1898 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1899
1900 #endif // !wxUSE_STL
1901
1902 // comparison with char (those are not defined by std::[w]string and so should
1903 // be always available)
1904 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
1905 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
1906 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
1907 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
1908 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
1909 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
1910 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
1911 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
1912 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
1913 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
1914 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
1915 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
1916 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
1917 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
1918 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
1919 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
1920 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
1921 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
1922
1923 // comparison with C string in Unicode build
1924 #if wxUSE_UNICODE
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 inline bool operator<=(const wxString& s1, const char* s2)
1942 { return s1 <= wxString(s2); }
1943 inline bool operator<=(const char* s1, const wxString& s2)
1944 { return wxString(s1) <= s2; }
1945 inline bool operator>=(const wxString& s1, const char* s2)
1946 { return s1 >= wxString(s2); }
1947 inline bool operator>=(const char* s1, const wxString& s2)
1948 { return wxString(s1) >= s2; }
1949 #endif // wxUSE_UNICODE
1950
1951 // ---------------------------------------------------------------------------
1952 // Implementation only from here until the end of file
1953 // ---------------------------------------------------------------------------
1954
1955 // don't pollute the library user's name space
1956 #undef wxASSERT_VALID_INDEX
1957
1958 #if wxUSE_STD_IOSTREAM
1959
1960 #include "wx/iosfwrap.h"
1961
1962 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
1963 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
1964
1965 #endif // wxSTD_STRING_COMPATIBILITY
1966
1967 // ---------------------------------------------------------------------------
1968 // wxCStrData implementation
1969 // ---------------------------------------------------------------------------
1970
1971 inline wxCStrData::wxCStrData(char *buf)
1972 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
1973 inline wxCStrData::wxCStrData(wchar_t *buf)
1974 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
1975
1976 inline wxCStrData::~wxCStrData()
1977 {
1978 if ( m_owned )
1979 delete m_str;
1980 }
1981
1982 #if wxUSE_UNICODE
1983 inline const wchar_t* wxCStrData::AsWChar() const
1984 #else
1985 inline const char* wxCStrData::AsChar() const
1986 #endif
1987 {
1988 if ( m_offset == 0 )
1989 return m_str->wx_str(); // FIXME
1990 else
1991 return (const wxChar*)(m_str->begin() + m_offset);
1992 }
1993
1994 inline wxString wxCStrData::AsString() const
1995 {
1996 if ( m_offset == 0 )
1997 return *m_str;
1998 else
1999 return m_str->Mid(m_offset);
2000 }
2001
2002 inline wxCStrData::operator wxString() const { return AsString(); }
2003
2004 inline wxUniChar wxCStrData::operator*() const
2005 {
2006 if ( m_str->empty() )
2007 return wxUniChar(_T('\0'));
2008 else
2009 return (*m_str)[m_offset];
2010 }
2011
2012 inline wxUniChar wxCStrData::operator[](size_t n) const
2013 {
2014 return m_str->at(m_offset + n);
2015 }
2016
2017 // ----------------------------------------------------------------------------
2018 // implementation of wxString inline methods using wxCStrData
2019 // ----------------------------------------------------------------------------
2020
2021 inline wxString& wxString::operator=(const wxCStrData& cstr)
2022 {
2023 return *this = cstr.AsString();
2024 }
2025
2026 // ----------------------------------------------------------------------------
2027 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2028 // ----------------------------------------------------------------------------
2029
2030 #if wxUSE_UNICODE
2031
2032 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
2033 : m_str(wxStrdupW(cstr))
2034 {
2035 }
2036
2037 #else // !wxUSE_UNICODE
2038
2039 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
2040 : m_str(wxStrdupA(cstr))
2041 {
2042 }
2043
2044 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2045
2046 #endif // _WX_WXSTRINGH__