1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/string.cpp
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin, Ryan Norton
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // (c) 2004 Ryan Norton <wxprojects@comcast.net>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
15 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
16 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
17 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
20 // ===========================================================================
21 // headers, declarations, constants
22 // ===========================================================================
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
34 #include "wx/thread.h"
50 // allocating extra space for each string consumes more memory but speeds up
51 // the concatenation operations (nLen is the current string's length)
52 // NB: EXTRA_ALLOC must be >= 0!
53 #define EXTRA_ALLOC (19 - nLen % 16)
56 // string handling functions used by wxString:
57 #if wxUSE_UNICODE_UTF8
58 #define wxStringMemcpy memcpy
59 #define wxStringMemcmp memcmp
60 #define wxStringMemchr memchr
61 #define wxStringStrlen strlen
63 #define wxStringMemcpy wxTmemcpy
64 #define wxStringMemcmp wxTmemcmp
65 #define wxStringMemchr wxTmemchr
66 #define wxStringStrlen wxStrlen
70 // ---------------------------------------------------------------------------
71 // static class variables definition
72 // ---------------------------------------------------------------------------
74 #if !wxUSE_STL_BASED_WXSTRING
75 //According to STL _must_ be a -1 size_t
76 const size_t wxStringImpl::npos
= (size_t) -1;
78 const size_t wxString::npos
= (size_t) -1;
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 #if wxUSE_STL_BASED_WXSTRING
86 extern const wxChar WXDLLIMPEXP_BASE
*wxEmptyString
= _T("");
90 // for an empty string, GetStringData() will return this address: this
91 // structure has the same layout as wxStringData and it's data() method will
92 // return the empty string (dummy pointer)
97 } g_strEmpty
= { {-1, 0, 0}, wxT('\0') };
99 // empty C style string: points to 'string data' byte of g_strEmpty
100 extern const wxChar WXDLLIMPEXP_BASE
*wxEmptyString
= &g_strEmpty
.dummy
;
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 #if wxUSE_STD_IOSTREAM
112 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxString
& str
)
114 return os
<< str
.c_str();
117 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxCStrData
& str
)
119 #if wxUSE_UNICODE && !defined(__BORLANDC__)
120 return os
<< str
.AsWChar();
122 return os
<< str
.AsChar();
126 #endif // wxUSE_STD_IOSTREAM
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 // this small class is used to gather statistics for performance tuning
133 //#define WXSTRING_STATISTICS
134 #ifdef WXSTRING_STATISTICS
138 Averager(const wxChar
*sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
140 { wxPrintf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
142 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
145 size_t m_nCount
, m_nTotal
;
147 } g_averageLength("allocation size"),
148 g_averageSummandLength("summand length"),
149 g_averageConcatHit("hit probability in concat"),
150 g_averageInitialLength("initial string length");
152 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
154 #define STATISTICS_ADD(av, val)
155 #endif // WXSTRING_STATISTICS
157 #if !wxUSE_STL_BASED_WXSTRING
159 // ===========================================================================
160 // wxStringData class deallocation
161 // ===========================================================================
163 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
164 # pragma message (__FILE__ ": building with Multithreaded non DLL runtime has a performance impact on wxString!")
165 void wxStringData::Free()
171 // ===========================================================================
173 // ===========================================================================
175 // takes nLength elements of psz starting at nPos
176 void wxStringImpl::InitWith(const wxChar
*psz
, size_t nPos
, size_t nLength
)
180 // if the length is not given, assume the string to be NUL terminated
181 if ( nLength
== npos
) {
182 wxASSERT_MSG( nPos
<= wxStrlen(psz
), _T("index out of bounds") );
184 nLength
= wxStrlen(psz
+ nPos
);
187 STATISTICS_ADD(InitialLength
, nLength
);
190 // trailing '\0' is written in AllocBuffer()
191 if ( !AllocBuffer(nLength
) ) {
192 wxFAIL_MSG( _T("out of memory in wxStringImpl::InitWith") );
195 wxStringMemcpy(m_pchData
, psz
+ nPos
, nLength
);
199 // poor man's iterators are "void *" pointers
200 wxStringImpl::wxStringImpl(const void *pStart
, const void *pEnd
)
202 if ( pEnd
>= pStart
)
204 InitWith((const wxChar
*)pStart
, 0,
205 (const wxChar
*)pEnd
- (const wxChar
*)pStart
);
209 wxFAIL_MSG( _T("pStart is not before pEnd") );
214 wxStringImpl::wxStringImpl(size_type n
, wxStringCharType ch
)
220 // ---------------------------------------------------------------------------
222 // ---------------------------------------------------------------------------
224 // allocates memory needed to store a C string of length nLen
225 bool wxStringImpl::AllocBuffer(size_t nLen
)
227 // allocating 0 sized buffer doesn't make sense, all empty strings should
229 wxASSERT( nLen
> 0 );
231 // make sure that we don't overflow
232 wxASSERT( nLen
< (INT_MAX
/ sizeof(wxChar
)) -
233 (sizeof(wxStringData
) + EXTRA_ALLOC
+ 1) );
235 STATISTICS_ADD(Length
, nLen
);
238 // 1) one extra character for '\0' termination
239 // 2) sizeof(wxStringData) for housekeeping info
240 wxStringData
* pData
= (wxStringData
*)
241 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(wxChar
));
243 if ( pData
== NULL
) {
244 // allocation failures are handled by the caller
249 pData
->nDataLength
= nLen
;
250 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
251 m_pchData
= pData
->data(); // data starts after wxStringData
252 m_pchData
[nLen
] = wxT('\0');
256 // must be called before changing this string
257 bool wxStringImpl::CopyBeforeWrite()
259 wxStringData
* pData
= GetStringData();
261 if ( pData
->IsShared() ) {
262 pData
->Unlock(); // memory not freed because shared
263 size_t nLen
= pData
->nDataLength
;
264 if ( !AllocBuffer(nLen
) ) {
265 // allocation failures are handled by the caller
268 wxStringMemcpy(m_pchData
, pData
->data(), nLen
);
271 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
276 // must be called before replacing contents of this string
277 bool wxStringImpl::AllocBeforeWrite(size_t nLen
)
279 wxASSERT( nLen
!= 0 ); // doesn't make any sense
281 // must not share string and must have enough space
282 wxStringData
* pData
= GetStringData();
283 if ( pData
->IsShared() || pData
->IsEmpty() ) {
284 // can't work with old buffer, get new one
286 if ( !AllocBuffer(nLen
) ) {
287 // allocation failures are handled by the caller
292 if ( nLen
> pData
->nAllocLength
) {
293 // realloc the buffer instead of calling malloc() again, this is more
295 STATISTICS_ADD(Length
, nLen
);
299 pData
= (wxStringData
*)
300 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
302 if ( pData
== NULL
) {
303 // allocation failures are handled by the caller
304 // keep previous data since reallocation failed
308 pData
->nAllocLength
= nLen
;
309 m_pchData
= pData
->data();
313 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
315 // it doesn't really matter what the string length is as it's going to be
316 // overwritten later but, for extra safety, set it to 0 for now as we may
317 // have some junk in m_pchData
318 GetStringData()->nDataLength
= 0;
323 wxStringImpl
& wxStringImpl::append(size_t n
, wxStringCharType ch
)
325 size_type len
= length();
327 if ( !Alloc(len
+ n
) || !CopyBeforeWrite() ) {
328 wxFAIL_MSG( _T("out of memory in wxStringImpl::append") );
330 GetStringData()->nDataLength
= len
+ n
;
331 m_pchData
[len
+ n
] = '\0';
332 for ( size_t i
= 0; i
< n
; ++i
)
333 m_pchData
[len
+ i
] = ch
;
337 void wxStringImpl::resize(size_t nSize
, wxStringCharType ch
)
339 size_t len
= length();
343 erase(begin() + nSize
, end());
345 else if ( nSize
> len
)
347 append(nSize
- len
, ch
);
349 //else: we have exactly the specified length, nothing to do
352 // allocate enough memory for nLen characters
353 bool wxStringImpl::Alloc(size_t nLen
)
355 wxStringData
*pData
= GetStringData();
356 if ( pData
->nAllocLength
<= nLen
) {
357 if ( pData
->IsEmpty() ) {
360 pData
= (wxStringData
*)
361 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
363 if ( pData
== NULL
) {
364 // allocation failure handled by caller
369 pData
->nDataLength
= 0;
370 pData
->nAllocLength
= nLen
;
371 m_pchData
= pData
->data(); // data starts after wxStringData
372 m_pchData
[0u] = wxT('\0');
374 else if ( pData
->IsShared() ) {
375 pData
->Unlock(); // memory not freed because shared
376 size_t nOldLen
= pData
->nDataLength
;
377 if ( !AllocBuffer(nLen
) ) {
378 // allocation failure handled by caller
381 // +1 to copy the terminator, too
382 memcpy(m_pchData
, pData
->data(), (nOldLen
+1)*sizeof(wxChar
));
383 GetStringData()->nDataLength
= nOldLen
;
388 pData
= (wxStringData
*)
389 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
391 if ( pData
== NULL
) {
392 // allocation failure handled by caller
393 // keep previous data since reallocation failed
397 // it's not important if the pointer changed or not (the check for this
398 // is not faster than assigning to m_pchData in all cases)
399 pData
->nAllocLength
= nLen
;
400 m_pchData
= pData
->data();
403 //else: we've already got enough
407 wxStringImpl::iterator
wxStringImpl::begin()
414 wxStringImpl::iterator
wxStringImpl::end()
418 return m_pchData
+ length();
421 wxStringImpl::iterator
wxStringImpl::erase(iterator it
)
423 size_type idx
= it
- begin();
425 return begin() + idx
;
428 wxStringImpl
& wxStringImpl::erase(size_t nStart
, size_t nLen
)
430 wxASSERT(nStart
<= length());
431 size_t strLen
= length() - nStart
;
432 // delete nLen or up to the end of the string characters
433 nLen
= strLen
< nLen
? strLen
: nLen
;
434 wxStringImpl
strTmp(c_str(), nStart
);
435 strTmp
.append(c_str() + nStart
+ nLen
, length() - nStart
- nLen
);
441 wxStringImpl
& wxStringImpl::insert(size_t nPos
, const wxChar
*sz
, size_t n
)
443 wxASSERT( nPos
<= length() );
445 if ( n
== npos
) n
= wxStrlen(sz
);
446 if ( n
== 0 ) return *this;
448 if ( !Alloc(length() + n
) || !CopyBeforeWrite() ) {
449 wxFAIL_MSG( _T("out of memory in wxStringImpl::insert") );
452 memmove(m_pchData
+ nPos
+ n
, m_pchData
+ nPos
,
453 (length() - nPos
) * sizeof(wxChar
));
454 memcpy(m_pchData
+ nPos
, sz
, n
* sizeof(wxChar
));
455 GetStringData()->nDataLength
= length() + n
;
456 m_pchData
[length()] = '\0';
461 void wxStringImpl::swap(wxStringImpl
& str
)
463 wxStringCharType
* tmp
= str
.m_pchData
;
464 str
.m_pchData
= m_pchData
;
468 size_t wxStringImpl::find(const wxStringImpl
& str
, size_t nStart
) const
470 // deal with the special case of empty string first
471 const size_t nLen
= length();
472 const size_t nLenOther
= str
.length();
476 // empty string is a substring of anything
482 // the other string is non empty so can't be our substring
486 wxASSERT( str
.GetStringData()->IsValid() );
487 wxASSERT( nStart
<= nLen
);
489 const wxStringCharType
* const other
= str
.c_str();
492 const wxStringCharType
* p
=
493 (const wxStringCharType
*)wxStringMemchr(c_str() + nStart
,
500 while ( p
- c_str() + nLenOther
<= nLen
&&
501 wxStringMemcmp(p
, other
, nLenOther
) )
506 p
= (const wxStringCharType
*)
507 wxStringMemchr(p
, *other
, nLen
- (p
- c_str()));
513 return p
- c_str() + nLenOther
<= nLen
? p
- c_str() : npos
;
516 size_t wxStringImpl::find(const wxChar
* sz
, size_t nStart
, size_t n
) const
518 return find(wxStringImpl(sz
, n
), nStart
);
521 size_t wxStringImpl::find(wxStringCharType ch
, size_t nStart
) const
523 wxASSERT( nStart
<= length() );
525 const wxStringCharType
*p
= (const wxStringCharType
*)
526 wxStringMemchr(c_str() + nStart
, ch
, length() - nStart
);
528 return p
== NULL
? npos
: p
- c_str();
531 size_t wxStringImpl::rfind(const wxStringImpl
& str
, size_t nStart
) const
533 wxASSERT( str
.GetStringData()->IsValid() );
534 wxASSERT( nStart
== npos
|| nStart
<= length() );
536 if ( length() >= str
.length() )
538 // avoids a corner case later
539 if ( length() == 0 && str
.length() == 0 )
542 // "top" is the point where search starts from
543 size_t top
= length() - str
.length();
545 if ( nStart
== npos
)
546 nStart
= length() - 1;
550 const wxStringCharType
*cursor
= c_str() + top
;
553 if ( wxStringMemcmp(cursor
, str
.c_str(), str
.length()) == 0 )
555 return cursor
- c_str();
557 } while ( cursor
-- > c_str() );
563 size_t wxStringImpl::rfind(const wxChar
* sz
, size_t nStart
, size_t n
) const
565 return rfind(wxStringImpl(sz
, n
), nStart
);
568 size_t wxStringImpl::rfind(wxStringCharType ch
, size_t nStart
) const
570 if ( nStart
== npos
)
576 wxASSERT( nStart
<= length() );
579 const wxStringCharType
*actual
;
580 for ( actual
= c_str() + ( nStart
== npos
? length() : nStart
+ 1 );
581 actual
> c_str(); --actual
)
583 if ( *(actual
- 1) == ch
)
584 return (actual
- 1) - c_str();
590 wxStringImpl
& wxStringImpl::replace(size_t nStart
, size_t nLen
,
593 wxASSERT_MSG( nStart
<= length(),
594 _T("index out of bounds in wxStringImpl::replace") );
595 size_t strLen
= length() - nStart
;
596 nLen
= strLen
< nLen
? strLen
: nLen
;
599 strTmp
.reserve(length()); // micro optimisation to avoid multiple mem allocs
601 //This is kind of inefficient, but its pretty good considering...
602 //we don't want to use character access operators here because on STL
603 //it will freeze the reference count of strTmp, which means a deep copy
604 //at the end when swap is called
606 //Also, we can't use append with the full character pointer and must
607 //do it manually because this string can contain null characters
608 for(size_t i1
= 0; i1
< nStart
; ++i1
)
609 strTmp
.append(1, this->c_str()[i1
]);
611 //its safe to do the full version here because
612 //sz must be a normal c string
615 for(size_t i2
= nStart
+ nLen
; i2
< length(); ++i2
)
616 strTmp
.append(1, this->c_str()[i2
]);
622 wxStringImpl
& wxStringImpl::replace(size_t nStart
, size_t nLen
,
623 size_t nCount
, wxStringCharType ch
)
625 return replace(nStart
, nLen
, wxStringImpl(nCount
, ch
).c_str());
628 wxStringImpl
& wxStringImpl::replace(size_t nStart
, size_t nLen
,
629 const wxStringImpl
& str
,
630 size_t nStart2
, size_t nLen2
)
632 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
635 wxStringImpl
& wxStringImpl::replace(size_t nStart
, size_t nLen
,
636 const wxChar
* sz
, size_t nCount
)
638 return replace(nStart
, nLen
, wxStringImpl(sz
, nCount
).c_str());
641 wxStringImpl
wxStringImpl::substr(size_t nStart
, size_t nLen
) const
644 nLen
= length() - nStart
;
645 return wxStringImpl(*this, nStart
, nLen
);
648 // assigns one string to another
649 wxStringImpl
& wxStringImpl::operator=(const wxStringImpl
& stringSrc
)
651 wxASSERT( stringSrc
.GetStringData()->IsValid() );
653 // don't copy string over itself
654 if ( m_pchData
!= stringSrc
.m_pchData
) {
655 if ( stringSrc
.GetStringData()->IsEmpty() ) {
660 GetStringData()->Unlock();
661 m_pchData
= stringSrc
.m_pchData
;
662 GetStringData()->Lock();
669 // assigns a single character
670 wxStringImpl
& wxStringImpl::operator=(wxStringCharType ch
)
673 if ( !AssignCopy(1, &c
) ) {
674 wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(wxChar)") );
680 wxStringImpl
& wxStringImpl::operator=(const wxChar
*psz
)
682 if ( !AssignCopy(wxStrlen(psz
), psz
) ) {
683 wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(const wxChar *)") );
688 // helper function: does real copy
689 bool wxStringImpl::AssignCopy(size_t nSrcLen
, const wxChar
*pszSrcData
)
691 if ( nSrcLen
== 0 ) {
695 if ( !AllocBeforeWrite(nSrcLen
) ) {
696 // allocation failure handled by caller
699 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
700 GetStringData()->nDataLength
= nSrcLen
;
701 m_pchData
[nSrcLen
] = wxT('\0');
706 // ---------------------------------------------------------------------------
707 // string concatenation
708 // ---------------------------------------------------------------------------
710 // add something to this string
711 bool wxStringImpl::ConcatSelf(size_t nSrcLen
, const wxChar
*pszSrcData
,
714 STATISTICS_ADD(SummandLength
, nSrcLen
);
716 nSrcLen
= nSrcLen
< nMaxLen
? nSrcLen
: nMaxLen
;
718 // concatenating an empty string is a NOP
720 wxStringData
*pData
= GetStringData();
721 size_t nLen
= pData
->nDataLength
;
722 size_t nNewLen
= nLen
+ nSrcLen
;
724 // alloc new buffer if current is too small
725 if ( pData
->IsShared() ) {
726 STATISTICS_ADD(ConcatHit
, 0);
728 // we have to allocate another buffer
729 wxStringData
* pOldData
= GetStringData();
730 if ( !AllocBuffer(nNewLen
) ) {
731 // allocation failure handled by caller
734 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(wxChar
));
737 else if ( nNewLen
> pData
->nAllocLength
) {
738 STATISTICS_ADD(ConcatHit
, 0);
741 // we have to grow the buffer
742 if ( capacity() < nNewLen
) {
743 // allocation failure handled by caller
748 STATISTICS_ADD(ConcatHit
, 1);
750 // the buffer is already big enough
753 // should be enough space
754 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
756 // fast concatenation - all is done in our buffer
757 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
759 m_pchData
[nNewLen
] = wxT('\0'); // put terminating '\0'
760 GetStringData()->nDataLength
= nNewLen
; // and fix the length
762 //else: the string to append was empty
766 #endif // !wxUSE_STL_BASED_WXSTRING
768 #ifdef HAVE_STD_STRING_COMPARE
770 // NB: Comparison code (both if HAVE_STD_STRING_COMPARE and if not) works with
771 // UTF-8 encoded strings too, thanks to UTF-8's design which allows us to
772 // sort strings in characters code point order by sorting the byte sequence
773 // in byte values order (i.e. what strcmp() and memcmp() do).
775 int wxString::compare(const wxString
& str
) const
777 return m_impl
.compare(str
.m_impl
);
780 int wxString::compare(size_t nStart
, size_t nLen
,
781 const wxString
& str
) const
784 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
785 return m_impl
.compare(pos
, len
, str
.m_impl
);
788 int wxString::compare(size_t nStart
, size_t nLen
,
790 size_t nStart2
, size_t nLen2
) const
793 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
796 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
798 return m_impl
.compare(pos
, len
, str
.m_impl
, pos2
, len2
);
801 int wxString::compare(const char* sz
) const
803 return m_impl
.compare(ImplStr(sz
));
806 int wxString::compare(const wchar_t* sz
) const
808 return m_impl
.compare(ImplStr(sz
));
811 int wxString::compare(size_t nStart
, size_t nLen
,
812 const char* sz
, size_t nCount
) const
815 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
817 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
819 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
822 int wxString::compare(size_t nStart
, size_t nLen
,
823 const wchar_t* sz
, size_t nCount
) const
826 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
828 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
830 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
833 #else // !HAVE_STD_STRING_COMPARE
835 static inline int wxDoCmp(const wxStringCharType
* s1
, size_t l1
,
836 const wxStringCharType
* s2
, size_t l2
)
839 return wxStringMemcmp(s1
, s2
, l1
);
842 int ret
= wxStringMemcmp(s1
, s2
, l1
);
843 return ret
== 0 ? -1 : ret
;
847 int ret
= wxStringMemcmp(s1
, s2
, l2
);
848 return ret
== 0 ? +1 : ret
;
852 int wxString::compare(const wxString
& str
) const
854 return ::wxDoCmp(m_impl
.data(), m_impl
.length(),
855 str
.m_impl
.data(), str
.m_impl
.length());
858 int wxString::compare(size_t nStart
, size_t nLen
,
859 const wxString
& str
) const
861 wxASSERT(nStart
<= length());
862 size_type strLen
= length() - nStart
;
863 nLen
= strLen
< nLen
? strLen
: nLen
;
866 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
868 return ::wxDoCmp(m_impl
.data() + pos
, len
,
869 str
.m_impl
.data(), str
.m_impl
.length());
872 int wxString::compare(size_t nStart
, size_t nLen
,
874 size_t nStart2
, size_t nLen2
) const
876 wxASSERT(nStart
<= length());
877 wxASSERT(nStart2
<= str
.length());
878 size_type strLen
= length() - nStart
,
879 strLen2
= str
.length() - nStart2
;
880 nLen
= strLen
< nLen
? strLen
: nLen
;
881 nLen2
= strLen2
< nLen2
? strLen2
: nLen2
;
884 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
886 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
888 return ::wxDoCmp(m_impl
.data() + pos
, len
,
889 str
.m_impl
.data() + pos2
, len2
);
892 int wxString::compare(const char* sz
) const
894 SubstrBufFromMB
str(ImplStr(sz
, npos
));
895 if ( str
.len
== npos
)
896 str
.len
= wxStringStrlen(str
.data
);
897 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
900 int wxString::compare(const wchar_t* sz
) const
902 SubstrBufFromWC
str(ImplStr(sz
, npos
));
903 if ( str
.len
== npos
)
904 str
.len
= wxStringStrlen(str
.data
);
905 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
908 int wxString::compare(size_t nStart
, size_t nLen
,
909 const char* sz
, size_t nCount
) const
911 wxASSERT(nStart
<= length());
912 size_type strLen
= length() - nStart
;
913 nLen
= strLen
< nLen
? strLen
: nLen
;
916 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
918 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
919 if ( str
.len
== npos
)
920 str
.len
= wxStringStrlen(str
.data
);
922 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
925 int wxString::compare(size_t nStart
, size_t nLen
,
926 const wchar_t* sz
, size_t nCount
) const
928 wxASSERT(nStart
<= length());
929 size_type strLen
= length() - nStart
;
930 nLen
= strLen
< nLen
? strLen
: nLen
;
933 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
935 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
936 if ( str
.len
== npos
)
937 str
.len
= wxStringStrlen(str
.data
);
939 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
942 #endif // HAVE_STD_STRING_COMPARE/!HAVE_STD_STRING_COMPARE
944 // ===========================================================================
945 // wxString class core
946 // ===========================================================================
948 // ---------------------------------------------------------------------------
949 // construction and conversion
950 // ---------------------------------------------------------------------------
954 wxString::SubstrBufFromMB
wxString::ConvertStr(const char *psz
, size_t nLength
,
955 const wxMBConv
& conv
)
958 if ( !psz
|| nLength
== 0 )
959 return SubstrBufFromMB();
961 if ( nLength
== npos
)
965 wxWCharBuffer
wcBuf(conv
.cMB2WC(psz
, nLength
, &wcLen
));
967 return SubstrBufFromMB();
969 return SubstrBufFromMB(wcBuf
, wcLen
);
973 wxString::SubstrBufFromWC
wxString::ConvertStr(const wchar_t *pwz
, size_t nLength
,
974 const wxMBConv
& conv
)
977 if ( !pwz
|| nLength
== 0 )
978 return SubstrBufFromWC();
980 if ( nLength
== npos
)
984 wxCharBuffer
mbBuf(conv
.cWC2MB(pwz
, nLength
, &mbLen
));
986 return SubstrBufFromWC();
988 return SubstrBufFromWC(mbBuf
, mbLen
);
995 // from multibyte string
996 wxString::wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
998 // FIXME-UTF8: this will need changes
1001 if ( psz
&& nLength
!= 0 )
1003 if ( nLength
== npos
)
1009 wxWCharBuffer wbuf
= conv
.cMB2WC(psz
, nLength
, &nLenWide
);
1012 assign(wbuf
, nLenWide
);
1016 wxString::wxString(const char *psz
, size_t nLength
)
1018 assign(psz
, nLength
);
1021 //Convert wxString in Unicode mode to a multi-byte string
1022 const wxCharBuffer
wxString::mb_str(const wxMBConv
& conv
) const
1024 return conv
.cWC2MB(c_str(), length() + 1 /* size, not length */, NULL
);
1032 wxString::wxString(const wchar_t *pwz
, const wxMBConv
& conv
, size_t nLength
)
1034 // FIXME-UTF8: this will need changes
1037 if ( pwz
&& nLength
!= 0 )
1039 if ( nLength
== npos
)
1045 wxCharBuffer buf
= conv
.cWC2MB(pwz
, nLength
, &nLenMB
);
1048 assign(buf
, nLenMB
);
1053 wxString::wxString(const wchar_t *pwz
, size_t nLength
)
1055 assign(pwz
, nLength
);
1058 //Converts this string to a wide character string if unicode
1059 //mode is not enabled and wxUSE_WCHAR_T is enabled
1060 const wxWCharBuffer
wxString::wc_str(const wxMBConv
& conv
) const
1062 return conv
.cMB2WC(c_str(), length() + 1 /* size, not length */, NULL
);
1065 #endif // wxUSE_WCHAR_T
1067 #endif // Unicode/ANSI
1069 // shrink to minimal size (releasing extra memory)
1070 bool wxString::Shrink()
1072 wxString
tmp(begin(), end());
1074 return tmp
.length() == length();
1077 #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1078 // get the pointer to writable buffer of (at least) nLen bytes
1079 wxChar
*wxStringImpl::DoGetWriteBuf(size_t nLen
)
1081 if ( !AllocBeforeWrite(nLen
) ) {
1082 // allocation failure handled by caller
1086 wxASSERT( GetStringData()->nRefs
== 1 );
1087 GetStringData()->Validate(false);
1092 // put string back in a reasonable state after GetWriteBuf
1093 void wxStringImpl::DoUngetWriteBuf()
1095 DoUngetWriteBuf(wxStrlen(m_pchData
));
1098 void wxStringImpl::DoUngetWriteBuf(size_t nLen
)
1100 wxStringData
* const pData
= GetStringData();
1102 wxASSERT_MSG( nLen
< pData
->nAllocLength
, _T("buffer overrun") );
1104 // the strings we store are always NUL-terminated
1105 pData
->data()[nLen
] = _T('\0');
1106 pData
->nDataLength
= nLen
;
1107 pData
->Validate(true);
1110 // deprecated compatibility code:
1111 #if WXWIN_COMPATIBILITY_2_8
1112 wxChar
*wxString::GetWriteBuf(size_t nLen
)
1114 return DoGetWriteBuf(nLen
);
1117 void wxString::UngetWriteBuf()
1122 void wxString::UngetWriteBuf(size_t nLen
)
1124 DoUngetWriteBuf(nLen
);
1126 #endif // WXWIN_COMPATIBILITY_2_8
1128 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1131 // ---------------------------------------------------------------------------
1133 // ---------------------------------------------------------------------------
1135 // all functions are inline in string.h
1137 // ---------------------------------------------------------------------------
1138 // assignment operators
1139 // ---------------------------------------------------------------------------
1143 // same as 'signed char' variant
1144 wxString
& wxString::operator=(const unsigned char* psz
)
1146 *this = (const char *)psz
;
1151 wxString
& wxString::operator=(const wchar_t *pwz
)
1162 * concatenation functions come in 5 flavours:
1164 * char + string and string + char
1165 * C str + string and string + C str
1168 wxString
operator+(const wxString
& str1
, const wxString
& str2
)
1170 #if !wxUSE_STL_BASED_WXSTRING
1171 wxASSERT( str1
.IsValid() );
1172 wxASSERT( str2
.IsValid() );
1181 wxString
operator+(const wxString
& str
, wxUniChar ch
)
1183 #if !wxUSE_STL_BASED_WXSTRING
1184 wxASSERT( str
.IsValid() );
1193 wxString
operator+(wxUniChar ch
, const wxString
& str
)
1195 #if !wxUSE_STL_BASED_WXSTRING
1196 wxASSERT( str
.IsValid() );
1205 wxString
operator+(const wxString
& str
, const char *psz
)
1207 #if !wxUSE_STL_BASED_WXSTRING
1208 wxASSERT( str
.IsValid() );
1212 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
1213 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1221 wxString
operator+(const wxString
& str
, const wchar_t *pwz
)
1223 #if !wxUSE_STL_BASED_WXSTRING
1224 wxASSERT( str
.IsValid() );
1228 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
1229 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1237 wxString
operator+(const char *psz
, const wxString
& str
)
1239 #if !wxUSE_STL_BASED_WXSTRING
1240 wxASSERT( str
.IsValid() );
1244 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
1245 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1253 wxString
operator+(const wchar_t *pwz
, const wxString
& str
)
1255 #if !wxUSE_STL_BASED_WXSTRING
1256 wxASSERT( str
.IsValid() );
1260 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
1261 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1269 // ---------------------------------------------------------------------------
1270 // find_{first,last}_[not]_of functions
1271 // ---------------------------------------------------------------------------
1273 #if !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
1275 // NB: All these functions are implemented with the argument being wxChar*,
1276 // i.e. widechar string in any Unicode build, even though native string
1277 // representation is char* in the UTF-8 build. This is because we couldn't
1278 // use memchr() to determine if a character is in a set encoded as UTF-8.
1280 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
1282 return find_first_of(sz
, nStart
, wxStrlen(sz
));
1285 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
1287 return find_first_not_of(sz
, nStart
, wxStrlen(sz
));
1290 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1292 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
1294 size_t idx
= nStart
;
1295 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
1297 if ( wxTmemchr(sz
, *i
, n
) )
1304 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1306 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
1308 size_t idx
= nStart
;
1309 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
1311 if ( !wxTmemchr(sz
, *i
, n
) )
1319 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
1321 return find_last_of(sz
, nStart
, wxStrlen(sz
));
1324 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
1326 return find_last_not_of(sz
, nStart
, wxStrlen(sz
));
1329 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1331 size_t len
= length();
1333 if ( nStart
== npos
)
1339 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
1342 size_t idx
= nStart
;
1343 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
1344 i
!= rend(); --idx
, ++i
)
1346 if ( wxTmemchr(sz
, *i
, n
) )
1353 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1355 size_t len
= length();
1357 if ( nStart
== npos
)
1363 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
1366 size_t idx
= nStart
;
1367 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
1368 i
!= rend(); --idx
, ++i
)
1370 if ( !wxTmemchr(sz
, *i
, n
) )
1377 size_t wxString::find_first_not_of(wxUniChar ch
, size_t nStart
) const
1379 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
1381 size_t idx
= nStart
;
1382 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
1391 size_t wxString::find_last_not_of(wxUniChar ch
, size_t nStart
) const
1393 size_t len
= length();
1395 if ( nStart
== npos
)
1401 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
1404 size_t idx
= nStart
;
1405 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
1406 i
!= rend(); --idx
, ++i
)
1415 // the functions above were implemented for wchar_t* arguments in Unicode
1416 // build and char* in ANSI build; below are implementations for the other
1419 #define wxOtherCharType char
1420 #define STRCONV (const wxChar*)wxConvLibc.cMB2WC
1422 #define wxOtherCharType wchar_t
1423 #define STRCONV (const wxChar*)wxConvLibc.cWC2MB
1426 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
) const
1427 { return find_first_of(STRCONV(sz
), nStart
); }
1429 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
,
1431 { return find_first_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1432 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
) const
1433 { return find_last_of(STRCONV(sz
), nStart
); }
1434 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
,
1436 { return find_last_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1437 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
1438 { return find_first_not_of(STRCONV(sz
), nStart
); }
1439 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
,
1441 { return find_first_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1442 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
1443 { return find_last_not_of(STRCONV(sz
), nStart
); }
1444 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
,
1446 { return find_last_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1448 #undef wxOtherCharType
1451 #endif // !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
1453 // ===========================================================================
1454 // other common string functions
1455 // ===========================================================================
1457 int wxString::CmpNoCase(const wxString
& s
) const
1459 // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
1462 const_iterator i1
= begin();
1463 const_iterator end1
= end();
1464 const_iterator i2
= s
.begin();
1465 const_iterator end2
= s
.end();
1467 for ( ; i1
!= end1
&& i2
!= end2
; ++idx
, ++i1
, ++i2
)
1469 wxUniChar lower1
= (wxChar
)wxTolower(*i1
);
1470 wxUniChar lower2
= (wxChar
)wxTolower(*i2
);
1471 if ( lower1
!= lower2
)
1472 return lower1
< lower2
? -1 : 1;
1475 size_t len1
= length();
1476 size_t len2
= s
.length();
1480 else if ( len1
> len2
)
1489 #ifndef __SCHAR_MAX__
1490 #define __SCHAR_MAX__ 127
1494 wxString
wxString::FromAscii(const char *ascii
)
1497 return wxEmptyString
;
1499 size_t len
= strlen( ascii
);
1504 wxStringBuffer
buf(res
, len
);
1506 wchar_t *dest
= buf
;
1510 if ( (*dest
++ = (wchar_t)(unsigned char)*ascii
++) == L
'\0' )
1518 wxString
wxString::FromAscii(const char ascii
)
1520 // What do we do with '\0' ?
1523 res
+= (wchar_t)(unsigned char) ascii
;
1528 const wxCharBuffer
wxString::ToAscii() const
1530 // this will allocate enough space for the terminating NUL too
1531 wxCharBuffer
buffer(length());
1534 char *dest
= buffer
.data();
1536 const wchar_t *pwc
= c_str();
1539 *dest
++ = (char)(*pwc
> SCHAR_MAX
? wxT('_') : *pwc
);
1541 // the output string can't have embedded NULs anyhow, so we can safely
1542 // stop at first of them even if we do have any
1552 // extract string of length nCount starting at nFirst
1553 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
1555 size_t nLen
= length();
1557 // default value of nCount is npos and means "till the end"
1558 if ( nCount
== npos
)
1560 nCount
= nLen
- nFirst
;
1563 // out-of-bounds requests return sensible things
1564 if ( nFirst
+ nCount
> nLen
)
1566 nCount
= nLen
- nFirst
;
1569 if ( nFirst
> nLen
)
1571 // AllocCopy() will return empty string
1572 return wxEmptyString
;
1575 wxString
dest(*this, nFirst
, nCount
);
1576 if ( dest
.length() != nCount
)
1578 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
1584 // check that the string starts with prefix and return the rest of the string
1585 // in the provided pointer if it is not NULL, otherwise return false
1586 bool wxString::StartsWith(const wxChar
*prefix
, wxString
*rest
) const
1588 wxASSERT_MSG( prefix
, _T("invalid parameter in wxString::StartsWith") );
1590 // first check if the beginning of the string matches the prefix: note
1591 // that we don't have to check that we don't run out of this string as
1592 // when we reach the terminating NUL, either prefix string ends too (and
1593 // then it's ok) or we break out of the loop because there is no match
1594 const wxChar
*p
= c_str();
1597 if ( *prefix
++ != *p
++ )
1606 // put the rest of the string into provided pointer
1614 // check that the string ends with suffix and return the rest of it in the
1615 // provided pointer if it is not NULL, otherwise return false
1616 bool wxString::EndsWith(const wxChar
*suffix
, wxString
*rest
) const
1618 wxASSERT_MSG( suffix
, _T("invalid parameter in wxString::EndssWith") );
1620 int start
= length() - wxStrlen(suffix
);
1621 if ( start
< 0 || wxStrcmp(wx_str() + start
, suffix
) != 0 )
1626 // put the rest of the string into provided pointer
1627 rest
->assign(*this, 0, start
);
1634 // extract nCount last (rightmost) characters
1635 wxString
wxString::Right(size_t nCount
) const
1637 if ( nCount
> length() )
1640 wxString
dest(*this, length() - nCount
, nCount
);
1641 if ( dest
.length() != nCount
) {
1642 wxFAIL_MSG( _T("out of memory in wxString::Right") );
1647 // get all characters after the last occurence of ch
1648 // (returns the whole string if ch not found)
1649 wxString
wxString::AfterLast(wxUniChar ch
) const
1652 int iPos
= Find(ch
, true);
1653 if ( iPos
== wxNOT_FOUND
)
1656 str
= wx_str() + iPos
+ 1;
1661 // extract nCount first (leftmost) characters
1662 wxString
wxString::Left(size_t nCount
) const
1664 if ( nCount
> length() )
1667 wxString
dest(*this, 0, nCount
);
1668 if ( dest
.length() != nCount
) {
1669 wxFAIL_MSG( _T("out of memory in wxString::Left") );
1674 // get all characters before the first occurence of ch
1675 // (returns the whole string if ch not found)
1676 wxString
wxString::BeforeFirst(wxUniChar ch
) const
1678 int iPos
= Find(ch
);
1679 if ( iPos
== wxNOT_FOUND
) iPos
= length();
1680 return wxString(*this, 0, iPos
);
1683 /// get all characters before the last occurence of ch
1684 /// (returns empty string if ch not found)
1685 wxString
wxString::BeforeLast(wxUniChar ch
) const
1688 int iPos
= Find(ch
, true);
1689 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
1690 str
= wxString(c_str(), iPos
);
1695 /// get all characters after the first occurence of ch
1696 /// (returns empty string if ch not found)
1697 wxString
wxString::AfterFirst(wxUniChar ch
) const
1700 int iPos
= Find(ch
);
1701 if ( iPos
!= wxNOT_FOUND
)
1702 str
= wx_str() + iPos
+ 1;
1707 // replace first (or all) occurences of some substring with another one
1708 size_t wxString::Replace(const wxChar
*szOld
,
1709 const wxChar
*szNew
, bool bReplaceAll
)
1711 // if we tried to replace an empty string we'd enter an infinite loop below
1712 wxCHECK_MSG( szOld
&& *szOld
&& szNew
, 0,
1713 _T("wxString::Replace(): invalid parameter") );
1715 size_t uiCount
= 0; // count of replacements made
1717 size_t uiOldLen
= wxStrlen(szOld
);
1718 size_t uiNewLen
= wxStrlen(szNew
);
1722 while ( this->c_str()[dwPos
] != wxT('\0') )
1724 //DO NOT USE STRSTR HERE
1725 //this string can contain embedded null characters,
1726 //so strstr will function incorrectly
1727 dwPos
= find(szOld
, dwPos
);
1728 if ( dwPos
== npos
)
1729 break; // exit the loop
1732 //replace this occurance of the old string with the new one
1733 replace(dwPos
, uiOldLen
, szNew
, uiNewLen
);
1735 //move up pos past the string that was replaced
1738 //increase replace count
1743 break; // exit the loop
1750 bool wxString::IsAscii() const
1752 const wxChar
*s
= (const wxChar
*) *this;
1754 if(!isascii(*s
)) return(false);
1760 bool wxString::IsWord() const
1762 const wxChar
*s
= (const wxChar
*) *this;
1764 if(!wxIsalpha(*s
)) return(false);
1770 bool wxString::IsNumber() const
1772 const wxChar
*s
= (const wxChar
*) *this;
1774 if ((s
[0] == wxT('-')) || (s
[0] == wxT('+'))) s
++;
1776 if(!wxIsdigit(*s
)) return(false);
1782 wxString
wxString::Strip(stripType w
) const
1785 if ( w
& leading
) s
.Trim(false);
1786 if ( w
& trailing
) s
.Trim(true);
1790 // ---------------------------------------------------------------------------
1792 // ---------------------------------------------------------------------------
1794 wxString
& wxString::MakeUpper()
1796 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1797 *it
= (wxChar
)wxToupper(*it
);
1802 wxString
& wxString::MakeLower()
1804 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1805 *it
= (wxChar
)wxTolower(*it
);
1810 // ---------------------------------------------------------------------------
1811 // trimming and padding
1812 // ---------------------------------------------------------------------------
1814 // some compilers (VC++ 6.0 not to name them) return true for a call to
1815 // isspace('ê') in the C locale which seems to be broken to me, but we have to
1816 // live with this by checking that the character is a 7 bit one - even if this
1817 // may fail to detect some spaces (I don't know if Unicode doesn't have
1818 // space-like symbols somewhere except in the first 128 chars), it is arguably
1819 // still better than trimming away accented letters
1820 inline int wxSafeIsspace(wxChar ch
) { return (ch
< 127) && wxIsspace(ch
); }
1822 // trims spaces (in the sense of isspace) from left or right side
1823 wxString
& wxString::Trim(bool bFromRight
)
1825 // first check if we're going to modify the string at all
1828 (bFromRight
&& wxSafeIsspace(GetChar(length() - 1))) ||
1829 (!bFromRight
&& wxSafeIsspace(GetChar(0u)))
1835 // find last non-space character
1836 reverse_iterator psz
= rbegin();
1837 while ( (psz
!= rend()) && wxSafeIsspace(*psz
) )
1840 // truncate at trailing space start
1841 erase(psz
.base(), end());
1845 // find first non-space character
1846 iterator psz
= begin();
1847 while ( (psz
!= end()) && wxSafeIsspace(*psz
) )
1850 // fix up data and length
1851 erase(begin(), psz
);
1858 // adds nCount characters chPad to the string from either side
1859 wxString
& wxString::Pad(size_t nCount
, wxUniChar chPad
, bool bFromRight
)
1861 wxString
s(chPad
, nCount
);
1874 // truncate the string
1875 wxString
& wxString::Truncate(size_t uiLen
)
1877 if ( uiLen
< length() )
1879 erase(begin() + uiLen
, end());
1881 //else: nothing to do, string is already short enough
1886 // ---------------------------------------------------------------------------
1887 // finding (return wxNOT_FOUND if not found and index otherwise)
1888 // ---------------------------------------------------------------------------
1891 int wxString::Find(wxUniChar ch
, bool bFromEnd
) const
1893 size_type idx
= bFromEnd
? find_last_of(ch
) : find_first_of(ch
);
1895 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1898 // find a sub-string (like strstr)
1899 int wxString::Find(const wxChar
*pszSub
) const
1901 size_type idx
= find(pszSub
);
1903 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1906 // ----------------------------------------------------------------------------
1907 // conversion to numbers
1908 // ----------------------------------------------------------------------------
1910 // the implementation of all the functions below is exactly the same so factor
1913 template <typename T
, typename F
>
1914 bool wxStringToIntType(const wxChar
*start
,
1919 wxCHECK_MSG( val
, false, _T("NULL output pointer") );
1920 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
1927 *val
= (*func
)(start
, &end
, base
);
1929 // return true only if scan was stopped by the terminating NUL and if the
1930 // string was not empty to start with and no under/overflow occurred
1931 return !*end
&& (end
!= start
)
1933 && (errno
!= ERANGE
)
1938 bool wxString::ToLong(long *val
, int base
) const
1940 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtol
);
1943 bool wxString::ToULong(unsigned long *val
, int base
) const
1945 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoul
);
1948 bool wxString::ToLongLong(wxLongLong_t
*val
, int base
) const
1950 #ifdef wxHAS_STRTOLL
1951 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoll
);
1953 // TODO: implement this ourselves
1957 #endif // wxHAS_STRTOLL
1960 bool wxString::ToULongLong(wxULongLong_t
*val
, int base
) const
1962 #ifdef wxHAS_STRTOLL
1963 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoull
);
1965 // TODO: implement this ourselves
1972 bool wxString::ToDouble(double *val
) const
1974 wxCHECK_MSG( val
, false, _T("NULL pointer in wxString::ToDouble") );
1980 const wxChar
*start
= c_str();
1982 *val
= wxStrtod(start
, &end
);
1984 // return true only if scan was stopped by the terminating NUL and if the
1985 // string was not empty to start with and no under/overflow occurred
1986 return !*end
&& (end
!= start
)
1988 && (errno
!= ERANGE
)
1993 // ---------------------------------------------------------------------------
1995 // ---------------------------------------------------------------------------
1998 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1999 wxString
wxStringPrintfMixinBase::DoFormat(const wxChar
*format
, ...)
2001 wxString
wxString::DoFormat(const wxChar
*format
, ...)
2005 va_start(argptr
, format
);
2008 s
.PrintfV(format
, argptr
);
2016 wxString
wxString::FormatV(const wxString
& format
, va_list argptr
)
2019 s
.PrintfV(format
, argptr
);
2023 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2024 int wxStringPrintfMixinBase::DoPrintf(const wxChar
*format
, ...)
2026 int wxString::DoPrintf(const wxChar
*format
, ...)
2030 va_start(argptr
, format
);
2032 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2033 // get a pointer to the wxString instance; we have to use dynamic_cast<>
2034 // because it's the only cast that works safely for downcasting when
2035 // multiple inheritance is used:
2036 wxString
*str
= static_cast<wxString
*>(this);
2038 wxString
*str
= this;
2041 int iLen
= str
->PrintfV(format
, argptr
);
2048 int wxString::PrintfV(const wxString
& format
, va_list argptr
)
2054 wxStringBuffer
tmp(*this, size
+ 1);
2063 // wxVsnprintf() may modify the original arg pointer, so pass it
2066 wxVaCopy(argptrcopy
, argptr
);
2067 int len
= wxVsnprintf(buf
, size
, format
, argptrcopy
);
2070 // some implementations of vsnprintf() don't NUL terminate
2071 // the string if there is not enough space for it so
2072 // always do it manually
2073 buf
[size
] = _T('\0');
2075 // vsnprintf() may return either -1 (traditional Unix behaviour) or the
2076 // total number of characters which would have been written if the
2077 // buffer were large enough (newer standards such as Unix98)
2080 #if wxUSE_WXVSNPRINTF
2081 // we know that our own implementation of wxVsnprintf() returns -1
2082 // only for a format error - thus there's something wrong with
2083 // the user's format string
2085 #else // assume that system version only returns error if not enough space
2086 // still not enough, as we don't know how much we need, double the
2087 // current size of the buffer
2089 #endif // wxUSE_WXVSNPRINTF/!wxUSE_WXVSNPRINTF
2091 else if ( len
>= size
)
2093 #if wxUSE_WXVSNPRINTF
2094 // we know that our own implementation of wxVsnprintf() returns
2095 // size+1 when there's not enough space but that's not the size
2096 // of the required buffer!
2097 size
*= 2; // so we just double the current size of the buffer
2099 // some vsnprintf() implementations NUL-terminate the buffer and
2100 // some don't in len == size case, to be safe always add 1
2104 else // ok, there was enough space
2110 // we could have overshot
2116 // ----------------------------------------------------------------------------
2117 // misc other operations
2118 // ----------------------------------------------------------------------------
2120 // returns true if the string matches the pattern which may contain '*' and
2121 // '?' metacharacters (as usual, '?' matches any character and '*' any number
2123 bool wxString::Matches(const wxChar
*pszMask
) const
2125 // I disable this code as it doesn't seem to be faster (in fact, it seems
2126 // to be much slower) than the old, hand-written code below and using it
2127 // here requires always linking with libregex even if the user code doesn't
2129 #if 0 // wxUSE_REGEX
2130 // first translate the shell-like mask into a regex
2132 pattern
.reserve(wxStrlen(pszMask
));
2144 pattern
+= _T(".*");
2155 // these characters are special in a RE, quote them
2156 // (however note that we don't quote '[' and ']' to allow
2157 // using them for Unix shell like matching)
2158 pattern
+= _T('\\');
2162 pattern
+= *pszMask
;
2170 return wxRegEx(pattern
, wxRE_NOSUB
| wxRE_EXTENDED
).Matches(c_str());
2171 #else // !wxUSE_REGEX
2172 // TODO: this is, of course, awfully inefficient...
2174 // the char currently being checked
2175 const wxChar
*pszTxt
= c_str();
2177 // the last location where '*' matched
2178 const wxChar
*pszLastStarInText
= NULL
;
2179 const wxChar
*pszLastStarInMask
= NULL
;
2182 for ( ; *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
2183 switch ( *pszMask
) {
2185 if ( *pszTxt
== wxT('\0') )
2188 // pszTxt and pszMask will be incremented in the loop statement
2194 // remember where we started to be able to backtrack later
2195 pszLastStarInText
= pszTxt
;
2196 pszLastStarInMask
= pszMask
;
2198 // ignore special chars immediately following this one
2199 // (should this be an error?)
2200 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
2203 // if there is nothing more, match
2204 if ( *pszMask
== wxT('\0') )
2207 // are there any other metacharacters in the mask?
2209 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
2211 if ( pEndMask
!= NULL
) {
2212 // we have to match the string between two metachars
2213 uiLenMask
= pEndMask
- pszMask
;
2216 // we have to match the remainder of the string
2217 uiLenMask
= wxStrlen(pszMask
);
2220 wxString
strToMatch(pszMask
, uiLenMask
);
2221 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
2222 if ( pMatch
== NULL
)
2225 // -1 to compensate "++" in the loop
2226 pszTxt
= pMatch
+ uiLenMask
- 1;
2227 pszMask
+= uiLenMask
- 1;
2232 if ( *pszMask
!= *pszTxt
)
2238 // match only if nothing left
2239 if ( *pszTxt
== wxT('\0') )
2242 // if we failed to match, backtrack if we can
2243 if ( pszLastStarInText
) {
2244 pszTxt
= pszLastStarInText
+ 1;
2245 pszMask
= pszLastStarInMask
;
2247 pszLastStarInText
= NULL
;
2249 // don't bother resetting pszLastStarInMask, it's unnecessary
2255 #endif // wxUSE_REGEX/!wxUSE_REGEX
2258 // Count the number of chars
2259 int wxString::Freq(wxUniChar ch
) const
2262 for ( const_iterator i
= begin(); i
!= end(); ++i
)
2270 // convert to upper case, return the copy of the string
2271 wxString
wxString::Upper() const
2272 { wxString
s(*this); return s
.MakeUpper(); }
2274 // convert to lower case, return the copy of the string
2275 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }
2277 // ============================================================================
2279 // ============================================================================
2281 #include "wx/arrstr.h"
2283 wxArrayString::wxArrayString(size_t sz
, const wxChar
** a
)
2288 for (size_t i
=0; i
< sz
; i
++)
2292 wxArrayString::wxArrayString(size_t sz
, const wxString
* a
)
2297 for (size_t i
=0; i
< sz
; i
++)
2303 // size increment = min(50% of current size, ARRAY_MAXSIZE_INCREMENT)
2304 #define ARRAY_MAXSIZE_INCREMENT 4096
2306 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
2307 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
2311 void wxArrayString::Init(bool autoSort
)
2316 m_autoSort
= autoSort
;
2320 wxArrayString::wxArrayString(const wxArrayString
& src
)
2322 Init(src
.m_autoSort
);
2327 // assignment operator
2328 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
2335 m_autoSort
= src
.m_autoSort
;
2340 void wxArrayString::Copy(const wxArrayString
& src
)
2342 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
2343 Alloc(src
.m_nCount
);
2345 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
2350 void wxArrayString::Grow(size_t nIncrement
)
2352 // only do it if no more place
2353 if ( (m_nSize
- m_nCount
) < nIncrement
) {
2354 // if ARRAY_DEFAULT_INITIAL_SIZE were set to 0, the initially empty would
2355 // be never resized!
2356 #if ARRAY_DEFAULT_INITIAL_SIZE == 0
2357 #error "ARRAY_DEFAULT_INITIAL_SIZE must be > 0!"
2360 if ( m_nSize
== 0 ) {
2361 // was empty, alloc some memory
2362 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
2363 if (m_nSize
< nIncrement
)
2364 m_nSize
= nIncrement
;
2365 m_pItems
= new wxString
[m_nSize
];
2368 // otherwise when it's called for the first time, nIncrement would be 0
2369 // and the array would never be expanded
2370 // add 50% but not too much
2371 size_t ndefIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
2372 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
2373 if ( ndefIncrement
> ARRAY_MAXSIZE_INCREMENT
)
2374 ndefIncrement
= ARRAY_MAXSIZE_INCREMENT
;
2375 if ( nIncrement
< ndefIncrement
)
2376 nIncrement
= ndefIncrement
;
2377 m_nSize
+= nIncrement
;
2378 wxString
*pNew
= new wxString
[m_nSize
];
2380 // copy data to new location
2381 for ( size_t j
= 0; j
< m_nCount
; j
++ )
2382 pNew
[j
] = m_pItems
[j
];
2384 // delete old memory (but do not release the strings!)
2385 wxDELETEA(m_pItems
);
2392 // deletes all the strings from the list
2393 void wxArrayString::Empty()
2398 // as Empty, but also frees memory
2399 void wxArrayString::Clear()
2404 wxDELETEA(m_pItems
);
2408 wxArrayString::~wxArrayString()
2410 wxDELETEA(m_pItems
);
2413 void wxArrayString::reserve(size_t nSize
)
2418 // pre-allocates memory (frees the previous data!)
2419 void wxArrayString::Alloc(size_t nSize
)
2421 // only if old buffer was not big enough
2422 if ( nSize
> m_nSize
) {
2423 wxString
*pNew
= new wxString
[nSize
];
2427 for ( size_t j
= 0; j
< m_nCount
; j
++ )
2428 pNew
[j
] = m_pItems
[j
];
2436 // minimizes the memory usage by freeing unused memory
2437 void wxArrayString::Shrink()
2439 // only do it if we have some memory to free
2440 if( m_nCount
< m_nSize
) {
2441 // allocates exactly as much memory as we need
2442 wxString
*pNew
= new wxString
[m_nCount
];
2444 // copy data to new location
2445 for ( size_t j
= 0; j
< m_nCount
; j
++ )
2446 pNew
[j
] = m_pItems
[j
];
2452 // searches the array for an item (forward or backwards)
2453 int wxArrayString::Index(const wxChar
*sz
, bool bCase
, bool bFromEnd
) const
2456 // use binary search in the sorted array
2457 wxASSERT_MSG( bCase
&& !bFromEnd
,
2458 wxT("search parameters ignored for auto sorted array") );
2467 res
= wxStrcmp(sz
, m_pItems
[i
]);
2479 // use linear search in unsorted array
2481 if ( m_nCount
> 0 ) {
2482 size_t ui
= m_nCount
;
2484 if ( m_pItems
[--ui
].IsSameAs(sz
, bCase
) )
2491 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
2492 if( m_pItems
[ui
].IsSameAs(sz
, bCase
) )
2501 // add item at the end
2502 size_t wxArrayString::Add(const wxString
& str
, size_t nInsert
)
2505 // insert the string at the correct position to keep the array sorted
2513 res
= str
.Cmp(m_pItems
[i
]);
2524 wxASSERT_MSG( lo
== hi
, wxT("binary search broken") );
2526 Insert(str
, lo
, nInsert
);
2533 for (size_t i
= 0; i
< nInsert
; i
++)
2536 m_pItems
[m_nCount
+ i
] = str
;
2538 size_t ret
= m_nCount
;
2539 m_nCount
+= nInsert
;
2544 // add item at the given position
2545 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
, size_t nInsert
)
2547 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Insert") );
2548 wxCHECK_RET( m_nCount
<= m_nCount
+ nInsert
,
2549 wxT("array size overflow in wxArrayString::Insert") );
2553 for (int j
= m_nCount
- nIndex
- 1; j
>= 0; j
--)
2554 m_pItems
[nIndex
+ nInsert
+ j
] = m_pItems
[nIndex
+ j
];
2556 for (size_t i
= 0; i
< nInsert
; i
++)
2558 m_pItems
[nIndex
+ i
] = str
;
2560 m_nCount
+= nInsert
;
2563 // range insert (STL 23.2.4.3)
2565 wxArrayString::insert(iterator it
, const_iterator first
, const_iterator last
)
2567 const int idx
= it
- begin();
2572 // reset "it" since it can change inside Grow()
2575 while ( first
!= last
)
2577 it
= insert(it
, *first
);
2579 // insert returns an iterator to the last element inserted but we need
2580 // insert the next after this one, that is before the next one
2588 void wxArrayString::SetCount(size_t count
)
2593 while ( m_nCount
< count
)
2594 m_pItems
[m_nCount
++] = s
;
2597 // removes item from array (by index)
2598 void wxArrayString::RemoveAt(size_t nIndex
, size_t nRemove
)
2600 wxCHECK_RET( nIndex
< m_nCount
, wxT("bad index in wxArrayString::Remove") );
2601 wxCHECK_RET( nIndex
+ nRemove
<= m_nCount
,
2602 wxT("removing too many elements in wxArrayString::Remove") );
2604 for ( size_t j
= 0; j
< m_nCount
- nIndex
-nRemove
; j
++)
2605 m_pItems
[nIndex
+ j
] = m_pItems
[nIndex
+ nRemove
+ j
];
2607 m_nCount
-= nRemove
;
2610 // removes item from array (by value)
2611 void wxArrayString::Remove(const wxChar
*sz
)
2613 int iIndex
= Index(sz
);
2615 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
2616 wxT("removing inexistent element in wxArrayString::Remove") );
2621 void wxArrayString::assign(const_iterator first
, const_iterator last
)
2623 reserve(last
- first
);
2624 for(; first
!= last
; ++first
)
2628 // ----------------------------------------------------------------------------
2630 // ----------------------------------------------------------------------------
2632 // we can only sort one array at a time with the quick-sort based
2635 // need a critical section to protect access to gs_compareFunction and
2636 // gs_sortAscending variables
2637 static wxCriticalSection gs_critsectStringSort
;
2638 #endif // wxUSE_THREADS
2640 // function to use for string comparaison
2641 static wxArrayString::CompareFunction gs_compareFunction
= NULL
;
2643 // if we don't use the compare function, this flag tells us if we sort the
2644 // array in ascending or descending order
2645 static bool gs_sortAscending
= true;
2647 // function which is called by quick sort
2648 extern "C" int wxC_CALLING_CONV
// LINKAGEMODE
2649 wxStringCompareFunction(const void *first
, const void *second
)
2651 wxString
*strFirst
= (wxString
*)first
;
2652 wxString
*strSecond
= (wxString
*)second
;
2654 if ( gs_compareFunction
) {
2655 return gs_compareFunction(*strFirst
, *strSecond
);
2658 // maybe we should use wxStrcoll
2659 int result
= strFirst
->Cmp(*strSecond
);
2661 return gs_sortAscending
? result
: -result
;
2665 // sort array elements using passed comparaison function
2666 void wxArrayString::Sort(CompareFunction compareFunction
)
2668 wxCRIT_SECT_LOCKER(lockCmpFunc
, gs_critsectStringSort
);
2670 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2671 gs_compareFunction
= compareFunction
;
2675 // reset it to NULL so that Sort(bool) will work the next time
2676 gs_compareFunction
= NULL
;
2681 typedef int (wxC_CALLING_CONV
* wxStringCompareFn
)(const void *first
,
2682 const void *second
);
2685 void wxArrayString::Sort(CompareFunction2 compareFunction
)
2687 qsort(m_pItems
, m_nCount
, sizeof(wxString
), (wxStringCompareFn
)compareFunction
);
2690 void wxArrayString::Sort(bool reverseOrder
)
2692 Sort(reverseOrder
? wxStringSortDescending
: wxStringSortAscending
);
2695 void wxArrayString::DoSort()
2697 wxCHECK_RET( !m_autoSort
, wxT("can't use this method with sorted arrays") );
2699 qsort(m_pItems
, m_nCount
, sizeof(wxString
), wxStringCompareFunction
);
2702 bool wxArrayString::operator==(const wxArrayString
& a
) const
2704 if ( m_nCount
!= a
.m_nCount
)
2707 for ( size_t n
= 0; n
< m_nCount
; n
++ )
2709 if ( Item(n
) != a
[n
] )
2716 #endif // !wxUSE_STL
2718 int wxCMPFUNC_CONV
wxStringSortAscending(wxString
* s1
, wxString
* s2
)
2720 return s1
->Cmp(*s2
);
2723 int wxCMPFUNC_CONV
wxStringSortDescending(wxString
* s1
, wxString
* s2
)
2725 return -s1
->Cmp(*s2
);
2730 // ===========================================================================
2731 // wxJoin and wxSplit
2732 // ===========================================================================
2734 #include "wx/tokenzr.h"
2736 wxString
wxJoin(const wxArrayString
& arr
, const wxChar sep
, const wxChar escape
)
2738 size_t count
= arr
.size();
2740 return wxEmptyString
;
2744 // pre-allocate memory using the estimation of the average length of the
2745 // strings in the given array: this is very imprecise, of course, but
2746 // better than nothing
2747 str
.reserve(count
*(arr
[0].length() + arr
[count
-1].length()) / 2);
2749 if ( escape
== wxT('\0') )
2751 // escaping is disabled:
2752 for ( size_t i
= 0; i
< count
; i
++ )
2759 else // use escape character
2761 for ( size_t n
= 0; n
< count
; n
++ )
2766 for ( wxString::const_iterator i
= arr
[n
].begin(),
2771 const wxChar ch
= *i
;
2773 str
+= escape
; // escape this separator
2779 str
.Shrink(); // release extra memory if we allocated too much
2783 wxArrayString
wxSplit(const wxString
& str
, const wxChar sep
, const wxChar escape
)
2785 if ( escape
== wxT('\0') )
2787 // simple case: we don't need to honour the escape character
2788 return wxStringTokenize(str
, sep
, wxTOKEN_RET_EMPTY_ALL
);
2793 wxChar prev
= wxT('\0');
2795 for ( wxString::const_iterator i
= str
.begin(),
2800 const wxChar ch
= *i
;
2804 if ( prev
== escape
)
2806 // remove the escape character and don't consider this
2807 // occurrence of 'sep' as a real separator
2808 *curr
.rbegin() = sep
;
2810 else // real separator
2812 ret
.push_back(curr
);
2816 else // normal character
2824 // add the last token
2825 if ( !curr
.empty() || prev
== sep
)