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