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
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() || (nLen
> pData
->nAllocLength
) ) {
395 // can't work with old buffer, get new one
400 // update the string length
401 pData
->nDataLength
= nLen
;
404 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
407 // allocate enough memory for nLen characters
408 void wxString::Alloc(size_t nLen
)
410 wxStringData
*pData
= GetStringData();
411 if ( pData
->nAllocLength
<= nLen
) {
412 if ( pData
->IsEmpty() ) {
415 wxStringData
* pData
= (wxStringData
*)
416 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
418 pData
->nDataLength
= 0;
419 pData
->nAllocLength
= nLen
;
420 m_pchData
= pData
->data(); // data starts after wxStringData
421 m_pchData
[0u] = wxT('\0');
423 else if ( pData
->IsShared() ) {
424 pData
->Unlock(); // memory not freed because shared
425 size_t nOldLen
= pData
->nDataLength
;
427 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(wxChar
));
432 wxStringData
*p
= (wxStringData
*)
433 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
436 // @@@ what to do on memory error?
440 // it's not important if the pointer changed or not (the check for this
441 // is not faster than assigning to m_pchData in all cases)
442 p
->nAllocLength
= nLen
;
443 m_pchData
= p
->data();
446 //else: we've already got enough
449 // shrink to minimal size (releasing extra memory)
450 void wxString::Shrink()
452 wxStringData
*pData
= GetStringData();
454 // this variable is unused in release build, so avoid the compiler warning by
455 // just not declaring it
459 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(wxChar
));
461 wxASSERT( p
!= NULL
); // can't free memory?
462 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
465 // get the pointer to writable buffer of (at least) nLen bytes
466 wxChar
*wxString::GetWriteBuf(size_t nLen
)
468 AllocBeforeWrite(nLen
);
470 wxASSERT( GetStringData()->nRefs
== 1 );
471 GetStringData()->Validate(FALSE
);
476 // put string back in a reasonable state after GetWriteBuf
477 void wxString::UngetWriteBuf()
479 GetStringData()->nDataLength
= wxStrlen(m_pchData
);
480 GetStringData()->Validate(TRUE
);
483 // ---------------------------------------------------------------------------
485 // ---------------------------------------------------------------------------
487 // all functions are inline in string.h
489 // ---------------------------------------------------------------------------
490 // assignment operators
491 // ---------------------------------------------------------------------------
493 // helper function: does real copy
494 void wxString::AssignCopy(size_t nSrcLen
, const wxChar
*pszSrcData
)
496 if ( nSrcLen
== 0 ) {
500 AllocBeforeWrite(nSrcLen
);
501 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
502 GetStringData()->nDataLength
= nSrcLen
;
503 m_pchData
[nSrcLen
] = wxT('\0');
507 // assigns one string to another
508 wxString
& wxString::operator=(const wxString
& stringSrc
)
510 wxASSERT( stringSrc
.GetStringData()->IsValid() );
512 // don't copy string over itself
513 if ( m_pchData
!= stringSrc
.m_pchData
) {
514 if ( stringSrc
.GetStringData()->IsEmpty() ) {
519 GetStringData()->Unlock();
520 m_pchData
= stringSrc
.m_pchData
;
521 GetStringData()->Lock();
528 // assigns a single character
529 wxString
& wxString::operator=(wxChar ch
)
536 wxString
& wxString::operator=(const wxChar
*psz
)
538 AssignCopy(wxStrlen(psz
), psz
);
544 // same as 'signed char' variant
545 wxString
& wxString::operator=(const unsigned char* psz
)
547 *this = (const char *)psz
;
552 wxString
& wxString::operator=(const wchar_t *pwz
)
562 // ---------------------------------------------------------------------------
563 // string concatenation
564 // ---------------------------------------------------------------------------
566 // add something to this string
567 void wxString::ConcatSelf(int nSrcLen
, const wxChar
*pszSrcData
)
569 STATISTICS_ADD(SummandLength
, nSrcLen
);
571 // concatenating an empty string is a NOP
573 wxStringData
*pData
= GetStringData();
574 size_t nLen
= pData
->nDataLength
;
575 size_t nNewLen
= nLen
+ nSrcLen
;
577 // alloc new buffer if current is too small
578 if ( pData
->IsShared() ) {
579 STATISTICS_ADD(ConcatHit
, 0);
581 // we have to allocate another buffer
582 wxStringData
* pOldData
= GetStringData();
583 AllocBuffer(nNewLen
);
584 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(wxChar
));
587 else if ( nNewLen
> pData
->nAllocLength
) {
588 STATISTICS_ADD(ConcatHit
, 0);
590 // we have to grow the buffer
594 STATISTICS_ADD(ConcatHit
, 1);
596 // the buffer is already big enough
599 // should be enough space
600 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
602 // fast concatenation - all is done in our buffer
603 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
605 m_pchData
[nNewLen
] = wxT('\0'); // put terminating '\0'
606 GetStringData()->nDataLength
= nNewLen
; // and fix the length
608 //else: the string to append was empty
612 * concatenation functions come in 5 flavours:
614 * char + string and string + char
615 * C str + string and string + C str
618 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
620 wxASSERT( string1
.GetStringData()->IsValid() );
621 wxASSERT( string2
.GetStringData()->IsValid() );
623 wxString s
= string1
;
629 wxString
operator+(const wxString
& string
, wxChar ch
)
631 wxASSERT( string
.GetStringData()->IsValid() );
639 wxString
operator+(wxChar ch
, const wxString
& string
)
641 wxASSERT( string
.GetStringData()->IsValid() );
649 wxString
operator+(const wxString
& string
, const wxChar
*psz
)
651 wxASSERT( string
.GetStringData()->IsValid() );
654 s
.Alloc(wxStrlen(psz
) + string
.Len());
661 wxString
operator+(const wxChar
*psz
, const wxString
& string
)
663 wxASSERT( string
.GetStringData()->IsValid() );
666 s
.Alloc(wxStrlen(psz
) + string
.Len());
673 // ===========================================================================
674 // other common string functions
675 // ===========================================================================
677 // ---------------------------------------------------------------------------
678 // simple sub-string extraction
679 // ---------------------------------------------------------------------------
681 // helper function: clone the data attached to this string
682 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
684 if ( nCopyLen
== 0 ) {
688 dest
.AllocBuffer(nCopyLen
);
689 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(wxChar
));
693 // extract string of length nCount starting at nFirst
694 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
696 wxStringData
*pData
= GetStringData();
697 size_t nLen
= pData
->nDataLength
;
699 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
700 if ( nCount
== wxSTRING_MAXLEN
)
702 nCount
= nLen
- nFirst
;
705 // out-of-bounds requests return sensible things
706 if ( nFirst
+ nCount
> nLen
)
708 nCount
= nLen
- nFirst
;
713 // AllocCopy() will return empty string
718 AllocCopy(dest
, nCount
, nFirst
);
723 // extract nCount last (rightmost) characters
724 wxString
wxString::Right(size_t nCount
) const
726 if ( nCount
> (size_t)GetStringData()->nDataLength
)
727 nCount
= GetStringData()->nDataLength
;
730 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
734 // get all characters after the last occurence of ch
735 // (returns the whole string if ch not found)
736 wxString
wxString::AfterLast(wxChar ch
) const
739 int iPos
= Find(ch
, TRUE
);
740 if ( iPos
== wxNOT_FOUND
)
743 str
= c_str() + iPos
+ 1;
748 // extract nCount first (leftmost) characters
749 wxString
wxString::Left(size_t nCount
) const
751 if ( nCount
> (size_t)GetStringData()->nDataLength
)
752 nCount
= GetStringData()->nDataLength
;
755 AllocCopy(dest
, nCount
, 0);
759 // get all characters before the first occurence of ch
760 // (returns the whole string if ch not found)
761 wxString
wxString::BeforeFirst(wxChar ch
) const
764 for ( const wxChar
*pc
= m_pchData
; *pc
!= wxT('\0') && *pc
!= ch
; pc
++ )
770 /// get all characters before the last occurence of ch
771 /// (returns empty string if ch not found)
772 wxString
wxString::BeforeLast(wxChar ch
) const
775 int iPos
= Find(ch
, TRUE
);
776 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
777 str
= wxString(c_str(), iPos
);
782 /// get all characters after the first occurence of ch
783 /// (returns empty string if ch not found)
784 wxString
wxString::AfterFirst(wxChar ch
) const
788 if ( iPos
!= wxNOT_FOUND
)
789 str
= c_str() + iPos
+ 1;
794 // replace first (or all) occurences of some substring with another one
795 size_t wxString::Replace(const wxChar
*szOld
, const wxChar
*szNew
, bool bReplaceAll
)
797 size_t uiCount
= 0; // count of replacements made
799 size_t uiOldLen
= wxStrlen(szOld
);
802 const wxChar
*pCurrent
= m_pchData
;
803 const wxChar
*pSubstr
;
804 while ( *pCurrent
!= wxT('\0') ) {
805 pSubstr
= wxStrstr(pCurrent
, szOld
);
806 if ( pSubstr
== NULL
) {
807 // strTemp is unused if no replacements were made, so avoid the copy
811 strTemp
+= pCurrent
; // copy the rest
812 break; // exit the loop
815 // take chars before match
816 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
818 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
823 if ( !bReplaceAll
) {
824 strTemp
+= pCurrent
; // copy the rest
825 break; // exit the loop
830 // only done if there were replacements, otherwise would have returned above
836 bool wxString::IsAscii() const
838 const wxChar
*s
= (const wxChar
*) *this;
840 if(!isascii(*s
)) return(FALSE
);
846 bool wxString::IsWord() const
848 const wxChar
*s
= (const wxChar
*) *this;
850 if(!wxIsalpha(*s
)) return(FALSE
);
856 bool wxString::IsNumber() const
858 const wxChar
*s
= (const wxChar
*) *this;
860 if(!wxIsdigit(*s
)) return(FALSE
);
866 wxString
wxString::Strip(stripType w
) const
869 if ( w
& leading
) s
.Trim(FALSE
);
870 if ( w
& trailing
) s
.Trim(TRUE
);
874 // ---------------------------------------------------------------------------
876 // ---------------------------------------------------------------------------
878 wxString
& wxString::MakeUpper()
882 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
883 *p
= (wxChar
)wxToupper(*p
);
888 wxString
& wxString::MakeLower()
892 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
893 *p
= (wxChar
)wxTolower(*p
);
898 // ---------------------------------------------------------------------------
899 // trimming and padding
900 // ---------------------------------------------------------------------------
902 // trims spaces (in the sense of isspace) from left or right side
903 wxString
& wxString::Trim(bool bFromRight
)
905 // first check if we're going to modify the string at all
908 (bFromRight
&& wxIsspace(GetChar(Len() - 1))) ||
909 (!bFromRight
&& wxIsspace(GetChar(0u)))
913 // ok, there is at least one space to trim
918 // find last non-space character
919 wxChar
*psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
920 while ( wxIsspace(*psz
) && (psz
>= m_pchData
) )
923 // truncate at trailing space start
925 GetStringData()->nDataLength
= psz
- m_pchData
;
929 // find first non-space character
930 const wxChar
*psz
= m_pchData
;
931 while ( wxIsspace(*psz
) )
934 // fix up data and length
935 int nDataLength
= GetStringData()->nDataLength
- (psz
- (const wxChar
*) m_pchData
);
936 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(wxChar
));
937 GetStringData()->nDataLength
= nDataLength
;
944 // adds nCount characters chPad to the string from either side
945 wxString
& wxString::Pad(size_t nCount
, wxChar chPad
, bool bFromRight
)
947 wxString
s(chPad
, nCount
);
960 // truncate the string
961 wxString
& wxString::Truncate(size_t uiLen
)
963 if ( uiLen
< Len() ) {
966 *(m_pchData
+ uiLen
) = wxT('\0');
967 GetStringData()->nDataLength
= uiLen
;
969 //else: nothing to do, string is already short enough
974 // ---------------------------------------------------------------------------
975 // finding (return wxNOT_FOUND if not found and index otherwise)
976 // ---------------------------------------------------------------------------
979 int wxString::Find(wxChar ch
, bool bFromEnd
) const
981 const wxChar
*psz
= bFromEnd
? wxStrrchr(m_pchData
, ch
) : wxStrchr(m_pchData
, ch
);
983 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
986 // find a sub-string (like strstr)
987 int wxString::Find(const wxChar
*pszSub
) const
989 const wxChar
*psz
= wxStrstr(m_pchData
, pszSub
);
991 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
994 // ---------------------------------------------------------------------------
995 // stream-like operators
996 // ---------------------------------------------------------------------------
997 wxString
& wxString::operator<<(int i
)
1000 res
.Printf(wxT("%d"), i
);
1002 return (*this) << res
;
1005 wxString
& wxString::operator<<(float f
)
1008 res
.Printf(wxT("%f"), f
);
1010 return (*this) << res
;
1013 wxString
& wxString::operator<<(double d
)
1016 res
.Printf(wxT("%g"), d
);
1018 return (*this) << res
;
1021 // ---------------------------------------------------------------------------
1023 // ---------------------------------------------------------------------------
1025 int wxString::Printf(const wxChar
*pszFormat
, ...)
1028 va_start(argptr
, pszFormat
);
1030 int iLen
= PrintfV(pszFormat
, argptr
);
1037 int wxString::PrintfV(const wxChar
* pszFormat
, va_list argptr
)
1039 #if wxUSE_EXPERIMENTAL_PRINTF
1040 // the new implementation
1042 // buffer to avoid dynamic memory allocation each time for small strings
1043 char szScratch
[1024];
1046 for (size_t n
= 0; pszFormat
[n
]; n
++)
1047 if (pszFormat
[n
] == wxT('%')) {
1048 static char s_szFlags
[256] = "%";
1050 bool adj_left
= FALSE
, in_prec
= FALSE
,
1051 prec_dot
= FALSE
, done
= FALSE
;
1053 size_t min_width
= 0, max_width
= wxSTRING_MAXLEN
;
1055 #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1056 switch (pszFormat
[++n
]) {
1070 s_szFlags
[flagofs
++] = pszFormat
[n
];
1075 s_szFlags
[flagofs
++] = pszFormat
[n
];
1082 // dot will be auto-added to s_szFlags if non-negative number follows
1087 s_szFlags
[flagofs
++] = pszFormat
[n
];
1092 s_szFlags
[flagofs
++] = pszFormat
[n
];
1098 s_szFlags
[flagofs
++] = pszFormat
[n
];
1103 s_szFlags
[flagofs
++] = pszFormat
[n
];
1107 int len
= va_arg(argptr
, int);
1114 adj_left
= !adj_left
;
1115 s_szFlags
[flagofs
++] = '-';
1120 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
1123 case wxT('1'): case wxT('2'): case wxT('3'):
1124 case wxT('4'): case wxT('5'): case wxT('6'):
1125 case wxT('7'): case wxT('8'): case wxT('9'):
1129 while ((pszFormat
[n
]>=wxT('0')) && (pszFormat
[n
]<=wxT('9'))) {
1130 s_szFlags
[flagofs
++] = pszFormat
[n
];
1131 len
= len
*10 + (pszFormat
[n
] - wxT('0'));
1134 if (in_prec
) max_width
= len
;
1135 else min_width
= len
;
1136 n
--; // the main loop pre-increments n again
1146 s_szFlags
[flagofs
++] = pszFormat
[n
];
1147 s_szFlags
[flagofs
] = '\0';
1149 int val
= va_arg(argptr
, int);
1150 ::sprintf(szScratch
, s_szFlags
, val
);
1152 else if (ilen
== -1) {
1153 short int val
= va_arg(argptr
, short int);
1154 ::sprintf(szScratch
, s_szFlags
, val
);
1156 else if (ilen
== 1) {
1157 long int val
= va_arg(argptr
, long int);
1158 ::sprintf(szScratch
, s_szFlags
, val
);
1160 else if (ilen
== 2) {
1161 #if SIZEOF_LONG_LONG
1162 long long int val
= va_arg(argptr
, long long int);
1163 ::sprintf(szScratch
, s_szFlags
, val
);
1165 long int val
= va_arg(argptr
, long int);
1166 ::sprintf(szScratch
, s_szFlags
, val
);
1169 else if (ilen
== 3) {
1170 size_t val
= va_arg(argptr
, size_t);
1171 ::sprintf(szScratch
, s_szFlags
, val
);
1173 *this += wxString(szScratch
);
1182 s_szFlags
[flagofs
++] = pszFormat
[n
];
1183 s_szFlags
[flagofs
] = '\0';
1185 long double val
= va_arg(argptr
, long double);
1186 ::sprintf(szScratch
, s_szFlags
, val
);
1188 double val
= va_arg(argptr
, double);
1189 ::sprintf(szScratch
, s_szFlags
, val
);
1191 *this += wxString(szScratch
);
1196 void *val
= va_arg(argptr
, void *);
1198 s_szFlags
[flagofs
++] = pszFormat
[n
];
1199 s_szFlags
[flagofs
] = '\0';
1200 ::sprintf(szScratch
, s_szFlags
, val
);
1201 *this += wxString(szScratch
);
1207 wxChar val
= va_arg(argptr
, int);
1208 // we don't need to honor padding here, do we?
1215 // wx extension: we'll let %hs mean non-Unicode strings
1216 char *val
= va_arg(argptr
, char *);
1218 // ASCII->Unicode constructor handles max_width right
1219 wxString
s(val
, wxConvLibc
, max_width
);
1221 size_t len
= wxSTRING_MAXLEN
;
1223 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1224 } else val
= wxT("(null)");
1225 wxString
s(val
, len
);
1227 if (s
.Len() < min_width
)
1228 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1231 wxChar
*val
= va_arg(argptr
, wxChar
*);
1232 size_t len
= wxSTRING_MAXLEN
;
1234 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1235 } else val
= wxT("(null)");
1236 wxString
s(val
, len
);
1237 if (s
.Len() < min_width
)
1238 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1245 int *val
= va_arg(argptr
, int *);
1248 else if (ilen
== -1) {
1249 short int *val
= va_arg(argptr
, short int *);
1252 else if (ilen
>= 1) {
1253 long int *val
= va_arg(argptr
, long int *);
1259 if (wxIsalpha(pszFormat
[n
]))
1260 // probably some flag not taken care of here yet
1261 s_szFlags
[flagofs
++] = pszFormat
[n
];
1264 *this += wxT('%'); // just to pass the glibc tst-printf.c
1272 } else *this += pszFormat
[n
];
1275 // buffer to avoid dynamic memory allocation each time for small strings
1276 char szScratch
[1024];
1278 // NB: wxVsnprintf() may return either less than the buffer size or -1 if
1279 // there is not enough place depending on implementation
1280 int iLen
= wxVsnprintfA(szScratch
, WXSIZEOF(szScratch
), pszFormat
, argptr
);
1282 // the whole string is in szScratch
1286 bool outOfMemory
= FALSE
;
1287 int size
= 2*WXSIZEOF(szScratch
);
1288 while ( !outOfMemory
) {
1289 char *buf
= GetWriteBuf(size
);
1291 iLen
= wxVsnprintfA(buf
, size
, pszFormat
, argptr
);
1298 // ok, there was enough space
1302 // still not enough, double it again
1306 if ( outOfMemory
) {
1311 #endif // wxUSE_EXPERIMENTAL_PRINTF/!wxUSE_EXPERIMENTAL_PRINTF
1316 // ----------------------------------------------------------------------------
1317 // misc other operations
1318 // ----------------------------------------------------------------------------
1320 // returns TRUE if the string matches the pattern which may contain '*' and
1321 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1323 bool wxString::Matches(const wxChar
*pszMask
) const
1325 // check char by char
1326 const wxChar
*pszTxt
;
1327 for ( pszTxt
= c_str(); *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
1328 switch ( *pszMask
) {
1330 if ( *pszTxt
== wxT('\0') )
1333 // pszText and pszMask will be incremented in the loop statement
1339 // ignore special chars immediately following this one
1340 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
1343 // if there is nothing more, match
1344 if ( *pszMask
== wxT('\0') )
1347 // are there any other metacharacters in the mask?
1349 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
1351 if ( pEndMask
!= NULL
) {
1352 // we have to match the string between two metachars
1353 uiLenMask
= pEndMask
- pszMask
;
1356 // we have to match the remainder of the string
1357 uiLenMask
= wxStrlen(pszMask
);
1360 wxString
strToMatch(pszMask
, uiLenMask
);
1361 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
1362 if ( pMatch
== NULL
)
1365 // -1 to compensate "++" in the loop
1366 pszTxt
= pMatch
+ uiLenMask
- 1;
1367 pszMask
+= uiLenMask
- 1;
1372 if ( *pszMask
!= *pszTxt
)
1378 // match only if nothing left
1379 return *pszTxt
== wxT('\0');
1382 // Count the number of chars
1383 int wxString::Freq(wxChar ch
) const
1387 for (int i
= 0; i
< len
; i
++)
1389 if (GetChar(i
) == ch
)
1395 // convert to upper case, return the copy of the string
1396 wxString
wxString::Upper() const
1397 { wxString
s(*this); return s
.MakeUpper(); }
1399 // convert to lower case, return the copy of the string
1400 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }
1402 int wxString::sprintf(const wxChar
*pszFormat
, ...)
1405 va_start(argptr
, pszFormat
);
1406 int iLen
= PrintfV(pszFormat
, argptr
);
1411 // ---------------------------------------------------------------------------
1412 // standard C++ library string functions
1413 // ---------------------------------------------------------------------------
1414 #ifdef wxSTD_STRING_COMPATIBILITY
1416 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1418 wxASSERT( str
.GetStringData()->IsValid() );
1419 wxASSERT( nPos
<= Len() );
1421 if ( !str
.IsEmpty() ) {
1423 wxChar
*pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1424 wxStrncpy(pc
, c_str(), nPos
);
1425 wxStrcpy(pc
+ nPos
, str
);
1426 wxStrcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1427 strTmp
.UngetWriteBuf();
1434 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1436 wxASSERT( str
.GetStringData()->IsValid() );
1437 wxASSERT( nStart
<= Len() );
1439 const wxChar
*p
= wxStrstr(c_str() + nStart
, str
);
1441 return p
== NULL
? npos
: p
- c_str();
1444 // VC++ 1.5 can't cope with the default argument in the header.
1445 #if !defined(__VISUALC__) || defined(__WIN32__)
1446 size_t wxString::find(const wxChar
* sz
, size_t nStart
, size_t n
) const
1448 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1452 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1453 #if !defined(__BORLANDC__)
1454 size_t wxString::find(wxChar ch
, size_t nStart
) const
1456 wxASSERT( nStart
<= Len() );
1458 const wxChar
*p
= wxStrchr(c_str() + nStart
, ch
);
1460 return p
== NULL
? npos
: p
- c_str();
1464 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1466 wxASSERT( str
.GetStringData()->IsValid() );
1467 wxASSERT( nStart
<= Len() );
1469 // TODO could be made much quicker than that
1470 const wxChar
*p
= c_str() + (nStart
== npos
? Len() : nStart
);
1471 while ( p
>= c_str() + str
.Len() ) {
1472 if ( wxStrncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1473 return p
- str
.Len() - c_str();
1480 // VC++ 1.5 can't cope with the default argument in the header.
1481 #if !defined(__VISUALC__) || defined(__WIN32__)
1482 size_t wxString::rfind(const wxChar
* sz
, size_t nStart
, size_t n
) const
1484 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1487 size_t wxString::rfind(wxChar ch
, size_t nStart
) const
1489 if ( nStart
== npos
)
1495 wxASSERT( nStart
<= Len() );
1498 const wxChar
*p
= wxStrrchr(c_str(), ch
);
1503 size_t result
= p
- c_str();
1504 return ( result
> nStart
) ? npos
: result
;
1508 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
1510 const wxChar
*start
= c_str() + nStart
;
1511 const wxChar
*firstOf
= wxStrpbrk(start
, sz
);
1513 return firstOf
- start
;
1518 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
1520 if ( nStart
== npos
)
1526 wxASSERT( nStart
<= Len() );
1529 for ( const wxChar
*p
= c_str() + length() - 1; p
>= c_str(); p
-- )
1531 if ( wxStrchr(sz
, *p
) )
1538 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
1540 if ( nStart
== npos
)
1546 wxASSERT( nStart
<= Len() );
1549 size_t nAccept
= wxStrspn(c_str() + nStart
, sz
);
1550 if ( nAccept
>= length() - nStart
)
1556 size_t wxString::find_first_not_of(wxChar ch
, size_t nStart
) const
1558 wxASSERT( nStart
<= Len() );
1560 for ( const wxChar
*p
= c_str() + nStart
; *p
; p
++ )
1569 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
1571 if ( nStart
== npos
)
1577 wxASSERT( nStart
<= Len() );
1580 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1582 if ( !wxStrchr(sz
, *p
) )
1589 size_t wxString::find_last_not_of(wxChar ch
, size_t nStart
) const
1591 if ( nStart
== npos
)
1597 wxASSERT( nStart
<= Len() );
1600 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1609 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1611 // npos means 'take all'
1615 wxASSERT( nStart
+ nLen
<= Len() );
1617 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1620 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1622 wxString
strTmp(c_str(), nStart
);
1623 if ( nLen
!= npos
) {
1624 wxASSERT( nStart
+ nLen
<= Len() );
1626 strTmp
.append(c_str() + nStart
+ nLen
);
1633 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const wxChar
*sz
)
1635 wxASSERT( nStart
+ nLen
<= wxStrlen(sz
) );
1639 strTmp
.append(c_str(), nStart
);
1641 strTmp
.append(c_str() + nStart
+ nLen
);
1647 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1649 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1652 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1653 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1655 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1658 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1659 const wxChar
* sz
, size_t nCount
)
1661 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1664 #endif //std::string compatibility
1666 // ============================================================================
1668 // ============================================================================
1670 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1671 #define ARRAY_MAXSIZE_INCREMENT 4096
1672 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1673 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1676 #define STRING(p) ((wxString *)(&(p)))
1679 wxArrayString::wxArrayString(bool autoSort
)
1683 m_pItems
= (wxChar
**) NULL
;
1684 m_autoSort
= autoSort
;
1688 wxArrayString::wxArrayString(const wxArrayString
& src
)
1692 m_pItems
= (wxChar
**) NULL
;
1693 m_autoSort
= src
.m_autoSort
;
1698 // assignment operator
1699 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1709 void wxArrayString::Copy(const wxArrayString
& src
)
1711 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1712 Alloc(src
.m_nCount
);
1714 // we can't just copy the pointers here because otherwise we would share
1715 // the strings with another array because strings are ref counted
1717 if ( m_nCount
!= 0 )
1718 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(wxChar
*));
1721 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1724 // if the other array is auto sorted too, we're already sorted, but
1725 // otherwise we should rearrange the items
1726 if ( m_autoSort
&& !src
.m_autoSort
)
1731 void wxArrayString::Grow()
1733 // only do it if no more place
1734 if( m_nCount
== m_nSize
) {
1735 if( m_nSize
== 0 ) {
1736 // was empty, alloc some memory
1737 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1738 m_pItems
= new wxChar
*[m_nSize
];
1741 // otherwise when it's called for the first time, nIncrement would be 0
1742 // and the array would never be expanded
1743 #if defined(__VISAGECPP__) && defined(__WXDEBUG__)
1744 int array_size
= ARRAY_DEFAULT_INITIAL_SIZE
;
1745 wxASSERT( array_size
!= 0 );
1747 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1750 // add 50% but not too much
1751 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1752 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1753 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1754 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1755 m_nSize
+= nIncrement
;
1756 wxChar
**pNew
= new wxChar
*[m_nSize
];
1758 // copy data to new location
1759 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1761 // delete old memory (but do not release the strings!)
1762 wxDELETEA(m_pItems
);
1769 void wxArrayString::Free()
1771 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1772 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1776 // deletes all the strings from the list
1777 void wxArrayString::Empty()
1784 // as Empty, but also frees memory
1785 void wxArrayString::Clear()
1792 wxDELETEA(m_pItems
);
1796 wxArrayString::~wxArrayString()
1800 wxDELETEA(m_pItems
);
1803 // pre-allocates memory (frees the previous data!)
1804 void wxArrayString::Alloc(size_t nSize
)
1806 wxASSERT( nSize
> 0 );
1808 // only if old buffer was not big enough
1809 if ( nSize
> m_nSize
) {
1811 wxDELETEA(m_pItems
);
1812 m_pItems
= new wxChar
*[nSize
];
1819 // minimizes the memory usage by freeing unused memory
1820 void wxArrayString::Shrink()
1822 // only do it if we have some memory to free
1823 if( m_nCount
< m_nSize
) {
1824 // allocates exactly as much memory as we need
1825 wxChar
**pNew
= new wxChar
*[m_nCount
];
1827 // copy data to new location
1828 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1834 // searches the array for an item (forward or backwards)
1835 int wxArrayString::Index(const wxChar
*sz
, bool bCase
, bool bFromEnd
) const
1838 // use binary search in the sorted array
1839 wxASSERT_MSG( bCase
&& !bFromEnd
,
1840 wxT("search parameters ignored for auto sorted array") );
1849 res
= wxStrcmp(sz
, m_pItems
[i
]);
1861 // use linear search in unsorted array
1863 if ( m_nCount
> 0 ) {
1864 size_t ui
= m_nCount
;
1866 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1873 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1874 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1883 // add item at the end
1884 size_t wxArrayString::Add(const wxString
& str
)
1887 // insert the string at the correct position to keep the array sorted
1895 res
= wxStrcmp(str
, m_pItems
[i
]);
1906 wxASSERT_MSG( lo
== hi
, wxT("binary search broken") );
1913 wxASSERT( str
.GetStringData()->IsValid() );
1917 // the string data must not be deleted!
1918 str
.GetStringData()->Lock();
1921 m_pItems
[m_nCount
] = (wxChar
*)str
.c_str(); // const_cast
1927 // add item at the given position
1928 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1930 wxASSERT( str
.GetStringData()->IsValid() );
1932 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Insert") );
1936 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1937 (m_nCount
- nIndex
)*sizeof(wxChar
*));
1939 str
.GetStringData()->Lock();
1940 m_pItems
[nIndex
] = (wxChar
*)str
.c_str();
1945 // removes item from array (by index)
1946 void wxArrayString::Remove(size_t nIndex
)
1948 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Remove") );
1951 Item(nIndex
).GetStringData()->Unlock();
1953 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1954 (m_nCount
- nIndex
- 1)*sizeof(wxChar
*));
1958 // removes item from array (by value)
1959 void wxArrayString::Remove(const wxChar
*sz
)
1961 int iIndex
= Index(sz
);
1963 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
1964 wxT("removing inexistent element in wxArrayString::Remove") );
1969 // ----------------------------------------------------------------------------
1971 // ----------------------------------------------------------------------------
1973 // we can only sort one array at a time with the quick-sort based
1976 // need a critical section to protect access to gs_compareFunction and
1977 // gs_sortAscending variables
1978 static wxCriticalSection
*gs_critsectStringSort
= NULL
;
1980 // call this before the value of the global sort vars is changed/after
1981 // you're finished with them
1982 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
1983 gs_critsectStringSort = new wxCriticalSection; \
1984 gs_critsectStringSort->Enter()
1985 #define END_SORT() gs_critsectStringSort->Leave(); \
1986 delete gs_critsectStringSort; \
1987 gs_critsectStringSort = NULL
1989 #define START_SORT()
1991 #endif // wxUSE_THREADS
1993 // function to use for string comparaison
1994 static wxArrayString::CompareFunction gs_compareFunction
= NULL
;
1996 // if we don't use the compare function, this flag tells us if we sort the
1997 // array in ascending or descending order
1998 static bool gs_sortAscending
= TRUE
;
2000 // function which is called by quick sort
2001 static int LINKAGEMODE
wxStringCompareFunction(const void *first
, const void *second
)
2003 wxString
*strFirst
= (wxString
*)first
;
2004 wxString
*strSecond
= (wxString
*)second
;
2006 if ( gs_compareFunction
) {
2007 return gs_compareFunction(*strFirst
, *strSecond
);
2010 // maybe we should use wxStrcoll
2011 int result
= wxStrcmp(strFirst
->c_str(), strSecond
->c_str());
2013 return gs_sortAscending
? result
: -result
;
2017 // sort array elements using passed comparaison function
2018 void wxArrayString::Sort(CompareFunction compareFunction
)
2022 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2023 gs_compareFunction
= compareFunction
;
2030 void wxArrayString::Sort(bool reverseOrder
)
2034 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2035 gs_sortAscending
= !reverseOrder
;
2042 void wxArrayString::DoSort()
2044 wxCHECK_RET( !m_autoSort
, wxT("can't use this method with sorted arrays") );
2046 // just sort the pointers using qsort() - of course it only works because
2047 // wxString() *is* a pointer to its data
2048 qsort(m_pItems
, m_nCount
, sizeof(wxChar
*), wxStringCompareFunction
);