1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString and wxArrayString classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
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.
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
51 #include <StringMgr.h>
54 #include "wx/wxchar.h" // for wxChar
55 #include "wx/strvararg.h"
56 #include "wx/buffer.h" // for wxCharBuffer
57 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
59 class WXDLLIMPEXP_BASE wxString
;
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 // casts [unfortunately!] needed to call some broken functions which require
66 // "char *" instead of "const char *"
67 #define WXSTRINGCAST (wxChar *)(const wxChar *)
68 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
69 #define wxMBSTRINGCAST (char *)(const char *)
70 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
72 // implementation only
73 #define wxASSERT_VALID_INDEX(i) \
74 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 #if WXWIN_COMPATIBILITY_2_6
82 // deprecated in favour of wxString::npos, don't use in new code
84 // maximum possible length for a string means "take all string" everywhere
85 #define wxSTRING_MAXLEN wxStringBase::npos
87 #endif // WXWIN_COMPATIBILITY_2_6
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // global pointer to empty string
94 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
96 // ---------------------------------------------------------------------------
97 // global functions complementing standard C string library replacements for
98 // strlen() and portable strcasecmp()
99 //---------------------------------------------------------------------------
101 // Use wxXXX() functions from wxchar.h instead! These functions are for
102 // backwards compatibility only.
104 // checks whether the passed in pointer is NULL and if the string is empty
105 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
107 // safe version of strlen() (returns 0 if passed NULL pointer)
108 inline size_t Strlen(const char *psz
)
109 { return psz
? strlen(psz
) : 0; }
111 // portable strcasecmp/_stricmp
112 inline int Stricmp(const char *psz1
, const char *psz2
)
114 #if defined(__VISUALC__) && defined(__WXWINCE__)
115 register char c1
, c2
;
117 c1
= tolower(*psz1
++);
118 c2
= tolower(*psz2
++);
119 } while ( c1
&& (c1
== c2
) );
122 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
123 return _stricmp(psz1
, psz2
);
124 #elif defined(__SC__)
125 return _stricmp(psz1
, psz2
);
126 #elif defined(__SALFORDC__)
127 return stricmp(psz1
, psz2
);
128 #elif defined(__BORLANDC__)
129 return stricmp(psz1
, psz2
);
130 #elif defined(__WATCOMC__)
131 return stricmp(psz1
, psz2
);
132 #elif defined(__DJGPP__)
133 return stricmp(psz1
, psz2
);
134 #elif defined(__EMX__)
135 return stricmp(psz1
, psz2
);
136 #elif defined(__WXPM__)
137 return stricmp(psz1
, psz2
);
138 #elif defined(__WXPALMOS__) || \
139 defined(HAVE_STRCASECMP_IN_STRING_H) || \
140 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
141 defined(__GNUWIN32__)
142 return strcasecmp(psz1
, psz2
);
143 #elif defined(__MWERKS__) && !defined(__INTEL__)
144 register char c1
, c2
;
146 c1
= tolower(*psz1
++);
147 c2
= tolower(*psz2
++);
148 } while ( c1
&& (c1
== c2
) );
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):
156 register char c1, c2;
158 c1 = tolower(*psz1++);
159 c2 = tolower(*psz2++);
160 } while ( c1 && (c1 == c2) );
165 #error "Please define string case-insensitive compare for your OS/compiler"
166 #endif // OS/compiler
169 // ----------------------------------------------------------------------------
170 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
171 // ----------------------------------------------------------------------------
173 // in both cases we need to define wxStdString
174 #if wxUSE_STL || wxUSE_STD_STRING
176 #include "wx/beforestd.h"
178 #include "wx/afterstd.h"
181 #ifdef HAVE_STD_WSTRING
182 typedef std::wstring wxStdString
;
184 typedef std::basic_string
<wxChar
> wxStdString
;
187 typedef std::string wxStdString
;
190 #endif // need <string>
194 // we don't need an extra ctor from std::string when copy ctor already does
196 #undef wxUSE_STD_STRING
197 #define wxUSE_STD_STRING 0
199 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
200 (defined(_MSC_VER) && (_MSC_VER <= 1200))
201 #define wxSTRING_BASE_HASNT_CLEAR
204 typedef wxStdString wxStringBase
;
205 #else // if !wxUSE_STL
207 #if !defined(HAVE_STD_STRING_COMPARE) && \
208 (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
209 #define HAVE_STD_STRING_COMPARE
212 // ---------------------------------------------------------------------------
213 // string data prepended with some housekeeping info (used by wxString class),
214 // is never used directly (but had to be put here to allow inlining)
215 // ---------------------------------------------------------------------------
217 struct WXDLLIMPEXP_BASE wxStringData
219 int nRefs
; // reference count
220 size_t nDataLength
, // actual string length
221 nAllocLength
; // allocated memory size
223 // mimics declaration 'wxChar data[nAllocLength]'
224 wxChar
* data() const { return (wxChar
*)(this + 1); }
226 // empty string has a special ref count so it's never deleted
227 bool IsEmpty() const { return (nRefs
== -1); }
228 bool IsShared() const { return (nRefs
> 1); }
231 void Lock() { if ( !IsEmpty() ) nRefs
++; }
233 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
234 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
237 // VC++ free must take place in same DLL as allocation when using non dll
238 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
239 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
240 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
241 // we must not inline deallocation since allocation is not inlined
244 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
247 // if we had taken control over string memory (GetWriteBuf), it's
248 // intentionally put in invalid state
249 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
250 bool IsValid() const { return (nRefs
!= 0); }
253 class WXDLLIMPEXP_BASE wxStringBase
256 // an 'invalid' value for string index, moved to this place due to a CW bug
257 static const size_t npos
;
259 // points to data preceded by wxStringData structure with ref count info
262 // accessor to string data
263 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
265 // string (re)initialization functions
266 // initializes the string to the empty value (must be called only from
267 // ctors, use Reinit() otherwise)
268 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
269 // initializes the string with (a part of) C-string
270 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
271 // as Init, but also frees old data
272 void Reinit() { GetStringData()->Unlock(); Init(); }
275 // allocates memory for string of length nLen
276 bool AllocBuffer(size_t nLen
);
277 // copies data to another string
278 bool AllocCopy(wxString
&, int, int) const;
279 // effectively copies data to string
280 bool AssignCopy(size_t, const wxChar
*);
282 // append a (sub)string
283 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
284 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
285 { return ConcatSelf(nLen
, src
, nLen
); }
287 // functions called before writing to the string: they copy it if there
288 // are other references to our data (should be the only owner when writing)
289 bool CopyBeforeWrite();
290 bool AllocBeforeWrite(size_t);
292 // compatibility with wxString
293 bool Alloc(size_t nLen
);
296 typedef wxUniChar value_type
;
297 typedef wxUniChar char_type
;
298 typedef wxUniCharRef reference
;
299 typedef wxChar
* pointer
;
300 typedef const wxChar
* const_pointer
;
302 typedef size_t size_type
;
303 typedef wxUniChar const_reference
;
305 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
306 reference_type, reference_ctor) \
308 typedef wxUniChar value_type; \
309 typedef reference_type reference; \
310 typedef pointer_type pointer; \
312 iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \
314 reference operator*() const { return reference_ctor; } \
316 iterator_name& operator++() \
317 { ++m_cur; return *this; } \
318 iterator_name operator++(int) \
319 { iterator_name tmp = *this; ++m_cur; return tmp; } \
320 iterator_name& operator--() \
321 { --m_cur; return *this; } \
322 iterator_name operator--(int) \
323 { iterator_name tmp = *this; --m_cur; return tmp; } \
325 iterator_name operator+(int n) const \
326 { return iterator_name(m_cur + n); } \
327 iterator_name operator+(size_t n) const \
328 { return iterator_name(m_cur + n); } \
329 iterator_name operator-(int n) const \
330 { return iterator_name(m_cur - n); } \
331 iterator_name operator-(size_t n) const \
332 { return iterator_name(m_cur - n); } \
333 iterator_name operator+=(int n) \
334 { m_cur += n; return *this; } \
335 iterator_name operator+=(size_t n) \
336 { m_cur += n; return *this; } \
337 iterator_name operator-=(int n) \
338 { m_cur -= n; return *this; } \
339 iterator_name operator-=(size_t n) \
340 { m_cur -= n; return *this; } \
342 unsigned operator-(const iterator_name& i) const \
343 { return m_cur - i.m_cur; } \
345 bool operator==(const iterator_name&i) const \
346 { return m_cur == i.m_cur; } \
347 bool operator!=(const iterator_name& i) const \
348 { return m_cur != i.m_cur; } \
350 bool operator<(const iterator_name& i) const \
351 { return m_cur < i.m_cur; } \
352 bool operator>(const iterator_name& i) const \
353 { return m_cur > i.m_cur; } \
354 bool operator<=(const iterator_name& i) const \
355 { return m_cur <= i.m_cur; } \
356 bool operator>=(const iterator_name& i) const \
357 { return m_cur >= i.m_cur; } \
360 /* for internal wxString use only: */ \
361 iterator_name(pointer ptr) : m_cur(ptr) {} \
362 operator pointer() const { return m_cur; } \
364 friend class WXDLLIMPEXP_BASE wxString; \
365 friend class WXDLLIMPEXP_BASE wxStringBase; \
366 friend class WXDLLIMPEXP_BASE wxCStrData; \
371 class const_iterator
;
375 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
376 wxUniCharRef::CreateForString(m_cur
))
378 friend class const_iterator
;
383 // NB: reference_type is intentionally value, not reference, the character
384 // may be encoded differently in wxString data:
385 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
,
389 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
392 #undef WX_STR_ITERATOR
394 template <typename T
>
395 class reverse_iterator_impl
398 typedef T iterator_type
;
399 typedef typename
T::value_type value_type
;
400 typedef typename
T::reference reference
;
401 typedef typename
T::pointer
*pointer
;
403 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
404 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
407 iterator_type
base() const { return m_cur
; }
409 reference
operator*() const { return *(m_cur
-1); }
411 reverse_iterator_impl
& operator++()
412 { --m_cur
; return *this; }
413 reverse_iterator_impl
operator++(int)
414 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
415 reverse_iterator_impl
& operator--()
416 { ++m_cur
; return *this; }
417 reverse_iterator_impl
operator--(int)
418 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
420 bool operator==(const reverse_iterator_impl
& ri
) const
421 { return m_cur
== ri
.m_cur
; }
422 bool operator!=(const reverse_iterator_impl
& ri
) const
423 { return !(*this == ri
); }
429 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
430 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
433 // constructors and destructor
434 // ctor for an empty string
435 wxStringBase() { Init(); }
437 wxStringBase(const wxStringBase
& stringSrc
)
439 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
440 _T("did you forget to call UngetWriteBuf()?") );
442 if ( stringSrc
.empty() ) {
443 // nothing to do for an empty string
447 m_pchData
= stringSrc
.m_pchData
; // share same data
448 GetStringData()->Lock(); // => one more copy
451 // string containing nRepeat copies of ch
452 wxStringBase(size_type nRepeat
, wxUniChar ch
);
453 // ctor takes first nLength characters from C string
454 // (default value of npos means take all the string)
455 wxStringBase(const wxChar
*psz
)
456 { InitWith(psz
, 0, npos
); }
457 wxStringBase(const wxChar
*psz
, size_t nLength
)
458 { InitWith(psz
, 0, nLength
); }
459 wxStringBase(const wxChar
*psz
,
460 const wxMBConv
& WXUNUSED(conv
),
461 size_t nLength
= npos
)
462 { InitWith(psz
, 0, nLength
); }
463 // take nLen chars starting at nPos
464 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
466 wxASSERT_MSG( str
.GetStringData()->IsValid(),
467 _T("did you forget to call UngetWriteBuf()?") );
469 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
470 InitWith(str
.c_str(), nPos
, nLen
);
472 // take all characters from pStart to pEnd
473 wxStringBase(const void *pStart
, const void *pEnd
);
475 // dtor is not virtual, this class must not be inherited from!
478 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
479 //RN - according to the above VC++ does indeed inline this,
480 //even though it spits out two warnings
481 #pragma warning (disable:4714)
484 GetStringData()->Unlock();
487 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
488 //re-enable inlining warning
489 #pragma warning (default:4714)
491 // overloaded assignment
492 // from another wxString
493 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
495 wxStringBase
& operator=(wxUniChar ch
);
497 wxStringBase
& operator=(const wxChar
*psz
);
499 // return the length of the string
500 size_type
length() const { return GetStringData()->nDataLength
; }
501 // return the length of the string
502 size_type
size() const { return length(); }
503 // return the maximum size of the string
504 size_type
max_size() const { return npos
; }
505 // resize the string, filling the space with c if c != 0
506 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'));
507 // delete the contents of the string
508 void clear() { erase(0, npos
); }
509 // returns true if the string is empty
510 bool empty() const { return length() == 0; }
511 // inform string about planned change in size
512 void reserve(size_t sz
) { Alloc(sz
); }
513 size_type
capacity() const { return GetStringData()->nAllocLength
; }
516 // return the character at position n
517 value_type
at(size_type n
) const
518 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
519 // returns the writable character at position n
520 reference
at(size_type n
)
522 wxASSERT_VALID_INDEX( n
);
524 return wxUniCharRef::CreateForString(&m_pchData
[n
]);
527 // lib.string.modifiers
528 // append elements str[pos], ..., str[pos+n]
529 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
531 wxASSERT(pos
<= str
.length());
532 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
536 wxStringBase
& append(const wxStringBase
& str
)
537 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
538 // append first n (or all if n == npos) characters of sz
539 wxStringBase
& append(const wxChar
*sz
)
540 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
541 wxStringBase
& append(const wxChar
*sz
, size_t n
)
542 { ConcatSelf(n
, sz
); return *this; }
543 // append n copies of ch
544 wxStringBase
& append(size_t n
, wxUniChar ch
);
545 // append from first to last
546 wxStringBase
& append(const_iterator first
, const_iterator last
)
547 { ConcatSelf(last
- first
, first
); return *this; }
549 // same as `this_string = str'
550 wxStringBase
& assign(const wxStringBase
& str
)
551 { return *this = str
; }
552 // same as ` = str[pos..pos + n]
553 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
554 { clear(); return append(str
, pos
, n
); }
555 // same as `= first n (or all if n == npos) characters of sz'
556 wxStringBase
& assign(const wxChar
*sz
)
557 { clear(); return append(sz
, wxStrlen(sz
)); }
558 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
559 { clear(); return append(sz
, n
); }
560 // same as `= n copies of ch'
561 wxStringBase
& assign(size_t n
, wxUniChar ch
)
562 { clear(); return append(n
, ch
); }
563 // assign from first to last
564 wxStringBase
& assign(const_iterator first
, const_iterator last
)
565 { clear(); return append(first
, last
); }
567 // first valid index position
568 const_iterator
begin() const { return m_pchData
; }
570 // position one after the last valid one
571 const_iterator
end() const { return m_pchData
+ length(); }
574 // first element of the reversed string
575 const_reverse_iterator
rbegin() const { return const_reverse_iterator(end()); }
576 reverse_iterator
rbegin() { return reverse_iterator(end()); }
577 // one beyond the end of the reversed string
578 const_reverse_iterator
rend() const { return const_reverse_iterator(begin()); }
579 reverse_iterator
rend() { return reverse_iterator(begin()); }
581 // insert another string
582 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
584 wxASSERT( str
.GetStringData()->IsValid() );
585 return insert(nPos
, str
.c_str(), str
.length());
587 // insert n chars of str starting at nStart (in str)
588 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
590 wxASSERT( str
.GetStringData()->IsValid() );
591 wxASSERT( nStart
< str
.length() );
592 size_t strLen
= str
.length() - nStart
;
593 n
= strLen
< n
? strLen
: n
;
594 return insert(nPos
, str
.c_str() + nStart
, n
);
596 // insert first n (or all if n == npos) characters of sz
597 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
598 // insert n copies of ch
599 wxStringBase
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
600 { return insert(nPos
, wxStringBase(n
, ch
)); }
601 iterator
insert(iterator it
, wxUniChar ch
)
602 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
603 void insert(iterator it
, const_iterator first
, const_iterator last
)
604 { insert(it
- begin(), first
, last
- first
); }
605 void insert(iterator it
, size_type n
, wxUniChar ch
)
606 { insert(it
- begin(), n
, ch
); }
608 // delete characters from nStart to nStart + nLen
609 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
610 iterator
erase(iterator first
, iterator last
)
612 size_t idx
= first
- begin();
613 erase(idx
, last
- first
);
614 return begin() + idx
;
616 iterator
erase(iterator first
);
618 // explicit conversion to C string (use this with printf()!)
619 const wxChar
* c_str() const { return m_pchData
; }
620 const wxChar
* data() const { return m_pchData
; }
622 // replaces the substring of length nLen starting at nStart
623 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
624 // replaces the substring of length nLen starting at nStart
625 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
626 { return replace(nStart
, nLen
, str
.c_str()); }
627 // replaces the substring with nCount copies of ch
628 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
);
629 // replaces a substring with another substring
630 wxStringBase
& replace(size_t nStart
, size_t nLen
,
631 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
632 // replaces the substring with first nCount chars of sz
633 wxStringBase
& replace(size_t nStart
, size_t nLen
,
634 const wxChar
* sz
, size_t nCount
);
635 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
636 { return replace(first
- begin(), last
- first
, s
); }
637 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
639 { return replace(first
- begin(), last
- first
, s
, n
); }
640 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
641 { return replace(first
- begin(), last
- first
, s
); }
642 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxUniChar c
)
643 { return replace(first
- begin(), last
- first
, n
, c
); }
644 wxStringBase
& replace(iterator first
, iterator last
,
645 const_iterator first1
, const_iterator last1
)
646 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
649 void swap(wxStringBase
& str
);
651 // All find() functions take the nStart argument which specifies the
652 // position to start the search on, the default value is 0. All functions
653 // return npos if there were no match.
656 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
658 // find first n characters of sz
659 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
661 // find the first occurence of character ch after nStart
662 size_t find(wxUniChar ch
, size_t nStart
= 0) const;
664 // rfind() family is exactly like find() but works right to left
666 // as find, but from the end
667 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
669 // as find, but from the end
670 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
671 size_t n
= npos
) const;
672 // as find, but from the end
673 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const;
675 // find first/last occurence of any character in the set
677 // as strpbrk() but starts at nStart, returns npos if not found
678 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
679 { return find_first_of(str
.c_str(), nStart
); }
681 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
682 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
683 // same as find(char, size_t)
684 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
685 { return find(c
, nStart
); }
686 // find the last (starting from nStart) char from str in this string
687 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
688 { return find_last_of(str
.c_str(), nStart
); }
690 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
691 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
693 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
694 { return rfind(c
, nStart
); }
696 // find first/last occurence of any character not in the set
698 // as strspn() (starting from nStart), returns npos on failure
699 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
700 { return find_first_not_of(str
.c_str(), nStart
); }
702 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
703 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
705 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
707 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
708 { return find_last_not_of(str
.c_str(), nStart
); }
710 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
711 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
713 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
715 // All compare functions return -1, 0 or 1 if the [sub]string is less,
716 // equal or greater than the compare() argument.
718 // comparison with another string
719 int compare(const wxStringBase
& str
) const;
720 // comparison with a substring
721 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
722 // comparison of 2 substrings
723 int compare(size_t nStart
, size_t nLen
,
724 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
725 // comparison with a c string
726 int compare(const wxChar
* sz
) const;
727 // substring comparison with first nCount characters of sz
728 int compare(size_t nStart
, size_t nLen
,
729 const wxChar
* sz
, size_t nCount
= npos
) const;
731 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
733 // substring extraction
734 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
737 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
738 // string += C string
739 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
741 wxStringBase
& operator+=(wxUniChar ch
) { return append(1, ch
); }
742 wxStringBase
& operator+=(wxUniCharRef ch
) { return append(1, ch
); }
743 wxStringBase
& operator+=(char ch
) { return append(1, ch
); }
744 wxStringBase
& operator+=(wchar_t ch
) { return append(1, ch
); }
749 // ----------------------------------------------------------------------------
751 // ----------------------------------------------------------------------------
753 // Lightweight object returned by wxString::c_str() and implicitly convertible
754 // to either const char* or const wchar_t*.
755 class WXDLLIMPEXP_BASE wxCStrData
758 // Ctors; for internal use by wxString and wxCStrData only
759 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
760 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
763 // Ctor constructs the object from char literal; they are needed to make
764 // operator?: compile and they intentionally take char*, not const char*
765 wxCStrData(char *buf
);
766 wxCStrData(wchar_t *buf
);
770 // FIXME: we'll need convertors for both char* and wchar_t* and NONE
771 // for wxChar*, but that's after completing the transition to
772 // "smart" wxUniChar class. For now, just have conversion to
773 // char* in ANSI build and wchar_t in Unicode build.
775 const wchar_t* AsWChar() const;
776 operator const wchar_t*() const { return AsWChar(); }
778 const char* AsChar() const;
779 operator const char*() const { return AsChar(); }
782 wxString
AsString() const;
783 operator wxString() const;
785 // allow expressions like "c_str()[0]":
786 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
787 wxUniChar
operator[](size_t n
) const;
788 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
789 #ifndef wxSIZE_T_IS_UINT
790 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
791 #endif // size_t != unsigned int
793 // these operators are needed to emulate the pointer semantics of c_str():
794 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
795 // (we need both versions to resolve ambiguities):
796 wxCStrData
operator+(int n
) const
797 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
798 wxCStrData
operator+(long n
) const
799 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
800 wxCStrData
operator+(size_t n
) const
801 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
803 // this operator is need to make expressions like "*c_str()" or
804 // "*(c_str() + 2)" work
805 wxUniChar
operator*() const;
808 const wxString
*m_str
;
812 friend class WXDLLIMPEXP_BASE wxString
;
815 // ----------------------------------------------------------------------------
816 // wxStringPrintfMixin
817 // ---------------------------------------------------------------------------
819 // NB: VC6 has a bug that causes linker errors if you have template methods
820 // in a class using __declspec(dllimport). The solution is to split such
821 // class into two classes, one that contains the template methods and does
822 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
823 // (with DLL linkage).
825 // We only do this for VC6 here, because the code is less efficient
826 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
827 // cannot compile this code.
829 #if defined(__VISUALC__) && __VISUALC__ < 1300
830 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
833 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
834 // this class contains implementation of wxString's vararg methods, it's
835 // exported from wxBase DLL
836 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
839 wxStringPrintfMixinBase() {}
841 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
842 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
845 // this class contains template wrappers for wxString's vararg methods, it's
846 // intentionally *not* exported from the DLL in order to fix the VC6 bug
848 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
851 // to further complicate things, we can't return wxString from
852 // wxStringPrintfMixin::Format() because wxString is not yet declared at
853 // this point; the solution is to use this fake type trait template - this
854 // way the compiler won't know the return type until Format() is used
855 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
856 template<typename T
> struct StringReturnType
858 typedef wxString type
;
862 // these are duplicated wxString methods, they're also declared below
863 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
865 // int Printf(const wxChar *pszFormat, ...);
866 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
867 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
868 WX_DEFINE_VARARG_FUNC(static typename StringReturnType
<T1
>::type
,
870 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
871 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
874 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
876 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
879 // ----------------------------------------------------------------------------
880 // wxString: string class trying to be compatible with std::string, MFC
881 // CString and wxWindows 1.x wxString all at once
882 // ---------------------------------------------------------------------------
884 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
885 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
886 // for dll-interface class 'wxString'" -- this is OK in our case
887 #pragma warning (disable:4275)
890 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
891 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
892 ,public wxStringPrintfMixin
895 // NB: special care was taken in arranging the member functions in such order
896 // that all inline functions can be effectively inlined, verify that all
897 // performance critical functions are still inlined if you change order!
899 // if we hadn't made these operators private, it would be possible to
900 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
901 // converted to char in C and we do have operator=(char)
903 // NB: we don't need other versions (short/long and unsigned) as attempt
904 // to assign another numeric type to wxString will now result in
905 // ambiguity between operator=(char) and operator=(int)
906 wxString
& operator=(int);
908 // these methods are not implemented - there is _no_ conversion from int to
909 // string, you're doing something wrong if the compiler wants to call it!
911 // try `s << i' or `s.Printf("%d", i)' instead
915 // constructors and destructor
916 // ctor for an empty string
917 wxString() : wxStringBase() { }
919 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
920 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
921 // string containing nRepeat copies of ch
922 wxString(wxUniChar ch
, size_t nRepeat
= 1)
923 : wxStringBase(nRepeat
, ch
) { }
924 wxString(size_t nRepeat
, wxUniChar ch
)
925 : wxStringBase(nRepeat
, ch
) { }
926 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
927 : wxStringBase(nRepeat
, ch
) { }
928 wxString(size_t nRepeat
, wxUniCharRef ch
)
929 : wxStringBase(nRepeat
, ch
) { }
930 wxString(char ch
, size_t nRepeat
= 1)
931 : wxStringBase(nRepeat
, ch
) { }
932 wxString(size_t nRepeat
, char ch
)
933 : wxStringBase(nRepeat
, ch
) { }
934 wxString(wchar_t ch
, size_t nRepeat
= 1)
935 : wxStringBase(nRepeat
, ch
) { }
936 wxString(size_t nRepeat
, wchar_t ch
)
937 : wxStringBase(nRepeat
, ch
) { }
938 // ctor takes first nLength characters from C string
939 // (default value of npos means take all the string)
940 wxString(const wxChar
*psz
)
941 : wxStringBase(psz
? psz
: wxT("")) { }
942 wxString(const wxChar
*psz
, size_t nLength
)
943 : wxStringBase(psz
, nLength
) { }
944 wxString(const wxChar
*psz
,
945 const wxMBConv
& WXUNUSED(conv
),
946 size_t nLength
= npos
)
947 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
949 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
950 // implicit conversions from std::string to wxString as this allows to use
951 // the same strings in non-GUI and GUI code, however we don't want to
952 // unconditionally add this ctor as it would make wx lib dependent on
953 // libstdc++ on some Linux versions which is bad, so instead we ask the
954 // client code to define this wxUSE_STD_STRING symbol if they need it
956 wxString(const wxStdString
& s
)
957 : wxStringBase(s
.c_str()) { }
958 #endif // wxUSE_STD_STRING
961 // from multibyte string
962 wxString(const char *psz
,
963 const wxMBConv
& conv
= wxConvLibc
,
964 size_t nLength
= npos
);
965 // from wxWCharBuffer (i.e. return from wxGetString)
966 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
968 // from C string (for compilers using unsigned char)
969 wxString(const unsigned char* psz
)
970 : wxStringBase((const char*)psz
) { }
971 // from part of C string (for compilers using unsigned char)
972 wxString(const unsigned char* psz
, size_t nLength
)
973 : wxStringBase((const char*)psz
, nLength
) { }
976 // from wide (Unicode) string
977 wxString(const wchar_t *pwz
,
978 const wxMBConv
& conv
= wxConvLibc
,
979 size_t nLength
= npos
);
980 #endif // !wxUSE_WCHAR_T
983 wxString(const wxCharBuffer
& psz
)
984 : wxStringBase(psz
) { }
985 #endif // Unicode/ANSI
987 // generic attributes & operations
988 // as standard strlen()
989 size_t Len() const { return length(); }
990 // string contains any characters?
991 bool IsEmpty() const { return empty(); }
992 // empty string is "false", so !str will return true
993 bool operator!() const { return empty(); }
994 // truncate the string to given length
995 wxString
& Truncate(size_t uiLen
);
996 // empty string contents
1001 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1003 // empty the string and free memory
1006 wxString
tmp(wxEmptyString
);
1011 // Is an ascii value
1012 bool IsAscii() const;
1014 bool IsNumber() const;
1016 bool IsWord() const;
1018 // data access (all indexes are 0 based)
1020 wxUniChar
GetChar(size_t n
) const
1022 // read/write access
1023 wxUniCharRef
GetWritableChar(size_t n
)
1026 void SetChar(size_t n
, wxUniChar ch
)
1029 // get last character
1030 wxUniChar
Last() const
1032 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1034 return at(length() - 1);
1037 // get writable last character
1040 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1041 return at(length() - 1);
1045 Note that we we must define all of the overloads below to avoid
1046 ambiguity when using str[0].
1048 wxUniChar
operator[](int n
) const
1049 { return wxStringBase::at(n
); }
1050 wxUniChar
operator[](long n
) const
1051 { return wxStringBase::at(n
); }
1052 wxUniChar
operator[](size_t n
) const
1053 { return wxStringBase::at(n
); }
1054 #ifndef wxSIZE_T_IS_UINT
1055 wxUniChar
operator[](unsigned int n
) const
1056 { return wxStringBase::at(n
); }
1057 #endif // size_t != unsigned int
1059 // operator versions of GetWriteableChar()
1060 wxUniCharRef
operator[](int n
)
1061 { return wxStringBase::at(n
); }
1062 wxUniCharRef
operator[](long n
)
1063 { return wxStringBase::at(n
); }
1064 wxUniCharRef
operator[](size_t n
)
1065 { return wxStringBase::at(n
); }
1066 #ifndef wxSIZE_T_IS_UINT
1067 wxUniCharRef
operator[](unsigned int n
)
1068 { return wxStringBase::at(n
); }
1069 #endif // size_t != unsigned int
1071 // explicit conversion to C string (use this with printf()!)
1072 wxCStrData
c_str() const { return wxCStrData(this); }
1074 // implicit conversion to C string
1075 operator wxCStrData() const { return c_str(); }
1076 operator const wxChar
*() const { return c_str(); }
1078 // identical to c_str(), for MFC compatibility
1079 const wxCStrData
GetData() const { return c_str(); }
1081 // explicit conversion to C string in internal representation (char*,
1082 // wchar_t*, UTF-8-encoded char*, depending on the build):
1083 const_pointer
wx_str() const { return data(); }
1085 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1086 // converting numbers or strings which are certain not to contain special
1087 // chars (typically system functions, X atoms, environment variables etc.)
1089 // the behaviour of these functions with the strings containing anything
1090 // else than 7 bit ASCII characters is undefined, use at your own risk.
1092 static wxString
FromAscii(const char *ascii
); // string
1093 static wxString
FromAscii(const char ascii
); // char
1094 const wxCharBuffer
ToAscii() const;
1096 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1097 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1098 const char *ToAscii() const { return c_str(); }
1099 #endif // Unicode/!Unicode
1101 // conversions with (possible) format conversions: have to return a
1102 // buffer with temporary data
1104 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1105 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1106 // fn_str() to return a string which should be used with the OS APIs
1107 // accepting the file names. The return value is always the same, but the
1108 // type differs because a function may either return pointer to the buffer
1109 // directly or have to use intermediate buffer for translation.
1111 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1113 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1115 const wxChar
* wc_str() const { return c_str(); }
1117 // for compatibility with !wxUSE_UNICODE version
1118 const wxChar
* wc_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
1121 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1123 const wxChar
* fn_str() const { return c_str(); }
1124 #endif // wxMBFILES/!wxMBFILES
1126 const wxChar
* mb_str() const { return c_str(); }
1128 // for compatibility with wxUSE_UNICODE version
1129 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
1131 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1134 const wxWCharBuffer
wc_str(const wxMBConv
& conv
) const;
1135 #endif // wxUSE_WCHAR_T
1137 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1139 const wxChar
* fn_str() const { return c_str(); }
1141 #endif // Unicode/ANSI
1143 // overloaded assignment
1144 // from another wxString
1145 wxString
& operator=(const wxStringBase
& stringSrc
)
1146 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
1147 wxString
& operator=(const wxCStrData
& cstr
);
1149 wxString
& operator=(wxUniChar ch
)
1150 { return (wxString
&)wxStringBase::operator=(ch
); }
1151 wxString
& operator=(wxUniCharRef ch
)
1152 { return (wxString
&)wxStringBase::operator=((wxUniChar
)ch
); }
1153 wxString
& operator=(char ch
)
1154 { return (wxString
&)wxStringBase::operator=(wxUniChar(ch
)); }
1155 wxString
& operator=(wchar_t ch
)
1156 { return (wxString
&)wxStringBase::operator=(wxUniChar(ch
)); }
1157 // from a C string - STL probably will crash on NULL,
1158 // so we need to compensate in that case
1160 wxString
& operator=(const wxChar
*psz
)
1161 { if(psz
) wxStringBase::operator=(psz
); else Clear(); return *this; }
1163 wxString
& operator=(const wxChar
*psz
)
1164 { return (wxString
&)wxStringBase::operator=(psz
); }
1168 // from wxWCharBuffer
1169 wxString
& operator=(const wxWCharBuffer
& psz
)
1170 { (void) operator=((const wchar_t *)psz
); return *this; }
1172 wxString
& operator=(const char* psz
)
1173 { return operator=(wxString(psz
)); }
1175 // from another kind of C string
1176 wxString
& operator=(const unsigned char* psz
);
1178 // from a wide string
1179 wxString
& operator=(const wchar_t *pwz
);
1181 // from wxCharBuffer
1182 wxString
& operator=(const wxCharBuffer
& psz
)
1183 { (void) operator=((const char *)psz
); return *this; }
1184 #endif // Unicode/ANSI
1186 // string concatenation
1187 // in place concatenation
1189 Concatenate and return the result. Note that the left to right
1190 associativity of << allows to write things like "str << str1 << str2
1191 << ..." (unlike with +=)
1194 wxString
& operator<<(const wxString
& s
)
1196 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1197 wxASSERT_MSG( s
.GetStringData()->IsValid(),
1198 _T("did you forget to call UngetWriteBuf()?") );
1204 // string += C string
1205 wxString
& operator<<(const wxChar
*psz
)
1206 { append(psz
); return *this; }
1207 wxString
& operator<<(const wxCStrData
& psz
)
1208 { append(psz
); return *this; }
1210 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1211 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1212 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1213 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1215 // string += buffer (i.e. from wxGetString)
1217 wxString
& operator<<(const wxWCharBuffer
& s
)
1218 { return operator<<((const wchar_t *)s
); }
1219 wxString
& operator+=(const wxWCharBuffer
& s
)
1220 { return operator<<((const wchar_t *)s
); }
1221 #else // !wxUSE_UNICODE
1222 wxString
& operator<<(const wxCharBuffer
& s
)
1223 { return operator<<((const char *)s
); }
1224 wxString
& operator+=(const wxCharBuffer
& s
)
1225 { return operator<<((const char *)s
); }
1226 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1229 // string += C string in Unicode build (with conversion)
1230 wxString
& operator<<(const char *s
)
1231 { return operator<<(wxString(s
)); }
1232 wxString
& operator+=(const char *s
)
1233 { return operator+=(wxString(s
)); }
1234 #endif // wxUSE_UNICODE
1236 // string += C string
1237 wxString
& Append(const wxString
& s
)
1239 // test for empty() to share the string if possible
1246 wxString
& Append(const wxCStrData
& psz
)
1247 { append(psz
); return *this; }
1248 wxString
& Append(const wxChar
* psz
)
1249 { append(psz
); return *this; }
1250 // append count copies of given character
1251 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1252 { append(count
, ch
); return *this; }
1253 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1254 { append(count
, ch
); return *this; }
1255 wxString
& Append(char ch
, size_t count
= 1u)
1256 { append(count
, ch
); return *this; }
1257 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1258 { append(count
, ch
); return *this; }
1259 wxString
& Append(const wxChar
* psz
, size_t nLen
)
1260 { append(psz
, nLen
); return *this; }
1262 // prepend a string, return the string itself
1263 wxString
& Prepend(const wxString
& str
)
1264 { *this = str
+ *this; return *this; }
1266 // non-destructive concatenation
1268 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1269 const wxString
& string2
);
1270 // string with a single char
1271 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1272 // char with a string
1273 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1274 // string with C string
1275 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1277 // C string with string
1278 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
,
1279 const wxString
& string
);
1281 // stream-like functions
1282 // insert an int into string
1283 wxString
& operator<<(int i
)
1284 { return (*this) << Format(_T("%d"), i
); }
1285 // insert an unsigned int into string
1286 wxString
& operator<<(unsigned int ui
)
1287 { return (*this) << Format(_T("%u"), ui
); }
1288 // insert a long into string
1289 wxString
& operator<<(long l
)
1290 { return (*this) << Format(_T("%ld"), l
); }
1291 // insert an unsigned long into string
1292 wxString
& operator<<(unsigned long ul
)
1293 { return (*this) << Format(_T("%lu"), ul
); }
1294 #if defined wxLongLong_t && !defined wxLongLongIsLong
1295 // insert a long long if they exist and aren't longs
1296 wxString
& operator<<(wxLongLong_t ll
)
1298 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1299 return (*this) << Format(fmt
, ll
);
1301 // insert an unsigned long long
1302 wxString
& operator<<(wxULongLong_t ull
)
1304 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1305 return (*this) << Format(fmt
, ull
);
1308 // insert a float into string
1309 wxString
& operator<<(float f
)
1310 { return (*this) << Format(_T("%f"), f
); }
1311 // insert a double into string
1312 wxString
& operator<<(double d
)
1313 { return (*this) << Format(_T("%g"), d
); }
1315 // string comparison
1316 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1317 int Cmp(const wxChar
*psz
) const;
1318 int Cmp(const wxString
& s
) const;
1319 // same as Cmp() but not case-sensitive
1320 int CmpNoCase(const wxChar
*psz
) const;
1321 int CmpNoCase(const wxString
& s
) const;
1322 // test for the string equality, either considering case or not
1323 // (if compareWithCase then the case matters)
1324 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
1325 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
1326 // comparison with a single character: returns true if equal
1327 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1329 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1330 : wxToupper(GetChar(0u)) == wxToupper(c
));
1333 // simple sub-string extraction
1334 // return substring starting at nFirst of length nCount (or till the end
1335 // if nCount = default value)
1336 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1338 // operator version of Mid()
1339 wxString
operator()(size_t start
, size_t len
) const
1340 { return Mid(start
, len
); }
1342 // check if the string starts with the given prefix and return the rest
1343 // of the string in the provided pointer if it is not NULL; otherwise
1345 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1346 // check if the string ends with the given suffix and return the
1347 // beginning of the string before the suffix in the provided pointer if
1348 // it is not NULL; otherwise return false
1349 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1351 // get first nCount characters
1352 wxString
Left(size_t nCount
) const;
1353 // get last nCount characters
1354 wxString
Right(size_t nCount
) const;
1355 // get all characters before the first occurance of ch
1356 // (returns the whole string if ch not found)
1357 wxString
BeforeFirst(wxUniChar ch
) const;
1358 // get all characters before the last occurence of ch
1359 // (returns empty string if ch not found)
1360 wxString
BeforeLast(wxUniChar ch
) const;
1361 // get all characters after the first occurence of ch
1362 // (returns empty string if ch not found)
1363 wxString
AfterFirst(wxUniChar ch
) const;
1364 // get all characters after the last occurence of ch
1365 // (returns the whole string if ch not found)
1366 wxString
AfterLast(wxUniChar ch
) const;
1368 // for compatibility only, use more explicitly named functions above
1369 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1370 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1373 // convert to upper case in place, return the string itself
1374 wxString
& MakeUpper();
1375 // convert to upper case, return the copy of the string
1376 // Here's something to remember: BC++ doesn't like returns in inlines.
1377 wxString
Upper() const ;
1378 // convert to lower case in place, return the string itself
1379 wxString
& MakeLower();
1380 // convert to lower case, return the copy of the string
1381 wxString
Lower() const ;
1383 // trimming/padding whitespace (either side) and truncating
1384 // remove spaces from left or from right (default) side
1385 wxString
& Trim(bool bFromRight
= true);
1386 // add nCount copies chPad in the beginning or at the end (default)
1387 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1389 // searching and replacing
1390 // searching (return starting index, or -1 if not found)
1391 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1392 // searching (return starting index, or -1 if not found)
1393 int Find(const wxChar
*pszSub
) const; // like strstr
1394 // replace first (or all of bReplaceAll) occurences of substring with
1395 // another string, returns the number of replacements made
1396 size_t Replace(const wxChar
*szOld
,
1397 const wxChar
*szNew
,
1398 bool bReplaceAll
= true);
1400 // check if the string contents matches a mask containing '*' and '?'
1401 bool Matches(const wxChar
*szMask
) const;
1403 // conversion to numbers: all functions return true only if the whole
1404 // string is a number and put the value of this number into the pointer
1405 // provided, the base is the numeric base in which the conversion should be
1406 // done and must be comprised between 2 and 36 or be 0 in which case the
1407 // standard C rules apply (leading '0' => octal, "0x" => hex)
1408 // convert to a signed integer
1409 bool ToLong(long *val
, int base
= 10) const;
1410 // convert to an unsigned integer
1411 bool ToULong(unsigned long *val
, int base
= 10) const;
1412 // convert to wxLongLong
1413 #if defined(wxLongLong_t)
1414 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1415 // convert to wxULongLong
1416 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1417 #endif // wxLongLong_t
1418 // convert to a double
1419 bool ToDouble(double *val
) const;
1422 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1423 // formatted input/output
1424 // as sprintf(), returns the number of characters written or < 0 on error
1425 // (take 'this' into account in attribute parameter count)
1426 // int Printf(const wxChar *pszFormat, ...);
1427 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
1428 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1429 // as vprintf(), returns the number of characters written or < 0 on error
1430 int PrintfV(const wxString
& format
, va_list argptr
);
1432 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1433 // returns the string containing the result of Printf() to it
1434 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1435 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, DoFormat
)
1437 // the same as above, but takes a va_list
1438 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1440 // raw access to string memory
1441 // ensure that string has space for at least nLen characters
1442 // only works if the data of this string is not shared
1443 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1444 // minimize the string's memory
1445 // only works if the data of this string is not shared
1447 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1448 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1450 // get writable buffer of at least nLen bytes. Unget() *must* be called
1451 // a.s.a.p. to put string back in a reasonable state!
1452 wxDEPRECATED( wxChar
*GetWriteBuf(size_t nLen
) );
1453 // call this immediately after GetWriteBuf() has been used
1454 wxDEPRECATED( void UngetWriteBuf() );
1455 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1456 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1458 // wxWidgets version 1 compatibility functions
1461 wxString
SubString(size_t from
, size_t to
) const
1462 { return Mid(from
, (to
- from
+ 1)); }
1463 // values for second parameter of CompareTo function
1464 enum caseCompare
{exact
, ignoreCase
};
1465 // values for first parameter of Strip function
1466 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1468 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1470 // (take 'this' into account in attribute parameter count)
1471 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1472 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
1473 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1476 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1477 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1480 size_t Length() const { return length(); }
1481 // Count the number of characters
1482 int Freq(wxUniChar ch
) const;
1484 void LowerCase() { MakeLower(); }
1486 void UpperCase() { MakeUpper(); }
1487 // use Trim except that it doesn't change this string
1488 wxString
Strip(stripType w
= trailing
) const;
1490 // use Find (more general variants not yet supported)
1491 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1492 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1494 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1495 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1497 wxString
& Remove(size_t nStart
, size_t nLen
)
1498 { return (wxString
&)erase( nStart
, nLen
); }
1501 int First( const wxUniChar ch
) const { return Find(ch
); }
1502 int First( char ch
) const { return Find(ch
); }
1503 int First( wchar_t ch
) const { return Find(ch
); }
1504 int First( const wxChar
* psz
) const { return Find(psz
); }
1505 int First( const wxString
&str
) const { return Find(str
); }
1506 int Last( const wxUniChar ch
) const { return Find(ch
, true); }
1507 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1510 bool IsNull() const { return empty(); }
1512 // std::string compatibility functions
1514 // take nLen chars starting at nPos
1515 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1516 : wxStringBase(str
, nPos
, nLen
) { }
1517 // take all characters from pStart to pEnd
1518 wxString(const void *pStart
, const void *pEnd
)
1519 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1520 wxString(const_iterator first
, const_iterator last
)
1521 : wxStringBase(first
, last
) { }
1522 wxString(iterator first
, iterator last
)
1523 : wxStringBase(first
, last
) { }
1525 // lib.string.modifiers
1526 // append elements str[pos], ..., str[pos+n]
1527 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1528 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1530 wxString
& append(const wxString
& str
)
1531 { return (wxString
&)wxStringBase::append(str
); }
1532 wxString
& append(const wxCStrData
& str
)
1533 { return (wxString
&)wxStringBase::append(str
.AsString()); }
1534 // append first n (or all if n == npos) characters of sz
1535 wxString
& append(const wxChar
*sz
)
1536 { return (wxString
&)wxStringBase::append(sz
); }
1537 wxString
& append(const wxChar
*sz
, size_t n
)
1538 { return (wxString
&)wxStringBase::append(sz
, n
); }
1539 // append n copies of ch
1540 wxString
& append(size_t n
, wxUniChar ch
)
1541 { return (wxString
&)wxStringBase::append(n
, ch
); }
1542 // append from first to last
1543 wxString
& append(const_iterator first
, const_iterator last
)
1544 { return (wxString
&)wxStringBase::append(first
, last
); }
1546 // same as `this_string = str'
1547 wxString
& assign(const wxString
& str
)
1548 { return (wxString
&)wxStringBase::assign(str
); }
1549 // same as ` = str[pos..pos + n]
1550 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1551 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1552 // same as `= first n (or all if n == npos) characters of sz'
1553 wxString
& assign(const wxChar
*sz
)
1554 { return (wxString
&)wxStringBase::assign(sz
); }
1555 wxString
& assign(const wxChar
*sz
, size_t n
)
1556 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1557 // same as `= n copies of ch'
1558 wxString
& assign(size_t n
, wxUniChar ch
)
1559 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1560 // assign from first to last
1561 wxString
& assign(const_iterator first
, const_iterator last
)
1562 { return (wxString
&)wxStringBase::assign(first
, last
); }
1564 // string comparison
1565 #if !defined(HAVE_STD_STRING_COMPARE)
1566 int compare(const wxStringBase
& str
) const;
1567 // comparison with a substring
1568 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1569 // comparison of 2 substrings
1570 int compare(size_t nStart
, size_t nLen
,
1571 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1572 // just like strcmp()
1573 int compare(const wxChar
* sz
) const;
1574 // substring comparison with first nCount characters of sz
1575 int compare(size_t nStart
, size_t nLen
,
1576 const wxChar
* sz
, size_t nCount
= npos
) const;
1577 #endif // !defined HAVE_STD_STRING_COMPARE
1579 // insert another string
1580 wxString
& insert(size_t nPos
, const wxString
& str
)
1581 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1582 // insert n chars of str starting at nStart (in str)
1583 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1584 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1585 // insert first n (or all if n == npos) characters of sz
1586 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1587 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1588 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1589 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1590 // insert n copies of ch
1591 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
1592 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1593 iterator
insert(iterator it
, wxUniChar ch
)
1594 { return wxStringBase::insert(it
, ch
); }
1595 void insert(iterator it
, const_iterator first
, const_iterator last
)
1596 { wxStringBase::insert(it
, first
, last
); }
1597 void insert(iterator it
, size_type n
, wxUniChar ch
)
1598 { wxStringBase::insert(it
, n
, ch
); }
1600 // delete characters from nStart to nStart + nLen
1601 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1602 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1603 iterator
erase(iterator first
, iterator last
)
1604 { return wxStringBase::erase(first
, last
); }
1605 iterator
erase(iterator first
)
1606 { return wxStringBase::erase(first
); }
1608 #ifdef wxSTRING_BASE_HASNT_CLEAR
1609 void clear() { erase(); }
1612 // replaces the substring of length nLen starting at nStart
1613 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1614 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1615 // replaces the substring of length nLen starting at nStart
1616 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1617 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1618 // replaces the substring with nCount copies of ch
1619 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
1620 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1621 // replaces a substring with another substring
1622 wxString
& replace(size_t nStart
, size_t nLen
,
1623 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1624 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1626 // replaces the substring with first nCount chars of sz
1627 wxString
& replace(size_t nStart
, size_t nLen
,
1628 const wxChar
* sz
, size_t nCount
)
1629 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1630 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1631 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1632 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1634 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1635 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1636 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1637 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar c
)
1638 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1639 wxString
& replace(iterator first
, iterator last
,
1640 const_iterator first1
, const_iterator last1
)
1641 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1644 wxString
& operator+=(const wxString
& s
)
1645 { return (wxString
&)wxStringBase::operator+=(s
); }
1646 // string += C string
1647 wxString
& operator+=(const wxChar
*psz
)
1648 { return (wxString
&)wxStringBase::operator+=(psz
); }
1649 wxString
& operator+=(const wxCStrData
& s
)
1650 { return (wxString
&)wxStringBase::operator+=(s
.AsString()); }
1652 wxString
& operator+=(wxUniChar ch
)
1653 { return (wxString
&)wxStringBase::operator+=(ch
); }
1654 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
1655 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
1656 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
1660 // helpers for wxStringBuffer and wxStringBufferLength
1661 wxChar
*DoGetWriteBuf(size_t nLen
);
1662 void DoUngetWriteBuf();
1663 void DoUngetWriteBuf(size_t nLen
);
1665 friend class WXDLLIMPEXP_BASE wxStringBuffer
;
1666 friend class WXDLLIMPEXP_BASE wxStringBufferLength
;
1669 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1670 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
1671 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
1675 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1676 #pragma warning (default:4275)
1679 // notice that even though for many compilers the friend declarations above are
1680 // enough, from the point of view of C++ standard we must have the declarations
1681 // here as friend ones are not injected in the enclosing namespace and without
1682 // them the code fails to compile with conforming compilers such as xlC or g++4
1683 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1684 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1685 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1687 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1688 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1690 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
1691 { return string
+ (wxUniChar
)ch
; }
1692 inline wxString
operator+(const wxString
& string
, char ch
)
1693 { return string
+ wxUniChar(ch
); }
1694 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
1695 { return string
+ wxUniChar(ch
); }
1696 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
1697 { return (wxUniChar
)ch
+ string
; }
1698 inline wxString
operator+(char ch
, const wxString
& string
)
1699 { return wxUniChar(ch
) + string
; }
1700 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
1701 { return wxUniChar(ch
) + string
; }
1705 // return an empty wxString (not very useful with wxUSE_STL == 1)
1706 inline const wxString
wxGetEmptyString() { return wxString(); }
1708 // return an empty wxString (more efficient than wxString() here)
1709 inline const wxString
& wxGetEmptyString()
1711 return *(wxString
*)&wxEmptyString
;
1713 #endif // wxUSE_STL/!wxUSE_STL
1715 // ----------------------------------------------------------------------------
1716 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1717 // ----------------------------------------------------------------------------
1721 class WXDLLIMPEXP_BASE wxStringBuffer
1724 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1725 : m_str(str
), m_buf(lenWanted
)
1728 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1730 operator wxChar
*() { return m_buf
.data(); }
1735 wxWCharBuffer m_buf
;
1740 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1743 class WXDLLIMPEXP_BASE wxStringBufferLength
1746 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1747 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1750 ~wxStringBufferLength()
1753 m_str
.assign(m_buf
.data(), m_len
);
1756 operator wxChar
*() { return m_buf
.data(); }
1757 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1762 wxWCharBuffer m_buf
;
1769 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1772 #else // if !wxUSE_STL
1774 class WXDLLIMPEXP_BASE wxStringBuffer
1777 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1778 : m_str(str
), m_buf(NULL
)
1779 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
1781 ~wxStringBuffer() { m_str
.DoUngetWriteBuf(); }
1783 operator wxChar
*() const { return m_buf
; }
1789 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1792 class WXDLLIMPEXP_BASE wxStringBufferLength
1795 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1796 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1798 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
1799 wxASSERT(m_buf
!= NULL
);
1802 ~wxStringBufferLength()
1805 m_str
.DoUngetWriteBuf(m_len
);
1808 operator wxChar
*() const { return m_buf
; }
1809 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1817 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1820 #endif // !wxUSE_STL
1822 // ---------------------------------------------------------------------------
1823 // wxString comparison functions: operator versions are always case sensitive
1824 // ---------------------------------------------------------------------------
1826 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1827 // are used and defining them also for wxString would only result in
1828 // compilation ambiguities when comparing std::string and wxString
1831 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1832 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1833 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1834 { return s1
.Cmp(s2
) == 0; }
1835 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1836 { return s2
.Cmp(s1
) == 0; }
1837 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1838 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1839 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1840 { return s1
.Cmp(s2
) != 0; }
1841 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1842 { return s2
.Cmp(s1
) != 0; }
1843 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1844 { return s1
.Cmp(s2
) < 0; }
1845 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1846 { return s1
.Cmp(s2
) < 0; }
1847 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1848 { return s2
.Cmp(s1
) > 0; }
1849 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1850 { return s1
.Cmp(s2
) > 0; }
1851 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1852 { return s1
.Cmp(s2
) > 0; }
1853 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1854 { return s2
.Cmp(s1
) < 0; }
1855 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1856 { return s1
.Cmp(s2
) <= 0; }
1857 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1858 { return s1
.Cmp(s2
) <= 0; }
1859 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1860 { return s2
.Cmp(s1
) >= 0; }
1861 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1862 { return s1
.Cmp(s2
) >= 0; }
1863 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1864 { return s1
.Cmp(s2
) >= 0; }
1865 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1866 { return s2
.Cmp(s1
) <= 0; }
1869 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1870 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1871 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1872 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1873 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1874 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1875 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1876 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1877 #else // !wxUSE_UNICODE
1878 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1879 { return (s1
.Cmp((const char *)s2
) == 0); }
1880 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1881 { return (s2
.Cmp((const char *)s1
) == 0); }
1882 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1883 { return (s1
.Cmp((const char *)s2
) != 0); }
1884 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1885 { return (s2
.Cmp((const char *)s1
) != 0); }
1886 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1889 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1890 { return string
+ (const wchar_t *)buf
; }
1891 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1892 { return (const wchar_t *)buf
+ string
; }
1893 #else // !wxUSE_UNICODE
1894 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1895 { return string
+ (const char *)buf
; }
1896 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1897 { return (const char *)buf
+ string
; }
1898 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1900 #endif // !wxUSE_STL
1902 // comparison with char (those are not defined by std::[w]string and so should
1903 // be always available)
1904 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1905 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1906 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1907 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1908 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1909 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
1910 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
1911 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
1912 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
1913 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1914 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1915 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1916 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1917 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1918 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
1919 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
1920 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
1921 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
1923 // comparison with C string in Unicode build
1925 inline bool operator==(const wxString
& s1
, const char* s2
)
1926 { return s1
== wxString(s2
); }
1927 inline bool operator==(const char* s1
, const wxString
& s2
)
1928 { return wxString(s1
) == s2
; }
1929 inline bool operator!=(const wxString
& s1
, const char* s2
)
1930 { return s1
!= wxString(s2
); }
1931 inline bool operator!=(const char* s1
, const wxString
& s2
)
1932 { return wxString(s1
) != s2
; }
1933 inline bool operator< (const wxString
& s1
, const char* s2
)
1934 { return s1
< wxString(s2
); }
1935 inline bool operator< (const char* s1
, const wxString
& s2
)
1936 { return wxString(s1
) < s2
; }
1937 inline bool operator> (const wxString
& s1
, const char* s2
)
1938 { return s1
> wxString(s2
); }
1939 inline bool operator> (const char* s1
, const wxString
& s2
)
1940 { return wxString(s1
) > s2
; }
1941 inline bool operator<=(const wxString
& s1
, const char* s2
)
1942 { return s1
<= wxString(s2
); }
1943 inline bool operator<=(const char* s1
, const wxString
& s2
)
1944 { return wxString(s1
) <= s2
; }
1945 inline bool operator>=(const wxString
& s1
, const char* s2
)
1946 { return s1
>= wxString(s2
); }
1947 inline bool operator>=(const char* s1
, const wxString
& s2
)
1948 { return wxString(s1
) >= s2
; }
1949 #endif // wxUSE_UNICODE
1951 // ---------------------------------------------------------------------------
1952 // Implementation only from here until the end of file
1953 // ---------------------------------------------------------------------------
1955 // don't pollute the library user's name space
1956 #undef wxASSERT_VALID_INDEX
1958 #if wxUSE_STD_IOSTREAM
1960 #include "wx/iosfwrap.h"
1962 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1963 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
1965 #endif // wxSTD_STRING_COMPATIBILITY
1967 // ---------------------------------------------------------------------------
1968 // wxCStrData implementation
1969 // ---------------------------------------------------------------------------
1971 inline wxCStrData::wxCStrData(char *buf
)
1972 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
1973 inline wxCStrData::wxCStrData(wchar_t *buf
)
1974 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
1976 inline wxCStrData::~wxCStrData()
1983 inline const wchar_t* wxCStrData::AsWChar() const
1985 inline const char* wxCStrData::AsChar() const
1988 if ( m_offset
== 0 )
1989 return m_str
->wx_str(); // FIXME
1991 return (const wxChar
*)(m_str
->begin() + m_offset
);
1994 inline wxString
wxCStrData::AsString() const
1996 if ( m_offset
== 0 )
1999 return m_str
->Mid(m_offset
);
2002 inline wxCStrData::operator wxString() const { return AsString(); }
2004 inline wxUniChar
wxCStrData::operator*() const
2006 if ( m_str
->empty() )
2007 return wxUniChar(_T('\0'));
2009 return (*m_str
)[m_offset
];
2012 inline wxUniChar
wxCStrData::operator[](size_t n
) const
2014 return m_str
->at(m_offset
+ n
);
2017 // ----------------------------------------------------------------------------
2018 // implementation of wxString inline methods using wxCStrData
2019 // ----------------------------------------------------------------------------
2021 inline wxString
& wxString::operator=(const wxCStrData
& cstr
)
2023 return *this = cstr
.AsString();
2026 // ----------------------------------------------------------------------------
2027 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2028 // ----------------------------------------------------------------------------
2032 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
2033 : m_str(wxStrdupW(cstr
))
2037 #else // !wxUSE_UNICODE
2039 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
2040 : m_str(wxStrdupA(cstr
))
2044 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2046 #endif // _WX_WXSTRINGH__