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"
49 #include <wchar.h> // for wcsrtombs(), see comments where it's used
52 #ifdef WXSTRING_IS_WXOBJECT
53 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
54 #endif //WXSTRING_IS_WXOBJECT
56 // allocating extra space for each string consumes more memory but speeds up
57 // the concatenation operations (nLen is the current string's length)
58 // NB: EXTRA_ALLOC must be >= 0!
59 #define EXTRA_ALLOC (19 - nLen % 16)
61 // ---------------------------------------------------------------------------
62 // static class variables definition
63 // ---------------------------------------------------------------------------
65 #ifdef wxSTD_STRING_COMPATIBILITY
66 const size_t wxString::npos
= STRING_MAXLEN
;
67 #endif // wxSTD_STRING_COMPATIBILITY
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // for an empty string, GetStringData() will return this address: this
74 // structure has the same layout as wxStringData and it's data() method will
75 // return the empty string (dummy pointer)
80 } g_strEmpty
= { {-1, 0, 0}, '\0' };
82 // empty C style string: points to 'string data' byte of g_strEmpty
83 extern const char WXDLLEXPORT
*g_szNul
= &g_strEmpty
.dummy
;
85 // ----------------------------------------------------------------------------
86 // conditional compilation
87 // ----------------------------------------------------------------------------
89 // we want to find out if the current platform supports vsnprintf()-like
90 // function: for Unix this is done with configure, for Windows we test the
91 // compiler explicitly.
94 #define wxVsprintf _vsnprintf
98 #define wxVsprintf vsnprintf
100 #endif // Windows/!Windows
103 // in this case we'll use vsprintf() (which is ANSI and thus should be
104 // always available), but it's unsafe because it doesn't check for buffer
105 // size - so give a warning
106 #define wxVsprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
108 #pragma message("Using sprintf() because no snprintf()-like function defined")
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 #ifdef wxSTD_STRING_COMPATIBILITY
118 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
121 // ATTN: you can _not_ use both of these in the same program!
123 #include <iostream.h>
130 // for msvc (bcc50+ also) you don't need these NAMESPACE defines,
131 // using namespace std; takes care of that.
132 #define NAMESPACE std::
137 #define wxVsprintf _vsnprintf
140 #if defined ( HAVE_VSNPRINTF )
141 #define wxVsprintf vsnprintf
146 // vsprintf() is ANSI so we can always use it, but it's unsafe!
147 #define wxVsprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
148 #pragma message("Using sprintf() because no snprintf()-like function defined")
151 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
156 NAMESPACE streambuf
*sb
= is
.rdbuf();
159 int ch
= sb
->sbumpc ();
161 is
.setstate(NAMESPACE
ios::eofbit
);
164 else if ( isspace(ch
) ) {
176 if ( str
.length() == 0 )
177 is
.setstate(NAMESPACE
ios::failbit
);
182 #endif //std::string compatibility
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 // this small class is used to gather statistics for performance tuning
189 //#define WXSTRING_STATISTICS
190 #ifdef WXSTRING_STATISTICS
194 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
196 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
198 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
201 size_t m_nCount
, m_nTotal
;
203 } g_averageLength("allocation size"),
204 g_averageSummandLength("summand length"),
205 g_averageConcatHit("hit probability in concat"),
206 g_averageInitialLength("initial string length");
208 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
210 #define STATISTICS_ADD(av, val)
211 #endif // WXSTRING_STATISTICS
213 // ===========================================================================
214 // wxString class core
215 // ===========================================================================
217 // ---------------------------------------------------------------------------
219 // ---------------------------------------------------------------------------
221 // constructs string of <nLength> copies of character <ch>
222 wxString::wxString(char ch
, size_t nLength
)
227 AllocBuffer(nLength
);
229 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
231 memset(m_pchData
, ch
, nLength
);
235 // takes nLength elements of psz starting at nPos
236 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
240 wxASSERT( nPos
<= Strlen(psz
) );
242 if ( nLength
== STRING_MAXLEN
)
243 nLength
= Strlen(psz
+ nPos
);
245 STATISTICS_ADD(InitialLength
, nLength
);
248 // trailing '\0' is written in AllocBuffer()
249 AllocBuffer(nLength
);
250 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
254 // the same as previous constructor, but for compilers using unsigned char
255 wxString::wxString(const unsigned char* psz
, size_t nLength
)
257 InitWith((const char *)psz
, 0, nLength
);
260 #ifdef wxSTD_STRING_COMPATIBILITY
262 // poor man's iterators are "void *" pointers
263 wxString::wxString(const void *pStart
, const void *pEnd
)
265 InitWith((const char *)pStart
, 0,
266 (const char *)pEnd
- (const char *)pStart
);
269 #endif //std::string compatibility
272 wxString::wxString(const wchar_t *pwz
)
274 // first get necessary size
276 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
277 // honor the 3rd parameter, thus it will happily crash here).
279 // don't know if it's really needed (or if we can pass NULL), but better safe
282 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
284 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
290 wcstombs(m_pchData
, pwz
, nLen
);
297 // ---------------------------------------------------------------------------
299 // ---------------------------------------------------------------------------
301 // allocates memory needed to store a C string of length nLen
302 void wxString::AllocBuffer(size_t nLen
)
304 wxASSERT( nLen
> 0 ); //
305 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
307 STATISTICS_ADD(Length
, nLen
);
310 // 1) one extra character for '\0' termination
311 // 2) sizeof(wxStringData) for housekeeping info
312 wxStringData
* pData
= (wxStringData
*)
313 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
315 pData
->nDataLength
= nLen
;
316 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
317 m_pchData
= pData
->data(); // data starts after wxStringData
318 m_pchData
[nLen
] = '\0';
321 // must be called before changing this string
322 void wxString::CopyBeforeWrite()
324 wxStringData
* pData
= GetStringData();
326 if ( pData
->IsShared() ) {
327 pData
->Unlock(); // memory not freed because shared
328 size_t nLen
= pData
->nDataLength
;
330 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
333 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
336 // must be called before replacing contents of this string
337 void wxString::AllocBeforeWrite(size_t nLen
)
339 wxASSERT( nLen
!= 0 ); // doesn't make any sense
341 // must not share string and must have enough space
342 wxStringData
* pData
= GetStringData();
343 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
344 // can't work with old buffer, get new one
349 // update the string length
350 pData
->nDataLength
= nLen
;
353 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
356 // allocate enough memory for nLen characters
357 void wxString::Alloc(size_t nLen
)
359 wxStringData
*pData
= GetStringData();
360 if ( pData
->nAllocLength
<= nLen
) {
361 if ( pData
->IsEmpty() ) {
364 wxStringData
* pData
= (wxStringData
*)
365 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
367 pData
->nDataLength
= 0;
368 pData
->nAllocLength
= nLen
;
369 m_pchData
= pData
->data(); // data starts after wxStringData
370 m_pchData
[0u] = '\0';
372 else if ( pData
->IsShared() ) {
373 pData
->Unlock(); // memory not freed because shared
374 size_t nOldLen
= pData
->nDataLength
;
376 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
381 wxStringData
*p
= (wxStringData
*)
382 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
385 // @@@ what to do on memory error?
389 // it's not important if the pointer changed or not (the check for this
390 // is not faster than assigning to m_pchData in all cases)
391 p
->nAllocLength
= nLen
;
392 m_pchData
= p
->data();
395 //else: we've already got enough
398 // shrink to minimal size (releasing extra memory)
399 void wxString::Shrink()
401 wxStringData
*pData
= GetStringData();
403 // this variable is unused in release build, so avoid the compiler warning by
404 // just not declaring it
408 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
410 wxASSERT( p
!= NULL
); // can't free memory?
411 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
414 // get the pointer to writable buffer of (at least) nLen bytes
415 char *wxString::GetWriteBuf(size_t nLen
)
417 AllocBeforeWrite(nLen
);
419 wxASSERT( GetStringData()->nRefs
== 1 );
420 GetStringData()->Validate(FALSE
);
425 // put string back in a reasonable state after GetWriteBuf
426 void wxString::UngetWriteBuf()
428 GetStringData()->nDataLength
= strlen(m_pchData
);
429 GetStringData()->Validate(TRUE
);
432 // ---------------------------------------------------------------------------
434 // ---------------------------------------------------------------------------
436 // all functions are inline in string.h
438 // ---------------------------------------------------------------------------
439 // assignment operators
440 // ---------------------------------------------------------------------------
442 // helper function: does real copy
443 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
445 if ( nSrcLen
== 0 ) {
449 AllocBeforeWrite(nSrcLen
);
450 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
451 GetStringData()->nDataLength
= nSrcLen
;
452 m_pchData
[nSrcLen
] = '\0';
456 // assigns one string to another
457 wxString
& wxString::operator=(const wxString
& stringSrc
)
459 wxASSERT( stringSrc
.GetStringData()->IsValid() );
461 // don't copy string over itself
462 if ( m_pchData
!= stringSrc
.m_pchData
) {
463 if ( stringSrc
.GetStringData()->IsEmpty() ) {
468 GetStringData()->Unlock();
469 m_pchData
= stringSrc
.m_pchData
;
470 GetStringData()->Lock();
477 // assigns a single character
478 wxString
& wxString::operator=(char ch
)
485 wxString
& wxString::operator=(const char *psz
)
487 AssignCopy(Strlen(psz
), psz
);
491 // same as 'signed char' variant
492 wxString
& wxString::operator=(const unsigned char* psz
)
494 *this = (const char *)psz
;
498 wxString
& wxString::operator=(const wchar_t *pwz
)
505 // ---------------------------------------------------------------------------
506 // string concatenation
507 // ---------------------------------------------------------------------------
509 // add something to this string
510 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
512 STATISTICS_ADD(SummandLength
, nSrcLen
);
514 // concatenating an empty string is a NOP
516 wxStringData
*pData
= GetStringData();
517 size_t nLen
= pData
->nDataLength
;
518 size_t nNewLen
= nLen
+ nSrcLen
;
520 // alloc new buffer if current is too small
521 if ( pData
->IsShared() ) {
522 STATISTICS_ADD(ConcatHit
, 0);
524 // we have to allocate another buffer
525 wxStringData
* pOldData
= GetStringData();
526 AllocBuffer(nNewLen
);
527 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
530 else if ( nNewLen
> pData
->nAllocLength
) {
531 STATISTICS_ADD(ConcatHit
, 0);
533 // we have to grow the buffer
537 STATISTICS_ADD(ConcatHit
, 1);
539 // the buffer is already big enough
542 // should be enough space
543 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
545 // fast concatenation - all is done in our buffer
546 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
548 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
549 GetStringData()->nDataLength
= nNewLen
; // and fix the length
551 //else: the string to append was empty
555 * concatenation functions come in 5 flavours:
557 * char + string and string + char
558 * C str + string and string + C str
561 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
563 wxASSERT( string1
.GetStringData()->IsValid() );
564 wxASSERT( string2
.GetStringData()->IsValid() );
566 wxString s
= string1
;
572 wxString
operator+(const wxString
& string
, char ch
)
574 wxASSERT( string
.GetStringData()->IsValid() );
582 wxString
operator+(char ch
, const wxString
& string
)
584 wxASSERT( string
.GetStringData()->IsValid() );
592 wxString
operator+(const wxString
& string
, const char *psz
)
594 wxASSERT( string
.GetStringData()->IsValid() );
597 s
.Alloc(Strlen(psz
) + string
.Len());
604 wxString
operator+(const char *psz
, const wxString
& string
)
606 wxASSERT( string
.GetStringData()->IsValid() );
609 s
.Alloc(Strlen(psz
) + string
.Len());
616 // ===========================================================================
617 // other common string functions
618 // ===========================================================================
620 // ---------------------------------------------------------------------------
621 // simple sub-string extraction
622 // ---------------------------------------------------------------------------
624 // helper function: clone the data attached to this string
625 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
627 if ( nCopyLen
== 0 ) {
631 dest
.AllocBuffer(nCopyLen
);
632 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
636 // extract string of length nCount starting at nFirst
637 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
639 wxStringData
*pData
= GetStringData();
640 size_t nLen
= pData
->nDataLength
;
642 // default value of nCount is STRING_MAXLEN and means "till the end"
643 if ( nCount
== STRING_MAXLEN
)
645 nCount
= nLen
- nFirst
;
648 // out-of-bounds requests return sensible things
649 if ( nFirst
+ nCount
> nLen
)
651 nCount
= nLen
- nFirst
;
656 // AllocCopy() will return empty string
661 AllocCopy(dest
, nCount
, nFirst
);
666 // extract nCount last (rightmost) characters
667 wxString
wxString::Right(size_t nCount
) const
669 if ( nCount
> (size_t)GetStringData()->nDataLength
)
670 nCount
= GetStringData()->nDataLength
;
673 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
677 // get all characters after the last occurence of ch
678 // (returns the whole string if ch not found)
679 wxString
wxString::AfterLast(char ch
) const
682 int iPos
= Find(ch
, TRUE
);
683 if ( iPos
== wxNOT_FOUND
)
686 str
= c_str() + iPos
+ 1;
691 // extract nCount first (leftmost) characters
692 wxString
wxString::Left(size_t nCount
) const
694 if ( nCount
> (size_t)GetStringData()->nDataLength
)
695 nCount
= GetStringData()->nDataLength
;
698 AllocCopy(dest
, nCount
, 0);
702 // get all characters before the first occurence of ch
703 // (returns the whole string if ch not found)
704 wxString
wxString::BeforeFirst(char ch
) const
707 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
713 /// get all characters before the last occurence of ch
714 /// (returns empty string if ch not found)
715 wxString
wxString::BeforeLast(char ch
) const
718 int iPos
= Find(ch
, TRUE
);
719 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
720 str
= wxString(c_str(), iPos
);
725 /// get all characters after the first occurence of ch
726 /// (returns empty string if ch not found)
727 wxString
wxString::AfterFirst(char ch
) const
731 if ( iPos
!= wxNOT_FOUND
)
732 str
= c_str() + iPos
+ 1;
737 // replace first (or all) occurences of some substring with another one
738 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
740 size_t uiCount
= 0; // count of replacements made
742 size_t uiOldLen
= Strlen(szOld
);
745 const char *pCurrent
= m_pchData
;
747 while ( *pCurrent
!= '\0' ) {
748 pSubstr
= strstr(pCurrent
, szOld
);
749 if ( pSubstr
== NULL
) {
750 // strTemp is unused if no replacements were made, so avoid the copy
754 strTemp
+= pCurrent
; // copy the rest
755 break; // exit the loop
758 // take chars before match
759 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
761 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
766 if ( !bReplaceAll
) {
767 strTemp
+= pCurrent
; // copy the rest
768 break; // exit the loop
773 // only done if there were replacements, otherwise would have returned above
779 bool wxString::IsAscii() const
781 const char *s
= (const char*) *this;
783 if(!isascii(*s
)) return(FALSE
);
789 bool wxString::IsWord() const
791 const char *s
= (const char*) *this;
793 if(!isalpha(*s
)) return(FALSE
);
799 bool wxString::IsNumber() const
801 const char *s
= (const char*) *this;
803 if(!isdigit(*s
)) return(FALSE
);
809 wxString
wxString::Strip(stripType w
) const
812 if ( w
& leading
) s
.Trim(FALSE
);
813 if ( w
& trailing
) s
.Trim(TRUE
);
817 // ---------------------------------------------------------------------------
819 // ---------------------------------------------------------------------------
821 wxString
& wxString::MakeUpper()
825 for ( char *p
= m_pchData
; *p
; p
++ )
826 *p
= (char)toupper(*p
);
831 wxString
& wxString::MakeLower()
835 for ( char *p
= m_pchData
; *p
; p
++ )
836 *p
= (char)tolower(*p
);
841 // ---------------------------------------------------------------------------
842 // trimming and padding
843 // ---------------------------------------------------------------------------
845 // trims spaces (in the sense of isspace) from left or right side
846 wxString
& wxString::Trim(bool bFromRight
)
848 // first check if we're going to modify the string at all
851 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
852 (!bFromRight
&& isspace(GetChar(0u)))
856 // ok, there is at least one space to trim
861 // find last non-space character
862 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
863 while ( isspace(*psz
) && (psz
>= m_pchData
) )
866 // truncate at trailing space start
868 GetStringData()->nDataLength
= psz
- m_pchData
;
872 // find first non-space character
873 const char *psz
= m_pchData
;
874 while ( isspace(*psz
) )
877 // fix up data and length
878 int nDataLength
= GetStringData()->nDataLength
- (psz
- (const char*) m_pchData
);
879 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
880 GetStringData()->nDataLength
= nDataLength
;
887 // adds nCount characters chPad to the string from either side
888 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
890 wxString
s(chPad
, nCount
);
903 // truncate the string
904 wxString
& wxString::Truncate(size_t uiLen
)
906 if ( uiLen
< Len() ) {
909 *(m_pchData
+ uiLen
) = '\0';
910 GetStringData()->nDataLength
= uiLen
;
912 //else: nothing to do, string is already short enough
917 // ---------------------------------------------------------------------------
918 // finding (return wxNOT_FOUND if not found and index otherwise)
919 // ---------------------------------------------------------------------------
922 int wxString::Find(char ch
, bool bFromEnd
) const
924 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
926 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const char*) m_pchData
;
929 // find a sub-string (like strstr)
930 int wxString::Find(const char *pszSub
) const
932 const char *psz
= strstr(m_pchData
, pszSub
);
934 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const char*) m_pchData
;
937 // ---------------------------------------------------------------------------
938 // stream-like operators
939 // ---------------------------------------------------------------------------
940 wxString
& wxString::operator<<(int i
)
945 return (*this) << res
;
948 wxString
& wxString::operator<<(float f
)
953 return (*this) << res
;
956 wxString
& wxString::operator<<(double d
)
961 return (*this) << res
;
964 // ---------------------------------------------------------------------------
966 // ---------------------------------------------------------------------------
967 int wxString::Printf(const char *pszFormat
, ...)
970 va_start(argptr
, pszFormat
);
972 int iLen
= PrintfV(pszFormat
, argptr
);
979 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
981 // static buffer to avoid dynamic memory allocation each time
982 static char s_szScratch
[1024];
984 // NB: wxVsprintf() may return either less than the buffer size or -1 if there
985 // is not enough place depending on implementation
986 int iLen
= wxVsprintf(s_szScratch
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
988 if ( iLen
< (int)WXSIZEOF(s_szScratch
) ) {
989 buffer
= s_szScratch
;
992 int size
= WXSIZEOF(s_szScratch
) * 2;
993 buffer
= (char *)malloc(size
);
994 while ( buffer
!= NULL
) {
995 iLen
= wxVsprintf(buffer
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
997 // ok, there was enough space
1001 // still not enough, double it again
1002 buffer
= (char *)realloc(buffer
, size
*= 2);
1011 AllocBeforeWrite(iLen
);
1012 strcpy(m_pchData
, buffer
);
1014 if ( buffer
!= s_szScratch
)
1020 // ----------------------------------------------------------------------------
1021 // misc other operations
1022 // ----------------------------------------------------------------------------
1023 bool wxString::Matches(const char *pszMask
) const
1025 // check char by char
1027 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
1028 switch ( *pszMask
) {
1030 if ( *pszTxt
== '\0' )
1039 // ignore special chars immediately following this one
1040 while ( *pszMask
== '*' || *pszMask
== '?' )
1043 // if there is nothing more, match
1044 if ( *pszMask
== '\0' )
1047 // are there any other metacharacters in the mask?
1049 const char *pEndMask
= strpbrk(pszMask
, "*?");
1051 if ( pEndMask
!= NULL
) {
1052 // we have to match the string between two metachars
1053 uiLenMask
= pEndMask
- pszMask
;
1056 // we have to match the remainder of the string
1057 uiLenMask
= strlen(pszMask
);
1060 wxString
strToMatch(pszMask
, uiLenMask
);
1061 const char* pMatch
= strstr(pszTxt
, strToMatch
);
1062 if ( pMatch
== NULL
)
1065 // -1 to compensate "++" in the loop
1066 pszTxt
= pMatch
+ uiLenMask
- 1;
1067 pszMask
+= uiLenMask
- 1;
1072 if ( *pszMask
!= *pszTxt
)
1078 // match only if nothing left
1079 return *pszTxt
== '\0';
1082 // Count the number of chars
1083 int wxString::Freq(char ch
) const
1087 for (int i
= 0; i
< len
; i
++)
1089 if (GetChar(i
) == ch
)
1095 // ---------------------------------------------------------------------------
1096 // standard C++ library string functions
1097 // ---------------------------------------------------------------------------
1098 #ifdef wxSTD_STRING_COMPATIBILITY
1100 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1102 wxASSERT( str
.GetStringData()->IsValid() );
1103 wxASSERT( nPos
<= Len() );
1105 if ( !str
.IsEmpty() ) {
1107 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1108 strncpy(pc
, c_str(), nPos
);
1109 strcpy(pc
+ nPos
, str
);
1110 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1111 strTmp
.UngetWriteBuf();
1118 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1120 wxASSERT( str
.GetStringData()->IsValid() );
1121 wxASSERT( nStart
<= Len() );
1123 const char *p
= strstr(c_str() + nStart
, str
);
1125 return p
== NULL
? npos
: p
- c_str();
1128 // VC++ 1.5 can't cope with the default argument in the header.
1129 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1130 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1132 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1136 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1137 #if !defined(__BORLANDC__)
1138 size_t wxString::find(char ch
, size_t nStart
) const
1140 wxASSERT( nStart
<= Len() );
1142 const char *p
= strchr(c_str() + nStart
, ch
);
1144 return p
== NULL
? npos
: p
- c_str();
1148 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1150 wxASSERT( str
.GetStringData()->IsValid() );
1151 wxASSERT( nStart
<= Len() );
1153 // # could be quicker than that
1154 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1155 while ( p
>= c_str() + str
.Len() ) {
1156 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1157 return p
- str
.Len() - c_str();
1164 // VC++ 1.5 can't cope with the default argument in the header.
1165 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1166 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1168 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1171 size_t wxString::rfind(char ch
, size_t nStart
) const
1173 wxASSERT( nStart
<= Len() );
1175 const char *p
= strrchr(c_str() + nStart
, ch
);
1177 return p
== NULL
? npos
: p
- c_str();
1181 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1183 // npos means 'take all'
1187 wxASSERT( nStart
+ nLen
<= Len() );
1189 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1192 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1194 wxString
strTmp(c_str(), nStart
);
1195 if ( nLen
!= npos
) {
1196 wxASSERT( nStart
+ nLen
<= Len() );
1198 strTmp
.append(c_str() + nStart
+ nLen
);
1205 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1207 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1211 strTmp
.append(c_str(), nStart
);
1213 strTmp
.append(c_str() + nStart
+ nLen
);
1219 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1221 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1224 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1225 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1227 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1230 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1231 const char* sz
, size_t nCount
)
1233 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1236 #endif //std::string compatibility
1238 // ============================================================================
1240 // ============================================================================
1242 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1243 #define ARRAY_MAXSIZE_INCREMENT 4096
1244 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1245 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1248 #define STRING(p) ((wxString *)(&(p)))
1251 wxArrayString::wxArrayString()
1255 m_pItems
= (char **) NULL
;
1259 wxArrayString::wxArrayString(const wxArrayString
& src
)
1263 m_pItems
= (char **) NULL
;
1268 // assignment operator
1269 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1274 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1275 Alloc(src
.m_nCount
);
1277 // we can't just copy the pointers here because otherwise we would share
1278 // the strings with another array
1279 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1282 if ( m_nCount
!= 0 )
1283 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1289 void wxArrayString::Grow()
1291 // only do it if no more place
1292 if( m_nCount
== m_nSize
) {
1293 if( m_nSize
== 0 ) {
1294 // was empty, alloc some memory
1295 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1296 m_pItems
= new char *[m_nSize
];
1299 // otherwise when it's called for the first time, nIncrement would be 0
1300 // and the array would never be expanded
1301 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1303 // add 50% but not too much
1304 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1305 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1306 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1307 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1308 m_nSize
+= nIncrement
;
1309 char **pNew
= new char *[m_nSize
];
1311 // copy data to new location
1312 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1314 // delete old memory (but do not release the strings!)
1315 wxDELETEA(m_pItems
);
1322 void wxArrayString::Free()
1324 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1325 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1329 // deletes all the strings from the list
1330 void wxArrayString::Empty()
1337 // as Empty, but also frees memory
1338 void wxArrayString::Clear()
1345 wxDELETEA(m_pItems
);
1349 wxArrayString::~wxArrayString()
1353 wxDELETEA(m_pItems
);
1356 // pre-allocates memory (frees the previous data!)
1357 void wxArrayString::Alloc(size_t nSize
)
1359 wxASSERT( nSize
> 0 );
1361 // only if old buffer was not big enough
1362 if ( nSize
> m_nSize
) {
1364 wxDELETEA(m_pItems
);
1365 m_pItems
= new char *[nSize
];
1372 // searches the array for an item (forward or backwards)
1373 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1376 if ( m_nCount
> 0 ) {
1377 size_t ui
= m_nCount
;
1379 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1386 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1387 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1395 // add item at the end
1396 void wxArrayString::Add(const wxString
& str
)
1398 wxASSERT( str
.GetStringData()->IsValid() );
1402 // the string data must not be deleted!
1403 str
.GetStringData()->Lock();
1404 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1407 // add item at the given position
1408 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1410 wxASSERT( str
.GetStringData()->IsValid() );
1412 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1416 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1417 (m_nCount
- nIndex
)*sizeof(char *));
1419 str
.GetStringData()->Lock();
1420 m_pItems
[nIndex
] = (char *)str
.c_str();
1425 // removes item from array (by index)
1426 void wxArrayString::Remove(size_t nIndex
)
1428 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1431 Item(nIndex
).GetStringData()->Unlock();
1433 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1434 (m_nCount
- nIndex
- 1)*sizeof(char *));
1438 // removes item from array (by value)
1439 void wxArrayString::Remove(const char *sz
)
1441 int iIndex
= Index(sz
);
1443 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
1444 _("removing inexistent element in wxArrayString::Remove") );
1449 // sort array elements using passed comparaison function
1451 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1454 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);