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