]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
removed just added UnsetVirtualSize(), it's not needed finally
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma interface "string.h"
23 #endif
24
25 // ----------------------------------------------------------------------------
26 // headers
27 // ----------------------------------------------------------------------------
28
29 #include "wx/defs.h" // everybody should include this
30
31 #if defined(__WXMAC__) || defined(__VISAGECPP__)
32 #include <ctype.h>
33 #endif
34
35 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
36 // problem in VACPP V4 with including stdlib.h multiple times
37 // strconv includes it anyway
38 # include <stdio.h>
39 # include <string.h>
40 # include <stdarg.h>
41 # include <limits.h>
42 #else
43 # include <string.h>
44 # include <stdio.h>
45 # include <stdarg.h>
46 # include <limits.h>
47 # include <stdlib.h>
48 #endif
49
50 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
51 #include <strings.h> // for strcasecmp()
52 #endif // HAVE_STRCASECMP_IN_STRINGS_H
53
54 #ifdef __WXPALMOS__
55 #include <StringMgr.h>
56 #endif
57
58 #include "wx/wxchar.h" // for wxChar
59 #include "wx/buffer.h" // for wxCharBuffer
60 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
61
62 class WXDLLIMPEXP_BASE wxString;
63
64 // ---------------------------------------------------------------------------
65 // macros
66 // ---------------------------------------------------------------------------
67
68 // casts [unfortunately!] needed to call some broken functions which require
69 // "char *" instead of "const char *"
70 #define WXSTRINGCAST (wxChar *)(const wxChar *)
71 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
72 #define wxMBSTRINGCAST (char *)(const char *)
73 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
74
75 // implementation only
76 #define wxASSERT_VALID_INDEX(i) \
77 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
78
79 // ----------------------------------------------------------------------------
80 // constants
81 // ----------------------------------------------------------------------------
82
83 // maximum possible length for a string means "take all string" everywhere
84 #define wxSTRING_MAXLEN wxStringBase::npos
85
86 // ----------------------------------------------------------------------------
87 // global data
88 // ----------------------------------------------------------------------------
89
90 // global pointer to empty string
91 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
92
93 // ---------------------------------------------------------------------------
94 // global functions complementing standard C string library replacements for
95 // strlen() and portable strcasecmp()
96 //---------------------------------------------------------------------------
97
98 // Use wxXXX() functions from wxchar.h instead! These functions are for
99 // backwards compatibility only.
100
101 // checks whether the passed in pointer is NULL and if the string is empty
102 inline bool IsEmpty(const char *p) { return (!p || !*p); }
103
104 // safe version of strlen() (returns 0 if passed NULL pointer)
105 inline size_t Strlen(const char *psz)
106 { return psz ? strlen(psz) : 0; }
107
108 // portable strcasecmp/_stricmp
109 inline int Stricmp(const char *psz1, const char *psz2)
110 {
111 #if defined(__VISUALC__) && defined(__WXWINCE__)
112 register char c1, c2;
113 do {
114 c1 = tolower(*psz1++);
115 c2 = tolower(*psz2++);
116 } while ( c1 && (c1 == c2) );
117
118 return c1 - c2;
119 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
120 return _stricmp(psz1, psz2);
121 #elif defined(__SC__)
122 return _stricmp(psz1, psz2);
123 #elif defined(__SALFORDC__)
124 return stricmp(psz1, psz2);
125 #elif defined(__BORLANDC__)
126 return stricmp(psz1, psz2);
127 #elif defined(__WATCOMC__)
128 return stricmp(psz1, psz2);
129 #elif defined(__DJGPP__)
130 return stricmp(psz1, psz2);
131 #elif defined(__EMX__)
132 return stricmp(psz1, psz2);
133 #elif defined(__WXPM__)
134 return stricmp(psz1, psz2);
135 #elif defined(__WXPALMOS__) || \
136 defined(HAVE_STRCASECMP_IN_STRING_H) || \
137 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
138 defined(__GNUWIN32__)
139 return strcasecmp(psz1, psz2);
140 #elif defined(__MWERKS__) && !defined(__INTEL__)
141 register char c1, c2;
142 do {
143 c1 = tolower(*psz1++);
144 c2 = tolower(*psz2++);
145 } while ( c1 && (c1 == c2) );
146
147 return c1 - c2;
148 #else
149 // almost all compilers/libraries provide this function (unfortunately under
150 // different names), that's why we don't implement our own which will surely
151 // be more efficient than this code (uncomment to use):
152 /*
153 register char c1, c2;
154 do {
155 c1 = tolower(*psz1++);
156 c2 = tolower(*psz2++);
157 } while ( c1 && (c1 == c2) );
158
159 return c1 - c2;
160 */
161
162 #error "Please define string case-insensitive compare for your OS/compiler"
163 #endif // OS/compiler
164 }
165
166 #if wxUSE_STL
167
168 #include "wx/beforestd.h"
169 #include <string>
170 #include "wx/afterstd.h"
171
172 #if wxUSE_UNICODE
173 #ifdef HAVE_STD_WSTRING
174 typedef std::wstring wxStringBase;
175 #else
176 typedef std::basic_string<wxChar> wxStringBase;
177 #endif
178 #else
179 typedef std::string wxStringBase;
180 #endif
181
182 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
183 (defined(_MSC_VER) && (_MSC_VER <= 1200))
184 #define wxSTRING_BASE_HASNT_CLEAR
185 #endif
186
187 #else // if !wxUSE_STL
188
189 #ifndef HAVE_STD_STRING_COMPARE
190 #define HAVE_STD_STRING_COMPARE
191 #endif
192
193 // ---------------------------------------------------------------------------
194 // string data prepended with some housekeeping info (used by wxString class),
195 // is never used directly (but had to be put here to allow inlining)
196 // ---------------------------------------------------------------------------
197
198 struct WXDLLIMPEXP_BASE wxStringData
199 {
200 int nRefs; // reference count
201 size_t nDataLength, // actual string length
202 nAllocLength; // allocated memory size
203
204 // mimics declaration 'wxChar data[nAllocLength]'
205 wxChar* data() const { return (wxChar*)(this + 1); }
206
207 // empty string has a special ref count so it's never deleted
208 bool IsEmpty() const { return (nRefs == -1); }
209 bool IsShared() const { return (nRefs > 1); }
210
211 // lock/unlock
212 void Lock() { if ( !IsEmpty() ) nRefs++; }
213
214 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
215 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
216 __forceinline
217 #endif
218 // VC++ free must take place in same DLL as allocation when using non dll
219 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
220 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
221 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
222 // we must not inline deallocation since allocation is not inlined
223 void Free();
224 #else
225 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
226 #endif
227
228 // if we had taken control over string memory (GetWriteBuf), it's
229 // intentionally put in invalid state
230 void Validate(bool b) { nRefs = (b ? 1 : 0); }
231 bool IsValid() const { return (nRefs != 0); }
232 };
233
234 class WXDLLIMPEXP_BASE wxStringBase
235 {
236 #if !wxUSE_STL
237 friend class WXDLLIMPEXP_BASE wxArrayString;
238 #endif
239 public :
240 // an 'invalid' value for string index, moved to this place due to a CW bug
241 static const size_t npos;
242 protected:
243 // points to data preceded by wxStringData structure with ref count info
244 wxChar *m_pchData;
245
246 // accessor to string data
247 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
248
249 // string (re)initialization functions
250 // initializes the string to the empty value (must be called only from
251 // ctors, use Reinit() otherwise)
252 void Init() { m_pchData = (wxChar *)wxEmptyString; }
253 // initializaes the string with (a part of) C-string
254 void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = npos);
255 // as Init, but also frees old data
256 void Reinit() { GetStringData()->Unlock(); Init(); }
257
258 // memory allocation
259 // allocates memory for string of length nLen
260 bool AllocBuffer(size_t nLen);
261 // copies data to another string
262 bool AllocCopy(wxString&, int, int) const;
263 // effectively copies data to string
264 bool AssignCopy(size_t, const wxChar *);
265
266 // append a (sub)string
267 bool ConcatSelf(size_t nLen, const wxChar *src, size_t nMaxLen);
268 bool ConcatSelf(size_t nLen, const wxChar *src)
269 { return ConcatSelf(nLen, src, nLen); }
270
271 // functions called before writing to the string: they copy it if there
272 // are other references to our data (should be the only owner when writing)
273 bool CopyBeforeWrite();
274 bool AllocBeforeWrite(size_t);
275
276 // compatibility with wxString
277 bool Alloc(size_t nLen);
278 public:
279 // standard types
280 typedef wxChar value_type;
281 typedef wxChar char_type;
282 typedef size_t size_type;
283 typedef value_type& reference;
284 typedef const value_type& const_reference;
285 typedef value_type* pointer;
286 typedef const value_type* const_pointer;
287 typedef value_type *iterator;
288 typedef const value_type *const_iterator;
289
290 // constructors and destructor
291 // ctor for an empty string
292 wxStringBase() { Init(); }
293 // copy ctor
294 wxStringBase(const wxStringBase& stringSrc)
295 {
296 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
297 _T("did you forget to call UngetWriteBuf()?") );
298
299 if ( stringSrc.empty() ) {
300 // nothing to do for an empty string
301 Init();
302 }
303 else {
304 m_pchData = stringSrc.m_pchData; // share same data
305 GetStringData()->Lock(); // => one more copy
306 }
307 }
308 // string containing nRepeat copies of ch
309 wxStringBase(size_type nRepeat, wxChar ch);
310 // ctor takes first nLength characters from C string
311 // (default value of npos means take all the string)
312 wxStringBase(const wxChar *psz)
313 { InitWith(psz, 0, npos); }
314 wxStringBase(const wxChar *psz, size_t nLength)
315 { InitWith(psz, 0, nLength); }
316 wxStringBase(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = npos)
317 { InitWith(psz, 0, nLength); }
318 // take nLen chars starting at nPos
319 wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen)
320 {
321 wxASSERT_MSG( str.GetStringData()->IsValid(),
322 _T("did you forget to call UngetWriteBuf()?") );
323 Init();
324 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
325 InitWith(str.c_str(), nPos, nLen);
326 }
327 // take all characters from pStart to pEnd
328 wxStringBase(const void *pStart, const void *pEnd);
329
330 // dtor is not virtual, this class must not be inherited from!
331 ~wxStringBase()
332 {
333 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
334 //RN - according to the above VC++ does indeed inline this,
335 //even though it spits out two warnings
336 #pragma warning (disable:4714)
337 #endif
338
339 GetStringData()->Unlock();
340 }
341
342 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
343 //re-enable inlining warning
344 #pragma warning (default:4714)
345 #endif
346 // overloaded assignment
347 // from another wxString
348 wxStringBase& operator=(const wxStringBase& stringSrc);
349 // from a character
350 wxStringBase& operator=(wxChar ch);
351 // from a C string
352 wxStringBase& operator=(const wxChar *psz);
353
354 // return the length of the string
355 size_type size() const { return GetStringData()->nDataLength; }
356 // return the length of the string
357 size_type length() const { return size(); }
358 // return the maximum size of the string
359 size_type max_size() const { return wxSTRING_MAXLEN; }
360 // resize the string, filling the space with c if c != 0
361 void resize(size_t nSize, wxChar ch = wxT('\0'));
362 // delete the contents of the string
363 void clear() { erase(0, npos); }
364 // returns true if the string is empty
365 bool empty() const { return size() == 0; }
366 // inform string about planned change in size
367 void reserve(size_t sz) { Alloc(sz); }
368 size_type capacity() const { return GetStringData()->nAllocLength; }
369
370 // lib.string.access
371 // return the character at position n
372 value_type at(size_type n) const
373 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
374 // returns the writable character at position n
375 reference at(size_type n)
376 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
377
378 // lib.string.modifiers
379 // append elements str[pos], ..., str[pos+n]
380 wxStringBase& append(const wxStringBase& str, size_t pos, size_t n)
381 {
382 wxASSERT(pos <= str.length());
383 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
384 return *this;
385 }
386 // append a string
387 wxStringBase& append(const wxStringBase& str)
388 { ConcatSelf(str.length(), str.c_str()); return *this; }
389 // append first n (or all if n == npos) characters of sz
390 wxStringBase& append(const wxChar *sz)
391 { ConcatSelf(wxStrlen(sz), sz); return *this; }
392 wxStringBase& append(const wxChar *sz, size_t n)
393 { ConcatSelf(n, sz); return *this; }
394 // append n copies of ch
395 wxStringBase& append(size_t n, wxChar ch);
396 // append from first to last
397 wxStringBase& append(const_iterator first, const_iterator last)
398 { ConcatSelf(last - first, first); return *this; }
399
400 // same as `this_string = str'
401 wxStringBase& assign(const wxStringBase& str)
402 { return *this = str; }
403 // same as ` = str[pos..pos + n]
404 wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n)
405 { clear(); return append(str, pos, n); }
406 // same as `= first n (or all if n == npos) characters of sz'
407 wxStringBase& assign(const wxChar *sz)
408 { clear(); return append(sz, wxStrlen(sz)); }
409 wxStringBase& assign(const wxChar *sz, size_t n)
410 { clear(); return append(sz, n); }
411 // same as `= n copies of ch'
412 wxStringBase& assign(size_t n, wxChar ch)
413 { clear(); return append(n, ch); }
414 // assign from first to last
415 wxStringBase& assign(const_iterator first, const_iterator last)
416 { clear(); return append(first, last); }
417
418 // first valid index position
419 const_iterator begin() const { return m_pchData; }
420 // position one after the last valid one
421 const_iterator end() const { return m_pchData + length(); }
422
423 // first valid index position
424 iterator begin();
425 // position one after the last valid one
426 iterator end();
427
428 // insert another string
429 wxStringBase& insert(size_t nPos, const wxStringBase& str)
430 {
431 wxASSERT( str.GetStringData()->IsValid() );
432 return insert(nPos, str.c_str(), str.length());
433 }
434 // insert n chars of str starting at nStart (in str)
435 wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n)
436 {
437 wxASSERT( str.GetStringData()->IsValid() );
438 wxASSERT( nStart < str.length() );
439 size_t strLen = str.length() - nStart;
440 n = strLen < n ? strLen : n;
441 return insert(nPos, str.c_str() + nStart, n);
442 }
443 // insert first n (or all if n == npos) characters of sz
444 wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos);
445 // insert n copies of ch
446 wxStringBase& insert(size_t nPos, size_t n, wxChar ch)
447 { return insert(nPos, wxStringBase(n, ch)); }
448 iterator insert(iterator it, wxChar ch)
449 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
450 void insert(iterator it, const_iterator first, const_iterator last)
451 { insert(it - begin(), first, last - first); }
452 void insert(iterator it, size_type n, wxChar ch)
453 { insert(it - begin(), n, ch); }
454
455 // delete characters from nStart to nStart + nLen
456 wxStringBase& erase(size_type pos = 0, size_type n = npos);
457 iterator erase(iterator first, iterator last)
458 {
459 size_t idx = first - begin();
460 erase(idx, last - first);
461 return begin() + idx;
462 }
463 iterator erase(iterator first);
464
465 // explicit conversion to C string (use this with printf()!)
466 const wxChar* c_str() const { return m_pchData; }
467 const wxChar* data() const { return m_pchData; }
468
469 // replaces the substring of length nLen starting at nStart
470 wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz);
471 // replaces the substring of length nLen starting at nStart
472 wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str)
473 { return replace(nStart, nLen, str.c_str()); }
474 // replaces the substring with nCount copies of ch
475 wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch);
476 // replaces a substring with another substring
477 wxStringBase& replace(size_t nStart, size_t nLen,
478 const wxStringBase& str, size_t nStart2, size_t nLen2);
479 // replaces the substring with first nCount chars of sz
480 wxStringBase& replace(size_t nStart, size_t nLen,
481 const wxChar* sz, size_t nCount);
482 wxStringBase& replace(iterator first, iterator last, const_pointer s)
483 { return replace(first - begin(), last - first, s); }
484 wxStringBase& replace(iterator first, iterator last, const_pointer s,
485 size_type n)
486 { return replace(first - begin(), last - first, s, n); }
487 wxStringBase& replace(iterator first, iterator last, const wxStringBase& s)
488 { return replace(first - begin(), last - first, s); }
489 wxStringBase& replace(iterator first, iterator last, size_type n, wxChar c)
490 { return replace(first - begin(), last - first, n, c); }
491 wxStringBase& replace(iterator first, iterator last,
492 const_iterator first1, const_iterator last1)
493 { return replace(first - begin(), last - first, first1, last1 - first1); }
494
495 // swap two strings
496 void swap(wxStringBase& str);
497
498 // All find() functions take the nStart argument which specifies the
499 // position to start the search on, the default value is 0. All functions
500 // return npos if there were no match.
501
502 // find a substring
503 size_t find(const wxStringBase& str, size_t nStart = 0) const;
504
505 // VC++ 1.5 can't cope with this syntax.
506 #if !defined(__VISUALC__) || defined(__WIN32__)
507 // find first n characters of sz
508 size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;
509 #endif // VC++ 1.5
510
511 // find the first occurence of character ch after nStart
512 size_t find(wxChar ch, size_t nStart = 0) const;
513
514 // rfind() family is exactly like find() but works right to left
515
516 // as find, but from the end
517 size_t rfind(const wxStringBase& str, size_t nStart = npos) const;
518
519 // VC++ 1.5 can't cope with this syntax.
520 // as find, but from the end
521 size_t rfind(const wxChar* sz, size_t nStart = npos,
522 size_t n = npos) const;
523 // as find, but from the end
524 size_t rfind(wxChar ch, size_t nStart = npos) const;
525
526 // find first/last occurence of any character in the set
527
528 // as strpbrk() but starts at nStart, returns npos if not found
529 size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const
530 { return find_first_of(str.c_str(), nStart); }
531 // same as above
532 size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
533 size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const;
534 // same as find(char, size_t)
535 size_t find_first_of(wxChar c, size_t nStart = 0) const
536 { return find(c, nStart); }
537 // find the last (starting from nStart) char from str in this string
538 size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const
539 { return find_last_of(str.c_str(), nStart); }
540 // same as above
541 size_t find_last_of (const wxChar* sz, size_t nStart = npos) const;
542 size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const;
543 // same as above
544 size_t find_last_of(wxChar c, size_t nStart = npos) const
545 { return rfind(c, nStart); }
546
547 // find first/last occurence of any character not in the set
548
549 // as strspn() (starting from nStart), returns npos on failure
550 size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const
551 { return find_first_not_of(str.c_str(), nStart); }
552 // same as above
553 size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const;
554 size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const;
555 // same as above
556 size_t find_first_not_of(wxChar ch, size_t nStart = 0) const;
557 // as strcspn()
558 size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const
559 { return find_last_not_of(str.c_str(), nStart); }
560 // same as above
561 size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const;
562 size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const;
563 // same as above
564 size_t find_last_not_of(wxChar ch, size_t nStart = npos) const;
565
566 // All compare functions return -1, 0 or 1 if the [sub]string is less,
567 // equal or greater than the compare() argument.
568
569 // comparison with another string
570 int compare(const wxStringBase& str) const;
571 // comparison with a substring
572 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
573 // comparison of 2 substrings
574 int compare(size_t nStart, size_t nLen,
575 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
576 // comparison with a c string
577 int compare(const wxChar* sz) const;
578 // substring comparison with first nCount characters of sz
579 int compare(size_t nStart, size_t nLen,
580 const wxChar* sz, size_t nCount = npos) const;
581
582 size_type copy(wxChar* s, size_type n, size_type pos = 0);
583
584 // substring extraction
585 wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const;
586
587 // string += string
588 wxStringBase& operator+=(const wxStringBase& s) { return append(s); }
589 // string += C string
590 wxStringBase& operator+=(const wxChar *psz) { return append(psz); }
591 // string += char
592 wxStringBase& operator+=(wxChar ch) { return append(1, ch); }
593 };
594
595 #endif // !wxUSE_STL
596
597 // ----------------------------------------------------------------------------
598 // wxString: string class trying to be compatible with std::string, MFC
599 // CString and wxWindows 1.x wxString all at once
600 // ---------------------------------------------------------------------------
601
602 class WXDLLIMPEXP_BASE wxString : public wxStringBase
603 {
604 #if !wxUSE_STL
605 friend class WXDLLIMPEXP_BASE wxArrayString;
606 #endif
607
608 // NB: special care was taken in arranging the member functions in such order
609 // that all inline functions can be effectively inlined, verify that all
610 // performace critical functions are still inlined if you change order!
611 private:
612 // if we hadn't made these operators private, it would be possible to
613 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
614 // converted to char in C and we do have operator=(char)
615 //
616 // NB: we don't need other versions (short/long and unsigned) as attempt
617 // to assign another numeric type to wxString will now result in
618 // ambiguity between operator=(char) and operator=(int)
619 wxString& operator=(int);
620
621 // these methods are not implemented - there is _no_ conversion from int to
622 // string, you're doing something wrong if the compiler wants to call it!
623 //
624 // try `s << i' or `s.Printf("%d", i)' instead
625 wxString(int);
626
627 public:
628 // constructors and destructor
629 // ctor for an empty string
630 wxString() : wxStringBase() { }
631 // copy ctor
632 wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { }
633 wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { }
634 // string containing nRepeat copies of ch
635 wxString(wxChar ch, size_t nRepeat = 1)
636 : wxStringBase(nRepeat, ch) { }
637 wxString(size_t nRepeat, wxChar ch)
638 : wxStringBase(nRepeat, ch) { }
639 // ctor takes first nLength characters from C string
640 // (default value of npos means take all the string)
641 wxString(const wxChar *psz)
642 : wxStringBase(psz ? psz : wxT("")) { }
643 wxString(const wxChar *psz, size_t nLength)
644 : wxStringBase(psz, nLength) { }
645 wxString(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = npos)
646 : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { }
647
648 #if wxUSE_UNICODE
649 // from multibyte string
650 wxString(const char *psz, wxMBConv& conv, size_t nLength = npos);
651 // from wxWCharBuffer (i.e. return from wxGetString)
652 wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { }
653 #else // ANSI
654 // from C string (for compilers using unsigned char)
655 wxString(const unsigned char* psz, size_t nLength = npos)
656 : wxStringBase((const char*)psz, nLength) { }
657
658 #if wxUSE_WCHAR_T
659 // from wide (Unicode) string
660 wxString(const wchar_t *pwz, wxMBConv& conv = wxConvLibc, size_t nLength = npos);
661 #endif // !wxUSE_WCHAR_T
662
663 // from wxCharBuffer
664 wxString(const wxCharBuffer& psz)
665 : wxStringBase(psz) { }
666 #endif // Unicode/ANSI
667
668 // generic attributes & operations
669 // as standard strlen()
670 size_t Len() const { return length(); }
671 // string contains any characters?
672 bool IsEmpty() const { return empty(); }
673 // empty string is "false", so !str will return true
674 bool operator!() const { return IsEmpty(); }
675 // truncate the string to given length
676 wxString& Truncate(size_t uiLen);
677 // empty string contents
678 void Empty()
679 {
680 Truncate(0);
681
682 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
683 }
684 // empty the string and free memory
685 void Clear()
686 {
687 wxString tmp(wxEmptyString);
688 swap(tmp);
689 }
690
691 // contents test
692 // Is an ascii value
693 bool IsAscii() const;
694 // Is a number
695 bool IsNumber() const;
696 // Is a word
697 bool IsWord() const;
698
699 // data access (all indexes are 0 based)
700 // read access
701 wxChar GetChar(size_t n) const
702 { return at(n); }
703 // read/write access
704 wxChar& GetWritableChar(size_t n)
705 { return at(n); }
706 // write access
707 void SetChar(size_t n, wxChar ch)
708 { at(n) = ch; }
709
710 // get last character
711 wxChar Last() const
712 {
713 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
714
715 return at(length() - 1);
716 }
717
718 // get writable last character
719 wxChar& Last()
720 {
721 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
722 return at(length() - 1);
723 }
724
725 /*
726 Note that we we must define all of the overloads below to avoid
727 ambiguity when using str[0]. Also note that we don't need const
728 version of operatorp[] at all as indexed access to const string
729 is provided by implicit conversion to "const wxChar *" below.
730 */
731
732 // operator versions of GetWriteableChar()
733 wxChar& operator[](int n)
734 { return wxStringBase::at(n); }
735 wxChar& operator[](size_type n)
736 { return wxStringBase::at(n); }
737 #ifndef wxSIZE_T_IS_UINT
738 wxChar& operator[](unsigned int n)
739 { return wxStringBase::at(n); }
740 #endif // size_t != unsigned int
741
742 // implicit conversion to C string
743 operator const wxChar*() const { return c_str(); }
744
745 // identical to c_str(), for wxWin 1.6x compatibility
746 const wxChar* wx_str() const { return c_str(); }
747 // identical to c_str(), for MFC compatibility
748 const wxChar* GetData() const { return c_str(); }
749
750 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
751 // converting numbers or strings which are certain not to contain special
752 // chars (typically system functions, X atoms, environment variables etc.)
753 //
754 // the behaviour of these functions with the strings containing anything
755 // else than 7 bit ASCII characters is undefined, use at your own risk.
756 #if wxUSE_UNICODE
757 static wxString FromAscii(const char *ascii); // string
758 static wxString FromAscii(const char ascii); // char
759 const wxCharBuffer ToAscii() const;
760 #else // ANSI
761 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
762 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
763 const char *ToAscii() const { return c_str(); }
764 #endif // Unicode/!Unicode
765
766 // conversions with (possible) format conversions: have to return a
767 // buffer with temporary data
768 //
769 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
770 // return an ANSI (multibyte) string, wc_str() to return a wide string and
771 // fn_str() to return a string which should be used with the OS APIs
772 // accepting the file names. The return value is always the same, but the
773 // type differs because a function may either return pointer to the buffer
774 // directly or have to use intermediate buffer for translation.
775 #if wxUSE_UNICODE
776 const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const;
777
778 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
779
780 const wxChar* wc_str() const { return c_str(); }
781
782 // for compatibility with !wxUSE_UNICODE version
783 const wxChar* wc_str(wxMBConv& WXUNUSED(conv)) const { return c_str(); }
784
785 #if wxMBFILES
786 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
787 #else // !wxMBFILES
788 const wxChar* fn_str() const { return c_str(); }
789 #endif // wxMBFILES/!wxMBFILES
790 #else // ANSI
791 const wxChar* mb_str() const { return c_str(); }
792
793 // for compatibility with wxUSE_UNICODE version
794 const wxChar* mb_str(wxMBConv& WXUNUSED(conv)) const { return c_str(); }
795
796 const wxWX2MBbuf mbc_str() const { return mb_str(); }
797
798 #if wxUSE_WCHAR_T
799 const wxWCharBuffer wc_str(wxMBConv& conv) const;
800 #endif // wxUSE_WCHAR_T
801 #ifdef __WXOSX__
802 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
803 #else
804 const wxChar* fn_str() const { return c_str(); }
805 #endif
806 #endif // Unicode/ANSI
807
808 // overloaded assignment
809 // from another wxString
810 wxString& operator=(const wxStringBase& stringSrc)
811 { return (wxString&)wxStringBase::operator=(stringSrc); }
812 // from a character
813 wxString& operator=(wxChar ch)
814 { return (wxString&)wxStringBase::operator=(ch); }
815 // from a C string
816 wxString& operator=(const wxChar *psz)
817 { return (wxString&)wxStringBase::operator=(psz); }
818 #if wxUSE_UNICODE
819 // from wxWCharBuffer
820 wxString& operator=(const wxWCharBuffer& psz)
821 { (void) operator=((const wchar_t *)psz); return *this; }
822 #else // ANSI
823 // from another kind of C string
824 wxString& operator=(const unsigned char* psz);
825 #if wxUSE_WCHAR_T
826 // from a wide string
827 wxString& operator=(const wchar_t *pwz);
828 #endif
829 // from wxCharBuffer
830 wxString& operator=(const wxCharBuffer& psz)
831 { (void) operator=((const char *)psz); return *this; }
832 #endif // Unicode/ANSI
833
834 // string concatenation
835 // in place concatenation
836 /*
837 Concatenate and return the result. Note that the left to right
838 associativity of << allows to write things like "str << str1 << str2
839 << ..." (unlike with +=)
840 */
841 // string += string
842 wxString& operator<<(const wxString& s)
843 {
844 #if !wxUSE_STL
845 wxASSERT_MSG( s.GetStringData()->IsValid(),
846 _T("did you forget to call UngetWriteBuf()?") );
847 #endif
848
849 append(s);
850 return *this;
851 }
852 // string += C string
853 wxString& operator<<(const wxChar *psz)
854 { append(psz); return *this; }
855 // string += char
856 wxString& operator<<(wxChar ch) { append(1, ch); return *this; }
857
858 // string += buffer (i.e. from wxGetString)
859 #if wxUSE_UNICODE
860 wxString& operator<<(const wxWCharBuffer& s)
861 { (void)operator<<((const wchar_t *)s); return *this; }
862 void operator+=(const wxWCharBuffer& s)
863 { (void)operator<<((const wchar_t *)s); }
864 #else // !wxUSE_UNICODE
865 wxString& operator<<(const wxCharBuffer& s)
866 { (void)operator<<((const char *)s); return *this; }
867 void operator+=(const wxCharBuffer& s)
868 { (void)operator<<((const char *)s); }
869 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
870
871 // string += C string
872 wxString& Append(const wxString& s)
873 {
874 // test for IsEmpty() to share the string if possible
875 if ( IsEmpty() )
876 *this = s;
877 else
878 append(s);
879 return *this;
880 }
881 wxString& Append(const wxChar* psz)
882 { append(psz); return *this; }
883 // append count copies of given character
884 wxString& Append(wxChar ch, size_t count = 1u)
885 { append(count, ch); return *this; }
886 wxString& Append(const wxChar* psz, size_t nLen)
887 { append(psz, nLen); return *this; }
888
889 // prepend a string, return the string itself
890 wxString& Prepend(const wxString& str)
891 { *this = str + *this; return *this; }
892
893 // non-destructive concatenation
894 //
895 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
896 //
897 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
898 //
899 friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
900 //
901 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
902 //
903 friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
904
905 // stream-like functions
906 // insert an int into string
907 wxString& operator<<(int i)
908 { return (*this) << Format(_T("%d"), i); }
909 // insert an unsigned int into string
910 wxString& operator<<(unsigned int ui)
911 { return (*this) << Format(_T("%u"), ui); }
912 // insert a long into string
913 wxString& operator<<(long l)
914 { return (*this) << Format(_T("%ld"), l); }
915 // insert an unsigned long into string
916 wxString& operator<<(unsigned long ul)
917 { return (*this) << Format(_T("%lu"), ul); }
918 // insert a float into string
919 wxString& operator<<(float f)
920 { return (*this) << Format(_T("%f"), f); }
921 // insert a double into string
922 wxString& operator<<(double d)
923 { return (*this) << Format(_T("%g"), d); }
924
925 // string comparison
926 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
927 int Cmp(const wxChar *psz) const;
928 int Cmp(const wxString& s) const;
929 // same as Cmp() but not case-sensitive
930 int CmpNoCase(const wxChar *psz) const;
931 int CmpNoCase(const wxString& s) const;
932 // test for the string equality, either considering case or not
933 // (if compareWithCase then the case matters)
934 bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const
935 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
936 // comparison with a signle character: returns true if equal
937 bool IsSameAs(wxChar c, bool compareWithCase = true) const
938 {
939 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
940 : wxToupper(GetChar(0u)) == wxToupper(c));
941 }
942
943 // simple sub-string extraction
944 // return substring starting at nFirst of length nCount (or till the end
945 // if nCount = default value)
946 wxString Mid(size_t nFirst, size_t nCount = npos) const;
947
948 // operator version of Mid()
949 wxString operator()(size_t start, size_t len) const
950 { return Mid(start, len); }
951
952 // check that the string starts with prefix and return the rest of the
953 // string in the provided pointer if it is not NULL, otherwise return
954 // false
955 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
956
957 // get first nCount characters
958 wxString Left(size_t nCount) const;
959 // get last nCount characters
960 wxString Right(size_t nCount) const;
961 // get all characters before the first occurance of ch
962 // (returns the whole string if ch not found)
963 wxString BeforeFirst(wxChar ch) const;
964 // get all characters before the last occurence of ch
965 // (returns empty string if ch not found)
966 wxString BeforeLast(wxChar ch) const;
967 // get all characters after the first occurence of ch
968 // (returns empty string if ch not found)
969 wxString AfterFirst(wxChar ch) const;
970 // get all characters after the last occurence of ch
971 // (returns the whole string if ch not found)
972 wxString AfterLast(wxChar ch) const;
973
974 // for compatibility only, use more explicitly named functions above
975 wxString Before(wxChar ch) const { return BeforeLast(ch); }
976 wxString After(wxChar ch) const { return AfterFirst(ch); }
977
978 // case conversion
979 // convert to upper case in place, return the string itself
980 wxString& MakeUpper();
981 // convert to upper case, return the copy of the string
982 // Here's something to remember: BC++ doesn't like returns in inlines.
983 wxString Upper() const ;
984 // convert to lower case in place, return the string itself
985 wxString& MakeLower();
986 // convert to lower case, return the copy of the string
987 wxString Lower() const ;
988
989 // trimming/padding whitespace (either side) and truncating
990 // remove spaces from left or from right (default) side
991 wxString& Trim(bool bFromRight = true);
992 // add nCount copies chPad in the beginning or at the end (default)
993 wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = true);
994
995 // searching and replacing
996 // searching (return starting index, or -1 if not found)
997 int Find(wxChar ch, bool bFromEnd = false) const; // like strchr/strrchr
998 // searching (return starting index, or -1 if not found)
999 int Find(const wxChar *pszSub) const; // like strstr
1000 // replace first (or all of bReplaceAll) occurences of substring with
1001 // another string, returns the number of replacements made
1002 size_t Replace(const wxChar *szOld,
1003 const wxChar *szNew,
1004 bool bReplaceAll = true);
1005
1006 // check if the string contents matches a mask containing '*' and '?'
1007 bool Matches(const wxChar *szMask) const;
1008
1009 // conversion to numbers: all functions return true only if the whole
1010 // string is a number and put the value of this number into the pointer
1011 // provided, the base is the numeric base in which the conversion should be
1012 // done and must be comprised between 2 and 36 or be 0 in which case the
1013 // standard C rules apply (leading '0' => octal, "0x" => hex)
1014 // convert to a signed integer
1015 bool ToLong(long *val, int base = 10) const;
1016 // convert to an unsigned integer
1017 bool ToULong(unsigned long *val, int base = 10) const;
1018 // convert to a double
1019 bool ToDouble(double *val) const;
1020
1021 // formated input/output
1022 // as sprintf(), returns the number of characters written or < 0 on error
1023 // (take 'this' into account in attribute parameter count)
1024 int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1025 // as vprintf(), returns the number of characters written or < 0 on error
1026 int PrintfV(const wxChar* pszFormat, va_list argptr);
1027
1028 // returns the string containing the result of Printf() to it
1029 static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1030 // the same as above, but takes a va_list
1031 static wxString FormatV(const wxChar *pszFormat, va_list argptr);
1032
1033 // raw access to string memory
1034 // ensure that string has space for at least nLen characters
1035 // only works if the data of this string is not shared
1036 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1037 // minimize the string's memory
1038 // only works if the data of this string is not shared
1039 bool Shrink();
1040 #if !wxUSE_STL
1041 // get writable buffer of at least nLen bytes. Unget() *must* be called
1042 // a.s.a.p. to put string back in a reasonable state!
1043 wxChar *GetWriteBuf(size_t nLen);
1044 // call this immediately after GetWriteBuf() has been used
1045 void UngetWriteBuf();
1046 void UngetWriteBuf(size_t nLen);
1047 #endif
1048
1049 // wxWidgets version 1 compatibility functions
1050
1051 // use Mid()
1052 wxString SubString(size_t from, size_t to) const
1053 { return Mid(from, (to - from + 1)); }
1054 // values for second parameter of CompareTo function
1055 enum caseCompare {exact, ignoreCase};
1056 // values for first parameter of Strip function
1057 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1058
1059 // use Printf()
1060 // (take 'this' into account in attribute parameter count)
1061 int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1062
1063 // use Cmp()
1064 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1065 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1066
1067 // use Len
1068 size_t Length() const { return length(); }
1069 // Count the number of characters
1070 int Freq(wxChar ch) const;
1071 // use MakeLower
1072 void LowerCase() { MakeLower(); }
1073 // use MakeUpper
1074 void UpperCase() { MakeUpper(); }
1075 // use Trim except that it doesn't change this string
1076 wxString Strip(stripType w = trailing) const;
1077
1078 // use Find (more general variants not yet supported)
1079 size_t Index(const wxChar* psz) const { return Find(psz); }
1080 size_t Index(wxChar ch) const { return Find(ch); }
1081 // use Truncate
1082 wxString& Remove(size_t pos) { return Truncate(pos); }
1083 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1084
1085 wxString& Remove(size_t nStart, size_t nLen)
1086 { return (wxString&)erase( nStart, nLen ); }
1087
1088 // use Find()
1089 int First( const wxChar ch ) const { return Find(ch); }
1090 int First( const wxChar* psz ) const { return Find(psz); }
1091 int First( const wxString &str ) const { return Find(str); }
1092 int Last( const wxChar ch ) const { return Find(ch, true); }
1093 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1094
1095 // use IsEmpty()
1096 bool IsNull() const { return IsEmpty(); }
1097
1098 // std::string compatibility functions
1099
1100 // take nLen chars starting at nPos
1101 wxString(const wxString& str, size_t nPos, size_t nLen)
1102 : wxStringBase(str, nPos, nLen) { }
1103 // take all characters from pStart to pEnd
1104 wxString(const void *pStart, const void *pEnd)
1105 : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { }
1106 #if wxUSE_STL
1107 wxString(const_iterator first, const_iterator last)
1108 : wxStringBase(first, last) { }
1109 #endif
1110
1111 // lib.string.modifiers
1112 // append elements str[pos], ..., str[pos+n]
1113 wxString& append(const wxString& str, size_t pos, size_t n)
1114 { return (wxString&)wxStringBase::append(str, pos, n); }
1115 // append a string
1116 wxString& append(const wxString& str)
1117 { return (wxString&)wxStringBase::append(str); }
1118 // append first n (or all if n == npos) characters of sz
1119 wxString& append(const wxChar *sz)
1120 { return (wxString&)wxStringBase::append(sz); }
1121 wxString& append(const wxChar *sz, size_t n)
1122 { return (wxString&)wxStringBase::append(sz, n); }
1123 // append n copies of ch
1124 wxString& append(size_t n, wxChar ch)
1125 { return (wxString&)wxStringBase::append(n, ch); }
1126 // append from first to last
1127 wxString& append(const_iterator first, const_iterator last)
1128 { return (wxString&)wxStringBase::append(first, last); }
1129
1130 // same as `this_string = str'
1131 wxString& assign(const wxString& str)
1132 { return (wxString&)wxStringBase::assign(str); }
1133 // same as ` = str[pos..pos + n]
1134 wxString& assign(const wxString& str, size_t pos, size_t n)
1135 { return (wxString&)wxStringBase::assign(str, pos, n); }
1136 // same as `= first n (or all if n == npos) characters of sz'
1137 wxString& assign(const wxChar *sz)
1138 { return (wxString&)wxStringBase::assign(sz); }
1139 wxString& assign(const wxChar *sz, size_t n)
1140 { return (wxString&)wxStringBase::assign(sz, n); }
1141 // same as `= n copies of ch'
1142 wxString& assign(size_t n, wxChar ch)
1143 { return (wxString&)wxStringBase::assign(n, ch); }
1144 // assign from first to last
1145 wxString& assign(const_iterator first, const_iterator last)
1146 { return (wxString&)wxStringBase::assign(first, last); }
1147
1148 // string comparison
1149 #ifndef HAVE_STD_STRING_COMPARE
1150 int compare(const wxStringBase& str) const;
1151 // comparison with a substring
1152 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
1153 // comparison of 2 substrings
1154 int compare(size_t nStart, size_t nLen,
1155 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
1156 // just like strcmp()
1157 int compare(const wxChar* sz) const;
1158 // substring comparison with first nCount characters of sz
1159 int compare(size_t nStart, size_t nLen,
1160 const wxChar* sz, size_t nCount = npos) const;
1161 #endif // !defined HAVE_STD_STRING_COMPARE
1162
1163 // insert another string
1164 wxString& insert(size_t nPos, const wxString& str)
1165 { return (wxString&)wxStringBase::insert(nPos, str); }
1166 // insert n chars of str starting at nStart (in str)
1167 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1168 { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); }
1169 // insert first n (or all if n == npos) characters of sz
1170 wxString& insert(size_t nPos, const wxChar *sz)
1171 { return (wxString&)wxStringBase::insert(nPos, sz); }
1172 wxString& insert(size_t nPos, const wxChar *sz, size_t n)
1173 { return (wxString&)wxStringBase::insert(nPos, sz, n); }
1174 // insert n copies of ch
1175 wxString& insert(size_t nPos, size_t n, wxChar ch)
1176 { return (wxString&)wxStringBase::insert(nPos, n, ch); }
1177 iterator insert(iterator it, wxChar ch)
1178 { return wxStringBase::insert(it, ch); }
1179 void insert(iterator it, const_iterator first, const_iterator last)
1180 { wxStringBase::insert(it, first, last); }
1181 void insert(iterator it, size_type n, wxChar ch)
1182 { wxStringBase::insert(it, n, ch); }
1183
1184 // delete characters from nStart to nStart + nLen
1185 wxString& erase(size_type pos = 0, size_type n = npos)
1186 { return (wxString&)wxStringBase::erase(pos, n); }
1187 iterator erase(iterator first, iterator last)
1188 { return wxStringBase::erase(first, last); }
1189 iterator erase(iterator first)
1190 { return wxStringBase::erase(first); }
1191
1192 #ifdef wxSTRING_BASE_HASNT_CLEAR
1193 void clear() { erase(); }
1194 #endif
1195
1196 // replaces the substring of length nLen starting at nStart
1197 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz)
1198 { return (wxString&)wxStringBase::replace(nStart, nLen, sz); }
1199 // replaces the substring of length nLen starting at nStart
1200 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1201 { return (wxString&)wxStringBase::replace(nStart, nLen, str); }
1202 // replaces the substring with nCount copies of ch
1203 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch)
1204 { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); }
1205 // replaces a substring with another substring
1206 wxString& replace(size_t nStart, size_t nLen,
1207 const wxString& str, size_t nStart2, size_t nLen2)
1208 { return (wxString&)wxStringBase::replace(nStart, nLen, str,
1209 nStart2, nLen2); }
1210 // replaces the substring with first nCount chars of sz
1211 wxString& replace(size_t nStart, size_t nLen,
1212 const wxChar* sz, size_t nCount)
1213 { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); }
1214 wxString& replace(iterator first, iterator last, const_pointer s)
1215 { return (wxString&)wxStringBase::replace(first, last, s); }
1216 wxString& replace(iterator first, iterator last, const_pointer s,
1217 size_type n)
1218 { return (wxString&)wxStringBase::replace(first, last, s, n); }
1219 wxString& replace(iterator first, iterator last, const wxString& s)
1220 { return (wxString&)wxStringBase::replace(first, last, s); }
1221 wxString& replace(iterator first, iterator last, size_type n, wxChar c)
1222 { return (wxString&)wxStringBase::replace(first, last, n, c); }
1223 wxString& replace(iterator first, iterator last,
1224 const_iterator first1, const_iterator last1)
1225 { return (wxString&)wxStringBase::replace(first, last, first1, last1); }
1226
1227 // string += string
1228 wxString& operator+=(const wxString& s)
1229 { return (wxString&)wxStringBase::operator+=(s); }
1230 // string += C string
1231 wxString& operator+=(const wxChar *psz)
1232 { return (wxString&)wxStringBase::operator+=(psz); }
1233 // string += char
1234 wxString& operator+=(wxChar ch)
1235 { return (wxString&)wxStringBase::operator+=(ch); }
1236 };
1237
1238 // define wxArrayString, for compatibility
1239 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1240 #include "wx/arrstr.h"
1241 #endif
1242
1243 #if wxUSE_STL
1244 // return an empty wxString (not very useful with wxUSE_STL == 1)
1245 inline const wxString wxGetEmptyString() { return wxString(); }
1246 #else // !wxUSE_STL
1247 // return an empty wxString (more efficient than wxString() here)
1248 inline const wxString& wxGetEmptyString()
1249 {
1250 return *(wxString *)&wxEmptyString;
1251 }
1252 #endif // wxUSE_STL/!wxUSE_STL
1253
1254 // ----------------------------------------------------------------------------
1255 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1256 // ----------------------------------------------------------------------------
1257
1258 #if wxUSE_STL
1259
1260 class WXDLLIMPEXP_BASE wxStringBuffer
1261 {
1262 public:
1263 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1264 : m_str(str), m_buf(lenWanted)
1265 { }
1266
1267 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
1268
1269 operator wxChar*() { return m_buf.data(); }
1270
1271 private:
1272 wxString& m_str;
1273 #if wxUSE_UNICODE
1274 wxWCharBuffer m_buf;
1275 #else
1276 wxCharBuffer m_buf;
1277 #endif
1278
1279 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1280 };
1281
1282 class WXDLLIMPEXP_BASE wxStringBufferLength
1283 {
1284 public:
1285 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1286 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
1287 { }
1288
1289 ~wxStringBufferLength()
1290 {
1291 wxASSERT(m_lenSet);
1292 m_str.assign(m_buf.data(), m_len);
1293 }
1294
1295 operator wxChar*() { return m_buf.data(); }
1296 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1297
1298 private:
1299 wxString& m_str;
1300 #if wxUSE_UNICODE
1301 wxWCharBuffer m_buf;
1302 #else
1303 wxCharBuffer m_buf;
1304 #endif
1305 size_t m_len;
1306 bool m_lenSet;
1307
1308 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1309 };
1310
1311 #else // if !wxUSE_STL
1312
1313 class WXDLLIMPEXP_BASE wxStringBuffer
1314 {
1315 public:
1316 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
1317 : m_str(str), m_buf(NULL)
1318 { m_buf = m_str.GetWriteBuf(lenWanted); }
1319
1320 ~wxStringBuffer() { m_str.UngetWriteBuf(); }
1321
1322 operator wxChar*() const { return m_buf; }
1323
1324 private:
1325 wxString& m_str;
1326 wxChar *m_buf;
1327
1328 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1329 };
1330
1331 class WXDLLIMPEXP_BASE wxStringBufferLength
1332 {
1333 public:
1334 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1335 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
1336 {
1337 m_buf = m_str.GetWriteBuf(lenWanted);
1338 wxASSERT(m_buf != NULL);
1339 }
1340
1341 ~wxStringBufferLength()
1342 {
1343 wxASSERT(m_lenSet);
1344 m_str.UngetWriteBuf(m_len);
1345 }
1346
1347 operator wxChar*() const { return m_buf; }
1348 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1349
1350 private:
1351 wxString& m_str;
1352 wxChar *m_buf;
1353 size_t m_len;
1354 bool m_lenSet;
1355
1356 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1357 };
1358
1359 #endif // !wxUSE_STL
1360
1361 // ---------------------------------------------------------------------------
1362 // wxString comparison functions: operator versions are always case sensitive
1363 // ---------------------------------------------------------------------------
1364
1365 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1366 // are used and defining them also for wxString would only result in
1367 // compilation ambiguities when comparing std::string and wxString
1368 #if !wxUSE_STL
1369
1370 inline bool operator==(const wxString& s1, const wxString& s2)
1371 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
1372 inline bool operator==(const wxString& s1, const wxChar * s2)
1373 { return s1.Cmp(s2) == 0; }
1374 inline bool operator==(const wxChar * s1, const wxString& s2)
1375 { return s2.Cmp(s1) == 0; }
1376 inline bool operator!=(const wxString& s1, const wxString& s2)
1377 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
1378 inline bool operator!=(const wxString& s1, const wxChar * s2)
1379 { return s1.Cmp(s2) != 0; }
1380 inline bool operator!=(const wxChar * s1, const wxString& s2)
1381 { return s2.Cmp(s1) != 0; }
1382 inline bool operator< (const wxString& s1, const wxString& s2)
1383 { return s1.Cmp(s2) < 0; }
1384 inline bool operator< (const wxString& s1, const wxChar * s2)
1385 { return s1.Cmp(s2) < 0; }
1386 inline bool operator< (const wxChar * s1, const wxString& s2)
1387 { return s2.Cmp(s1) > 0; }
1388 inline bool operator> (const wxString& s1, const wxString& s2)
1389 { return s1.Cmp(s2) > 0; }
1390 inline bool operator> (const wxString& s1, const wxChar * s2)
1391 { return s1.Cmp(s2) > 0; }
1392 inline bool operator> (const wxChar * s1, const wxString& s2)
1393 { return s2.Cmp(s1) < 0; }
1394 inline bool operator<=(const wxString& s1, const wxString& s2)
1395 { return s1.Cmp(s2) <= 0; }
1396 inline bool operator<=(const wxString& s1, const wxChar * s2)
1397 { return s1.Cmp(s2) <= 0; }
1398 inline bool operator<=(const wxChar * s1, const wxString& s2)
1399 { return s2.Cmp(s1) >= 0; }
1400 inline bool operator>=(const wxString& s1, const wxString& s2)
1401 { return s1.Cmp(s2) >= 0; }
1402 inline bool operator>=(const wxString& s1, const wxChar * s2)
1403 { return s1.Cmp(s2) >= 0; }
1404 inline bool operator>=(const wxChar * s1, const wxString& s2)
1405 { return s2.Cmp(s1) <= 0; }
1406
1407 #if wxUSE_UNICODE
1408 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
1409 { return (s1.Cmp((const wchar_t *)s2) == 0); }
1410 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
1411 { return (s2.Cmp((const wchar_t *)s1) == 0); }
1412 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
1413 { return (s1.Cmp((const wchar_t *)s2) != 0); }
1414 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
1415 { return (s2.Cmp((const wchar_t *)s1) != 0); }
1416 #else // !wxUSE_UNICODE
1417 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
1418 { return (s1.Cmp((const char *)s2) == 0); }
1419 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
1420 { return (s2.Cmp((const char *)s1) == 0); }
1421 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
1422 { return (s1.Cmp((const char *)s2) != 0); }
1423 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
1424 { return (s2.Cmp((const char *)s1) != 0); }
1425 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1426
1427 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
1428 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
1429 wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
1430 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
1431 wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
1432
1433 #if wxUSE_UNICODE
1434 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
1435 { return string + (const wchar_t *)buf; }
1436 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
1437 { return (const wchar_t *)buf + string; }
1438 #else // !wxUSE_UNICODE
1439 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
1440 { return string + (const char *)buf; }
1441 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
1442 { return (const char *)buf + string; }
1443 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1444
1445 #endif // !wxUSE_STL
1446
1447 // comparison with char (those are not defined by std::[w]string and so should
1448 // be always available)
1449 inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); }
1450 inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); }
1451 inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); }
1452 inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); }
1453
1454 // ---------------------------------------------------------------------------
1455 // Implementation only from here until the end of file
1456 // ---------------------------------------------------------------------------
1457
1458 // don't pollute the library user's name space
1459 #undef wxASSERT_VALID_INDEX
1460
1461 #if wxUSE_STD_IOSTREAM
1462
1463 #include "wx/iosfwrap.h"
1464
1465 WXDLLIMPEXP_BASE wxSTD istream& operator>>(wxSTD istream&, wxString&);
1466 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
1467
1468 #endif // wxSTD_STRING_COMPATIBILITY
1469
1470 #endif // _WX_WXSTRINGH__