1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "string.h"
18 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
19 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
20 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
23 // ===========================================================================
24 // headers, declarations, constants
25 // ===========================================================================
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
36 #include "wx/string.h"
38 #include "wx/thread.h"
50 #include <wchar.h> // for wcsrtombs(), see comments where it's used
53 #ifdef WXSTRING_IS_WXOBJECT
54 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
55 #endif //WXSTRING_IS_WXOBJECT
58 #undef wxUSE_EXPERIMENTAL_PRINTF
59 #define wxUSE_EXPERIMENTAL_PRINTF 1
62 // allocating extra space for each string consumes more memory but speeds up
63 // the concatenation operations (nLen is the current string's length)
64 // NB: EXTRA_ALLOC must be >= 0!
65 #define EXTRA_ALLOC (19 - nLen % 16)
67 // ---------------------------------------------------------------------------
68 // static class variables definition
69 // ---------------------------------------------------------------------------
71 #ifdef wxSTD_STRING_COMPATIBILITY
72 const size_t wxString::npos
= wxSTRING_MAXLEN
;
73 #endif // wxSTD_STRING_COMPATIBILITY
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // for an empty string, GetStringData() will return this address: this
80 // structure has the same layout as wxStringData and it's data() method will
81 // return the empty string (dummy pointer)
86 } g_strEmpty
= { {-1, 0, 0}, wxT('\0') };
88 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
89 // must define this static for VA or else you get multiply defined symbols everywhere
90 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
94 // empty C style string: points to 'string data' byte of g_strEmpty
95 extern const wxChar WXDLLEXPORT
*wxEmptyString
= &g_strEmpty
.dummy
;
97 // ----------------------------------------------------------------------------
98 // conditional compilation
99 // ----------------------------------------------------------------------------
101 #if !defined(__WXSW__) && wxUSE_UNICODE
102 #ifdef wxUSE_EXPERIMENTAL_PRINTF
103 #undef wxUSE_EXPERIMENTAL_PRINTF
105 #define wxUSE_EXPERIMENTAL_PRINTF 1
108 // we want to find out if the current platform supports vsnprintf()-like
109 // function: for Unix this is done with configure, for Windows we test the
110 // compiler explicitly.
112 // FIXME currently, this is only for ANSI (!Unicode) strings, so we call this
113 // function wxVsnprintfA (A for ANSI), should also find one for Unicode
114 // strings in Unicode build
116 #if defined(__VISUALC__) || wxUSE_NORLANDER_HEADERS
117 #define wxVsnprintfA _vsnprintf
120 #ifdef HAVE_VSNPRINTF
121 #define wxVsnprintfA vsnprintf
123 #endif // Windows/!Windows
126 // in this case we'll use vsprintf() (which is ANSI and thus should be
127 // always available), but it's unsafe because it doesn't check for buffer
128 // size - so give a warning
129 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
131 #if defined(__VISUALC__)
132 #pragma message("Using sprintf() because no snprintf()-like function defined")
133 #elif defined(__GNUG__) && !defined(__UNIX__)
134 #warning "Using sprintf() because no snprintf()-like function defined"
135 #elif defined(__MWERKS__)
136 #warning "Using sprintf() because no snprintf()-like function defined"
138 #endif // no vsnprintf
141 // AIX has vsnprintf, but there's no prototype in the system headers.
142 extern "C" int vsnprintf(char* str
, size_t n
, const char* format
, va_list ap
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
151 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
154 // ATTN: you can _not_ use both of these in the same program!
156 istream
& operator>>(istream
& is
, wxString
& WXUNUSED(str
))
161 streambuf
*sb
= is
.rdbuf();
164 int ch
= sb
->sbumpc ();
166 is
.setstate(ios::eofbit
);
169 else if ( isspace(ch
) ) {
181 if ( str
.length() == 0 )
182 is
.setstate(ios::failbit
);
187 ostream
& operator<<(ostream
& os
, const wxString
& str
)
193 #endif //std::string compatibility
195 extern int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
196 const wxChar
*format
, va_list argptr
)
199 // FIXME should use wvsnprintf() or whatever if it's available
201 int iLen
= s
.PrintfV(format
, argptr
);
204 wxStrncpy(buf
, s
.c_str(), iLen
);
209 // vsnprintf() will not terminate the string with '\0' if there is not
210 // enough place, but we want the string to always be NUL terminated
211 int rc
= wxVsnprintfA(buf
, len
- 1, format
, argptr
);
218 #endif // Unicode/ANSI
221 extern int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
222 const wxChar
*format
, ...)
225 va_start(argptr
, format
);
227 int iLen
= wxVsnprintf(buf
, len
, format
, argptr
);
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 // this small class is used to gather statistics for performance tuning
239 //#define WXSTRING_STATISTICS
240 #ifdef WXSTRING_STATISTICS
244 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
246 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
248 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
251 size_t m_nCount
, m_nTotal
;
253 } g_averageLength("allocation size"),
254 g_averageSummandLength("summand length"),
255 g_averageConcatHit("hit probability in concat"),
256 g_averageInitialLength("initial string length");
258 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
260 #define STATISTICS_ADD(av, val)
261 #endif // WXSTRING_STATISTICS
263 // ===========================================================================
264 // wxString class core
265 // ===========================================================================
267 // ---------------------------------------------------------------------------
269 // ---------------------------------------------------------------------------
271 // constructs string of <nLength> copies of character <ch>
272 wxString::wxString(wxChar ch
, size_t nLength
)
277 AllocBuffer(nLength
);
280 // memset only works on char
281 for (size_t n
=0; n
<nLength
; n
++) m_pchData
[n
] = ch
;
283 memset(m_pchData
, ch
, nLength
);
288 // takes nLength elements of psz starting at nPos
289 void wxString::InitWith(const wxChar
*psz
, size_t nPos
, size_t nLength
)
293 if ( nLength
== wxSTRING_MAXLEN
)
294 nLength
= wxStrlen(psz
+ nPos
);
296 wxASSERT_MSG( nPos
+ nLength
<= wxStrlen(psz
), _T("index out of bounds") );
298 STATISTICS_ADD(InitialLength
, nLength
);
301 // trailing '\0' is written in AllocBuffer()
302 AllocBuffer(nLength
);
303 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(wxChar
));
307 #ifdef wxSTD_STRING_COMPATIBILITY
309 // poor man's iterators are "void *" pointers
310 wxString::wxString(const void *pStart
, const void *pEnd
)
312 InitWith((const wxChar
*)pStart
, 0,
313 (const wxChar
*)pEnd
- (const wxChar
*)pStart
);
316 #endif //std::string compatibility
320 // from multibyte string
321 wxString::wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
)
323 // first get necessary size
324 size_t nLen
= psz
? conv
.MB2WC((wchar_t *) NULL
, psz
, 0) : 0;
326 // nLength is number of *Unicode* characters here!
327 if ((nLen
!= (size_t)-1) && (nLen
> nLength
))
331 if ( (nLen
!= 0) && (nLen
!= (size_t)-1) ) {
333 conv
.MB2WC(m_pchData
, psz
, nLen
);
344 wxString::wxString(const wchar_t *pwz
)
346 // first get necessary size
347 size_t nLen
= pwz
? wxWC2MB((char *) NULL
, pwz
, 0) : 0;
350 if ( (nLen
!= 0) && (nLen
!= (size_t)-1) ) {
352 wxWC2MB(m_pchData
, pwz
, nLen
);
358 #endif // wxUSE_WCHAR_T
360 #endif // Unicode/ANSI
362 // ---------------------------------------------------------------------------
364 // ---------------------------------------------------------------------------
366 // allocates memory needed to store a C string of length nLen
367 void wxString::AllocBuffer(size_t nLen
)
369 // allocating 0 sized buffer doesn't make sense, all empty strings should
371 wxASSERT( nLen
> 0 );
373 // make sure that we don't overflow
374 wxASSERT( nLen
< (INT_MAX
/ sizeof(wxChar
)) -
375 (sizeof(wxStringData
) + EXTRA_ALLOC
+ 1) );
377 STATISTICS_ADD(Length
, nLen
);
380 // 1) one extra character for '\0' termination
381 // 2) sizeof(wxStringData) for housekeeping info
382 wxStringData
* pData
= (wxStringData
*)
383 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(wxChar
));
385 pData
->nDataLength
= nLen
;
386 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
387 m_pchData
= pData
->data(); // data starts after wxStringData
388 m_pchData
[nLen
] = wxT('\0');
391 // must be called before changing this string
392 void wxString::CopyBeforeWrite()
394 wxStringData
* pData
= GetStringData();
396 if ( pData
->IsShared() ) {
397 pData
->Unlock(); // memory not freed because shared
398 size_t nLen
= pData
->nDataLength
;
400 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(wxChar
));
403 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
406 // must be called before replacing contents of this string
407 void wxString::AllocBeforeWrite(size_t nLen
)
409 wxASSERT( nLen
!= 0 ); // doesn't make any sense
411 // must not share string and must have enough space
412 wxStringData
* pData
= GetStringData();
413 if ( pData
->IsShared() || pData
->IsEmpty() ) {
414 // can't work with old buffer, get new one
419 if ( nLen
> pData
->nAllocLength
) {
420 // realloc the buffer instead of calling malloc() again, this is more
422 STATISTICS_ADD(Length
, nLen
);
426 wxStringData
*pDataOld
= pData
;
427 pData
= (wxStringData
*)
428 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
433 // FIXME we're going to crash...
437 pData
->nAllocLength
= nLen
;
438 m_pchData
= pData
->data();
441 // now we have enough space, just update the string length
442 pData
->nDataLength
= nLen
;
445 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
448 // allocate enough memory for nLen characters
449 void wxString::Alloc(size_t nLen
)
451 wxStringData
*pData
= GetStringData();
452 if ( pData
->nAllocLength
<= nLen
) {
453 if ( pData
->IsEmpty() ) {
456 wxStringData
* pData
= (wxStringData
*)
457 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
459 pData
->nDataLength
= 0;
460 pData
->nAllocLength
= nLen
;
461 m_pchData
= pData
->data(); // data starts after wxStringData
462 m_pchData
[0u] = wxT('\0');
464 else if ( pData
->IsShared() ) {
465 pData
->Unlock(); // memory not freed because shared
466 size_t nOldLen
= pData
->nDataLength
;
468 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(wxChar
));
473 wxStringData
*pDataOld
= pData
;
474 wxStringData
*p
= (wxStringData
*)
475 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
481 // FIXME what to do on memory error?
485 // it's not important if the pointer changed or not (the check for this
486 // is not faster than assigning to m_pchData in all cases)
487 p
->nAllocLength
= nLen
;
488 m_pchData
= p
->data();
491 //else: we've already got enough
494 // shrink to minimal size (releasing extra memory)
495 void wxString::Shrink()
497 wxStringData
*pData
= GetStringData();
499 // this variable is unused in release build, so avoid the compiler warning
500 // by just not declaring it
504 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(wxChar
));
506 // we rely on a reasonable realloc() implementation here - so far I haven't
507 // seen any which wouldn't behave like this
509 wxASSERT( p
!= NULL
); // can't free memory?
510 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
513 // get the pointer to writable buffer of (at least) nLen bytes
514 wxChar
*wxString::GetWriteBuf(size_t nLen
)
516 AllocBeforeWrite(nLen
);
518 wxASSERT( GetStringData()->nRefs
== 1 );
519 GetStringData()->Validate(FALSE
);
524 // put string back in a reasonable state after GetWriteBuf
525 void wxString::UngetWriteBuf()
527 GetStringData()->nDataLength
= wxStrlen(m_pchData
);
528 GetStringData()->Validate(TRUE
);
531 void wxString::UngetWriteBuf(size_t nLen
)
533 GetStringData()->nDataLength
= nLen
;
534 GetStringData()->Validate(TRUE
);
537 // ---------------------------------------------------------------------------
539 // ---------------------------------------------------------------------------
541 // all functions are inline in string.h
543 // ---------------------------------------------------------------------------
544 // assignment operators
545 // ---------------------------------------------------------------------------
547 // helper function: does real copy
548 void wxString::AssignCopy(size_t nSrcLen
, const wxChar
*pszSrcData
)
550 if ( nSrcLen
== 0 ) {
554 AllocBeforeWrite(nSrcLen
);
555 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
556 GetStringData()->nDataLength
= nSrcLen
;
557 m_pchData
[nSrcLen
] = wxT('\0');
561 // assigns one string to another
562 wxString
& wxString::operator=(const wxString
& stringSrc
)
564 wxASSERT( stringSrc
.GetStringData()->IsValid() );
566 // don't copy string over itself
567 if ( m_pchData
!= stringSrc
.m_pchData
) {
568 if ( stringSrc
.GetStringData()->IsEmpty() ) {
573 GetStringData()->Unlock();
574 m_pchData
= stringSrc
.m_pchData
;
575 GetStringData()->Lock();
582 // assigns a single character
583 wxString
& wxString::operator=(wxChar ch
)
590 wxString
& wxString::operator=(const wxChar
*psz
)
592 AssignCopy(wxStrlen(psz
), psz
);
598 // same as 'signed char' variant
599 wxString
& wxString::operator=(const unsigned char* psz
)
601 *this = (const char *)psz
;
606 wxString
& wxString::operator=(const wchar_t *pwz
)
616 // ---------------------------------------------------------------------------
617 // string concatenation
618 // ---------------------------------------------------------------------------
620 // add something to this string
621 void wxString::ConcatSelf(int nSrcLen
, const wxChar
*pszSrcData
)
623 STATISTICS_ADD(SummandLength
, nSrcLen
);
625 // concatenating an empty string is a NOP
627 wxStringData
*pData
= GetStringData();
628 size_t nLen
= pData
->nDataLength
;
629 size_t nNewLen
= nLen
+ nSrcLen
;
631 // alloc new buffer if current is too small
632 if ( pData
->IsShared() ) {
633 STATISTICS_ADD(ConcatHit
, 0);
635 // we have to allocate another buffer
636 wxStringData
* pOldData
= GetStringData();
637 AllocBuffer(nNewLen
);
638 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(wxChar
));
641 else if ( nNewLen
> pData
->nAllocLength
) {
642 STATISTICS_ADD(ConcatHit
, 0);
644 // we have to grow the buffer
648 STATISTICS_ADD(ConcatHit
, 1);
650 // the buffer is already big enough
653 // should be enough space
654 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
656 // fast concatenation - all is done in our buffer
657 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
659 m_pchData
[nNewLen
] = wxT('\0'); // put terminating '\0'
660 GetStringData()->nDataLength
= nNewLen
; // and fix the length
662 //else: the string to append was empty
666 * concatenation functions come in 5 flavours:
668 * char + string and string + char
669 * C str + string and string + C str
672 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
674 wxASSERT( string1
.GetStringData()->IsValid() );
675 wxASSERT( string2
.GetStringData()->IsValid() );
677 wxString s
= string1
;
683 wxString
operator+(const wxString
& string
, wxChar ch
)
685 wxASSERT( string
.GetStringData()->IsValid() );
693 wxString
operator+(wxChar ch
, const wxString
& string
)
695 wxASSERT( string
.GetStringData()->IsValid() );
703 wxString
operator+(const wxString
& string
, const wxChar
*psz
)
705 wxASSERT( string
.GetStringData()->IsValid() );
708 s
.Alloc(wxStrlen(psz
) + string
.Len());
715 wxString
operator+(const wxChar
*psz
, const wxString
& string
)
717 wxASSERT( string
.GetStringData()->IsValid() );
720 s
.Alloc(wxStrlen(psz
) + string
.Len());
727 // ===========================================================================
728 // other common string functions
729 // ===========================================================================
731 // ---------------------------------------------------------------------------
732 // simple sub-string extraction
733 // ---------------------------------------------------------------------------
735 // helper function: clone the data attached to this string
736 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
738 if ( nCopyLen
== 0 ) {
742 dest
.AllocBuffer(nCopyLen
);
743 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(wxChar
));
747 // extract string of length nCount starting at nFirst
748 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
750 wxStringData
*pData
= GetStringData();
751 size_t nLen
= pData
->nDataLength
;
753 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
754 if ( nCount
== wxSTRING_MAXLEN
)
756 nCount
= nLen
- nFirst
;
759 // out-of-bounds requests return sensible things
760 if ( nFirst
+ nCount
> nLen
)
762 nCount
= nLen
- nFirst
;
767 // AllocCopy() will return empty string
772 AllocCopy(dest
, nCount
, nFirst
);
777 // extract nCount last (rightmost) characters
778 wxString
wxString::Right(size_t nCount
) const
780 if ( nCount
> (size_t)GetStringData()->nDataLength
)
781 nCount
= GetStringData()->nDataLength
;
784 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
788 // get all characters after the last occurence of ch
789 // (returns the whole string if ch not found)
790 wxString
wxString::AfterLast(wxChar ch
) const
793 int iPos
= Find(ch
, TRUE
);
794 if ( iPos
== wxNOT_FOUND
)
797 str
= c_str() + iPos
+ 1;
802 // extract nCount first (leftmost) characters
803 wxString
wxString::Left(size_t nCount
) const
805 if ( nCount
> (size_t)GetStringData()->nDataLength
)
806 nCount
= GetStringData()->nDataLength
;
809 AllocCopy(dest
, nCount
, 0);
813 // get all characters before the first occurence of ch
814 // (returns the whole string if ch not found)
815 wxString
wxString::BeforeFirst(wxChar ch
) const
818 for ( const wxChar
*pc
= m_pchData
; *pc
!= wxT('\0') && *pc
!= ch
; pc
++ )
824 /// get all characters before the last occurence of ch
825 /// (returns empty string if ch not found)
826 wxString
wxString::BeforeLast(wxChar ch
) const
829 int iPos
= Find(ch
, TRUE
);
830 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
831 str
= wxString(c_str(), iPos
);
836 /// get all characters after the first occurence of ch
837 /// (returns empty string if ch not found)
838 wxString
wxString::AfterFirst(wxChar ch
) const
842 if ( iPos
!= wxNOT_FOUND
)
843 str
= c_str() + iPos
+ 1;
848 // replace first (or all) occurences of some substring with another one
849 size_t wxString::Replace(const wxChar
*szOld
, const wxChar
*szNew
, bool bReplaceAll
)
851 size_t uiCount
= 0; // count of replacements made
853 size_t uiOldLen
= wxStrlen(szOld
);
856 const wxChar
*pCurrent
= m_pchData
;
857 const wxChar
*pSubstr
;
858 while ( *pCurrent
!= wxT('\0') ) {
859 pSubstr
= wxStrstr(pCurrent
, szOld
);
860 if ( pSubstr
== NULL
) {
861 // strTemp is unused if no replacements were made, so avoid the copy
865 strTemp
+= pCurrent
; // copy the rest
866 break; // exit the loop
869 // take chars before match
870 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
872 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
877 if ( !bReplaceAll
) {
878 strTemp
+= pCurrent
; // copy the rest
879 break; // exit the loop
884 // only done if there were replacements, otherwise would have returned above
890 bool wxString::IsAscii() const
892 const wxChar
*s
= (const wxChar
*) *this;
894 if(!isascii(*s
)) return(FALSE
);
900 bool wxString::IsWord() const
902 const wxChar
*s
= (const wxChar
*) *this;
904 if(!wxIsalpha(*s
)) return(FALSE
);
910 bool wxString::IsNumber() const
912 const wxChar
*s
= (const wxChar
*) *this;
914 if(!wxIsdigit(*s
)) return(FALSE
);
920 wxString
wxString::Strip(stripType w
) const
923 if ( w
& leading
) s
.Trim(FALSE
);
924 if ( w
& trailing
) s
.Trim(TRUE
);
928 // ---------------------------------------------------------------------------
930 // ---------------------------------------------------------------------------
932 wxString
& wxString::MakeUpper()
936 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
937 *p
= (wxChar
)wxToupper(*p
);
942 wxString
& wxString::MakeLower()
946 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
947 *p
= (wxChar
)wxTolower(*p
);
952 // ---------------------------------------------------------------------------
953 // trimming and padding
954 // ---------------------------------------------------------------------------
956 // trims spaces (in the sense of isspace) from left or right side
957 wxString
& wxString::Trim(bool bFromRight
)
959 // first check if we're going to modify the string at all
962 (bFromRight
&& wxIsspace(GetChar(Len() - 1))) ||
963 (!bFromRight
&& wxIsspace(GetChar(0u)))
967 // ok, there is at least one space to trim
972 // find last non-space character
973 wxChar
*psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
974 while ( wxIsspace(*psz
) && (psz
>= m_pchData
) )
977 // truncate at trailing space start
979 GetStringData()->nDataLength
= psz
- m_pchData
;
983 // find first non-space character
984 const wxChar
*psz
= m_pchData
;
985 while ( wxIsspace(*psz
) )
988 // fix up data and length
989 int nDataLength
= GetStringData()->nDataLength
- (psz
- (const wxChar
*) m_pchData
);
990 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(wxChar
));
991 GetStringData()->nDataLength
= nDataLength
;
998 // adds nCount characters chPad to the string from either side
999 wxString
& wxString::Pad(size_t nCount
, wxChar chPad
, bool bFromRight
)
1001 wxString
s(chPad
, nCount
);
1014 // truncate the string
1015 wxString
& wxString::Truncate(size_t uiLen
)
1017 if ( uiLen
< Len() ) {
1020 *(m_pchData
+ uiLen
) = wxT('\0');
1021 GetStringData()->nDataLength
= uiLen
;
1023 //else: nothing to do, string is already short enough
1028 // ---------------------------------------------------------------------------
1029 // finding (return wxNOT_FOUND if not found and index otherwise)
1030 // ---------------------------------------------------------------------------
1033 int wxString::Find(wxChar ch
, bool bFromEnd
) const
1035 const wxChar
*psz
= bFromEnd
? wxStrrchr(m_pchData
, ch
) : wxStrchr(m_pchData
, ch
);
1037 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
1040 // find a sub-string (like strstr)
1041 int wxString::Find(const wxChar
*pszSub
) const
1043 const wxChar
*psz
= wxStrstr(m_pchData
, pszSub
);
1045 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
1048 // ----------------------------------------------------------------------------
1049 // conversion to numbers
1050 // ----------------------------------------------------------------------------
1052 bool wxString::ToLong(long *val
) const
1054 wxCHECK_MSG( val
, FALSE
, _T("NULL pointer in wxString::ToLong") );
1056 const wxChar
*start
= c_str();
1058 *val
= wxStrtol(start
, &end
, 10);
1060 // return TRUE only if scan was stopped by the terminating NUL and if the
1061 // string was not empty to start with
1062 return !*end
&& (end
!= start
);
1065 bool wxString::ToULong(unsigned long *val
) const
1067 wxCHECK_MSG( val
, FALSE
, _T("NULL pointer in wxString::ToULong") );
1069 const wxChar
*start
= c_str();
1071 *val
= wxStrtoul(start
, &end
, 10);
1073 // return TRUE only if scan was stopped by the terminating NUL and if the
1074 // string was not empty to start with
1075 return !*end
&& (end
!= start
);
1078 bool wxString::ToDouble(double *val
) const
1080 wxCHECK_MSG( val
, FALSE
, _T("NULL pointer in wxString::ToDouble") );
1082 const wxChar
*start
= c_str();
1084 *val
= wxStrtod(start
, &end
);
1086 // return TRUE only if scan was stopped by the terminating NUL and if the
1087 // string was not empty to start with
1088 return !*end
&& (end
!= start
);
1091 // ---------------------------------------------------------------------------
1093 // ---------------------------------------------------------------------------
1096 wxString
wxString::Format(const wxChar
*pszFormat
, ...)
1099 va_start(argptr
, pszFormat
);
1102 s
.PrintfV(pszFormat
, argptr
);
1110 wxString
wxString::FormatV(const wxChar
*pszFormat
, va_list argptr
)
1113 s
.Printf(pszFormat
, argptr
);
1117 int wxString::Printf(const wxChar
*pszFormat
, ...)
1120 va_start(argptr
, pszFormat
);
1122 int iLen
= PrintfV(pszFormat
, argptr
);
1129 int wxString::PrintfV(const wxChar
* pszFormat
, va_list argptr
)
1131 #if wxUSE_EXPERIMENTAL_PRINTF
1132 // the new implementation
1134 // buffer to avoid dynamic memory allocation each time for small strings
1135 char szScratch
[1024];
1138 for (size_t n
= 0; pszFormat
[n
]; n
++)
1139 if (pszFormat
[n
] == wxT('%')) {
1140 static char s_szFlags
[256] = "%";
1142 bool adj_left
= FALSE
, in_prec
= FALSE
,
1143 prec_dot
= FALSE
, done
= FALSE
;
1145 size_t min_width
= 0, max_width
= wxSTRING_MAXLEN
;
1147 #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1148 switch (pszFormat
[++n
]) {
1162 s_szFlags
[flagofs
++] = pszFormat
[n
];
1167 s_szFlags
[flagofs
++] = pszFormat
[n
];
1174 // dot will be auto-added to s_szFlags if non-negative number follows
1179 s_szFlags
[flagofs
++] = pszFormat
[n
];
1184 s_szFlags
[flagofs
++] = pszFormat
[n
];
1190 s_szFlags
[flagofs
++] = pszFormat
[n
];
1195 s_szFlags
[flagofs
++] = pszFormat
[n
];
1199 int len
= va_arg(argptr
, int);
1206 adj_left
= !adj_left
;
1207 s_szFlags
[flagofs
++] = '-';
1212 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
1215 case wxT('1'): case wxT('2'): case wxT('3'):
1216 case wxT('4'): case wxT('5'): case wxT('6'):
1217 case wxT('7'): case wxT('8'): case wxT('9'):
1221 while ((pszFormat
[n
]>=wxT('0')) && (pszFormat
[n
]<=wxT('9'))) {
1222 s_szFlags
[flagofs
++] = pszFormat
[n
];
1223 len
= len
*10 + (pszFormat
[n
] - wxT('0'));
1226 if (in_prec
) max_width
= len
;
1227 else min_width
= len
;
1228 n
--; // the main loop pre-increments n again
1238 s_szFlags
[flagofs
++] = pszFormat
[n
];
1239 s_szFlags
[flagofs
] = '\0';
1241 int val
= va_arg(argptr
, int);
1242 ::sprintf(szScratch
, s_szFlags
, val
);
1244 else if (ilen
== -1) {
1245 short int val
= va_arg(argptr
, short int);
1246 ::sprintf(szScratch
, s_szFlags
, val
);
1248 else if (ilen
== 1) {
1249 long int val
= va_arg(argptr
, long int);
1250 ::sprintf(szScratch
, s_szFlags
, val
);
1252 else if (ilen
== 2) {
1253 #if SIZEOF_LONG_LONG
1254 long long int val
= va_arg(argptr
, long long int);
1255 ::sprintf(szScratch
, s_szFlags
, val
);
1257 long int val
= va_arg(argptr
, long int);
1258 ::sprintf(szScratch
, s_szFlags
, val
);
1261 else if (ilen
== 3) {
1262 size_t val
= va_arg(argptr
, size_t);
1263 ::sprintf(szScratch
, s_szFlags
, val
);
1265 *this += wxString(szScratch
);
1274 s_szFlags
[flagofs
++] = pszFormat
[n
];
1275 s_szFlags
[flagofs
] = '\0';
1277 long double val
= va_arg(argptr
, long double);
1278 ::sprintf(szScratch
, s_szFlags
, val
);
1280 double val
= va_arg(argptr
, double);
1281 ::sprintf(szScratch
, s_szFlags
, val
);
1283 *this += wxString(szScratch
);
1288 void *val
= va_arg(argptr
, void *);
1290 s_szFlags
[flagofs
++] = pszFormat
[n
];
1291 s_szFlags
[flagofs
] = '\0';
1292 ::sprintf(szScratch
, s_szFlags
, val
);
1293 *this += wxString(szScratch
);
1299 wxChar val
= va_arg(argptr
, int);
1300 // we don't need to honor padding here, do we?
1307 // wx extension: we'll let %hs mean non-Unicode strings
1308 char *val
= va_arg(argptr
, char *);
1310 // ASCII->Unicode constructor handles max_width right
1311 wxString
s(val
, wxConvLibc
, max_width
);
1313 size_t len
= wxSTRING_MAXLEN
;
1315 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1316 } else val
= wxT("(null)");
1317 wxString
s(val
, len
);
1319 if (s
.Len() < min_width
)
1320 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1323 wxChar
*val
= va_arg(argptr
, wxChar
*);
1324 size_t len
= wxSTRING_MAXLEN
;
1326 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1327 } else val
= wxT("(null)");
1328 wxString
s(val
, len
);
1329 if (s
.Len() < min_width
)
1330 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1337 int *val
= va_arg(argptr
, int *);
1340 else if (ilen
== -1) {
1341 short int *val
= va_arg(argptr
, short int *);
1344 else if (ilen
>= 1) {
1345 long int *val
= va_arg(argptr
, long int *);
1351 if (wxIsalpha(pszFormat
[n
]))
1352 // probably some flag not taken care of here yet
1353 s_szFlags
[flagofs
++] = pszFormat
[n
];
1356 *this += wxT('%'); // just to pass the glibc tst-printf.c
1364 } else *this += pszFormat
[n
];
1367 // buffer to avoid dynamic memory allocation each time for small strings
1368 char szScratch
[1024];
1370 // NB: wxVsnprintf() may return either less than the buffer size or -1 if
1371 // there is not enough place depending on implementation
1372 int iLen
= wxVsnprintfA(szScratch
, WXSIZEOF(szScratch
), pszFormat
, argptr
);
1374 // the whole string is in szScratch
1378 bool outOfMemory
= FALSE
;
1379 int size
= 2*WXSIZEOF(szScratch
);
1380 while ( !outOfMemory
) {
1381 char *buf
= GetWriteBuf(size
);
1383 iLen
= wxVsnprintfA(buf
, size
, pszFormat
, argptr
);
1390 // ok, there was enough space
1394 // still not enough, double it again
1398 if ( outOfMemory
) {
1403 #endif // wxUSE_EXPERIMENTAL_PRINTF/!wxUSE_EXPERIMENTAL_PRINTF
1408 // ----------------------------------------------------------------------------
1409 // misc other operations
1410 // ----------------------------------------------------------------------------
1412 // returns TRUE if the string matches the pattern which may contain '*' and
1413 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1415 bool wxString::Matches(const wxChar
*pszMask
) const
1417 // check char by char
1418 const wxChar
*pszTxt
;
1419 for ( pszTxt
= c_str(); *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
1420 switch ( *pszMask
) {
1422 if ( *pszTxt
== wxT('\0') )
1425 // pszText and pszMask will be incremented in the loop statement
1431 // ignore special chars immediately following this one
1432 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
1435 // if there is nothing more, match
1436 if ( *pszMask
== wxT('\0') )
1439 // are there any other metacharacters in the mask?
1441 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
1443 if ( pEndMask
!= NULL
) {
1444 // we have to match the string between two metachars
1445 uiLenMask
= pEndMask
- pszMask
;
1448 // we have to match the remainder of the string
1449 uiLenMask
= wxStrlen(pszMask
);
1452 wxString
strToMatch(pszMask
, uiLenMask
);
1453 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
1454 if ( pMatch
== NULL
)
1457 // -1 to compensate "++" in the loop
1458 pszTxt
= pMatch
+ uiLenMask
- 1;
1459 pszMask
+= uiLenMask
- 1;
1464 if ( *pszMask
!= *pszTxt
)
1470 // match only if nothing left
1471 return *pszTxt
== wxT('\0');
1474 // Count the number of chars
1475 int wxString::Freq(wxChar ch
) const
1479 for (int i
= 0; i
< len
; i
++)
1481 if (GetChar(i
) == ch
)
1487 // convert to upper case, return the copy of the string
1488 wxString
wxString::Upper() const
1489 { wxString
s(*this); return s
.MakeUpper(); }
1491 // convert to lower case, return the copy of the string
1492 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }
1494 int wxString::sprintf(const wxChar
*pszFormat
, ...)
1497 va_start(argptr
, pszFormat
);
1498 int iLen
= PrintfV(pszFormat
, argptr
);
1503 // ---------------------------------------------------------------------------
1504 // standard C++ library string functions
1505 // ---------------------------------------------------------------------------
1506 #ifdef wxSTD_STRING_COMPATIBILITY
1508 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1510 wxASSERT( str
.GetStringData()->IsValid() );
1511 wxASSERT( nPos
<= Len() );
1513 if ( !str
.IsEmpty() ) {
1515 wxChar
*pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1516 wxStrncpy(pc
, c_str(), nPos
);
1517 wxStrcpy(pc
+ nPos
, str
);
1518 wxStrcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1519 strTmp
.UngetWriteBuf();
1526 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1528 wxASSERT( str
.GetStringData()->IsValid() );
1529 wxASSERT( nStart
<= Len() );
1531 const wxChar
*p
= wxStrstr(c_str() + nStart
, str
);
1533 return p
== NULL
? npos
: p
- c_str();
1536 // VC++ 1.5 can't cope with the default argument in the header.
1537 #if !defined(__VISUALC__) || defined(__WIN32__)
1538 size_t wxString::find(const wxChar
* sz
, size_t nStart
, size_t n
) const
1540 return find(wxString(sz
, n
), nStart
);
1544 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1545 #if !defined(__BORLANDC__)
1546 size_t wxString::find(wxChar ch
, size_t nStart
) const
1548 wxASSERT( nStart
<= Len() );
1550 const wxChar
*p
= wxStrchr(c_str() + nStart
, ch
);
1552 return p
== NULL
? npos
: p
- c_str();
1556 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1558 wxASSERT( str
.GetStringData()->IsValid() );
1559 wxASSERT( nStart
<= Len() );
1561 // TODO could be made much quicker than that
1562 const wxChar
*p
= c_str() + (nStart
== npos
? Len() : nStart
);
1563 while ( p
>= c_str() + str
.Len() ) {
1564 if ( wxStrncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1565 return p
- str
.Len() - c_str();
1572 // VC++ 1.5 can't cope with the default argument in the header.
1573 #if !defined(__VISUALC__) || defined(__WIN32__)
1574 size_t wxString::rfind(const wxChar
* sz
, size_t nStart
, size_t n
) const
1576 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1579 size_t wxString::rfind(wxChar ch
, size_t nStart
) const
1581 if ( nStart
== npos
)
1587 wxASSERT( nStart
<= Len() );
1590 const wxChar
*p
= wxStrrchr(c_str(), ch
);
1595 size_t result
= p
- c_str();
1596 return ( result
> nStart
) ? npos
: result
;
1600 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
1602 const wxChar
*start
= c_str() + nStart
;
1603 const wxChar
*firstOf
= wxStrpbrk(start
, sz
);
1605 return firstOf
- c_str();
1610 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
1612 if ( nStart
== npos
)
1618 wxASSERT( nStart
<= Len() );
1621 for ( const wxChar
*p
= c_str() + length() - 1; p
>= c_str(); p
-- )
1623 if ( wxStrchr(sz
, *p
) )
1630 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
1632 if ( nStart
== npos
)
1638 wxASSERT( nStart
<= Len() );
1641 size_t nAccept
= wxStrspn(c_str() + nStart
, sz
);
1642 if ( nAccept
>= length() - nStart
)
1648 size_t wxString::find_first_not_of(wxChar ch
, size_t nStart
) const
1650 wxASSERT( nStart
<= Len() );
1652 for ( const wxChar
*p
= c_str() + nStart
; *p
; p
++ )
1661 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
1663 if ( nStart
== npos
)
1669 wxASSERT( nStart
<= Len() );
1672 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1674 if ( !wxStrchr(sz
, *p
) )
1681 size_t wxString::find_last_not_of(wxChar ch
, size_t nStart
) const
1683 if ( nStart
== npos
)
1689 wxASSERT( nStart
<= Len() );
1692 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1701 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1703 wxString
strTmp(c_str(), nStart
);
1704 if ( nLen
!= npos
) {
1705 wxASSERT( nStart
+ nLen
<= Len() );
1707 strTmp
.append(c_str() + nStart
+ nLen
);
1714 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const wxChar
*sz
)
1716 wxASSERT( nStart
+ nLen
<= wxStrlen(sz
) );
1720 strTmp
.append(c_str(), nStart
);
1722 strTmp
.append(c_str() + nStart
+ nLen
);
1728 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1730 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1733 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1734 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1736 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1739 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1740 const wxChar
* sz
, size_t nCount
)
1742 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1745 #endif //std::string compatibility
1747 // ============================================================================
1749 // ============================================================================
1751 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1752 #define ARRAY_MAXSIZE_INCREMENT 4096
1753 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1754 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1757 #define STRING(p) ((wxString *)(&(p)))
1760 wxArrayString::wxArrayString(bool autoSort
)
1764 m_pItems
= (wxChar
**) NULL
;
1765 m_autoSort
= autoSort
;
1769 wxArrayString::wxArrayString(const wxArrayString
& src
)
1773 m_pItems
= (wxChar
**) NULL
;
1774 m_autoSort
= src
.m_autoSort
;
1779 // assignment operator
1780 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1790 void wxArrayString::Copy(const wxArrayString
& src
)
1792 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1793 Alloc(src
.m_nCount
);
1795 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1800 void wxArrayString::Grow()
1802 // only do it if no more place
1803 if( m_nCount
== m_nSize
) {
1804 if( m_nSize
== 0 ) {
1805 // was empty, alloc some memory
1806 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1807 m_pItems
= new wxChar
*[m_nSize
];
1810 // otherwise when it's called for the first time, nIncrement would be 0
1811 // and the array would never be expanded
1812 #if defined(__VISAGECPP__) && defined(__WXDEBUG__)
1813 int array_size
= ARRAY_DEFAULT_INITIAL_SIZE
;
1814 wxASSERT( array_size
!= 0 );
1816 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1819 // add 50% but not too much
1820 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1821 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1822 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1823 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1824 m_nSize
+= nIncrement
;
1825 wxChar
**pNew
= new wxChar
*[m_nSize
];
1827 // copy data to new location
1828 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1830 // delete old memory (but do not release the strings!)
1831 wxDELETEA(m_pItems
);
1838 void wxArrayString::Free()
1840 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1841 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1845 // deletes all the strings from the list
1846 void wxArrayString::Empty()
1853 // as Empty, but also frees memory
1854 void wxArrayString::Clear()
1861 wxDELETEA(m_pItems
);
1865 wxArrayString::~wxArrayString()
1869 wxDELETEA(m_pItems
);
1872 // pre-allocates memory (frees the previous data!)
1873 void wxArrayString::Alloc(size_t nSize
)
1875 wxASSERT( nSize
> 0 );
1877 // only if old buffer was not big enough
1878 if ( nSize
> m_nSize
) {
1880 wxDELETEA(m_pItems
);
1881 m_pItems
= new wxChar
*[nSize
];
1888 // minimizes the memory usage by freeing unused memory
1889 void wxArrayString::Shrink()
1891 // only do it if we have some memory to free
1892 if( m_nCount
< m_nSize
) {
1893 // allocates exactly as much memory as we need
1894 wxChar
**pNew
= new wxChar
*[m_nCount
];
1896 // copy data to new location
1897 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1903 // searches the array for an item (forward or backwards)
1904 int wxArrayString::Index(const wxChar
*sz
, bool bCase
, bool bFromEnd
) const
1907 // use binary search in the sorted array
1908 wxASSERT_MSG( bCase
&& !bFromEnd
,
1909 wxT("search parameters ignored for auto sorted array") );
1918 res
= wxStrcmp(sz
, m_pItems
[i
]);
1930 // use linear search in unsorted array
1932 if ( m_nCount
> 0 ) {
1933 size_t ui
= m_nCount
;
1935 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1942 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1943 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1952 // add item at the end
1953 size_t wxArrayString::Add(const wxString
& str
)
1956 // insert the string at the correct position to keep the array sorted
1964 res
= wxStrcmp(str
, m_pItems
[i
]);
1975 wxASSERT_MSG( lo
== hi
, wxT("binary search broken") );
1982 wxASSERT( str
.GetStringData()->IsValid() );
1986 // the string data must not be deleted!
1987 str
.GetStringData()->Lock();
1990 m_pItems
[m_nCount
] = (wxChar
*)str
.c_str(); // const_cast
1996 // add item at the given position
1997 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1999 wxASSERT( str
.GetStringData()->IsValid() );
2001 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Insert") );
2005 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
2006 (m_nCount
- nIndex
)*sizeof(wxChar
*));
2008 str
.GetStringData()->Lock();
2009 m_pItems
[nIndex
] = (wxChar
*)str
.c_str();
2014 // removes item from array (by index)
2015 void wxArrayString::Remove(size_t nIndex
)
2017 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Remove") );
2020 Item(nIndex
).GetStringData()->Unlock();
2022 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
2023 (m_nCount
- nIndex
- 1)*sizeof(wxChar
*));
2027 // removes item from array (by value)
2028 void wxArrayString::Remove(const wxChar
*sz
)
2030 int iIndex
= Index(sz
);
2032 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
2033 wxT("removing inexistent element in wxArrayString::Remove") );
2038 // ----------------------------------------------------------------------------
2040 // ----------------------------------------------------------------------------
2042 // we can only sort one array at a time with the quick-sort based
2045 // need a critical section to protect access to gs_compareFunction and
2046 // gs_sortAscending variables
2047 static wxCriticalSection
*gs_critsectStringSort
= NULL
;
2049 // call this before the value of the global sort vars is changed/after
2050 // you're finished with them
2051 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
2052 gs_critsectStringSort = new wxCriticalSection; \
2053 gs_critsectStringSort->Enter()
2054 #define END_SORT() gs_critsectStringSort->Leave(); \
2055 delete gs_critsectStringSort; \
2056 gs_critsectStringSort = NULL
2058 #define START_SORT()
2060 #endif // wxUSE_THREADS
2062 // function to use for string comparaison
2063 static wxArrayString::CompareFunction gs_compareFunction
= NULL
;
2065 // if we don't use the compare function, this flag tells us if we sort the
2066 // array in ascending or descending order
2067 static bool gs_sortAscending
= TRUE
;
2069 // function which is called by quick sort
2070 static int LINKAGEMODE
wxStringCompareFunction(const void *first
, const void *second
)
2072 wxString
*strFirst
= (wxString
*)first
;
2073 wxString
*strSecond
= (wxString
*)second
;
2075 if ( gs_compareFunction
) {
2076 return gs_compareFunction(*strFirst
, *strSecond
);
2079 // maybe we should use wxStrcoll
2080 int result
= wxStrcmp(strFirst
->c_str(), strSecond
->c_str());
2082 return gs_sortAscending
? result
: -result
;
2086 // sort array elements using passed comparaison function
2087 void wxArrayString::Sort(CompareFunction compareFunction
)
2091 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2092 gs_compareFunction
= compareFunction
;
2099 void wxArrayString::Sort(bool reverseOrder
)
2103 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2104 gs_sortAscending
= !reverseOrder
;
2111 void wxArrayString::DoSort()
2113 wxCHECK_RET( !m_autoSort
, wxT("can't use this method with sorted arrays") );
2115 // just sort the pointers using qsort() - of course it only works because
2116 // wxString() *is* a pointer to its data
2117 qsort(m_pItems
, m_nCount
, sizeof(wxChar
*), wxStringCompareFunction
);