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