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