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