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