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