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 // empty C style string: points to 'string data' byte of g_strEmpty
89 extern const wxChar WXDLLEXPORT
*wxEmptyString
= &g_strEmpty
.dummy
;
91 // ----------------------------------------------------------------------------
92 // conditional compilation
93 // ----------------------------------------------------------------------------
95 #if !defined(__WXSW__) && wxUSE_UNICODE
96 #ifdef wxUSE_EXPERIMENTAL_PRINTF
97 #undef wxUSE_EXPERIMENTAL_PRINTF
99 #define wxUSE_EXPERIMENTAL_PRINTF 1
102 // we want to find out if the current platform supports vsnprintf()-like
103 // function: for Unix this is done with configure, for Windows we test the
104 // compiler explicitly.
106 // FIXME currently, this is only for ANSI (!Unicode) strings, so we call this
107 // function wxVsnprintfA (A for ANSI), should also find one for Unicode
108 // strings in Unicode build
110 #if defined(__VISUALC__) || defined(wxUSE_NORLANDER_HEADERS)
111 #define wxVsnprintfA _vsnprintf
114 #ifdef HAVE_VSNPRINTF
115 #define wxVsnprintfA vsnprintf
117 #endif // Windows/!Windows
120 // in this case we'll use vsprintf() (which is ANSI and thus should be
121 // always available), but it's unsafe because it doesn't check for buffer
122 // size - so give a warning
123 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
125 #if defined(__VISUALC__)
126 #pragma message("Using sprintf() because no snprintf()-like function defined")
127 #elif defined(__GNUG__) && !defined(__UNIX__)
128 #warning "Using sprintf() because no snprintf()-like function defined"
129 #elif defined(__MWERKS__)
130 #warning "Using sprintf() because no snprintf()-like function defined"
132 #endif // no vsnprintf
135 // AIX has vsnprintf, but there's no prototype in the system headers.
136 extern "C" int vsnprintf(char* str
, size_t n
, const char* format
, va_list ap
);
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
145 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
148 // ATTN: you can _not_ use both of these in the same program!
150 istream
& operator>>(istream
& is
, wxString
& WXUNUSED(str
))
155 streambuf
*sb
= is
.rdbuf();
158 int ch
= sb
->sbumpc ();
160 is
.setstate(ios::eofbit
);
163 else if ( isspace(ch
) ) {
175 if ( str
.length() == 0 )
176 is
.setstate(ios::failbit
);
181 ostream
& operator<<(ostream
& os
, const wxString
& str
)
187 #endif //std::string compatibility
189 extern int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
190 const wxChar
*format
, va_list argptr
)
193 // FIXME should use wvsnprintf() or whatever if it's available
195 int iLen
= s
.PrintfV(format
, argptr
);
198 wxStrncpy(buf
, s
.c_str(), iLen
);
203 return wxVsnprintfA(buf
, len
, format
, argptr
);
204 #endif // Unicode/ANSI
207 extern int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
208 const wxChar
*format
, ...)
211 va_start(argptr
, format
);
213 int iLen
= wxVsnprintf(buf
, len
, format
, argptr
);
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 // this small class is used to gather statistics for performance tuning
225 //#define WXSTRING_STATISTICS
226 #ifdef WXSTRING_STATISTICS
230 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
232 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
234 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
237 size_t m_nCount
, m_nTotal
;
239 } g_averageLength("allocation size"),
240 g_averageSummandLength("summand length"),
241 g_averageConcatHit("hit probability in concat"),
242 g_averageInitialLength("initial string length");
244 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
246 #define STATISTICS_ADD(av, val)
247 #endif // WXSTRING_STATISTICS
249 // ===========================================================================
250 // wxString class core
251 // ===========================================================================
253 // ---------------------------------------------------------------------------
255 // ---------------------------------------------------------------------------
257 // constructs string of <nLength> copies of character <ch>
258 wxString::wxString(wxChar ch
, size_t nLength
)
263 AllocBuffer(nLength
);
266 // memset only works on char
267 for (size_t n
=0; n
<nLength
; n
++) m_pchData
[n
] = ch
;
269 memset(m_pchData
, ch
, nLength
);
274 // takes nLength elements of psz starting at nPos
275 void wxString::InitWith(const wxChar
*psz
, size_t nPos
, size_t nLength
)
279 wxASSERT( nPos
<= wxStrlen(psz
) );
281 if ( nLength
== wxSTRING_MAXLEN
)
282 nLength
= wxStrlen(psz
+ nPos
);
284 STATISTICS_ADD(InitialLength
, nLength
);
287 // trailing '\0' is written in AllocBuffer()
288 AllocBuffer(nLength
);
289 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(wxChar
));
293 #ifdef wxSTD_STRING_COMPATIBILITY
295 // poor man's iterators are "void *" pointers
296 wxString::wxString(const void *pStart
, const void *pEnd
)
298 InitWith((const wxChar
*)pStart
, 0,
299 (const wxChar
*)pEnd
- (const wxChar
*)pStart
);
302 #endif //std::string compatibility
306 // from multibyte string
307 wxString::wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
)
309 // first get necessary size
310 size_t nLen
= psz
? conv
.MB2WC((wchar_t *) NULL
, psz
, 0) : 0;
312 // nLength is number of *Unicode* characters here!
313 if ((nLen
!= (size_t)-1) && (nLen
> nLength
))
317 if ( (nLen
!= 0) && (nLen
!= (size_t)-1) ) {
319 conv
.MB2WC(m_pchData
, psz
, nLen
);
330 wxString::wxString(const wchar_t *pwz
)
332 // first get necessary size
333 size_t nLen
= pwz
? wxWC2MB((char *) NULL
, pwz
, 0) : 0;
336 if ( (nLen
!= 0) && (nLen
!= (size_t)-1) ) {
338 wxWC2MB(m_pchData
, pwz
, nLen
);
344 #endif // wxUSE_WCHAR_T
346 #endif // Unicode/ANSI
348 // ---------------------------------------------------------------------------
350 // ---------------------------------------------------------------------------
352 // allocates memory needed to store a C string of length nLen
353 void wxString::AllocBuffer(size_t nLen
)
355 wxASSERT( nLen
> 0 ); //
356 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
358 STATISTICS_ADD(Length
, nLen
);
361 // 1) one extra character for '\0' termination
362 // 2) sizeof(wxStringData) for housekeeping info
363 wxStringData
* pData
= (wxStringData
*)
364 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(wxChar
));
366 pData
->nDataLength
= nLen
;
367 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
368 m_pchData
= pData
->data(); // data starts after wxStringData
369 m_pchData
[nLen
] = wxT('\0');
372 // must be called before changing this string
373 void wxString::CopyBeforeWrite()
375 wxStringData
* pData
= GetStringData();
377 if ( pData
->IsShared() ) {
378 pData
->Unlock(); // memory not freed because shared
379 size_t nLen
= pData
->nDataLength
;
381 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(wxChar
));
384 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
387 // must be called before replacing contents of this string
388 void wxString::AllocBeforeWrite(size_t nLen
)
390 wxASSERT( nLen
!= 0 ); // doesn't make any sense
392 // must not share string and must have enough space
393 wxStringData
* pData
= GetStringData();
394 if ( pData
->IsShared() || pData
->IsEmpty() ) {
395 // can't work with old buffer, get new one
400 if ( nLen
> pData
->nAllocLength
) {
401 // realloc the buffer instead of calling malloc() again, this is more
403 STATISTICS_ADD(Length
, nLen
);
407 wxStringData
*pDataOld
= pData
;
408 pData
= (wxStringData
*)
409 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
414 // FIXME we're going to crash...
418 pData
->nAllocLength
= nLen
;
419 m_pchData
= pData
->data();
422 // now we have enough space, just update the string length
423 pData
->nDataLength
= nLen
;
426 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
429 // allocate enough memory for nLen characters
430 void wxString::Alloc(size_t nLen
)
432 wxStringData
*pData
= GetStringData();
433 if ( pData
->nAllocLength
<= nLen
) {
434 if ( pData
->IsEmpty() ) {
437 wxStringData
* pData
= (wxStringData
*)
438 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
440 pData
->nDataLength
= 0;
441 pData
->nAllocLength
= nLen
;
442 m_pchData
= pData
->data(); // data starts after wxStringData
443 m_pchData
[0u] = wxT('\0');
445 else if ( pData
->IsShared() ) {
446 pData
->Unlock(); // memory not freed because shared
447 size_t nOldLen
= pData
->nDataLength
;
449 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(wxChar
));
454 wxStringData
*pDataOld
= pData
;
455 wxStringData
*p
= (wxStringData
*)
456 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
462 // FIXME what to do on memory error?
466 // it's not important if the pointer changed or not (the check for this
467 // is not faster than assigning to m_pchData in all cases)
468 p
->nAllocLength
= nLen
;
469 m_pchData
= p
->data();
472 //else: we've already got enough
475 // shrink to minimal size (releasing extra memory)
476 void wxString::Shrink()
478 wxStringData
*pData
= GetStringData();
480 // this variable is unused in release build, so avoid the compiler warning
481 // by just not declaring it
485 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(wxChar
));
487 // we rely on a reasonable realloc() implementation here - so far I haven't
488 // seen any which wouldn't behave like this
490 wxASSERT( p
!= NULL
); // can't free memory?
491 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
494 // get the pointer to writable buffer of (at least) nLen bytes
495 wxChar
*wxString::GetWriteBuf(size_t nLen
)
497 AllocBeforeWrite(nLen
);
499 wxASSERT( GetStringData()->nRefs
== 1 );
500 GetStringData()->Validate(FALSE
);
505 // put string back in a reasonable state after GetWriteBuf
506 void wxString::UngetWriteBuf()
508 GetStringData()->nDataLength
= wxStrlen(m_pchData
);
509 GetStringData()->Validate(TRUE
);
512 // ---------------------------------------------------------------------------
514 // ---------------------------------------------------------------------------
516 // all functions are inline in string.h
518 // ---------------------------------------------------------------------------
519 // assignment operators
520 // ---------------------------------------------------------------------------
522 // helper function: does real copy
523 void wxString::AssignCopy(size_t nSrcLen
, const wxChar
*pszSrcData
)
525 if ( nSrcLen
== 0 ) {
529 AllocBeforeWrite(nSrcLen
);
530 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
531 GetStringData()->nDataLength
= nSrcLen
;
532 m_pchData
[nSrcLen
] = wxT('\0');
536 // assigns one string to another
537 wxString
& wxString::operator=(const wxString
& stringSrc
)
539 wxASSERT( stringSrc
.GetStringData()->IsValid() );
541 // don't copy string over itself
542 if ( m_pchData
!= stringSrc
.m_pchData
) {
543 if ( stringSrc
.GetStringData()->IsEmpty() ) {
548 GetStringData()->Unlock();
549 m_pchData
= stringSrc
.m_pchData
;
550 GetStringData()->Lock();
557 // assigns a single character
558 wxString
& wxString::operator=(wxChar ch
)
565 wxString
& wxString::operator=(const wxChar
*psz
)
567 AssignCopy(wxStrlen(psz
), psz
);
573 // same as 'signed char' variant
574 wxString
& wxString::operator=(const unsigned char* psz
)
576 *this = (const char *)psz
;
581 wxString
& wxString::operator=(const wchar_t *pwz
)
591 // ---------------------------------------------------------------------------
592 // string concatenation
593 // ---------------------------------------------------------------------------
595 // add something to this string
596 void wxString::ConcatSelf(int nSrcLen
, const wxChar
*pszSrcData
)
598 STATISTICS_ADD(SummandLength
, nSrcLen
);
600 // concatenating an empty string is a NOP
602 wxStringData
*pData
= GetStringData();
603 size_t nLen
= pData
->nDataLength
;
604 size_t nNewLen
= nLen
+ nSrcLen
;
606 // alloc new buffer if current is too small
607 if ( pData
->IsShared() ) {
608 STATISTICS_ADD(ConcatHit
, 0);
610 // we have to allocate another buffer
611 wxStringData
* pOldData
= GetStringData();
612 AllocBuffer(nNewLen
);
613 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(wxChar
));
616 else if ( nNewLen
> pData
->nAllocLength
) {
617 STATISTICS_ADD(ConcatHit
, 0);
619 // we have to grow the buffer
623 STATISTICS_ADD(ConcatHit
, 1);
625 // the buffer is already big enough
628 // should be enough space
629 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
631 // fast concatenation - all is done in our buffer
632 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
634 m_pchData
[nNewLen
] = wxT('\0'); // put terminating '\0'
635 GetStringData()->nDataLength
= nNewLen
; // and fix the length
637 //else: the string to append was empty
641 * concatenation functions come in 5 flavours:
643 * char + string and string + char
644 * C str + string and string + C str
647 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
649 wxASSERT( string1
.GetStringData()->IsValid() );
650 wxASSERT( string2
.GetStringData()->IsValid() );
652 wxString s
= string1
;
658 wxString
operator+(const wxString
& string
, wxChar ch
)
660 wxASSERT( string
.GetStringData()->IsValid() );
668 wxString
operator+(wxChar ch
, const wxString
& string
)
670 wxASSERT( string
.GetStringData()->IsValid() );
678 wxString
operator+(const wxString
& string
, const wxChar
*psz
)
680 wxASSERT( string
.GetStringData()->IsValid() );
683 s
.Alloc(wxStrlen(psz
) + string
.Len());
690 wxString
operator+(const wxChar
*psz
, const wxString
& string
)
692 wxASSERT( string
.GetStringData()->IsValid() );
695 s
.Alloc(wxStrlen(psz
) + string
.Len());
702 // ===========================================================================
703 // other common string functions
704 // ===========================================================================
706 // ---------------------------------------------------------------------------
707 // simple sub-string extraction
708 // ---------------------------------------------------------------------------
710 // helper function: clone the data attached to this string
711 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
713 if ( nCopyLen
== 0 ) {
717 dest
.AllocBuffer(nCopyLen
);
718 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(wxChar
));
722 // extract string of length nCount starting at nFirst
723 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
725 wxStringData
*pData
= GetStringData();
726 size_t nLen
= pData
->nDataLength
;
728 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
729 if ( nCount
== wxSTRING_MAXLEN
)
731 nCount
= nLen
- nFirst
;
734 // out-of-bounds requests return sensible things
735 if ( nFirst
+ nCount
> nLen
)
737 nCount
= nLen
- nFirst
;
742 // AllocCopy() will return empty string
747 AllocCopy(dest
, nCount
, nFirst
);
752 // extract nCount last (rightmost) characters
753 wxString
wxString::Right(size_t nCount
) const
755 if ( nCount
> (size_t)GetStringData()->nDataLength
)
756 nCount
= GetStringData()->nDataLength
;
759 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
763 // get all characters after the last occurence of ch
764 // (returns the whole string if ch not found)
765 wxString
wxString::AfterLast(wxChar ch
) const
768 int iPos
= Find(ch
, TRUE
);
769 if ( iPos
== wxNOT_FOUND
)
772 str
= c_str() + iPos
+ 1;
777 // extract nCount first (leftmost) characters
778 wxString
wxString::Left(size_t nCount
) const
780 if ( nCount
> (size_t)GetStringData()->nDataLength
)
781 nCount
= GetStringData()->nDataLength
;
784 AllocCopy(dest
, nCount
, 0);
788 // get all characters before the first occurence of ch
789 // (returns the whole string if ch not found)
790 wxString
wxString::BeforeFirst(wxChar ch
) const
793 for ( const wxChar
*pc
= m_pchData
; *pc
!= wxT('\0') && *pc
!= ch
; pc
++ )
799 /// get all characters before the last occurence of ch
800 /// (returns empty string if ch not found)
801 wxString
wxString::BeforeLast(wxChar ch
) const
804 int iPos
= Find(ch
, TRUE
);
805 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
806 str
= wxString(c_str(), iPos
);
811 /// get all characters after the first occurence of ch
812 /// (returns empty string if ch not found)
813 wxString
wxString::AfterFirst(wxChar ch
) const
817 if ( iPos
!= wxNOT_FOUND
)
818 str
= c_str() + iPos
+ 1;
823 // replace first (or all) occurences of some substring with another one
824 size_t wxString::Replace(const wxChar
*szOld
, const wxChar
*szNew
, bool bReplaceAll
)
826 size_t uiCount
= 0; // count of replacements made
828 size_t uiOldLen
= wxStrlen(szOld
);
831 const wxChar
*pCurrent
= m_pchData
;
832 const wxChar
*pSubstr
;
833 while ( *pCurrent
!= wxT('\0') ) {
834 pSubstr
= wxStrstr(pCurrent
, szOld
);
835 if ( pSubstr
== NULL
) {
836 // strTemp is unused if no replacements were made, so avoid the copy
840 strTemp
+= pCurrent
; // copy the rest
841 break; // exit the loop
844 // take chars before match
845 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
847 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
852 if ( !bReplaceAll
) {
853 strTemp
+= pCurrent
; // copy the rest
854 break; // exit the loop
859 // only done if there were replacements, otherwise would have returned above
865 bool wxString::IsAscii() const
867 const wxChar
*s
= (const wxChar
*) *this;
869 if(!isascii(*s
)) return(FALSE
);
875 bool wxString::IsWord() const
877 const wxChar
*s
= (const wxChar
*) *this;
879 if(!wxIsalpha(*s
)) return(FALSE
);
885 bool wxString::IsNumber() const
887 const wxChar
*s
= (const wxChar
*) *this;
889 if(!wxIsdigit(*s
)) return(FALSE
);
895 wxString
wxString::Strip(stripType w
) const
898 if ( w
& leading
) s
.Trim(FALSE
);
899 if ( w
& trailing
) s
.Trim(TRUE
);
903 // ---------------------------------------------------------------------------
905 // ---------------------------------------------------------------------------
907 wxString
& wxString::MakeUpper()
911 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
912 *p
= (wxChar
)wxToupper(*p
);
917 wxString
& wxString::MakeLower()
921 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
922 *p
= (wxChar
)wxTolower(*p
);
927 // ---------------------------------------------------------------------------
928 // trimming and padding
929 // ---------------------------------------------------------------------------
931 // trims spaces (in the sense of isspace) from left or right side
932 wxString
& wxString::Trim(bool bFromRight
)
934 // first check if we're going to modify the string at all
937 (bFromRight
&& wxIsspace(GetChar(Len() - 1))) ||
938 (!bFromRight
&& wxIsspace(GetChar(0u)))
942 // ok, there is at least one space to trim
947 // find last non-space character
948 wxChar
*psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
949 while ( wxIsspace(*psz
) && (psz
>= m_pchData
) )
952 // truncate at trailing space start
954 GetStringData()->nDataLength
= psz
- m_pchData
;
958 // find first non-space character
959 const wxChar
*psz
= m_pchData
;
960 while ( wxIsspace(*psz
) )
963 // fix up data and length
964 int nDataLength
= GetStringData()->nDataLength
- (psz
- (const wxChar
*) m_pchData
);
965 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(wxChar
));
966 GetStringData()->nDataLength
= nDataLength
;
973 // adds nCount characters chPad to the string from either side
974 wxString
& wxString::Pad(size_t nCount
, wxChar chPad
, bool bFromRight
)
976 wxString
s(chPad
, nCount
);
989 // truncate the string
990 wxString
& wxString::Truncate(size_t uiLen
)
992 if ( uiLen
< Len() ) {
995 *(m_pchData
+ uiLen
) = wxT('\0');
996 GetStringData()->nDataLength
= uiLen
;
998 //else: nothing to do, string is already short enough
1003 // ---------------------------------------------------------------------------
1004 // finding (return wxNOT_FOUND if not found and index otherwise)
1005 // ---------------------------------------------------------------------------
1008 int wxString::Find(wxChar ch
, bool bFromEnd
) const
1010 const wxChar
*psz
= bFromEnd
? wxStrrchr(m_pchData
, ch
) : wxStrchr(m_pchData
, ch
);
1012 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
1015 // find a sub-string (like strstr)
1016 int wxString::Find(const wxChar
*pszSub
) const
1018 const wxChar
*psz
= wxStrstr(m_pchData
, pszSub
);
1020 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
1023 // ---------------------------------------------------------------------------
1024 // stream-like operators
1025 // ---------------------------------------------------------------------------
1026 wxString
& wxString::operator<<(int i
)
1029 res
.Printf(wxT("%d"), i
);
1031 return (*this) << res
;
1034 wxString
& wxString::operator<<(float f
)
1037 res
.Printf(wxT("%f"), f
);
1039 return (*this) << res
;
1042 wxString
& wxString::operator<<(double d
)
1045 res
.Printf(wxT("%g"), d
);
1047 return (*this) << res
;
1050 // ---------------------------------------------------------------------------
1052 // ---------------------------------------------------------------------------
1054 int wxString::Printf(const wxChar
*pszFormat
, ...)
1057 va_start(argptr
, pszFormat
);
1059 int iLen
= PrintfV(pszFormat
, argptr
);
1066 int wxString::PrintfV(const wxChar
* pszFormat
, va_list argptr
)
1068 #if wxUSE_EXPERIMENTAL_PRINTF
1069 // the new implementation
1071 // buffer to avoid dynamic memory allocation each time for small strings
1072 char szScratch
[1024];
1075 for (size_t n
= 0; pszFormat
[n
]; n
++)
1076 if (pszFormat
[n
] == wxT('%')) {
1077 static char s_szFlags
[256] = "%";
1079 bool adj_left
= FALSE
, in_prec
= FALSE
,
1080 prec_dot
= FALSE
, done
= FALSE
;
1082 size_t min_width
= 0, max_width
= wxSTRING_MAXLEN
;
1084 #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1085 switch (pszFormat
[++n
]) {
1099 s_szFlags
[flagofs
++] = pszFormat
[n
];
1104 s_szFlags
[flagofs
++] = pszFormat
[n
];
1111 // dot will be auto-added to s_szFlags if non-negative number follows
1116 s_szFlags
[flagofs
++] = pszFormat
[n
];
1121 s_szFlags
[flagofs
++] = pszFormat
[n
];
1127 s_szFlags
[flagofs
++] = pszFormat
[n
];
1132 s_szFlags
[flagofs
++] = pszFormat
[n
];
1136 int len
= va_arg(argptr
, int);
1143 adj_left
= !adj_left
;
1144 s_szFlags
[flagofs
++] = '-';
1149 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
1152 case wxT('1'): case wxT('2'): case wxT('3'):
1153 case wxT('4'): case wxT('5'): case wxT('6'):
1154 case wxT('7'): case wxT('8'): case wxT('9'):
1158 while ((pszFormat
[n
]>=wxT('0')) && (pszFormat
[n
]<=wxT('9'))) {
1159 s_szFlags
[flagofs
++] = pszFormat
[n
];
1160 len
= len
*10 + (pszFormat
[n
] - wxT('0'));
1163 if (in_prec
) max_width
= len
;
1164 else min_width
= len
;
1165 n
--; // the main loop pre-increments n again
1175 s_szFlags
[flagofs
++] = pszFormat
[n
];
1176 s_szFlags
[flagofs
] = '\0';
1178 int val
= va_arg(argptr
, int);
1179 ::sprintf(szScratch
, s_szFlags
, val
);
1181 else if (ilen
== -1) {
1182 short int val
= va_arg(argptr
, short int);
1183 ::sprintf(szScratch
, s_szFlags
, val
);
1185 else if (ilen
== 1) {
1186 long int val
= va_arg(argptr
, long int);
1187 ::sprintf(szScratch
, s_szFlags
, val
);
1189 else if (ilen
== 2) {
1190 #if SIZEOF_LONG_LONG
1191 long long int val
= va_arg(argptr
, long long int);
1192 ::sprintf(szScratch
, s_szFlags
, val
);
1194 long int val
= va_arg(argptr
, long int);
1195 ::sprintf(szScratch
, s_szFlags
, val
);
1198 else if (ilen
== 3) {
1199 size_t val
= va_arg(argptr
, size_t);
1200 ::sprintf(szScratch
, s_szFlags
, val
);
1202 *this += wxString(szScratch
);
1211 s_szFlags
[flagofs
++] = pszFormat
[n
];
1212 s_szFlags
[flagofs
] = '\0';
1214 long double val
= va_arg(argptr
, long double);
1215 ::sprintf(szScratch
, s_szFlags
, val
);
1217 double val
= va_arg(argptr
, double);
1218 ::sprintf(szScratch
, s_szFlags
, val
);
1220 *this += wxString(szScratch
);
1225 void *val
= va_arg(argptr
, void *);
1227 s_szFlags
[flagofs
++] = pszFormat
[n
];
1228 s_szFlags
[flagofs
] = '\0';
1229 ::sprintf(szScratch
, s_szFlags
, val
);
1230 *this += wxString(szScratch
);
1236 wxChar val
= va_arg(argptr
, int);
1237 // we don't need to honor padding here, do we?
1244 // wx extension: we'll let %hs mean non-Unicode strings
1245 char *val
= va_arg(argptr
, char *);
1247 // ASCII->Unicode constructor handles max_width right
1248 wxString
s(val
, wxConvLibc
, max_width
);
1250 size_t len
= wxSTRING_MAXLEN
;
1252 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1253 } else val
= wxT("(null)");
1254 wxString
s(val
, len
);
1256 if (s
.Len() < min_width
)
1257 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1260 wxChar
*val
= va_arg(argptr
, wxChar
*);
1261 size_t len
= wxSTRING_MAXLEN
;
1263 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1264 } else val
= wxT("(null)");
1265 wxString
s(val
, len
);
1266 if (s
.Len() < min_width
)
1267 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1274 int *val
= va_arg(argptr
, int *);
1277 else if (ilen
== -1) {
1278 short int *val
= va_arg(argptr
, short int *);
1281 else if (ilen
>= 1) {
1282 long int *val
= va_arg(argptr
, long int *);
1288 if (wxIsalpha(pszFormat
[n
]))
1289 // probably some flag not taken care of here yet
1290 s_szFlags
[flagofs
++] = pszFormat
[n
];
1293 *this += wxT('%'); // just to pass the glibc tst-printf.c
1301 } else *this += pszFormat
[n
];
1304 // buffer to avoid dynamic memory allocation each time for small strings
1305 char szScratch
[1024];
1307 // NB: wxVsnprintf() may return either less than the buffer size or -1 if
1308 // there is not enough place depending on implementation
1309 int iLen
= wxVsnprintfA(szScratch
, WXSIZEOF(szScratch
), pszFormat
, argptr
);
1311 // the whole string is in szScratch
1315 bool outOfMemory
= FALSE
;
1316 int size
= 2*WXSIZEOF(szScratch
);
1317 while ( !outOfMemory
) {
1318 char *buf
= GetWriteBuf(size
);
1320 iLen
= wxVsnprintfA(buf
, size
, pszFormat
, argptr
);
1327 // ok, there was enough space
1331 // still not enough, double it again
1335 if ( outOfMemory
) {
1340 #endif // wxUSE_EXPERIMENTAL_PRINTF/!wxUSE_EXPERIMENTAL_PRINTF
1345 // ----------------------------------------------------------------------------
1346 // misc other operations
1347 // ----------------------------------------------------------------------------
1349 // returns TRUE if the string matches the pattern which may contain '*' and
1350 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1352 bool wxString::Matches(const wxChar
*pszMask
) const
1354 // check char by char
1355 const wxChar
*pszTxt
;
1356 for ( pszTxt
= c_str(); *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
1357 switch ( *pszMask
) {
1359 if ( *pszTxt
== wxT('\0') )
1362 // pszText and pszMask will be incremented in the loop statement
1368 // ignore special chars immediately following this one
1369 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
1372 // if there is nothing more, match
1373 if ( *pszMask
== wxT('\0') )
1376 // are there any other metacharacters in the mask?
1378 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
1380 if ( pEndMask
!= NULL
) {
1381 // we have to match the string between two metachars
1382 uiLenMask
= pEndMask
- pszMask
;
1385 // we have to match the remainder of the string
1386 uiLenMask
= wxStrlen(pszMask
);
1389 wxString
strToMatch(pszMask
, uiLenMask
);
1390 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
1391 if ( pMatch
== NULL
)
1394 // -1 to compensate "++" in the loop
1395 pszTxt
= pMatch
+ uiLenMask
- 1;
1396 pszMask
+= uiLenMask
- 1;
1401 if ( *pszMask
!= *pszTxt
)
1407 // match only if nothing left
1408 return *pszTxt
== wxT('\0');
1411 // Count the number of chars
1412 int wxString::Freq(wxChar ch
) const
1416 for (int i
= 0; i
< len
; i
++)
1418 if (GetChar(i
) == ch
)
1424 // convert to upper case, return the copy of the string
1425 wxString
wxString::Upper() const
1426 { wxString
s(*this); return s
.MakeUpper(); }
1428 // convert to lower case, return the copy of the string
1429 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }
1431 int wxString::sprintf(const wxChar
*pszFormat
, ...)
1434 va_start(argptr
, pszFormat
);
1435 int iLen
= PrintfV(pszFormat
, argptr
);
1440 // ---------------------------------------------------------------------------
1441 // standard C++ library string functions
1442 // ---------------------------------------------------------------------------
1443 #ifdef wxSTD_STRING_COMPATIBILITY
1445 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1447 wxASSERT( str
.GetStringData()->IsValid() );
1448 wxASSERT( nPos
<= Len() );
1450 if ( !str
.IsEmpty() ) {
1452 wxChar
*pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1453 wxStrncpy(pc
, c_str(), nPos
);
1454 wxStrcpy(pc
+ nPos
, str
);
1455 wxStrcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1456 strTmp
.UngetWriteBuf();
1463 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1465 wxASSERT( str
.GetStringData()->IsValid() );
1466 wxASSERT( nStart
<= Len() );
1468 const wxChar
*p
= wxStrstr(c_str() + nStart
, str
);
1470 return p
== NULL
? npos
: p
- c_str();
1473 // VC++ 1.5 can't cope with the default argument in the header.
1474 #if !defined(__VISUALC__) || defined(__WIN32__)
1475 size_t wxString::find(const wxChar
* sz
, size_t nStart
, size_t n
) const
1477 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1481 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1482 #if !defined(__BORLANDC__)
1483 size_t wxString::find(wxChar ch
, size_t nStart
) const
1485 wxASSERT( nStart
<= Len() );
1487 const wxChar
*p
= wxStrchr(c_str() + nStart
, ch
);
1489 return p
== NULL
? npos
: p
- c_str();
1493 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1495 wxASSERT( str
.GetStringData()->IsValid() );
1496 wxASSERT( nStart
<= Len() );
1498 // TODO could be made much quicker than that
1499 const wxChar
*p
= c_str() + (nStart
== npos
? Len() : nStart
);
1500 while ( p
>= c_str() + str
.Len() ) {
1501 if ( wxStrncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1502 return p
- str
.Len() - c_str();
1509 // VC++ 1.5 can't cope with the default argument in the header.
1510 #if !defined(__VISUALC__) || defined(__WIN32__)
1511 size_t wxString::rfind(const wxChar
* sz
, size_t nStart
, size_t n
) const
1513 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1516 size_t wxString::rfind(wxChar ch
, size_t nStart
) const
1518 if ( nStart
== npos
)
1524 wxASSERT( nStart
<= Len() );
1527 const wxChar
*p
= wxStrrchr(c_str(), ch
);
1532 size_t result
= p
- c_str();
1533 return ( result
> nStart
) ? npos
: result
;
1537 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
1539 const wxChar
*start
= c_str() + nStart
;
1540 const wxChar
*firstOf
= wxStrpbrk(start
, sz
);
1542 return firstOf
- start
;
1547 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
1549 if ( nStart
== npos
)
1555 wxASSERT( nStart
<= Len() );
1558 for ( const wxChar
*p
= c_str() + length() - 1; p
>= c_str(); p
-- )
1560 if ( wxStrchr(sz
, *p
) )
1567 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
1569 if ( nStart
== npos
)
1575 wxASSERT( nStart
<= Len() );
1578 size_t nAccept
= wxStrspn(c_str() + nStart
, sz
);
1579 if ( nAccept
>= length() - nStart
)
1585 size_t wxString::find_first_not_of(wxChar ch
, size_t nStart
) const
1587 wxASSERT( nStart
<= Len() );
1589 for ( const wxChar
*p
= c_str() + nStart
; *p
; p
++ )
1598 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
1600 if ( nStart
== npos
)
1606 wxASSERT( nStart
<= Len() );
1609 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1611 if ( !wxStrchr(sz
, *p
) )
1618 size_t wxString::find_last_not_of(wxChar ch
, size_t nStart
) const
1620 if ( nStart
== npos
)
1626 wxASSERT( nStart
<= Len() );
1629 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1638 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1640 // npos means 'take all'
1644 wxASSERT( nStart
+ nLen
<= Len() );
1646 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1649 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1651 wxString
strTmp(c_str(), nStart
);
1652 if ( nLen
!= npos
) {
1653 wxASSERT( nStart
+ nLen
<= Len() );
1655 strTmp
.append(c_str() + nStart
+ nLen
);
1662 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const wxChar
*sz
)
1664 wxASSERT( nStart
+ nLen
<= wxStrlen(sz
) );
1668 strTmp
.append(c_str(), nStart
);
1670 strTmp
.append(c_str() + nStart
+ nLen
);
1676 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1678 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1681 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1682 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1684 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1687 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1688 const wxChar
* sz
, size_t nCount
)
1690 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1693 #endif //std::string compatibility
1695 // ============================================================================
1697 // ============================================================================
1699 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1700 #define ARRAY_MAXSIZE_INCREMENT 4096
1701 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1702 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1705 #define STRING(p) ((wxString *)(&(p)))
1708 wxArrayString::wxArrayString(bool autoSort
)
1712 m_pItems
= (wxChar
**) NULL
;
1713 m_autoSort
= autoSort
;
1717 wxArrayString::wxArrayString(const wxArrayString
& src
)
1721 m_pItems
= (wxChar
**) NULL
;
1722 m_autoSort
= src
.m_autoSort
;
1727 // assignment operator
1728 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1738 void wxArrayString::Copy(const wxArrayString
& src
)
1740 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1741 Alloc(src
.m_nCount
);
1743 // we can't just copy the pointers here because otherwise we would share
1744 // the strings with another array because strings are ref counted
1746 if ( m_nCount
!= 0 )
1747 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(wxChar
*));
1750 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1753 // if the other array is auto sorted too, we're already sorted, but
1754 // otherwise we should rearrange the items
1755 if ( m_autoSort
&& !src
.m_autoSort
)
1760 void wxArrayString::Grow()
1762 // only do it if no more place
1763 if( m_nCount
== m_nSize
) {
1764 if( m_nSize
== 0 ) {
1765 // was empty, alloc some memory
1766 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1767 m_pItems
= new wxChar
*[m_nSize
];
1770 // otherwise when it's called for the first time, nIncrement would be 0
1771 // and the array would never be expanded
1772 #if defined(__VISAGECPP__) && defined(__WXDEBUG__)
1773 int array_size
= ARRAY_DEFAULT_INITIAL_SIZE
;
1774 wxASSERT( array_size
!= 0 );
1776 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1779 // add 50% but not too much
1780 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1781 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1782 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1783 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1784 m_nSize
+= nIncrement
;
1785 wxChar
**pNew
= new wxChar
*[m_nSize
];
1787 // copy data to new location
1788 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1790 // delete old memory (but do not release the strings!)
1791 wxDELETEA(m_pItems
);
1798 void wxArrayString::Free()
1800 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1801 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1805 // deletes all the strings from the list
1806 void wxArrayString::Empty()
1813 // as Empty, but also frees memory
1814 void wxArrayString::Clear()
1821 wxDELETEA(m_pItems
);
1825 wxArrayString::~wxArrayString()
1829 wxDELETEA(m_pItems
);
1832 // pre-allocates memory (frees the previous data!)
1833 void wxArrayString::Alloc(size_t nSize
)
1835 wxASSERT( nSize
> 0 );
1837 // only if old buffer was not big enough
1838 if ( nSize
> m_nSize
) {
1840 wxDELETEA(m_pItems
);
1841 m_pItems
= new wxChar
*[nSize
];
1848 // minimizes the memory usage by freeing unused memory
1849 void wxArrayString::Shrink()
1851 // only do it if we have some memory to free
1852 if( m_nCount
< m_nSize
) {
1853 // allocates exactly as much memory as we need
1854 wxChar
**pNew
= new wxChar
*[m_nCount
];
1856 // copy data to new location
1857 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1863 // searches the array for an item (forward or backwards)
1864 int wxArrayString::Index(const wxChar
*sz
, bool bCase
, bool bFromEnd
) const
1867 // use binary search in the sorted array
1868 wxASSERT_MSG( bCase
&& !bFromEnd
,
1869 wxT("search parameters ignored for auto sorted array") );
1878 res
= wxStrcmp(sz
, m_pItems
[i
]);
1890 // use linear search in unsorted array
1892 if ( m_nCount
> 0 ) {
1893 size_t ui
= m_nCount
;
1895 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1902 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1903 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1912 // add item at the end
1913 size_t wxArrayString::Add(const wxString
& str
)
1916 // insert the string at the correct position to keep the array sorted
1924 res
= wxStrcmp(str
, m_pItems
[i
]);
1935 wxASSERT_MSG( lo
== hi
, wxT("binary search broken") );
1942 wxASSERT( str
.GetStringData()->IsValid() );
1946 // the string data must not be deleted!
1947 str
.GetStringData()->Lock();
1950 m_pItems
[m_nCount
] = (wxChar
*)str
.c_str(); // const_cast
1956 // add item at the given position
1957 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1959 wxASSERT( str
.GetStringData()->IsValid() );
1961 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Insert") );
1965 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1966 (m_nCount
- nIndex
)*sizeof(wxChar
*));
1968 str
.GetStringData()->Lock();
1969 m_pItems
[nIndex
] = (wxChar
*)str
.c_str();
1974 // removes item from array (by index)
1975 void wxArrayString::Remove(size_t nIndex
)
1977 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Remove") );
1980 Item(nIndex
).GetStringData()->Unlock();
1982 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1983 (m_nCount
- nIndex
- 1)*sizeof(wxChar
*));
1987 // removes item from array (by value)
1988 void wxArrayString::Remove(const wxChar
*sz
)
1990 int iIndex
= Index(sz
);
1992 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
1993 wxT("removing inexistent element in wxArrayString::Remove") );
1998 // ----------------------------------------------------------------------------
2000 // ----------------------------------------------------------------------------
2002 // we can only sort one array at a time with the quick-sort based
2005 // need a critical section to protect access to gs_compareFunction and
2006 // gs_sortAscending variables
2007 static wxCriticalSection
*gs_critsectStringSort
= NULL
;
2009 // call this before the value of the global sort vars is changed/after
2010 // you're finished with them
2011 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
2012 gs_critsectStringSort = new wxCriticalSection; \
2013 gs_critsectStringSort->Enter()
2014 #define END_SORT() gs_critsectStringSort->Leave(); \
2015 delete gs_critsectStringSort; \
2016 gs_critsectStringSort = NULL
2018 #define START_SORT()
2020 #endif // wxUSE_THREADS
2022 // function to use for string comparaison
2023 static wxArrayString::CompareFunction gs_compareFunction
= NULL
;
2025 // if we don't use the compare function, this flag tells us if we sort the
2026 // array in ascending or descending order
2027 static bool gs_sortAscending
= TRUE
;
2029 // function which is called by quick sort
2030 static int LINKAGEMODE
wxStringCompareFunction(const void *first
, const void *second
)
2032 wxString
*strFirst
= (wxString
*)first
;
2033 wxString
*strSecond
= (wxString
*)second
;
2035 if ( gs_compareFunction
) {
2036 return gs_compareFunction(*strFirst
, *strSecond
);
2039 // maybe we should use wxStrcoll
2040 int result
= wxStrcmp(strFirst
->c_str(), strSecond
->c_str());
2042 return gs_sortAscending
? result
: -result
;
2046 // sort array elements using passed comparaison function
2047 void wxArrayString::Sort(CompareFunction compareFunction
)
2051 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2052 gs_compareFunction
= compareFunction
;
2059 void wxArrayString::Sort(bool reverseOrder
)
2063 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2064 gs_sortAscending
= !reverseOrder
;
2071 void wxArrayString::DoSort()
2073 wxCHECK_RET( !m_autoSort
, wxT("can't use this method with sorted arrays") );
2075 // just sort the pointers using qsort() - of course it only works because
2076 // wxString() *is* a pointer to its data
2077 qsort(m_pItems
, m_nCount
, sizeof(wxChar
*), wxStringCompareFunction
);