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