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