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