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