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"
44 // This probably isn't right, what should it be Vadim?
45 // Otherwise we end up with no wxVsprintf defined.
50 #ifdef wxUSE_WCSRTOMBS
51 #include <wchar.h> // for wcsrtombs(), see comments where it's used
54 #ifdef WXSTRING_IS_WXOBJECT
55 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
56 #endif //WXSTRING_IS_WXOBJECT
58 // allocating extra space for each string consumes more memory but speeds up
59 // the concatenation operations (nLen is the current string's length)
60 // NB: EXTRA_ALLOC must be >= 0!
61 #define EXTRA_ALLOC (19 - nLen % 16)
63 // ---------------------------------------------------------------------------
64 // static class variables definition
65 // ---------------------------------------------------------------------------
67 #ifdef STD_STRING_COMPATIBILITY
68 const size_t wxString::npos
= STRING_MAXLEN
;
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // for an empty string, GetStringData() will return this address: this
76 // structure has the same layout as wxStringData and it's data() method will
77 // return the empty string (dummy pointer)
82 } g_strEmpty
= { {-1, 0, 0}, '\0' };
84 // empty C style string: points to 'string data' byte of g_strEmpty
85 extern const char *g_szNul
= &g_strEmpty
.dummy
;
87 // ----------------------------------------------------------------------------
88 // conditional compilation
89 // ----------------------------------------------------------------------------
91 // we want to find out if the current platform supports vsnprintf()-like
92 // function: for Unix this is done with configure, for Windows we test the
93 // compiler explicitly.
96 #define wxVsprintf _vsnprintf
100 #define wxVsprintf vsnprintf
102 #endif // Windows/!Windows
105 // in this case we'll use vsprintf() (which is ANSI and thus should be
106 // always available), but it's unsafe because it doesn't check for buffer
107 // size - so give a warning
108 #define wxVsprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
110 #pragma message("Using sprintf() because no snprintf()-like function defined")
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 #ifdef STD_STRING_COMPATIBILITY
120 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
123 // ATTN: you can _not_ use both of these in the same program!
125 #include <iostream.h>
132 // for msvc (bcc50+ also) you don't need these NAMESPACE defines,
133 // using namespace std; takes care of that.
134 #define NAMESPACE std::
139 #define wxVsprintf _vsnprintf
142 #if defined ( HAVE_VSNPRINTF )
143 #define wxVsprintf vsnprintf
148 // vsprintf() is ANSI so we can always use it, but it's unsafe!
149 #define wxVsprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
150 #pragma message("Using sprintf() because no snprintf()-like function defined")
153 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
158 NAMESPACE streambuf
*sb
= is
.rdbuf();
161 int ch
= sb
->sbumpc ();
163 is
.setstate(NAMESPACE
ios::eofbit
);
166 else if ( isspace(ch
) ) {
178 if ( str
.length() == 0 )
179 is
.setstate(NAMESPACE
ios::failbit
);
184 #endif //std::string compatibility
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 // this small class is used to gather statistics for performance tuning
191 //#define WXSTRING_STATISTICS
192 #ifdef WXSTRING_STATISTICS
196 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
198 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
200 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
203 size_t m_nCount
, m_nTotal
;
205 } g_averageLength("allocation size"),
206 g_averageSummandLength("summand length"),
207 g_averageConcatHit("hit probability in concat"),
208 g_averageInitialLength("initial string length");
210 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
212 #define STATISTICS_ADD(av, val)
213 #endif // WXSTRING_STATISTICS
215 // ===========================================================================
216 // wxString class core
217 // ===========================================================================
219 // ---------------------------------------------------------------------------
221 // ---------------------------------------------------------------------------
223 // constructs string of <nLength> copies of character <ch>
224 wxString::wxString(char ch
, size_t nLength
)
229 AllocBuffer(nLength
);
231 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
233 memset(m_pchData
, ch
, nLength
);
237 // takes nLength elements of psz starting at nPos
238 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
242 wxASSERT( nPos
<= Strlen(psz
) );
244 if ( nLength
== STRING_MAXLEN
)
245 nLength
= Strlen(psz
+ nPos
);
247 STATISTICS_ADD(InitialLength
, nLength
);
250 // trailing '\0' is written in AllocBuffer()
251 AllocBuffer(nLength
);
252 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
256 // the same as previous constructor, but for compilers using unsigned char
257 wxString::wxString(const unsigned char* psz
, size_t nLength
)
259 InitWith((const char *)psz
, 0, nLength
);
262 #ifdef STD_STRING_COMPATIBILITY
264 // poor man's iterators are "void *" pointers
265 wxString::wxString(const void *pStart
, const void *pEnd
)
267 InitWith((const char *)pStart
, 0,
268 (const char *)pEnd
- (const char *)pStart
);
271 #endif //std::string compatibility
274 wxString::wxString(const wchar_t *pwz
)
276 // first get necessary size
278 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
279 // honor the 3rd parameter, thus it will happily crash here).
280 #ifdef wxUSE_WCSRTOMBS
281 // don't know if it's really needed (or if we can pass NULL), but better safe
284 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
286 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
292 wcstombs(m_pchData
, pwz
, nLen
);
299 // ---------------------------------------------------------------------------
301 // ---------------------------------------------------------------------------
303 // allocates memory needed to store a C string of length nLen
304 void wxString::AllocBuffer(size_t nLen
)
306 wxASSERT( nLen
> 0 ); //
307 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
309 STATISTICS_ADD(Length
, nLen
);
312 // 1) one extra character for '\0' termination
313 // 2) sizeof(wxStringData) for housekeeping info
314 wxStringData
* pData
= (wxStringData
*)
315 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
317 pData
->nDataLength
= nLen
;
318 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
319 m_pchData
= pData
->data(); // data starts after wxStringData
320 m_pchData
[nLen
] = '\0';
323 // must be called before changing this string
324 void wxString::CopyBeforeWrite()
326 wxStringData
* pData
= GetStringData();
328 if ( pData
->IsShared() ) {
329 pData
->Unlock(); // memory not freed because shared
330 size_t nLen
= pData
->nDataLength
;
332 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
335 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
338 // must be called before replacing contents of this string
339 void wxString::AllocBeforeWrite(size_t nLen
)
341 wxASSERT( nLen
!= 0 ); // doesn't make any sense
343 // must not share string and must have enough space
344 wxStringData
* pData
= GetStringData();
345 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
346 // can't work with old buffer, get new one
351 // update the string length
352 pData
->nDataLength
= nLen
;
355 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
358 // allocate enough memory for nLen characters
359 void wxString::Alloc(size_t nLen
)
361 wxStringData
*pData
= GetStringData();
362 if ( pData
->nAllocLength
<= nLen
) {
363 if ( pData
->IsEmpty() ) {
366 wxStringData
* pData
= (wxStringData
*)
367 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
369 pData
->nDataLength
= 0;
370 pData
->nAllocLength
= nLen
;
371 m_pchData
= pData
->data(); // data starts after wxStringData
372 m_pchData
[0u] = '\0';
374 else if ( pData
->IsShared() ) {
375 pData
->Unlock(); // memory not freed because shared
376 size_t nOldLen
= pData
->nDataLength
;
378 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
383 wxStringData
*p
= (wxStringData
*)
384 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
387 // @@@ what to do on memory error?
391 // it's not important if the pointer changed or not (the check for this
392 // is not faster than assigning to m_pchData in all cases)
393 p
->nAllocLength
= nLen
;
394 m_pchData
= p
->data();
397 //else: we've already got enough
400 // shrink to minimal size (releasing extra memory)
401 void wxString::Shrink()
403 wxStringData
*pData
= GetStringData();
405 // this variable is unused in release build, so avoid the compiler warning by
406 // just not declaring it
410 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
412 wxASSERT( p
!= NULL
); // can't free memory?
413 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
416 // get the pointer to writable buffer of (at least) nLen bytes
417 char *wxString::GetWriteBuf(size_t nLen
)
419 AllocBeforeWrite(nLen
);
421 wxASSERT( GetStringData()->nRefs
== 1 );
422 GetStringData()->Validate(FALSE
);
427 // put string back in a reasonable state after GetWriteBuf
428 void wxString::UngetWriteBuf()
430 GetStringData()->nDataLength
= strlen(m_pchData
);
431 GetStringData()->Validate(TRUE
);
434 // ---------------------------------------------------------------------------
436 // ---------------------------------------------------------------------------
438 // all functions are inline in string.h
440 // ---------------------------------------------------------------------------
441 // assignment operators
442 // ---------------------------------------------------------------------------
444 // helper function: does real copy
445 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
447 if ( nSrcLen
== 0 ) {
451 AllocBeforeWrite(nSrcLen
);
452 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
453 GetStringData()->nDataLength
= nSrcLen
;
454 m_pchData
[nSrcLen
] = '\0';
458 // assigns one string to another
459 wxString
& wxString::operator=(const wxString
& stringSrc
)
461 wxASSERT( stringSrc
.GetStringData()->IsValid() );
463 // don't copy string over itself
464 if ( m_pchData
!= stringSrc
.m_pchData
) {
465 if ( stringSrc
.GetStringData()->IsEmpty() ) {
470 GetStringData()->Unlock();
471 m_pchData
= stringSrc
.m_pchData
;
472 GetStringData()->Lock();
479 // assigns a single character
480 wxString
& wxString::operator=(char ch
)
487 wxString
& wxString::operator=(const char *psz
)
489 AssignCopy(Strlen(psz
), psz
);
493 // same as 'signed char' variant
494 wxString
& wxString::operator=(const unsigned char* psz
)
496 *this = (const char *)psz
;
500 wxString
& wxString::operator=(const wchar_t *pwz
)
507 // ---------------------------------------------------------------------------
508 // string concatenation
509 // ---------------------------------------------------------------------------
511 // add something to this string
512 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
514 STATISTICS_ADD(SummandLength
, nSrcLen
);
516 // concatenating an empty string is a NOP
518 wxStringData
*pData
= GetStringData();
519 size_t nLen
= pData
->nDataLength
;
520 size_t nNewLen
= nLen
+ nSrcLen
;
522 // alloc new buffer if current is too small
523 if ( pData
->IsShared() ) {
524 STATISTICS_ADD(ConcatHit
, 0);
526 // we have to allocate another buffer
527 wxStringData
* pOldData
= GetStringData();
528 AllocBuffer(nNewLen
);
529 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
532 else if ( nNewLen
> pData
->nAllocLength
) {
533 STATISTICS_ADD(ConcatHit
, 0);
535 // we have to grow the buffer
539 STATISTICS_ADD(ConcatHit
, 1);
541 // the buffer is already big enough
544 // should be enough space
545 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
547 // fast concatenation - all is done in our buffer
548 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
550 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
551 GetStringData()->nDataLength
= nNewLen
; // and fix the length
553 //else: the string to append was empty
557 * concatenation functions come in 5 flavours:
559 * char + string and string + char
560 * C str + string and string + C str
563 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
565 wxASSERT( string1
.GetStringData()->IsValid() );
566 wxASSERT( string2
.GetStringData()->IsValid() );
568 wxString s
= string1
;
574 wxString
operator+(const wxString
& string
, char ch
)
576 wxASSERT( string
.GetStringData()->IsValid() );
584 wxString
operator+(char ch
, const wxString
& string
)
586 wxASSERT( string
.GetStringData()->IsValid() );
594 wxString
operator+(const wxString
& string
, const char *psz
)
596 wxASSERT( string
.GetStringData()->IsValid() );
599 s
.Alloc(Strlen(psz
) + string
.Len());
606 wxString
operator+(const char *psz
, const wxString
& string
)
608 wxASSERT( string
.GetStringData()->IsValid() );
611 s
.Alloc(Strlen(psz
) + string
.Len());
618 // ===========================================================================
619 // other common string functions
620 // ===========================================================================
622 // ---------------------------------------------------------------------------
623 // simple sub-string extraction
624 // ---------------------------------------------------------------------------
626 // helper function: clone the data attached to this string
627 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
629 if ( nCopyLen
== 0 ) {
633 dest
.AllocBuffer(nCopyLen
);
634 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
638 // extract string of length nCount starting at nFirst
639 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
641 wxStringData
*pData
= GetStringData();
642 size_t nLen
= pData
->nDataLength
;
644 // default value of nCount is STRING_MAXLEN and means "till the end"
645 if ( nCount
== STRING_MAXLEN
)
647 nCount
= nLen
- nFirst
;
650 // out-of-bounds requests return sensible things
651 if ( nFirst
+ nCount
> nLen
)
653 nCount
= nLen
- nFirst
;
658 // AllocCopy() will return empty string
663 AllocCopy(dest
, nCount
, nFirst
);
668 // extract nCount last (rightmost) characters
669 wxString
wxString::Right(size_t nCount
) const
671 if ( nCount
> (size_t)GetStringData()->nDataLength
)
672 nCount
= GetStringData()->nDataLength
;
675 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
679 // get all characters after the last occurence of ch
680 // (returns the whole string if ch not found)
681 wxString
wxString::Right(char ch
) const
684 int iPos
= Find(ch
, TRUE
);
685 if ( iPos
== NOT_FOUND
)
688 str
= c_str() + iPos
+ 1;
693 // extract nCount first (leftmost) characters
694 wxString
wxString::Left(size_t nCount
) const
696 if ( nCount
> (size_t)GetStringData()->nDataLength
)
697 nCount
= GetStringData()->nDataLength
;
700 AllocCopy(dest
, nCount
, 0);
704 // get all characters before the first occurence of ch
705 // (returns the whole string if ch not found)
706 wxString
wxString::Left(char ch
) const
709 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
715 /// get all characters before the last occurence of ch
716 /// (returns empty string if ch not found)
717 wxString
wxString::Before(char ch
) const
720 int iPos
= Find(ch
, TRUE
);
721 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
722 str
= wxString(c_str(), iPos
);
727 /// get all characters after the first occurence of ch
728 /// (returns empty string if ch not found)
729 wxString
wxString::After(char ch
) const
733 if ( iPos
!= NOT_FOUND
)
734 str
= c_str() + iPos
+ 1;
739 // replace first (or all) occurences of some substring with another one
740 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
742 size_t uiCount
= 0; // count of replacements made
744 size_t uiOldLen
= Strlen(szOld
);
747 const char *pCurrent
= m_pchData
;
749 while ( *pCurrent
!= '\0' ) {
750 pSubstr
= strstr(pCurrent
, szOld
);
751 if ( pSubstr
== NULL
) {
752 // strTemp is unused if no replacements were made, so avoid the copy
756 strTemp
+= pCurrent
; // copy the rest
757 break; // exit the loop
760 // take chars before match
761 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
763 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
768 if ( !bReplaceAll
) {
769 strTemp
+= pCurrent
; // copy the rest
770 break; // exit the loop
775 // only done if there were replacements, otherwise would have returned above
781 bool wxString::IsAscii() const
783 const char *s
= (const char*) *this;
785 if(!isascii(*s
)) return(FALSE
);
791 bool wxString::IsWord() const
793 const char *s
= (const char*) *this;
795 if(!isalpha(*s
)) return(FALSE
);
801 bool wxString::IsNumber() const
803 const char *s
= (const char*) *this;
805 if(!isdigit(*s
)) return(FALSE
);
811 wxString
wxString::Strip(stripType w
) const
814 if ( w
& leading
) s
.Trim(FALSE
);
815 if ( w
& trailing
) s
.Trim(TRUE
);
819 // ---------------------------------------------------------------------------
821 // ---------------------------------------------------------------------------
823 wxString
& wxString::MakeUpper()
827 for ( char *p
= m_pchData
; *p
; p
++ )
828 *p
= (char)toupper(*p
);
833 wxString
& wxString::MakeLower()
837 for ( char *p
= m_pchData
; *p
; p
++ )
838 *p
= (char)tolower(*p
);
843 // ---------------------------------------------------------------------------
844 // trimming and padding
845 // ---------------------------------------------------------------------------
847 // trims spaces (in the sense of isspace) from left or right side
848 wxString
& wxString::Trim(bool bFromRight
)
850 // first check if we're going to modify the string at all
853 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
854 (!bFromRight
&& isspace(GetChar(0u)))
858 // ok, there is at least one space to trim
863 // find last non-space character
864 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
865 while ( isspace(*psz
) && (psz
>= m_pchData
) )
868 // truncate at trailing space start
870 GetStringData()->nDataLength
= psz
- m_pchData
;
874 // find first non-space character
875 const char *psz
= m_pchData
;
876 while ( isspace(*psz
) )
879 // fix up data and length
880 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
881 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
882 GetStringData()->nDataLength
= nDataLength
;
889 // adds nCount characters chPad to the string from either side
890 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
892 wxString
s(chPad
, nCount
);
905 // truncate the string
906 wxString
& wxString::Truncate(size_t uiLen
)
908 if ( uiLen
< Len() ) {
911 *(m_pchData
+ uiLen
) = '\0';
912 GetStringData()->nDataLength
= uiLen
;
914 //else: nothing to do, string is already short enough
919 // ---------------------------------------------------------------------------
920 // finding (return NOT_FOUND if not found and index otherwise)
921 // ---------------------------------------------------------------------------
924 int wxString::Find(char ch
, bool bFromEnd
) const
926 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
928 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
931 // find a sub-string (like strstr)
932 int wxString::Find(const char *pszSub
) const
934 const char *psz
= strstr(m_pchData
, pszSub
);
936 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
939 // ---------------------------------------------------------------------------
940 // stream-like operators
941 // ---------------------------------------------------------------------------
942 wxString
& wxString::operator<<(int i
)
947 return (*this) << res
;
950 wxString
& wxString::operator<<(float f
)
955 return (*this) << res
;
958 wxString
& wxString::operator<<(double d
)
963 return (*this) << res
;
966 // ---------------------------------------------------------------------------
968 // ---------------------------------------------------------------------------
969 int wxString::Printf(const char *pszFormat
, ...)
972 va_start(argptr
, pszFormat
);
974 int iLen
= PrintfV(pszFormat
, argptr
);
981 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
983 // static buffer to avoid dynamic memory allocation each time
984 static char s_szScratch
[1024];
986 // NB: wxVsprintf() may return either less than the buffer size or -1 if there
987 // is not enough place depending on implementation
988 int iLen
= wxVsprintf(s_szScratch
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
990 if ( iLen
< (int)WXSIZEOF(s_szScratch
) ) {
991 buffer
= s_szScratch
;
994 int size
= WXSIZEOF(s_szScratch
) * 2;
995 buffer
= (char *)malloc(size
);
996 while ( buffer
!= NULL
) {
997 iLen
= wxVsprintf(buffer
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
999 // ok, there was enough space
1003 // still not enough, double it again
1004 buffer
= (char *)realloc(buffer
, size
*= 2);
1013 AllocBeforeWrite(iLen
);
1014 strcpy(m_pchData
, buffer
);
1016 if ( buffer
!= s_szScratch
)
1022 // ----------------------------------------------------------------------------
1023 // misc other operations
1024 // ----------------------------------------------------------------------------
1025 bool wxString::Matches(const char *pszMask
) const
1027 // check char by char
1029 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
1030 switch ( *pszMask
) {
1032 if ( *pszTxt
== '\0' )
1041 // ignore special chars immediately following this one
1042 while ( *pszMask
== '*' || *pszMask
== '?' )
1045 // if there is nothing more, match
1046 if ( *pszMask
== '\0' )
1049 // are there any other metacharacters in the mask?
1051 const char *pEndMask
= strpbrk(pszMask
, "*?");
1053 if ( pEndMask
!= NULL
) {
1054 // we have to match the string between two metachars
1055 uiLenMask
= pEndMask
- pszMask
;
1058 // we have to match the remainder of the string
1059 uiLenMask
= strlen(pszMask
);
1062 wxString
strToMatch(pszMask
, uiLenMask
);
1063 const char* pMatch
= strstr(pszTxt
, strToMatch
);
1064 if ( pMatch
== NULL
)
1067 // -1 to compensate "++" in the loop
1068 pszTxt
= pMatch
+ uiLenMask
- 1;
1069 pszMask
+= uiLenMask
- 1;
1074 if ( *pszMask
!= *pszTxt
)
1080 // match only if nothing left
1081 return *pszTxt
== '\0';
1084 // Count the number of chars
1085 int wxString::Freq(char ch
) const
1089 for (int i
= 0; i
< len
; i
++)
1091 if (GetChar(i
) == ch
)
1097 // ---------------------------------------------------------------------------
1098 // standard C++ library string functions
1099 // ---------------------------------------------------------------------------
1100 #ifdef STD_STRING_COMPATIBILITY
1102 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1104 wxASSERT( str
.GetStringData()->IsValid() );
1105 wxASSERT( nPos
<= Len() );
1107 if ( !str
.IsEmpty() ) {
1109 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1110 strncpy(pc
, c_str(), nPos
);
1111 strcpy(pc
+ nPos
, str
);
1112 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1113 strTmp
.UngetWriteBuf();
1120 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1122 wxASSERT( str
.GetStringData()->IsValid() );
1123 wxASSERT( nStart
<= Len() );
1125 const char *p
= strstr(c_str() + nStart
, str
);
1127 return p
== NULL
? npos
: p
- c_str();
1130 // VC++ 1.5 can't cope with the default argument in the header.
1131 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1132 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1134 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1138 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1139 #if !defined(__BORLANDC__)
1140 size_t wxString::find(char ch
, size_t nStart
) const
1142 wxASSERT( nStart
<= Len() );
1144 const char *p
= strchr(c_str() + nStart
, ch
);
1146 return p
== NULL
? npos
: p
- c_str();
1150 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1152 wxASSERT( str
.GetStringData()->IsValid() );
1153 wxASSERT( nStart
<= Len() );
1155 // # could be quicker than that
1156 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1157 while ( p
>= c_str() + str
.Len() ) {
1158 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1159 return p
- str
.Len() - c_str();
1166 // VC++ 1.5 can't cope with the default argument in the header.
1167 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1168 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1170 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1173 size_t wxString::rfind(char ch
, size_t nStart
) const
1175 wxASSERT( nStart
<= Len() );
1177 const char *p
= strrchr(c_str() + nStart
, ch
);
1179 return p
== NULL
? npos
: p
- c_str();
1183 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1185 // npos means 'take all'
1189 wxASSERT( nStart
+ nLen
<= Len() );
1191 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1194 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1196 wxString
strTmp(c_str(), nStart
);
1197 if ( nLen
!= npos
) {
1198 wxASSERT( nStart
+ nLen
<= Len() );
1200 strTmp
.append(c_str() + nStart
+ nLen
);
1207 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1209 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1213 strTmp
.append(c_str(), nStart
);
1215 strTmp
.append(c_str() + nStart
+ nLen
);
1221 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1223 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1226 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1227 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1229 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1232 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1233 const char* sz
, size_t nCount
)
1235 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1238 #endif //std::string compatibility
1240 // ============================================================================
1242 // ============================================================================
1244 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1245 #define ARRAY_MAXSIZE_INCREMENT 4096
1246 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1247 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1250 #define STRING(p) ((wxString *)(&(p)))
1253 wxArrayString::wxArrayString()
1257 m_pItems
= (char **) NULL
;
1261 wxArrayString::wxArrayString(const wxArrayString
& src
)
1265 m_pItems
= (char **) NULL
;
1270 // assignment operator
1271 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1276 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1277 Alloc(src
.m_nCount
);
1279 // we can't just copy the pointers here because otherwise we would share
1280 // the strings with another array
1281 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1284 if ( m_nCount
!= 0 )
1285 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1291 void wxArrayString::Grow()
1293 // only do it if no more place
1294 if( m_nCount
== m_nSize
) {
1295 if( m_nSize
== 0 ) {
1296 // was empty, alloc some memory
1297 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1298 m_pItems
= new char *[m_nSize
];
1301 // otherwise when it's called for the first time, nIncrement would be 0
1302 // and the array would never be expanded
1303 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1305 // add 50% but not too much
1306 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1307 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1308 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1309 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1310 m_nSize
+= nIncrement
;
1311 char **pNew
= new char *[m_nSize
];
1313 // copy data to new location
1314 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1316 // delete old memory (but do not release the strings!)
1317 wxDELETEA(m_pItems
);
1324 void wxArrayString::Free()
1326 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1327 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1331 // deletes all the strings from the list
1332 void wxArrayString::Empty()
1339 // as Empty, but also frees memory
1340 void wxArrayString::Clear()
1347 wxDELETEA(m_pItems
);
1351 wxArrayString::~wxArrayString()
1355 wxDELETEA(m_pItems
);
1358 // pre-allocates memory (frees the previous data!)
1359 void wxArrayString::Alloc(size_t nSize
)
1361 wxASSERT( nSize
> 0 );
1363 // only if old buffer was not big enough
1364 if ( nSize
> m_nSize
) {
1366 wxDELETEA(m_pItems
);
1367 m_pItems
= new char *[nSize
];
1374 // searches the array for an item (forward or backwards)
1375 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1378 if ( m_nCount
> 0 ) {
1379 size_t ui
= m_nCount
;
1381 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1388 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1389 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1397 // add item at the end
1398 void wxArrayString::Add(const wxString
& str
)
1400 wxASSERT( str
.GetStringData()->IsValid() );
1404 // the string data must not be deleted!
1405 str
.GetStringData()->Lock();
1406 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1409 // add item at the given position
1410 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1412 wxASSERT( str
.GetStringData()->IsValid() );
1414 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1418 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1419 (m_nCount
- nIndex
)*sizeof(char *));
1421 str
.GetStringData()->Lock();
1422 m_pItems
[nIndex
] = (char *)str
.c_str();
1427 // removes item from array (by index)
1428 void wxArrayString::Remove(size_t nIndex
)
1430 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1433 Item(nIndex
).GetStringData()->Unlock();
1435 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1436 (m_nCount
- nIndex
- 1)*sizeof(char *));
1440 // removes item from array (by value)
1441 void wxArrayString::Remove(const char *sz
)
1443 int iIndex
= Index(sz
);
1445 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1446 _("removing inexistent element in wxArrayString::Remove") );
1451 // sort array elements using passed comparaison function
1453 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1456 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);