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