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 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
68 // must define this in .cpp for VA or else you get multiply defined symbols everywhere
70 // maximum possible length for a string means "take all string" everywhere
71 // (as sizeof(StringData) is unknown here, we substract 100)
72 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
76 // ---------------------------------------------------------------------------
77 // static class variables definition
78 // ---------------------------------------------------------------------------
80 #ifdef wxSTD_STRING_COMPATIBILITY
81 const size_t wxString::npos
= wxSTRING_MAXLEN
;
82 #endif // wxSTD_STRING_COMPATIBILITY
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // for an empty string, GetStringData() will return this address: this
89 // structure has the same layout as wxStringData and it's data() method will
90 // return the empty string (dummy pointer)
95 } g_strEmpty
= { {-1, 0, 0}, wxT('\0') };
97 // empty C style string: points to 'string data' byte of g_strEmpty
98 extern const wxChar WXDLLEXPORT
*wxEmptyString
= &g_strEmpty
.dummy
;
100 // ----------------------------------------------------------------------------
101 // conditional compilation
102 // ----------------------------------------------------------------------------
104 #if !defined(__WXSW__) && wxUSE_UNICODE
105 #ifdef wxUSE_EXPERIMENTAL_PRINTF
106 #undef wxUSE_EXPERIMENTAL_PRINTF
108 #define wxUSE_EXPERIMENTAL_PRINTF 1
111 // we want to find out if the current platform supports vsnprintf()-like
112 // function: for Unix this is done with configure, for Windows we test the
113 // compiler explicitly.
115 // FIXME currently, this is only for ANSI (!Unicode) strings, so we call this
116 // function wxVsnprintfA (A for ANSI), should also find one for Unicode
117 // strings in Unicode build
119 #if (defined(__VISUALC__) || defined(wxUSE_NORLANDER_HEADERS)) && !defined(__MINGW32__)
120 #define wxVsnprintfA _vsnprintf
123 #ifdef HAVE_VSNPRINTF
124 #define wxVsnprintfA vsnprintf
126 #endif // Windows/!Windows
129 // in this case we'll use vsprintf() (which is ANSI and thus should be
130 // always available), but it's unsafe because it doesn't check for buffer
131 // size - so give a warning
132 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
134 #if defined(__VISUALC__)
135 #pragma message("Using sprintf() because no snprintf()-like function defined")
136 #elif defined(__GNUG__) && !defined(__UNIX__)
137 #warning "Using sprintf() because no snprintf()-like function defined"
138 #elif defined(__MWERKS__)
139 #warning "Using sprintf() because no snprintf()-like function defined"
141 #endif // no vsnprintf
144 // AIX has vsnprintf, but there's no prototype in the system headers.
145 extern "C" int vsnprintf(char* str
, size_t n
, const char* format
, va_list ap
);
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
154 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
157 // ATTN: you can _not_ use both of these in the same program!
159 istream
& operator>>(istream
& is
, wxString
& WXUNUSED(str
))
164 streambuf
*sb
= is
.rdbuf();
167 int ch
= sb
->sbumpc ();
169 is
.setstate(ios::eofbit
);
172 else if ( isspace(ch
) ) {
184 if ( str
.length() == 0 )
185 is
.setstate(ios::failbit
);
190 ostream
& operator<<(ostream
& os
, const wxString
& str
)
196 #endif //std::string compatibility
198 extern int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
199 const wxChar
*format
, va_list argptr
)
202 // FIXME should use wvsnprintf() or whatever if it's available
204 int iLen
= s
.PrintfV(format
, argptr
);
207 wxStrncpy(buf
, s
.c_str(), iLen
);
212 // vsnprintf() will not terminate the string with '\0' if there is not
213 // enough place, but we want the string to always be NUL terminated
214 int rc
= wxVsnprintfA(buf
, len
- 1, format
, argptr
);
221 #endif // Unicode/ANSI
224 extern int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
225 const wxChar
*format
, ...)
228 va_start(argptr
, format
);
230 int iLen
= wxVsnprintf(buf
, len
, format
, argptr
);
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 // this small class is used to gather statistics for performance tuning
242 //#define WXSTRING_STATISTICS
243 #ifdef WXSTRING_STATISTICS
247 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
249 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
251 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
254 size_t m_nCount
, m_nTotal
;
256 } g_averageLength("allocation size"),
257 g_averageSummandLength("summand length"),
258 g_averageConcatHit("hit probability in concat"),
259 g_averageInitialLength("initial string length");
261 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
263 #define STATISTICS_ADD(av, val)
264 #endif // WXSTRING_STATISTICS
266 // ===========================================================================
267 // wxString class core
268 // ===========================================================================
270 // ---------------------------------------------------------------------------
272 // ---------------------------------------------------------------------------
274 // constructs string of <nLength> copies of character <ch>
275 wxString::wxString(wxChar ch
, size_t nLength
)
280 AllocBuffer(nLength
);
283 // memset only works on char
284 for (size_t n
=0; n
<nLength
; n
++) m_pchData
[n
] = ch
;
286 memset(m_pchData
, ch
, nLength
);
291 // takes nLength elements of psz starting at nPos
292 void wxString::InitWith(const wxChar
*psz
, size_t nPos
, size_t nLength
)
296 wxASSERT( nPos
<= wxStrlen(psz
) );
298 if ( nLength
== wxSTRING_MAXLEN
)
299 nLength
= wxStrlen(psz
+ nPos
);
301 STATISTICS_ADD(InitialLength
, nLength
);
304 // trailing '\0' is written in AllocBuffer()
305 AllocBuffer(nLength
);
306 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(wxChar
));
310 #ifdef wxSTD_STRING_COMPATIBILITY
312 // poor man's iterators are "void *" pointers
313 wxString::wxString(const void *pStart
, const void *pEnd
)
315 InitWith((const wxChar
*)pStart
, 0,
316 (const wxChar
*)pEnd
- (const wxChar
*)pStart
);
319 #endif //std::string compatibility
323 // from multibyte string
324 wxString::wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
)
326 // first get necessary size
327 size_t nLen
= psz
? conv
.MB2WC((wchar_t *) NULL
, psz
, 0) : 0;
329 // nLength is number of *Unicode* characters here!
330 if ((nLen
!= (size_t)-1) && (nLen
> nLength
))
334 if ( (nLen
!= 0) && (nLen
!= (size_t)-1) ) {
336 conv
.MB2WC(m_pchData
, psz
, nLen
);
347 wxString::wxString(const wchar_t *pwz
)
349 // first get necessary size
350 size_t nLen
= pwz
? wxWC2MB((char *) NULL
, pwz
, 0) : 0;
353 if ( (nLen
!= 0) && (nLen
!= (size_t)-1) ) {
355 wxWC2MB(m_pchData
, pwz
, nLen
);
361 #endif // wxUSE_WCHAR_T
363 #endif // Unicode/ANSI
365 // ---------------------------------------------------------------------------
367 // ---------------------------------------------------------------------------
369 // allocates memory needed to store a C string of length nLen
370 void wxString::AllocBuffer(size_t nLen
)
372 // allocating 0 sized buffer doesn't make sense, all empty strings should
374 wxASSERT( nLen
> 0 );
376 // make sure that we don't overflow
377 wxASSERT( nLen
< (INT_MAX
/ sizeof(wxChar
)) -
378 (sizeof(wxStringData
) + EXTRA_ALLOC
+ 1) );
380 STATISTICS_ADD(Length
, nLen
);
383 // 1) one extra character for '\0' termination
384 // 2) sizeof(wxStringData) for housekeeping info
385 wxStringData
* pData
= (wxStringData
*)
386 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(wxChar
));
388 pData
->nDataLength
= nLen
;
389 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
390 m_pchData
= pData
->data(); // data starts after wxStringData
391 m_pchData
[nLen
] = wxT('\0');
394 // must be called before changing this string
395 void wxString::CopyBeforeWrite()
397 wxStringData
* pData
= GetStringData();
399 if ( pData
->IsShared() ) {
400 pData
->Unlock(); // memory not freed because shared
401 size_t nLen
= pData
->nDataLength
;
403 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(wxChar
));
406 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
409 // must be called before replacing contents of this string
410 void wxString::AllocBeforeWrite(size_t nLen
)
412 wxASSERT( nLen
!= 0 ); // doesn't make any sense
414 // must not share string and must have enough space
415 wxStringData
* pData
= GetStringData();
416 if ( pData
->IsShared() || pData
->IsEmpty() ) {
417 // can't work with old buffer, get new one
422 if ( nLen
> pData
->nAllocLength
) {
423 // realloc the buffer instead of calling malloc() again, this is more
425 STATISTICS_ADD(Length
, nLen
);
429 wxStringData
*pDataOld
= pData
;
430 pData
= (wxStringData
*)
431 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
436 // FIXME we're going to crash...
440 pData
->nAllocLength
= nLen
;
441 m_pchData
= pData
->data();
444 // now we have enough space, just update the string length
445 pData
->nDataLength
= nLen
;
448 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
451 // allocate enough memory for nLen characters
452 void wxString::Alloc(size_t nLen
)
454 wxStringData
*pData
= GetStringData();
455 if ( pData
->nAllocLength
<= nLen
) {
456 if ( pData
->IsEmpty() ) {
459 wxStringData
* pData
= (wxStringData
*)
460 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
462 pData
->nDataLength
= 0;
463 pData
->nAllocLength
= nLen
;
464 m_pchData
= pData
->data(); // data starts after wxStringData
465 m_pchData
[0u] = wxT('\0');
467 else if ( pData
->IsShared() ) {
468 pData
->Unlock(); // memory not freed because shared
469 size_t nOldLen
= pData
->nDataLength
;
471 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(wxChar
));
476 wxStringData
*pDataOld
= pData
;
477 wxStringData
*p
= (wxStringData
*)
478 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(wxChar
));
484 // FIXME what to do on memory error?
488 // it's not important if the pointer changed or not (the check for this
489 // is not faster than assigning to m_pchData in all cases)
490 p
->nAllocLength
= nLen
;
491 m_pchData
= p
->data();
494 //else: we've already got enough
497 // shrink to minimal size (releasing extra memory)
498 void wxString::Shrink()
500 wxStringData
*pData
= GetStringData();
502 // this variable is unused in release build, so avoid the compiler warning
503 // by just not declaring it
507 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(wxChar
));
509 // we rely on a reasonable realloc() implementation here - so far I haven't
510 // seen any which wouldn't behave like this
512 wxASSERT( p
!= NULL
); // can't free memory?
513 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
516 // get the pointer to writable buffer of (at least) nLen bytes
517 wxChar
*wxString::GetWriteBuf(size_t nLen
)
519 AllocBeforeWrite(nLen
);
521 wxASSERT( GetStringData()->nRefs
== 1 );
522 GetStringData()->Validate(FALSE
);
527 // put string back in a reasonable state after GetWriteBuf
528 void wxString::UngetWriteBuf()
530 GetStringData()->nDataLength
= wxStrlen(m_pchData
);
531 GetStringData()->Validate(TRUE
);
534 // ---------------------------------------------------------------------------
536 // ---------------------------------------------------------------------------
538 // all functions are inline in string.h
540 // ---------------------------------------------------------------------------
541 // assignment operators
542 // ---------------------------------------------------------------------------
544 // helper function: does real copy
545 void wxString::AssignCopy(size_t nSrcLen
, const wxChar
*pszSrcData
)
547 if ( nSrcLen
== 0 ) {
551 AllocBeforeWrite(nSrcLen
);
552 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
553 GetStringData()->nDataLength
= nSrcLen
;
554 m_pchData
[nSrcLen
] = wxT('\0');
558 // assigns one string to another
559 wxString
& wxString::operator=(const wxString
& stringSrc
)
561 wxASSERT( stringSrc
.GetStringData()->IsValid() );
563 // don't copy string over itself
564 if ( m_pchData
!= stringSrc
.m_pchData
) {
565 if ( stringSrc
.GetStringData()->IsEmpty() ) {
570 GetStringData()->Unlock();
571 m_pchData
= stringSrc
.m_pchData
;
572 GetStringData()->Lock();
579 // assigns a single character
580 wxString
& wxString::operator=(wxChar ch
)
587 wxString
& wxString::operator=(const wxChar
*psz
)
589 AssignCopy(wxStrlen(psz
), psz
);
595 // same as 'signed char' variant
596 wxString
& wxString::operator=(const unsigned char* psz
)
598 *this = (const char *)psz
;
603 wxString
& wxString::operator=(const wchar_t *pwz
)
613 // ---------------------------------------------------------------------------
614 // string concatenation
615 // ---------------------------------------------------------------------------
617 // add something to this string
618 void wxString::ConcatSelf(int nSrcLen
, const wxChar
*pszSrcData
)
620 STATISTICS_ADD(SummandLength
, nSrcLen
);
622 // concatenating an empty string is a NOP
624 wxStringData
*pData
= GetStringData();
625 size_t nLen
= pData
->nDataLength
;
626 size_t nNewLen
= nLen
+ nSrcLen
;
628 // alloc new buffer if current is too small
629 if ( pData
->IsShared() ) {
630 STATISTICS_ADD(ConcatHit
, 0);
632 // we have to allocate another buffer
633 wxStringData
* pOldData
= GetStringData();
634 AllocBuffer(nNewLen
);
635 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(wxChar
));
638 else if ( nNewLen
> pData
->nAllocLength
) {
639 STATISTICS_ADD(ConcatHit
, 0);
641 // we have to grow the buffer
645 STATISTICS_ADD(ConcatHit
, 1);
647 // the buffer is already big enough
650 // should be enough space
651 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
653 // fast concatenation - all is done in our buffer
654 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(wxChar
));
656 m_pchData
[nNewLen
] = wxT('\0'); // put terminating '\0'
657 GetStringData()->nDataLength
= nNewLen
; // and fix the length
659 //else: the string to append was empty
663 * concatenation functions come in 5 flavours:
665 * char + string and string + char
666 * C str + string and string + C str
669 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
671 wxASSERT( string1
.GetStringData()->IsValid() );
672 wxASSERT( string2
.GetStringData()->IsValid() );
674 wxString s
= string1
;
680 wxString
operator+(const wxString
& string
, wxChar ch
)
682 wxASSERT( string
.GetStringData()->IsValid() );
690 wxString
operator+(wxChar ch
, const wxString
& string
)
692 wxASSERT( string
.GetStringData()->IsValid() );
700 wxString
operator+(const wxString
& string
, const wxChar
*psz
)
702 wxASSERT( string
.GetStringData()->IsValid() );
705 s
.Alloc(wxStrlen(psz
) + string
.Len());
712 wxString
operator+(const wxChar
*psz
, const wxString
& string
)
714 wxASSERT( string
.GetStringData()->IsValid() );
717 s
.Alloc(wxStrlen(psz
) + string
.Len());
724 // ===========================================================================
725 // other common string functions
726 // ===========================================================================
728 // ---------------------------------------------------------------------------
729 // simple sub-string extraction
730 // ---------------------------------------------------------------------------
732 // helper function: clone the data attached to this string
733 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
735 if ( nCopyLen
== 0 ) {
739 dest
.AllocBuffer(nCopyLen
);
740 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(wxChar
));
744 // extract string of length nCount starting at nFirst
745 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
747 wxStringData
*pData
= GetStringData();
748 size_t nLen
= pData
->nDataLength
;
750 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
751 if ( nCount
== wxSTRING_MAXLEN
)
753 nCount
= nLen
- nFirst
;
756 // out-of-bounds requests return sensible things
757 if ( nFirst
+ nCount
> nLen
)
759 nCount
= nLen
- nFirst
;
764 // AllocCopy() will return empty string
769 AllocCopy(dest
, nCount
, nFirst
);
774 // extract nCount last (rightmost) characters
775 wxString
wxString::Right(size_t nCount
) const
777 if ( nCount
> (size_t)GetStringData()->nDataLength
)
778 nCount
= GetStringData()->nDataLength
;
781 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
785 // get all characters after the last occurence of ch
786 // (returns the whole string if ch not found)
787 wxString
wxString::AfterLast(wxChar ch
) const
790 int iPos
= Find(ch
, TRUE
);
791 if ( iPos
== wxNOT_FOUND
)
794 str
= c_str() + iPos
+ 1;
799 // extract nCount first (leftmost) characters
800 wxString
wxString::Left(size_t nCount
) const
802 if ( nCount
> (size_t)GetStringData()->nDataLength
)
803 nCount
= GetStringData()->nDataLength
;
806 AllocCopy(dest
, nCount
, 0);
810 // get all characters before the first occurence of ch
811 // (returns the whole string if ch not found)
812 wxString
wxString::BeforeFirst(wxChar ch
) const
815 for ( const wxChar
*pc
= m_pchData
; *pc
!= wxT('\0') && *pc
!= ch
; pc
++ )
821 /// get all characters before the last occurence of ch
822 /// (returns empty string if ch not found)
823 wxString
wxString::BeforeLast(wxChar ch
) const
826 int iPos
= Find(ch
, TRUE
);
827 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
828 str
= wxString(c_str(), iPos
);
833 /// get all characters after the first occurence of ch
834 /// (returns empty string if ch not found)
835 wxString
wxString::AfterFirst(wxChar ch
) const
839 if ( iPos
!= wxNOT_FOUND
)
840 str
= c_str() + iPos
+ 1;
845 // replace first (or all) occurences of some substring with another one
846 size_t wxString::Replace(const wxChar
*szOld
, const wxChar
*szNew
, bool bReplaceAll
)
848 size_t uiCount
= 0; // count of replacements made
850 size_t uiOldLen
= wxStrlen(szOld
);
853 const wxChar
*pCurrent
= m_pchData
;
854 const wxChar
*pSubstr
;
855 while ( *pCurrent
!= wxT('\0') ) {
856 pSubstr
= wxStrstr(pCurrent
, szOld
);
857 if ( pSubstr
== NULL
) {
858 // strTemp is unused if no replacements were made, so avoid the copy
862 strTemp
+= pCurrent
; // copy the rest
863 break; // exit the loop
866 // take chars before match
867 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
869 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
874 if ( !bReplaceAll
) {
875 strTemp
+= pCurrent
; // copy the rest
876 break; // exit the loop
881 // only done if there were replacements, otherwise would have returned above
887 bool wxString::IsAscii() const
889 const wxChar
*s
= (const wxChar
*) *this;
891 if(!isascii(*s
)) return(FALSE
);
897 bool wxString::IsWord() const
899 const wxChar
*s
= (const wxChar
*) *this;
901 if(!wxIsalpha(*s
)) return(FALSE
);
907 bool wxString::IsNumber() const
909 const wxChar
*s
= (const wxChar
*) *this;
911 if(!wxIsdigit(*s
)) return(FALSE
);
917 wxString
wxString::Strip(stripType w
) const
920 if ( w
& leading
) s
.Trim(FALSE
);
921 if ( w
& trailing
) s
.Trim(TRUE
);
925 // ---------------------------------------------------------------------------
927 // ---------------------------------------------------------------------------
929 wxString
& wxString::MakeUpper()
933 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
934 *p
= (wxChar
)wxToupper(*p
);
939 wxString
& wxString::MakeLower()
943 for ( wxChar
*p
= m_pchData
; *p
; p
++ )
944 *p
= (wxChar
)wxTolower(*p
);
949 // ---------------------------------------------------------------------------
950 // trimming and padding
951 // ---------------------------------------------------------------------------
953 // trims spaces (in the sense of isspace) from left or right side
954 wxString
& wxString::Trim(bool bFromRight
)
956 // first check if we're going to modify the string at all
959 (bFromRight
&& wxIsspace(GetChar(Len() - 1))) ||
960 (!bFromRight
&& wxIsspace(GetChar(0u)))
964 // ok, there is at least one space to trim
969 // find last non-space character
970 wxChar
*psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
971 while ( wxIsspace(*psz
) && (psz
>= m_pchData
) )
974 // truncate at trailing space start
976 GetStringData()->nDataLength
= psz
- m_pchData
;
980 // find first non-space character
981 const wxChar
*psz
= m_pchData
;
982 while ( wxIsspace(*psz
) )
985 // fix up data and length
986 int nDataLength
= GetStringData()->nDataLength
- (psz
- (const wxChar
*) m_pchData
);
987 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(wxChar
));
988 GetStringData()->nDataLength
= nDataLength
;
995 // adds nCount characters chPad to the string from either side
996 wxString
& wxString::Pad(size_t nCount
, wxChar chPad
, bool bFromRight
)
998 wxString
s(chPad
, nCount
);
1011 // truncate the string
1012 wxString
& wxString::Truncate(size_t uiLen
)
1014 if ( uiLen
< Len() ) {
1017 *(m_pchData
+ uiLen
) = wxT('\0');
1018 GetStringData()->nDataLength
= uiLen
;
1020 //else: nothing to do, string is already short enough
1025 // ---------------------------------------------------------------------------
1026 // finding (return wxNOT_FOUND if not found and index otherwise)
1027 // ---------------------------------------------------------------------------
1030 int wxString::Find(wxChar ch
, bool bFromEnd
) const
1032 const wxChar
*psz
= bFromEnd
? wxStrrchr(m_pchData
, ch
) : wxStrchr(m_pchData
, ch
);
1034 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
1037 // find a sub-string (like strstr)
1038 int wxString::Find(const wxChar
*pszSub
) const
1040 const wxChar
*psz
= wxStrstr(m_pchData
, pszSub
);
1042 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const wxChar
*) m_pchData
;
1045 // ----------------------------------------------------------------------------
1046 // conversion to numbers
1047 // ----------------------------------------------------------------------------
1049 bool wxString::ToLong(long *val
) const
1051 wxCHECK_MSG( val
, FALSE
, _T("NULL pointer in wxString::ToLong") );
1053 const wxChar
*start
= c_str();
1055 *val
= wxStrtol(start
, &end
, 10);
1057 // return TRUE only if scan was stopped by the terminating NUL and if the
1058 // string was not empty to start with
1059 return !*end
&& (end
!= start
);
1062 bool wxString::ToULong(unsigned long *val
) const
1064 wxCHECK_MSG( val
, FALSE
, _T("NULL pointer in wxString::ToULong") );
1066 const wxChar
*start
= c_str();
1068 *val
= wxStrtoul(start
, &end
, 10);
1070 // return TRUE only if scan was stopped by the terminating NUL and if the
1071 // string was not empty to start with
1072 return !*end
&& (end
!= start
);
1075 bool wxString::ToDouble(double *val
) const
1077 wxCHECK_MSG( val
, FALSE
, _T("NULL pointer in wxString::ToDouble") );
1079 const wxChar
*start
= c_str();
1081 *val
= wxStrtod(start
, &end
);
1083 // return TRUE only if scan was stopped by the terminating NUL and if the
1084 // string was not empty to start with
1085 return !*end
&& (end
!= start
);
1088 // ---------------------------------------------------------------------------
1090 // ---------------------------------------------------------------------------
1093 wxString
wxString::Format(const wxChar
*pszFormat
, ...)
1096 va_start(argptr
, pszFormat
);
1099 s
.PrintfV(pszFormat
, argptr
);
1107 wxString
wxString::FormatV(const wxChar
*pszFormat
, va_list argptr
)
1110 s
.Printf(pszFormat
, argptr
);
1114 int wxString::Printf(const wxChar
*pszFormat
, ...)
1117 va_start(argptr
, pszFormat
);
1119 int iLen
= PrintfV(pszFormat
, argptr
);
1126 int wxString::PrintfV(const wxChar
* pszFormat
, va_list argptr
)
1128 #if wxUSE_EXPERIMENTAL_PRINTF
1129 // the new implementation
1131 // buffer to avoid dynamic memory allocation each time for small strings
1132 char szScratch
[1024];
1135 for (size_t n
= 0; pszFormat
[n
]; n
++)
1136 if (pszFormat
[n
] == wxT('%')) {
1137 static char s_szFlags
[256] = "%";
1139 bool adj_left
= FALSE
, in_prec
= FALSE
,
1140 prec_dot
= FALSE
, done
= FALSE
;
1142 size_t min_width
= 0, max_width
= wxSTRING_MAXLEN
;
1144 #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1145 switch (pszFormat
[++n
]) {
1159 s_szFlags
[flagofs
++] = pszFormat
[n
];
1164 s_szFlags
[flagofs
++] = pszFormat
[n
];
1171 // dot will be auto-added to s_szFlags if non-negative number follows
1176 s_szFlags
[flagofs
++] = pszFormat
[n
];
1181 s_szFlags
[flagofs
++] = pszFormat
[n
];
1187 s_szFlags
[flagofs
++] = pszFormat
[n
];
1192 s_szFlags
[flagofs
++] = pszFormat
[n
];
1196 int len
= va_arg(argptr
, int);
1203 adj_left
= !adj_left
;
1204 s_szFlags
[flagofs
++] = '-';
1209 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
1212 case wxT('1'): case wxT('2'): case wxT('3'):
1213 case wxT('4'): case wxT('5'): case wxT('6'):
1214 case wxT('7'): case wxT('8'): case wxT('9'):
1218 while ((pszFormat
[n
]>=wxT('0')) && (pszFormat
[n
]<=wxT('9'))) {
1219 s_szFlags
[flagofs
++] = pszFormat
[n
];
1220 len
= len
*10 + (pszFormat
[n
] - wxT('0'));
1223 if (in_prec
) max_width
= len
;
1224 else min_width
= len
;
1225 n
--; // the main loop pre-increments n again
1235 s_szFlags
[flagofs
++] = pszFormat
[n
];
1236 s_szFlags
[flagofs
] = '\0';
1238 int val
= va_arg(argptr
, int);
1239 ::sprintf(szScratch
, s_szFlags
, val
);
1241 else if (ilen
== -1) {
1242 short int val
= va_arg(argptr
, short int);
1243 ::sprintf(szScratch
, s_szFlags
, val
);
1245 else if (ilen
== 1) {
1246 long int val
= va_arg(argptr
, long int);
1247 ::sprintf(szScratch
, s_szFlags
, val
);
1249 else if (ilen
== 2) {
1250 #if SIZEOF_LONG_LONG
1251 long long int val
= va_arg(argptr
, long long int);
1252 ::sprintf(szScratch
, s_szFlags
, val
);
1254 long int val
= va_arg(argptr
, long int);
1255 ::sprintf(szScratch
, s_szFlags
, val
);
1258 else if (ilen
== 3) {
1259 size_t val
= va_arg(argptr
, size_t);
1260 ::sprintf(szScratch
, s_szFlags
, val
);
1262 *this += wxString(szScratch
);
1271 s_szFlags
[flagofs
++] = pszFormat
[n
];
1272 s_szFlags
[flagofs
] = '\0';
1274 long double val
= va_arg(argptr
, long double);
1275 ::sprintf(szScratch
, s_szFlags
, val
);
1277 double val
= va_arg(argptr
, double);
1278 ::sprintf(szScratch
, s_szFlags
, val
);
1280 *this += wxString(szScratch
);
1285 void *val
= va_arg(argptr
, void *);
1287 s_szFlags
[flagofs
++] = pszFormat
[n
];
1288 s_szFlags
[flagofs
] = '\0';
1289 ::sprintf(szScratch
, s_szFlags
, val
);
1290 *this += wxString(szScratch
);
1296 wxChar val
= va_arg(argptr
, int);
1297 // we don't need to honor padding here, do we?
1304 // wx extension: we'll let %hs mean non-Unicode strings
1305 char *val
= va_arg(argptr
, char *);
1307 // ASCII->Unicode constructor handles max_width right
1308 wxString
s(val
, wxConvLibc
, max_width
);
1310 size_t len
= wxSTRING_MAXLEN
;
1312 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1313 } else val
= wxT("(null)");
1314 wxString
s(val
, len
);
1316 if (s
.Len() < min_width
)
1317 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1320 wxChar
*val
= va_arg(argptr
, wxChar
*);
1321 size_t len
= wxSTRING_MAXLEN
;
1323 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
1324 } else val
= wxT("(null)");
1325 wxString
s(val
, len
);
1326 if (s
.Len() < min_width
)
1327 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
1334 int *val
= va_arg(argptr
, int *);
1337 else if (ilen
== -1) {
1338 short int *val
= va_arg(argptr
, short int *);
1341 else if (ilen
>= 1) {
1342 long int *val
= va_arg(argptr
, long int *);
1348 if (wxIsalpha(pszFormat
[n
]))
1349 // probably some flag not taken care of here yet
1350 s_szFlags
[flagofs
++] = pszFormat
[n
];
1353 *this += wxT('%'); // just to pass the glibc tst-printf.c
1361 } else *this += pszFormat
[n
];
1364 // buffer to avoid dynamic memory allocation each time for small strings
1365 char szScratch
[1024];
1367 // NB: wxVsnprintf() may return either less than the buffer size or -1 if
1368 // there is not enough place depending on implementation
1369 int iLen
= wxVsnprintfA(szScratch
, WXSIZEOF(szScratch
), pszFormat
, argptr
);
1371 // the whole string is in szScratch
1375 bool outOfMemory
= FALSE
;
1376 int size
= 2*WXSIZEOF(szScratch
);
1377 while ( !outOfMemory
) {
1378 char *buf
= GetWriteBuf(size
);
1380 iLen
= wxVsnprintfA(buf
, size
, pszFormat
, argptr
);
1387 // ok, there was enough space
1391 // still not enough, double it again
1395 if ( outOfMemory
) {
1400 #endif // wxUSE_EXPERIMENTAL_PRINTF/!wxUSE_EXPERIMENTAL_PRINTF
1405 // ----------------------------------------------------------------------------
1406 // misc other operations
1407 // ----------------------------------------------------------------------------
1409 // returns TRUE if the string matches the pattern which may contain '*' and
1410 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1412 bool wxString::Matches(const wxChar
*pszMask
) const
1414 // check char by char
1415 const wxChar
*pszTxt
;
1416 for ( pszTxt
= c_str(); *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
1417 switch ( *pszMask
) {
1419 if ( *pszTxt
== wxT('\0') )
1422 // pszText and pszMask will be incremented in the loop statement
1428 // ignore special chars immediately following this one
1429 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
1432 // if there is nothing more, match
1433 if ( *pszMask
== wxT('\0') )
1436 // are there any other metacharacters in the mask?
1438 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
1440 if ( pEndMask
!= NULL
) {
1441 // we have to match the string between two metachars
1442 uiLenMask
= pEndMask
- pszMask
;
1445 // we have to match the remainder of the string
1446 uiLenMask
= wxStrlen(pszMask
);
1449 wxString
strToMatch(pszMask
, uiLenMask
);
1450 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
1451 if ( pMatch
== NULL
)
1454 // -1 to compensate "++" in the loop
1455 pszTxt
= pMatch
+ uiLenMask
- 1;
1456 pszMask
+= uiLenMask
- 1;
1461 if ( *pszMask
!= *pszTxt
)
1467 // match only if nothing left
1468 return *pszTxt
== wxT('\0');
1471 // Count the number of chars
1472 int wxString::Freq(wxChar ch
) const
1476 for (int i
= 0; i
< len
; i
++)
1478 if (GetChar(i
) == ch
)
1484 // convert to upper case, return the copy of the string
1485 wxString
wxString::Upper() const
1486 { wxString
s(*this); return s
.MakeUpper(); }
1488 // convert to lower case, return the copy of the string
1489 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }
1491 int wxString::sprintf(const wxChar
*pszFormat
, ...)
1494 va_start(argptr
, pszFormat
);
1495 int iLen
= PrintfV(pszFormat
, argptr
);
1500 // ---------------------------------------------------------------------------
1501 // standard C++ library string functions
1502 // ---------------------------------------------------------------------------
1503 #ifdef wxSTD_STRING_COMPATIBILITY
1505 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1507 wxASSERT( str
.GetStringData()->IsValid() );
1508 wxASSERT( nPos
<= Len() );
1510 if ( !str
.IsEmpty() ) {
1512 wxChar
*pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1513 wxStrncpy(pc
, c_str(), nPos
);
1514 wxStrcpy(pc
+ nPos
, str
);
1515 wxStrcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1516 strTmp
.UngetWriteBuf();
1523 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1525 wxASSERT( str
.GetStringData()->IsValid() );
1526 wxASSERT( nStart
<= Len() );
1528 const wxChar
*p
= wxStrstr(c_str() + nStart
, str
);
1530 return p
== NULL
? npos
: p
- c_str();
1533 // VC++ 1.5 can't cope with the default argument in the header.
1534 #if !defined(__VISUALC__) || defined(__WIN32__)
1535 size_t wxString::find(const wxChar
* sz
, size_t nStart
, size_t n
) const
1537 return find(wxString(sz
, n
), nStart
);
1541 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1542 #if !defined(__BORLANDC__)
1543 size_t wxString::find(wxChar ch
, size_t nStart
) const
1545 wxASSERT( nStart
<= Len() );
1547 const wxChar
*p
= wxStrchr(c_str() + nStart
, ch
);
1549 return p
== NULL
? npos
: p
- c_str();
1553 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1555 wxASSERT( str
.GetStringData()->IsValid() );
1556 wxASSERT( nStart
<= Len() );
1558 // TODO could be made much quicker than that
1559 const wxChar
*p
= c_str() + (nStart
== npos
? Len() : nStart
);
1560 while ( p
>= c_str() + str
.Len() ) {
1561 if ( wxStrncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1562 return p
- str
.Len() - c_str();
1569 // VC++ 1.5 can't cope with the default argument in the header.
1570 #if !defined(__VISUALC__) || defined(__WIN32__)
1571 size_t wxString::rfind(const wxChar
* sz
, size_t nStart
, size_t n
) const
1573 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1576 size_t wxString::rfind(wxChar ch
, size_t nStart
) const
1578 if ( nStart
== npos
)
1584 wxASSERT( nStart
<= Len() );
1587 const wxChar
*p
= wxStrrchr(c_str(), ch
);
1592 size_t result
= p
- c_str();
1593 return ( result
> nStart
) ? npos
: result
;
1597 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
1599 const wxChar
*start
= c_str() + nStart
;
1600 const wxChar
*firstOf
= wxStrpbrk(start
, sz
);
1602 return firstOf
- start
;
1607 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
1609 if ( nStart
== npos
)
1615 wxASSERT( nStart
<= Len() );
1618 for ( const wxChar
*p
= c_str() + length() - 1; p
>= c_str(); p
-- )
1620 if ( wxStrchr(sz
, *p
) )
1627 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
1629 if ( nStart
== npos
)
1635 wxASSERT( nStart
<= Len() );
1638 size_t nAccept
= wxStrspn(c_str() + nStart
, sz
);
1639 if ( nAccept
>= length() - nStart
)
1645 size_t wxString::find_first_not_of(wxChar ch
, size_t nStart
) const
1647 wxASSERT( nStart
<= Len() );
1649 for ( const wxChar
*p
= c_str() + nStart
; *p
; p
++ )
1658 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
1660 if ( nStart
== npos
)
1666 wxASSERT( nStart
<= Len() );
1669 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1671 if ( !wxStrchr(sz
, *p
) )
1678 size_t wxString::find_last_not_of(wxChar ch
, size_t nStart
) const
1680 if ( nStart
== npos
)
1686 wxASSERT( nStart
<= Len() );
1689 for ( const wxChar
*p
= c_str() + nStart
- 1; p
>= c_str(); p
-- )
1698 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1700 wxString
strTmp(c_str(), nStart
);
1701 if ( nLen
!= npos
) {
1702 wxASSERT( nStart
+ nLen
<= Len() );
1704 strTmp
.append(c_str() + nStart
+ nLen
);
1711 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const wxChar
*sz
)
1713 wxASSERT( nStart
+ nLen
<= wxStrlen(sz
) );
1717 strTmp
.append(c_str(), nStart
);
1719 strTmp
.append(c_str() + nStart
+ nLen
);
1725 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1727 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1730 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1731 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1733 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1736 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1737 const wxChar
* sz
, size_t nCount
)
1739 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1742 #endif //std::string compatibility
1744 // ============================================================================
1746 // ============================================================================
1748 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1749 #define ARRAY_MAXSIZE_INCREMENT 4096
1750 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1751 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1754 #define STRING(p) ((wxString *)(&(p)))
1757 wxArrayString::wxArrayString(bool autoSort
)
1761 m_pItems
= (wxChar
**) NULL
;
1762 m_autoSort
= autoSort
;
1766 wxArrayString::wxArrayString(const wxArrayString
& src
)
1770 m_pItems
= (wxChar
**) NULL
;
1771 m_autoSort
= src
.m_autoSort
;
1776 // assignment operator
1777 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1787 void wxArrayString::Copy(const wxArrayString
& src
)
1789 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1790 Alloc(src
.m_nCount
);
1792 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1797 void wxArrayString::Grow()
1799 // only do it if no more place
1800 if( m_nCount
== m_nSize
) {
1801 if( m_nSize
== 0 ) {
1802 // was empty, alloc some memory
1803 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1804 m_pItems
= new wxChar
*[m_nSize
];
1807 // otherwise when it's called for the first time, nIncrement would be 0
1808 // and the array would never be expanded
1809 #if defined(__VISAGECPP__) && defined(__WXDEBUG__)
1810 int array_size
= ARRAY_DEFAULT_INITIAL_SIZE
;
1811 wxASSERT( array_size
!= 0 );
1813 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1816 // add 50% but not too much
1817 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1818 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1819 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1820 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1821 m_nSize
+= nIncrement
;
1822 wxChar
**pNew
= new wxChar
*[m_nSize
];
1824 // copy data to new location
1825 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1827 // delete old memory (but do not release the strings!)
1828 wxDELETEA(m_pItems
);
1835 void wxArrayString::Free()
1837 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1838 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1842 // deletes all the strings from the list
1843 void wxArrayString::Empty()
1850 // as Empty, but also frees memory
1851 void wxArrayString::Clear()
1858 wxDELETEA(m_pItems
);
1862 wxArrayString::~wxArrayString()
1866 wxDELETEA(m_pItems
);
1869 // pre-allocates memory (frees the previous data!)
1870 void wxArrayString::Alloc(size_t nSize
)
1872 wxASSERT( nSize
> 0 );
1874 // only if old buffer was not big enough
1875 if ( nSize
> m_nSize
) {
1877 wxDELETEA(m_pItems
);
1878 m_pItems
= new wxChar
*[nSize
];
1885 // minimizes the memory usage by freeing unused memory
1886 void wxArrayString::Shrink()
1888 // only do it if we have some memory to free
1889 if( m_nCount
< m_nSize
) {
1890 // allocates exactly as much memory as we need
1891 wxChar
**pNew
= new wxChar
*[m_nCount
];
1893 // copy data to new location
1894 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(wxChar
*));
1900 // searches the array for an item (forward or backwards)
1901 int wxArrayString::Index(const wxChar
*sz
, bool bCase
, bool bFromEnd
) const
1904 // use binary search in the sorted array
1905 wxASSERT_MSG( bCase
&& !bFromEnd
,
1906 wxT("search parameters ignored for auto sorted array") );
1915 res
= wxStrcmp(sz
, m_pItems
[i
]);
1927 // use linear search in unsorted array
1929 if ( m_nCount
> 0 ) {
1930 size_t ui
= m_nCount
;
1932 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1939 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1940 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1949 // add item at the end
1950 size_t wxArrayString::Add(const wxString
& str
)
1953 // insert the string at the correct position to keep the array sorted
1961 res
= wxStrcmp(str
, m_pItems
[i
]);
1972 wxASSERT_MSG( lo
== hi
, wxT("binary search broken") );
1979 wxASSERT( str
.GetStringData()->IsValid() );
1983 // the string data must not be deleted!
1984 str
.GetStringData()->Lock();
1987 m_pItems
[m_nCount
] = (wxChar
*)str
.c_str(); // const_cast
1993 // add item at the given position
1994 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1996 wxASSERT( str
.GetStringData()->IsValid() );
1998 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Insert") );
2002 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
2003 (m_nCount
- nIndex
)*sizeof(wxChar
*));
2005 str
.GetStringData()->Lock();
2006 m_pItems
[nIndex
] = (wxChar
*)str
.c_str();
2011 // removes item from array (by index)
2012 void wxArrayString::Remove(size_t nIndex
)
2014 wxCHECK_RET( nIndex
<= m_nCount
, wxT("bad index in wxArrayString::Remove") );
2017 Item(nIndex
).GetStringData()->Unlock();
2019 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
2020 (m_nCount
- nIndex
- 1)*sizeof(wxChar
*));
2024 // removes item from array (by value)
2025 void wxArrayString::Remove(const wxChar
*sz
)
2027 int iIndex
= Index(sz
);
2029 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
2030 wxT("removing inexistent element in wxArrayString::Remove") );
2035 // ----------------------------------------------------------------------------
2037 // ----------------------------------------------------------------------------
2039 // we can only sort one array at a time with the quick-sort based
2042 // need a critical section to protect access to gs_compareFunction and
2043 // gs_sortAscending variables
2044 static wxCriticalSection
*gs_critsectStringSort
= NULL
;
2046 // call this before the value of the global sort vars is changed/after
2047 // you're finished with them
2048 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
2049 gs_critsectStringSort = new wxCriticalSection; \
2050 gs_critsectStringSort->Enter()
2051 #define END_SORT() gs_critsectStringSort->Leave(); \
2052 delete gs_critsectStringSort; \
2053 gs_critsectStringSort = NULL
2055 #define START_SORT()
2057 #endif // wxUSE_THREADS
2059 // function to use for string comparaison
2060 static wxArrayString::CompareFunction gs_compareFunction
= NULL
;
2062 // if we don't use the compare function, this flag tells us if we sort the
2063 // array in ascending or descending order
2064 static bool gs_sortAscending
= TRUE
;
2066 // function which is called by quick sort
2067 static int LINKAGEMODE
wxStringCompareFunction(const void *first
, const void *second
)
2069 wxString
*strFirst
= (wxString
*)first
;
2070 wxString
*strSecond
= (wxString
*)second
;
2072 if ( gs_compareFunction
) {
2073 return gs_compareFunction(*strFirst
, *strSecond
);
2076 // maybe we should use wxStrcoll
2077 int result
= wxStrcmp(strFirst
->c_str(), strSecond
->c_str());
2079 return gs_sortAscending
? result
: -result
;
2083 // sort array elements using passed comparaison function
2084 void wxArrayString::Sort(CompareFunction compareFunction
)
2088 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2089 gs_compareFunction
= compareFunction
;
2096 void wxArrayString::Sort(bool reverseOrder
)
2100 wxASSERT( !gs_compareFunction
); // must have been reset to NULL
2101 gs_sortAscending
= !reverseOrder
;
2108 void wxArrayString::DoSort()
2110 wxCHECK_RET( !m_autoSort
, wxT("can't use this method with sorted arrays") );
2112 // just sort the pointers using qsort() - of course it only works because
2113 // wxString() *is* a pointer to its data
2114 qsort(m_pItems
, m_nCount
, sizeof(wxChar
*), wxStringCompareFunction
);