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