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