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