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 // convert to upper case, return the copy of the string
1096 wxString
wxString::Upper() const
1097 { wxString
s(*this); return s
.MakeUpper(); }
1099 // convert to lower case, return the copy of the string
1100 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }
1102 int wxString::sprintf(const char *pszFormat
, ...)
1105 va_start(argptr
, pszFormat
);
1106 int iLen
= PrintfV(pszFormat
, argptr
);
1111 // ---------------------------------------------------------------------------
1112 // standard C++ library string functions
1113 // ---------------------------------------------------------------------------
1114 #ifdef wxSTD_STRING_COMPATIBILITY
1116 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1118 wxASSERT( str
.GetStringData()->IsValid() );
1119 wxASSERT( nPos
<= Len() );
1121 if ( !str
.IsEmpty() ) {
1123 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1124 strncpy(pc
, c_str(), nPos
);
1125 strcpy(pc
+ nPos
, str
);
1126 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1127 strTmp
.UngetWriteBuf();
1134 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1136 wxASSERT( str
.GetStringData()->IsValid() );
1137 wxASSERT( nStart
<= Len() );
1139 const char *p
= strstr(c_str() + nStart
, str
);
1141 return p
== NULL
? npos
: p
- c_str();
1144 // VC++ 1.5 can't cope with the default argument in the header.
1145 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1146 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1148 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1152 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1153 #if !defined(__BORLANDC__)
1154 size_t wxString::find(char ch
, size_t nStart
) const
1156 wxASSERT( nStart
<= Len() );
1158 const char *p
= strchr(c_str() + nStart
, ch
);
1160 return p
== NULL
? npos
: p
- c_str();
1164 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1166 wxASSERT( str
.GetStringData()->IsValid() );
1167 wxASSERT( nStart
<= Len() );
1169 // # could be quicker than that
1170 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1171 while ( p
>= c_str() + str
.Len() ) {
1172 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1173 return p
- str
.Len() - c_str();
1180 // VC++ 1.5 can't cope with the default argument in the header.
1181 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1182 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1184 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1187 size_t wxString::rfind(char ch
, size_t nStart
) const
1189 wxASSERT( nStart
<= Len() );
1191 const char *p
= strrchr(c_str() + nStart
, ch
);
1193 return p
== NULL
? npos
: p
- c_str();
1197 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1199 // npos means 'take all'
1203 wxASSERT( nStart
+ nLen
<= Len() );
1205 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1208 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1210 wxString
strTmp(c_str(), nStart
);
1211 if ( nLen
!= npos
) {
1212 wxASSERT( nStart
+ nLen
<= Len() );
1214 strTmp
.append(c_str() + nStart
+ nLen
);
1221 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1223 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1227 strTmp
.append(c_str(), nStart
);
1229 strTmp
.append(c_str() + nStart
+ nLen
);
1235 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1237 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1240 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1241 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1243 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1246 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1247 const char* sz
, size_t nCount
)
1249 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1252 #endif //std::string compatibility
1254 // ============================================================================
1256 // ============================================================================
1258 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1259 #define ARRAY_MAXSIZE_INCREMENT 4096
1260 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1261 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1264 #define STRING(p) ((wxString *)(&(p)))
1267 wxArrayString::wxArrayString()
1271 m_pItems
= (char **) NULL
;
1275 wxArrayString::wxArrayString(const wxArrayString
& src
)
1279 m_pItems
= (char **) NULL
;
1284 // assignment operator
1285 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1290 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1291 Alloc(src
.m_nCount
);
1293 // we can't just copy the pointers here because otherwise we would share
1294 // the strings with another array
1295 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1298 if ( m_nCount
!= 0 )
1299 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1305 void wxArrayString::Grow()
1307 // only do it if no more place
1308 if( m_nCount
== m_nSize
) {
1309 if( m_nSize
== 0 ) {
1310 // was empty, alloc some memory
1311 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1312 m_pItems
= new char *[m_nSize
];
1315 // otherwise when it's called for the first time, nIncrement would be 0
1316 // and the array would never be expanded
1317 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1319 // add 50% but not too much
1320 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1321 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1322 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1323 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1324 m_nSize
+= nIncrement
;
1325 char **pNew
= new char *[m_nSize
];
1327 // copy data to new location
1328 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1330 // delete old memory (but do not release the strings!)
1331 wxDELETEA(m_pItems
);
1338 void wxArrayString::Free()
1340 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1341 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1345 // deletes all the strings from the list
1346 void wxArrayString::Empty()
1353 // as Empty, but also frees memory
1354 void wxArrayString::Clear()
1361 wxDELETEA(m_pItems
);
1365 wxArrayString::~wxArrayString()
1369 wxDELETEA(m_pItems
);
1372 // pre-allocates memory (frees the previous data!)
1373 void wxArrayString::Alloc(size_t nSize
)
1375 wxASSERT( nSize
> 0 );
1377 // only if old buffer was not big enough
1378 if ( nSize
> m_nSize
) {
1380 wxDELETEA(m_pItems
);
1381 m_pItems
= new char *[nSize
];
1388 // searches the array for an item (forward or backwards)
1389 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1392 if ( m_nCount
> 0 ) {
1393 size_t ui
= m_nCount
;
1395 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1402 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1403 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1411 // add item at the end
1412 void wxArrayString::Add(const wxString
& str
)
1414 wxASSERT( str
.GetStringData()->IsValid() );
1418 // the string data must not be deleted!
1419 str
.GetStringData()->Lock();
1420 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1423 // add item at the given position
1424 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1426 wxASSERT( str
.GetStringData()->IsValid() );
1428 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1432 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1433 (m_nCount
- nIndex
)*sizeof(char *));
1435 str
.GetStringData()->Lock();
1436 m_pItems
[nIndex
] = (char *)str
.c_str();
1441 // removes item from array (by index)
1442 void wxArrayString::Remove(size_t nIndex
)
1444 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1447 Item(nIndex
).GetStringData()->Unlock();
1449 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1450 (m_nCount
- nIndex
- 1)*sizeof(char *));
1454 // removes item from array (by value)
1455 void wxArrayString::Remove(const char *sz
)
1457 int iIndex
= Index(sz
);
1459 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
1460 _("removing inexistent element in wxArrayString::Remove") );
1465 // sort array elements using passed comparaison function
1467 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1470 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);