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 #ifndef wxSIZE_T_IS_UINT
789 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
790 #endif // size_t != unsigned int
792 // this operator is needed to emulate the pointer semantics of c_str():
793 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
794 // (we need both versions to resolve ambiguities):
795 wxCStrData
operator+(int n
) const
796 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
797 wxCStrData
operator+(size_t n
) const
798 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
800 // this operator is need to make expressions like "*c_str()" or
801 // "*(c_str() + 2)" work
802 wxUniChar
operator*() const;
805 const wxString
*m_str
;
809 friend class WXDLLIMPEXP_BASE wxString
;
812 // ----------------------------------------------------------------------------
813 // wxStringPrintfMixin
814 // ---------------------------------------------------------------------------
816 // NB: VC6 has a bug that causes linker errors if you have template methods
817 // in a class using __declspec(dllimport). The solution is to split such
818 // class into two classes, one that contains the template methods and does
819 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
820 // (with DLL linkage).
822 // We only do this for VC6 here, because the code is less efficient
823 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
824 // cannot compile this code.
826 #if defined(__VISUALC__) && __VISUALC__ < 1300
827 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
830 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
831 // this class contains implementation of wxString's vararg methods, it's
832 // exported from wxBase DLL
833 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
836 wxStringPrintfMixinBase() {}
838 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
839 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
842 // this class contains template wrappers for wxString's vararg methods, it's
843 // intentionally *not* exported from the DLL in order to fix the VC6 bug
845 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
848 // to further complicate things, we can't return wxString from
849 // wxStringPrintfMixin::Format() because wxString is not yet declared at
850 // this point; the solution is to use this fake type trait template - this
851 // way the compiler won't know the return type until Format() is used
852 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
853 template<typename T
> struct StringReturnType
855 typedef wxString type
;
859 // these are duplicated wxString methods, they're also declared below
860 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
862 // int Printf(const wxChar *pszFormat, ...);
863 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
864 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
865 WX_DEFINE_VARARG_FUNC(static typename StringReturnType
<T1
>::type
,
867 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
868 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
871 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
873 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
876 // ----------------------------------------------------------------------------
877 // wxString: string class trying to be compatible with std::string, MFC
878 // CString and wxWindows 1.x wxString all at once
879 // ---------------------------------------------------------------------------
881 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
882 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
883 // for dll-interface class 'wxString'" -- this is OK in our case
884 #pragma warning (disable:4275)
887 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
888 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
889 ,public wxStringPrintfMixin
892 // NB: special care was taken in arranging the member functions in such order
893 // that all inline functions can be effectively inlined, verify that all
894 // performance critical functions are still inlined if you change order!
896 // if we hadn't made these operators private, it would be possible to
897 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
898 // converted to char in C and we do have operator=(char)
900 // NB: we don't need other versions (short/long and unsigned) as attempt
901 // to assign another numeric type to wxString will now result in
902 // ambiguity between operator=(char) and operator=(int)
903 wxString
& operator=(int);
905 // these methods are not implemented - there is _no_ conversion from int to
906 // string, you're doing something wrong if the compiler wants to call it!
908 // try `s << i' or `s.Printf("%d", i)' instead
912 // constructors and destructor
913 // ctor for an empty string
914 wxString() : wxStringBase() { }
916 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
917 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
918 // string containing nRepeat copies of ch
919 wxString(wxUniChar ch
, size_t nRepeat
= 1)
920 : wxStringBase(nRepeat
, ch
) { }
921 wxString(size_t nRepeat
, wxUniChar ch
)
922 : wxStringBase(nRepeat
, ch
) { }
923 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
924 : wxStringBase(nRepeat
, ch
) { }
925 wxString(size_t nRepeat
, wxUniCharRef ch
)
926 : wxStringBase(nRepeat
, ch
) { }
927 wxString(char ch
, size_t nRepeat
= 1)
928 : wxStringBase(nRepeat
, ch
) { }
929 wxString(size_t nRepeat
, char ch
)
930 : wxStringBase(nRepeat
, ch
) { }
931 wxString(wchar_t ch
, size_t nRepeat
= 1)
932 : wxStringBase(nRepeat
, ch
) { }
933 wxString(size_t nRepeat
, wchar_t ch
)
934 : wxStringBase(nRepeat
, ch
) { }
935 // ctor takes first nLength characters from C string
936 // (default value of npos means take all the string)
937 wxString(const wxChar
*psz
)
938 : wxStringBase(psz
? psz
: wxT("")) { }
939 wxString(const wxChar
*psz
, size_t nLength
)
940 : wxStringBase(psz
, nLength
) { }
941 wxString(const wxChar
*psz
,
942 const wxMBConv
& WXUNUSED(conv
),
943 size_t nLength
= npos
)
944 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
946 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
947 // implicit conversions from std::string to wxString as this allows to use
948 // the same strings in non-GUI and GUI code, however we don't want to
949 // unconditionally add this ctor as it would make wx lib dependent on
950 // libstdc++ on some Linux versions which is bad, so instead we ask the
951 // client code to define this wxUSE_STD_STRING symbol if they need it
953 wxString(const wxStdString
& s
)
954 : wxStringBase(s
.c_str()) { }
955 #endif // wxUSE_STD_STRING
958 // from multibyte string
959 wxString(const char *psz
,
960 const wxMBConv
& conv
= wxConvLibc
,
961 size_t nLength
= npos
);
962 // from wxWCharBuffer (i.e. return from wxGetString)
963 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
965 // from C string (for compilers using unsigned char)
966 wxString(const unsigned char* psz
)
967 : wxStringBase((const char*)psz
) { }
968 // from part of C string (for compilers using unsigned char)
969 wxString(const unsigned char* psz
, size_t nLength
)
970 : wxStringBase((const char*)psz
, nLength
) { }
973 // from wide (Unicode) string
974 wxString(const wchar_t *pwz
,
975 const wxMBConv
& conv
= wxConvLibc
,
976 size_t nLength
= npos
);
977 #endif // !wxUSE_WCHAR_T
980 wxString(const wxCharBuffer
& psz
)
981 : wxStringBase(psz
) { }
982 #endif // Unicode/ANSI
984 // generic attributes & operations
985 // as standard strlen()
986 size_t Len() const { return length(); }
987 // string contains any characters?
988 bool IsEmpty() const { return empty(); }
989 // empty string is "false", so !str will return true
990 bool operator!() const { return empty(); }
991 // truncate the string to given length
992 wxString
& Truncate(size_t uiLen
);
993 // empty string contents
998 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1000 // empty the string and free memory
1003 wxString
tmp(wxEmptyString
);
1008 // Is an ascii value
1009 bool IsAscii() const;
1011 bool IsNumber() const;
1013 bool IsWord() const;
1015 // data access (all indexes are 0 based)
1017 wxUniChar
GetChar(size_t n
) const
1019 // read/write access
1020 wxUniCharRef
GetWritableChar(size_t n
)
1023 void SetChar(size_t n
, wxUniChar ch
)
1026 // get last character
1027 wxUniChar
Last() const
1029 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1031 return at(length() - 1);
1034 // get writable last character
1037 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1038 return at(length() - 1);
1042 Note that we we must define all of the overloads below to avoid
1043 ambiguity when using str[0].
1045 wxUniChar
operator[](int n
) const
1046 { return wxStringBase::at(n
); }
1047 wxUniChar
operator[](size_t n
) const
1048 { return wxStringBase::at(n
); }
1049 #ifndef wxSIZE_T_IS_UINT
1050 wxUniChar
operator[](unsigned int n
) const
1051 { return wxStringBase::at(n
); }
1052 #endif // size_t != unsigned int
1054 // operator versions of GetWriteableChar()
1055 wxUniCharRef
operator[](int n
)
1056 { return wxStringBase::at(n
); }
1057 wxUniCharRef
operator[](size_t n
)
1058 { return wxStringBase::at(n
); }
1059 #ifndef wxSIZE_T_IS_UINT
1060 wxUniCharRef
operator[](unsigned int n
)
1061 { return wxStringBase::at(n
); }
1062 #endif // size_t != unsigned int
1064 // explicit conversion to C string (use this with printf()!)
1065 wxCStrData
c_str() const { return wxCStrData(this); }
1067 // implicit conversion to C string
1068 operator wxCStrData() const { return c_str(); }
1069 operator const wxChar
*() const { return c_str(); }
1071 // identical to c_str(), for MFC compatibility
1072 const wxCStrData
GetData() const { return c_str(); }
1074 // explicit conversion to C string in internal representation (char*,
1075 // wchar_t*, UTF-8-encoded char*, depending on the build):
1076 const_pointer
wx_str() const { return data(); }
1078 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1079 // converting numbers or strings which are certain not to contain special
1080 // chars (typically system functions, X atoms, environment variables etc.)
1082 // the behaviour of these functions with the strings containing anything
1083 // else than 7 bit ASCII characters is undefined, use at your own risk.
1085 static wxString
FromAscii(const char *ascii
); // string
1086 static wxString
FromAscii(const char ascii
); // char
1087 const wxCharBuffer
ToAscii() const;
1089 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1090 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1091 const char *ToAscii() const { return c_str(); }
1092 #endif // Unicode/!Unicode
1094 // conversions with (possible) format conversions: have to return a
1095 // buffer with temporary data
1097 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1098 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1099 // fn_str() to return a string which should be used with the OS APIs
1100 // accepting the file names. The return value is always the same, but the
1101 // type differs because a function may either return pointer to the buffer
1102 // directly or have to use intermediate buffer for translation.
1104 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1106 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1108 const wxChar
* wc_str() const { return c_str(); }
1110 // for compatibility with !wxUSE_UNICODE version
1111 const wxChar
* wc_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
1114 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1116 const wxChar
* fn_str() const { return c_str(); }
1117 #endif // wxMBFILES/!wxMBFILES
1119 const wxChar
* mb_str() const { return c_str(); }
1121 // for compatibility with wxUSE_UNICODE version
1122 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
1124 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1127 const wxWCharBuffer
wc_str(const wxMBConv
& conv
) const;
1128 #endif // wxUSE_WCHAR_T
1130 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1132 const wxChar
* fn_str() const { return c_str(); }
1134 #endif // Unicode/ANSI
1136 // overloaded assignment
1137 // from another wxString
1138 wxString
& operator=(const wxStringBase
& stringSrc
)
1139 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
1141 wxString
& operator=(wxUniChar ch
)
1142 { return (wxString
&)wxStringBase::operator=(ch
); }
1143 wxString
& operator=(wxUniCharRef ch
)
1144 { return (wxString
&)wxStringBase::operator=((wxUniChar
)ch
); }
1145 wxString
& operator=(char ch
)
1146 { return (wxString
&)wxStringBase::operator=(wxUniChar(ch
)); }
1147 wxString
& operator=(wchar_t ch
)
1148 { return (wxString
&)wxStringBase::operator=(wxUniChar(ch
)); }
1149 // from a C string - STL probably will crash on NULL,
1150 // so we need to compensate in that case
1152 wxString
& operator=(const wxChar
*psz
)
1153 { if(psz
) wxStringBase::operator=(psz
); else Clear(); return *this; }
1155 wxString
& operator=(const wxChar
*psz
)
1156 { return (wxString
&)wxStringBase::operator=(psz
); }
1160 // from wxWCharBuffer
1161 wxString
& operator=(const wxWCharBuffer
& psz
)
1162 { (void) operator=((const wchar_t *)psz
); return *this; }
1164 wxString
& operator=(const char* psz
)
1165 { return operator=(wxString(psz
)); }
1167 // from another kind of C string
1168 wxString
& operator=(const unsigned char* psz
);
1170 // from a wide string
1171 wxString
& operator=(const wchar_t *pwz
);
1173 // from wxCharBuffer
1174 wxString
& operator=(const wxCharBuffer
& psz
)
1175 { (void) operator=((const char *)psz
); return *this; }
1176 #endif // Unicode/ANSI
1178 // string concatenation
1179 // in place concatenation
1181 Concatenate and return the result. Note that the left to right
1182 associativity of << allows to write things like "str << str1 << str2
1183 << ..." (unlike with +=)
1186 wxString
& operator<<(const wxString
& s
)
1188 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1189 wxASSERT_MSG( s
.GetStringData()->IsValid(),
1190 _T("did you forget to call UngetWriteBuf()?") );
1196 // string += C string
1197 wxString
& operator<<(const wxChar
*psz
)
1198 { append(psz
); return *this; }
1199 wxString
& operator<<(const wxCStrData
& psz
)
1200 { append(psz
); return *this; }
1202 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1203 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1204 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1205 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1207 // string += buffer (i.e. from wxGetString)
1209 wxString
& operator<<(const wxWCharBuffer
& s
)
1210 { return operator<<((const wchar_t *)s
); }
1211 wxString
& operator+=(const wxWCharBuffer
& s
)
1212 { return operator<<((const wchar_t *)s
); }
1213 #else // !wxUSE_UNICODE
1214 wxString
& operator<<(const wxCharBuffer
& s
)
1215 { return operator<<((const char *)s
); }
1216 wxString
& operator+=(const wxCharBuffer
& s
)
1217 { return operator<<((const char *)s
); }
1218 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1221 // string += C string in Unicode build (with conversion)
1222 wxString
& operator<<(const char *s
)
1223 { return operator<<(wxString(s
)); }
1224 wxString
& operator+=(const char *s
)
1225 { return operator+=(wxString(s
)); }
1226 #endif // wxUSE_UNICODE
1228 // string += C string
1229 wxString
& Append(const wxString
& s
)
1231 // test for empty() to share the string if possible
1238 wxString
& Append(const wxCStrData
& psz
)
1239 { append(psz
); return *this; }
1240 wxString
& Append(const wxChar
* psz
)
1241 { append(psz
); return *this; }
1242 // append count copies of given character
1243 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1244 { append(count
, ch
); return *this; }
1245 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1246 { append(count
, ch
); return *this; }
1247 wxString
& Append(char ch
, size_t count
= 1u)
1248 { append(count
, ch
); return *this; }
1249 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1250 { append(count
, ch
); return *this; }
1251 wxString
& Append(const wxChar
* psz
, size_t nLen
)
1252 { append(psz
, nLen
); return *this; }
1254 // prepend a string, return the string itself
1255 wxString
& Prepend(const wxString
& str
)
1256 { *this = str
+ *this; return *this; }
1258 // non-destructive concatenation
1260 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1261 const wxString
& string2
);
1262 // string with a single char
1263 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1264 // char with a string
1265 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1266 // string with C string
1267 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1269 // C string with string
1270 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
,
1271 const wxString
& string
);
1273 // stream-like functions
1274 // insert an int into string
1275 wxString
& operator<<(int i
)
1276 { return (*this) << Format(_T("%d"), i
); }
1277 // insert an unsigned int into string
1278 wxString
& operator<<(unsigned int ui
)
1279 { return (*this) << Format(_T("%u"), ui
); }
1280 // insert a long into string
1281 wxString
& operator<<(long l
)
1282 { return (*this) << Format(_T("%ld"), l
); }
1283 // insert an unsigned long into string
1284 wxString
& operator<<(unsigned long ul
)
1285 { return (*this) << Format(_T("%lu"), ul
); }
1286 #if defined wxLongLong_t && !defined wxLongLongIsLong
1287 // insert a long long if they exist and aren't longs
1288 wxString
& operator<<(wxLongLong_t ll
)
1290 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1291 return (*this) << Format(fmt
, ll
);
1293 // insert an unsigned long long
1294 wxString
& operator<<(wxULongLong_t ull
)
1296 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1297 return (*this) << Format(fmt
, ull
);
1300 // insert a float into string
1301 wxString
& operator<<(float f
)
1302 { return (*this) << Format(_T("%f"), f
); }
1303 // insert a double into string
1304 wxString
& operator<<(double d
)
1305 { return (*this) << Format(_T("%g"), d
); }
1307 // string comparison
1308 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1309 int Cmp(const wxChar
*psz
) const;
1310 int Cmp(const wxString
& s
) const;
1311 // same as Cmp() but not case-sensitive
1312 int CmpNoCase(const wxChar
*psz
) const;
1313 int CmpNoCase(const wxString
& s
) const;
1314 // test for the string equality, either considering case or not
1315 // (if compareWithCase then the case matters)
1316 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
1317 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
1318 // comparison with a single character: returns true if equal
1319 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1321 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1322 : wxToupper(GetChar(0u)) == wxToupper(c
));
1325 // simple sub-string extraction
1326 // return substring starting at nFirst of length nCount (or till the end
1327 // if nCount = default value)
1328 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1330 // operator version of Mid()
1331 wxString
operator()(size_t start
, size_t len
) const
1332 { return Mid(start
, len
); }
1334 // check if the string starts with the given prefix and return the rest
1335 // of the string in the provided pointer if it is not NULL; otherwise
1337 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1338 // check if the string ends with the given suffix and return the
1339 // beginning of the string before the suffix in the provided pointer if
1340 // it is not NULL; otherwise return false
1341 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1343 // get first nCount characters
1344 wxString
Left(size_t nCount
) const;
1345 // get last nCount characters
1346 wxString
Right(size_t nCount
) const;
1347 // get all characters before the first occurance of ch
1348 // (returns the whole string if ch not found)
1349 wxString
BeforeFirst(wxUniChar ch
) const;
1350 // get all characters before the last occurence of ch
1351 // (returns empty string if ch not found)
1352 wxString
BeforeLast(wxUniChar ch
) const;
1353 // get all characters after the first occurence of ch
1354 // (returns empty string if ch not found)
1355 wxString
AfterFirst(wxUniChar ch
) const;
1356 // get all characters after the last occurence of ch
1357 // (returns the whole string if ch not found)
1358 wxString
AfterLast(wxUniChar ch
) const;
1360 // for compatibility only, use more explicitly named functions above
1361 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1362 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1365 // convert to upper case in place, return the string itself
1366 wxString
& MakeUpper();
1367 // convert to upper case, return the copy of the string
1368 // Here's something to remember: BC++ doesn't like returns in inlines.
1369 wxString
Upper() const ;
1370 // convert to lower case in place, return the string itself
1371 wxString
& MakeLower();
1372 // convert to lower case, return the copy of the string
1373 wxString
Lower() const ;
1375 // trimming/padding whitespace (either side) and truncating
1376 // remove spaces from left or from right (default) side
1377 wxString
& Trim(bool bFromRight
= true);
1378 // add nCount copies chPad in the beginning or at the end (default)
1379 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1381 // searching and replacing
1382 // searching (return starting index, or -1 if not found)
1383 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1384 // searching (return starting index, or -1 if not found)
1385 int Find(const wxChar
*pszSub
) const; // like strstr
1386 // replace first (or all of bReplaceAll) occurences of substring with
1387 // another string, returns the number of replacements made
1388 size_t Replace(const wxChar
*szOld
,
1389 const wxChar
*szNew
,
1390 bool bReplaceAll
= true);
1392 // check if the string contents matches a mask containing '*' and '?'
1393 bool Matches(const wxChar
*szMask
) const;
1395 // conversion to numbers: all functions return true only if the whole
1396 // string is a number and put the value of this number into the pointer
1397 // provided, the base is the numeric base in which the conversion should be
1398 // done and must be comprised between 2 and 36 or be 0 in which case the
1399 // standard C rules apply (leading '0' => octal, "0x" => hex)
1400 // convert to a signed integer
1401 bool ToLong(long *val
, int base
= 10) const;
1402 // convert to an unsigned integer
1403 bool ToULong(unsigned long *val
, int base
= 10) const;
1404 // convert to wxLongLong
1405 #if defined(wxLongLong_t)
1406 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1407 // convert to wxULongLong
1408 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1409 #endif // wxLongLong_t
1410 // convert to a double
1411 bool ToDouble(double *val
) const;
1414 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1415 // formatted input/output
1416 // as sprintf(), returns the number of characters written or < 0 on error
1417 // (take 'this' into account in attribute parameter count)
1418 // int Printf(const wxChar *pszFormat, ...);
1419 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
1420 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1421 // as vprintf(), returns the number of characters written or < 0 on error
1422 int PrintfV(const wxString
& format
, va_list argptr
);
1424 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1425 // returns the string containing the result of Printf() to it
1426 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1427 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, DoFormat
)
1429 // the same as above, but takes a va_list
1430 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1432 // raw access to string memory
1433 // ensure that string has space for at least nLen characters
1434 // only works if the data of this string is not shared
1435 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1436 // minimize the string's memory
1437 // only works if the data of this string is not shared
1439 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1440 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1442 // get writable buffer of at least nLen bytes. Unget() *must* be called
1443 // a.s.a.p. to put string back in a reasonable state!
1444 wxDEPRECATED( wxChar
*GetWriteBuf(size_t nLen
) );
1445 // call this immediately after GetWriteBuf() has been used
1446 wxDEPRECATED( void UngetWriteBuf() );
1447 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1448 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
1450 // wxWidgets version 1 compatibility functions
1453 wxString
SubString(size_t from
, size_t to
) const
1454 { return Mid(from
, (to
- from
+ 1)); }
1455 // values for second parameter of CompareTo function
1456 enum caseCompare
{exact
, ignoreCase
};
1457 // values for first parameter of Strip function
1458 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1460 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1462 // (take 'this' into account in attribute parameter count)
1463 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1464 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
1465 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1468 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1469 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1472 size_t Length() const { return length(); }
1473 // Count the number of characters
1474 int Freq(wxUniChar ch
) const;
1476 void LowerCase() { MakeLower(); }
1478 void UpperCase() { MakeUpper(); }
1479 // use Trim except that it doesn't change this string
1480 wxString
Strip(stripType w
= trailing
) const;
1482 // use Find (more general variants not yet supported)
1483 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1484 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1486 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1487 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1489 wxString
& Remove(size_t nStart
, size_t nLen
)
1490 { return (wxString
&)erase( nStart
, nLen
); }
1493 int First( const wxUniChar ch
) const { return Find(ch
); }
1494 int First( char ch
) const { return Find(ch
); }
1495 int First( wchar_t ch
) const { return Find(ch
); }
1496 int First( const wxChar
* psz
) const { return Find(psz
); }
1497 int First( const wxString
&str
) const { return Find(str
); }
1498 int Last( const wxUniChar ch
) const { return Find(ch
, true); }
1499 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1502 bool IsNull() const { return empty(); }
1504 // std::string compatibility functions
1506 // take nLen chars starting at nPos
1507 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1508 : wxStringBase(str
, nPos
, nLen
) { }
1509 // take all characters from pStart to pEnd
1510 wxString(const void *pStart
, const void *pEnd
)
1511 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1512 wxString(const_iterator first
, const_iterator last
)
1513 : wxStringBase(first
, last
) { }
1514 wxString(iterator first
, iterator last
)
1515 : wxStringBase(first
, last
) { }
1517 // lib.string.modifiers
1518 // append elements str[pos], ..., str[pos+n]
1519 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1520 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1522 wxString
& append(const wxString
& str
)
1523 { return (wxString
&)wxStringBase::append(str
); }
1524 wxString
& append(const wxCStrData
& str
)
1525 { return (wxString
&)wxStringBase::append(str
.AsString()); }
1526 // append first n (or all if n == npos) characters of sz
1527 wxString
& append(const wxChar
*sz
)
1528 { return (wxString
&)wxStringBase::append(sz
); }
1529 wxString
& append(const wxChar
*sz
, size_t n
)
1530 { return (wxString
&)wxStringBase::append(sz
, n
); }
1531 // append n copies of ch
1532 wxString
& append(size_t n
, wxUniChar ch
)
1533 { return (wxString
&)wxStringBase::append(n
, ch
); }
1534 // append from first to last
1535 wxString
& append(const_iterator first
, const_iterator last
)
1536 { return (wxString
&)wxStringBase::append(first
, last
); }
1538 // same as `this_string = str'
1539 wxString
& assign(const wxString
& str
)
1540 { return (wxString
&)wxStringBase::assign(str
); }
1541 // same as ` = str[pos..pos + n]
1542 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1543 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1544 // same as `= first n (or all if n == npos) characters of sz'
1545 wxString
& assign(const wxChar
*sz
)
1546 { return (wxString
&)wxStringBase::assign(sz
); }
1547 wxString
& assign(const wxChar
*sz
, size_t n
)
1548 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1549 // same as `= n copies of ch'
1550 wxString
& assign(size_t n
, wxUniChar ch
)
1551 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1552 // assign from first to last
1553 wxString
& assign(const_iterator first
, const_iterator last
)
1554 { return (wxString
&)wxStringBase::assign(first
, last
); }
1556 // string comparison
1557 #if !defined(HAVE_STD_STRING_COMPARE)
1558 int compare(const wxStringBase
& str
) const;
1559 // comparison with a substring
1560 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1561 // comparison of 2 substrings
1562 int compare(size_t nStart
, size_t nLen
,
1563 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1564 // just like strcmp()
1565 int compare(const wxChar
* sz
) const;
1566 // substring comparison with first nCount characters of sz
1567 int compare(size_t nStart
, size_t nLen
,
1568 const wxChar
* sz
, size_t nCount
= npos
) const;
1569 #endif // !defined HAVE_STD_STRING_COMPARE
1571 // insert another string
1572 wxString
& insert(size_t nPos
, const wxString
& str
)
1573 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1574 // insert n chars of str starting at nStart (in str)
1575 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1576 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1577 // insert first n (or all if n == npos) characters of sz
1578 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1579 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1580 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1581 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1582 // insert n copies of ch
1583 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
1584 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1585 iterator
insert(iterator it
, wxUniChar ch
)
1586 { return wxStringBase::insert(it
, ch
); }
1587 void insert(iterator it
, const_iterator first
, const_iterator last
)
1588 { wxStringBase::insert(it
, first
, last
); }
1589 void insert(iterator it
, size_type n
, wxUniChar ch
)
1590 { wxStringBase::insert(it
, n
, ch
); }
1592 // delete characters from nStart to nStart + nLen
1593 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1594 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1595 iterator
erase(iterator first
, iterator last
)
1596 { return wxStringBase::erase(first
, last
); }
1597 iterator
erase(iterator first
)
1598 { return wxStringBase::erase(first
); }
1600 #ifdef wxSTRING_BASE_HASNT_CLEAR
1601 void clear() { erase(); }
1604 // replaces the substring of length nLen starting at nStart
1605 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1606 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1607 // replaces the substring of length nLen starting at nStart
1608 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1609 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1610 // replaces the substring with nCount copies of ch
1611 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
1612 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1613 // replaces a substring with another substring
1614 wxString
& replace(size_t nStart
, size_t nLen
,
1615 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1616 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1618 // replaces the substring with first nCount chars of sz
1619 wxString
& replace(size_t nStart
, size_t nLen
,
1620 const wxChar
* sz
, size_t nCount
)
1621 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1622 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1623 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1624 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1626 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1627 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1628 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1629 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar c
)
1630 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1631 wxString
& replace(iterator first
, iterator last
,
1632 const_iterator first1
, const_iterator last1
)
1633 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1636 wxString
& operator+=(const wxString
& s
)
1637 { return (wxString
&)wxStringBase::operator+=(s
); }
1638 // string += C string
1639 wxString
& operator+=(const wxChar
*psz
)
1640 { return (wxString
&)wxStringBase::operator+=(psz
); }
1641 wxString
& operator+=(const wxCStrData
& s
)
1642 { return (wxString
&)wxStringBase::operator+=(s
.AsString()); }
1644 wxString
& operator+=(wxUniChar ch
)
1645 { return (wxString
&)wxStringBase::operator+=(ch
); }
1646 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
1647 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
1648 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
1652 // helpers for wxStringBuffer and wxStringBufferLength
1653 wxChar
*DoGetWriteBuf(size_t nLen
);
1654 void DoUngetWriteBuf();
1655 void DoUngetWriteBuf(size_t nLen
);
1657 friend class WXDLLIMPEXP_BASE wxStringBuffer
;
1658 friend class WXDLLIMPEXP_BASE wxStringBufferLength
;
1661 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1662 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
1663 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
1667 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1668 #pragma warning (default:4275)
1671 // notice that even though for many compilers the friend declarations above are
1672 // enough, from the point of view of C++ standard we must have the declarations
1673 // here as friend ones are not injected in the enclosing namespace and without
1674 // them the code fails to compile with conforming compilers such as xlC or g++4
1675 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1676 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1677 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1679 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1680 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1682 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
1683 { return string
+ (wxUniChar
)ch
; }
1684 inline wxString
operator+(const wxString
& string
, char ch
)
1685 { return string
+ wxUniChar(ch
); }
1686 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
1687 { return string
+ wxUniChar(ch
); }
1688 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
1689 { return (wxUniChar
)ch
+ string
; }
1690 inline wxString
operator+(char ch
, const wxString
& string
)
1691 { return wxUniChar(ch
) + string
; }
1692 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
1693 { return wxUniChar(ch
) + string
; }
1697 // return an empty wxString (not very useful with wxUSE_STL == 1)
1698 inline const wxString
wxGetEmptyString() { return wxString(); }
1700 // return an empty wxString (more efficient than wxString() here)
1701 inline const wxString
& wxGetEmptyString()
1703 return *(wxString
*)&wxEmptyString
;
1705 #endif // wxUSE_STL/!wxUSE_STL
1707 // ----------------------------------------------------------------------------
1708 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1709 // ----------------------------------------------------------------------------
1713 class WXDLLIMPEXP_BASE wxStringBuffer
1716 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1717 : m_str(str
), m_buf(lenWanted
)
1720 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1722 operator wxChar
*() { return m_buf
.data(); }
1727 wxWCharBuffer m_buf
;
1732 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1735 class WXDLLIMPEXP_BASE wxStringBufferLength
1738 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1739 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1742 ~wxStringBufferLength()
1745 m_str
.assign(m_buf
.data(), m_len
);
1748 operator wxChar
*() { return m_buf
.data(); }
1749 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1754 wxWCharBuffer m_buf
;
1761 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1764 #else // if !wxUSE_STL
1766 class WXDLLIMPEXP_BASE wxStringBuffer
1769 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1770 : m_str(str
), m_buf(NULL
)
1771 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
1773 ~wxStringBuffer() { m_str
.DoUngetWriteBuf(); }
1775 operator wxChar
*() const { return m_buf
; }
1781 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1784 class WXDLLIMPEXP_BASE wxStringBufferLength
1787 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1788 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1790 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
1791 wxASSERT(m_buf
!= NULL
);
1794 ~wxStringBufferLength()
1797 m_str
.DoUngetWriteBuf(m_len
);
1800 operator wxChar
*() const { return m_buf
; }
1801 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1809 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1812 #endif // !wxUSE_STL
1814 // ---------------------------------------------------------------------------
1815 // wxString comparison functions: operator versions are always case sensitive
1816 // ---------------------------------------------------------------------------
1818 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1819 // are used and defining them also for wxString would only result in
1820 // compilation ambiguities when comparing std::string and wxString
1823 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1824 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1825 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1826 { return s1
.Cmp(s2
) == 0; }
1827 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1828 { return s2
.Cmp(s1
) == 0; }
1829 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1830 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1831 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1832 { return s1
.Cmp(s2
) != 0; }
1833 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1834 { return s2
.Cmp(s1
) != 0; }
1835 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1836 { return s1
.Cmp(s2
) < 0; }
1837 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1838 { return s1
.Cmp(s2
) < 0; }
1839 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1840 { return s2
.Cmp(s1
) > 0; }
1841 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1842 { return s1
.Cmp(s2
) > 0; }
1843 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1844 { return s1
.Cmp(s2
) > 0; }
1845 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1846 { return s2
.Cmp(s1
) < 0; }
1847 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1848 { return s1
.Cmp(s2
) <= 0; }
1849 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1850 { return s1
.Cmp(s2
) <= 0; }
1851 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1852 { return s2
.Cmp(s1
) >= 0; }
1853 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1854 { return s1
.Cmp(s2
) >= 0; }
1855 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1856 { return s1
.Cmp(s2
) >= 0; }
1857 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1858 { return s2
.Cmp(s1
) <= 0; }
1861 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1862 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1863 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1864 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1865 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1866 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1867 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1868 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1869 #else // !wxUSE_UNICODE
1870 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1871 { return (s1
.Cmp((const char *)s2
) == 0); }
1872 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1873 { return (s2
.Cmp((const char *)s1
) == 0); }
1874 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1875 { return (s1
.Cmp((const char *)s2
) != 0); }
1876 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1877 { return (s2
.Cmp((const char *)s1
) != 0); }
1878 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1881 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1882 { return string
+ (const wchar_t *)buf
; }
1883 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1884 { return (const wchar_t *)buf
+ string
; }
1885 #else // !wxUSE_UNICODE
1886 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1887 { return string
+ (const char *)buf
; }
1888 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1889 { return (const char *)buf
+ string
; }
1890 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1892 #endif // !wxUSE_STL
1894 // comparison with char (those are not defined by std::[w]string and so should
1895 // be always available)
1896 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1897 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1898 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1899 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1900 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1901 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
1902 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
1903 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
1904 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
1905 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1906 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1907 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1908 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1909 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1910 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
1911 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
1912 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
1913 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
1915 // comparison with C string in Unicode build
1917 inline bool operator==(const wxString
& s1
, const char* s2
)
1918 { return s1
== wxString(s2
); }
1919 inline bool operator==(const char* s1
, const wxString
& s2
)
1920 { return wxString(s1
) == s2
; }
1921 inline bool operator!=(const wxString
& s1
, const char* s2
)
1922 { return s1
!= wxString(s2
); }
1923 inline bool operator!=(const char* s1
, const wxString
& s2
)
1924 { return wxString(s1
) != s2
; }
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 #endif // wxUSE_UNICODE
1943 // ---------------------------------------------------------------------------
1944 // Implementation only from here until the end of file
1945 // ---------------------------------------------------------------------------
1947 // don't pollute the library user's name space
1948 #undef wxASSERT_VALID_INDEX
1950 #if wxUSE_STD_IOSTREAM
1952 #include "wx/iosfwrap.h"
1954 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1955 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
1957 #endif // wxSTD_STRING_COMPATIBILITY
1959 // ---------------------------------------------------------------------------
1960 // wxCStrData implementation
1961 // ---------------------------------------------------------------------------
1963 inline wxCStrData::wxCStrData(char *buf
)
1964 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
1965 inline wxCStrData::wxCStrData(wchar_t *buf
)
1966 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
1968 inline wxCStrData::~wxCStrData()
1975 inline const wchar_t* wxCStrData::AsWChar() const
1977 inline const char* wxCStrData::AsChar() const
1980 if ( m_offset
== 0 )
1981 return m_str
->wx_str(); // FIXME
1983 return (const wxChar
*)(m_str
->begin() + m_offset
);
1986 inline wxString
wxCStrData::AsString() const
1988 if ( m_offset
== 0 )
1991 return m_str
->Mid(m_offset
);
1994 inline wxCStrData::operator wxString() const { return AsString(); }
1996 inline wxUniChar
wxCStrData::operator*() const
1998 if ( m_str
->empty() )
1999 return wxUniChar(_T('\0'));
2001 return (*m_str
)[m_offset
];
2004 inline wxUniChar
wxCStrData::operator[](size_t n
) const
2006 return m_str
->at(m_offset
+ n
);
2009 #endif // _WX_WXSTRINGH__