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