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)
109 #pragma message("Using sprintf() because no snprintf()-like function defined")
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 #ifdef STD_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 STD_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).
278 #ifdef wxUSE_WCSRTOMBS
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 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
352 // allocate enough memory for nLen characters
353 void wxString::Alloc(size_t nLen
)
355 wxStringData
*pData
= GetStringData();
356 if ( pData
->nAllocLength
<= nLen
) {
357 if ( pData
->IsEmpty() ) {
360 wxStringData
* pData
= (wxStringData
*)
361 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
363 pData
->nDataLength
= 0;
364 pData
->nAllocLength
= nLen
;
365 m_pchData
= pData
->data(); // data starts after wxStringData
366 m_pchData
[0u] = '\0';
368 else if ( pData
->IsShared() ) {
369 pData
->Unlock(); // memory not freed because shared
370 size_t nOldLen
= pData
->nDataLength
;
372 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
377 wxStringData
*p
= (wxStringData
*)
378 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
381 // @@@ what to do on memory error?
385 // it's not important if the pointer changed or not (the check for this
386 // is not faster than assigning to m_pchData in all cases)
387 p
->nAllocLength
= nLen
;
388 m_pchData
= p
->data();
391 //else: we've already got enough
394 // shrink to minimal size (releasing extra memory)
395 void wxString::Shrink()
397 wxStringData
*pData
= GetStringData();
399 // this variable is unused in release build, so avoid the compiler warning by
400 // just not declaring it
404 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
406 wxASSERT( p
!= NULL
); // can't free memory?
407 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
410 // get the pointer to writable buffer of (at least) nLen bytes
411 char *wxString::GetWriteBuf(size_t nLen
)
413 AllocBeforeWrite(nLen
);
415 wxASSERT( GetStringData()->nRefs
== 1 );
416 GetStringData()->Validate(FALSE
);
421 // put string back in a reasonable state after GetWriteBuf
422 void wxString::UngetWriteBuf()
424 GetStringData()->nDataLength
= strlen(m_pchData
);
425 GetStringData()->Validate(TRUE
);
428 // ---------------------------------------------------------------------------
430 // ---------------------------------------------------------------------------
432 // all functions are inline in string.h
434 // ---------------------------------------------------------------------------
435 // assignment operators
436 // ---------------------------------------------------------------------------
438 // helper function: does real copy
439 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
441 if ( nSrcLen
== 0 ) {
445 AllocBeforeWrite(nSrcLen
);
446 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
447 GetStringData()->nDataLength
= nSrcLen
;
448 m_pchData
[nSrcLen
] = '\0';
452 // assigns one string to another
453 wxString
& wxString::operator=(const wxString
& stringSrc
)
455 wxASSERT( stringSrc
.GetStringData()->IsValid() );
457 // don't copy string over itself
458 if ( m_pchData
!= stringSrc
.m_pchData
) {
459 if ( stringSrc
.GetStringData()->IsEmpty() ) {
464 GetStringData()->Unlock();
465 m_pchData
= stringSrc
.m_pchData
;
466 GetStringData()->Lock();
473 // assigns a single character
474 wxString
& wxString::operator=(char ch
)
481 wxString
& wxString::operator=(const char *psz
)
483 AssignCopy(Strlen(psz
), psz
);
487 // same as 'signed char' variant
488 wxString
& wxString::operator=(const unsigned char* psz
)
490 *this = (const char *)psz
;
494 wxString
& wxString::operator=(const wchar_t *pwz
)
501 // ---------------------------------------------------------------------------
502 // string concatenation
503 // ---------------------------------------------------------------------------
505 // add something to this string
506 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
508 STATISTICS_ADD(SummandLength
, nSrcLen
);
510 // concatenating an empty string is a NOP
512 wxStringData
*pData
= GetStringData();
513 size_t nLen
= pData
->nDataLength
;
514 size_t nNewLen
= nLen
+ nSrcLen
;
516 // alloc new buffer if current is too small
517 if ( pData
->IsShared() ) {
518 STATISTICS_ADD(ConcatHit
, 0);
520 // we have to allocate another buffer
521 wxStringData
* pOldData
= GetStringData();
522 AllocBuffer(nNewLen
);
523 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
526 else if ( nNewLen
> pData
->nAllocLength
) {
527 STATISTICS_ADD(ConcatHit
, 0);
529 // we have to grow the buffer
533 STATISTICS_ADD(ConcatHit
, 1);
535 // the buffer is already big enough
538 // should be enough space
539 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
541 // fast concatenation - all is done in our buffer
542 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
544 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
545 GetStringData()->nDataLength
= nNewLen
; // and fix the length
547 //else: the string to append was empty
551 * concatenation functions come in 5 flavours:
553 * char + string and string + char
554 * C str + string and string + C str
557 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
559 wxASSERT( string1
.GetStringData()->IsValid() );
560 wxASSERT( string2
.GetStringData()->IsValid() );
562 wxString s
= string1
;
568 wxString
operator+(const wxString
& string
, char ch
)
570 wxASSERT( string
.GetStringData()->IsValid() );
578 wxString
operator+(char ch
, const wxString
& string
)
580 wxASSERT( string
.GetStringData()->IsValid() );
588 wxString
operator+(const wxString
& string
, const char *psz
)
590 wxASSERT( string
.GetStringData()->IsValid() );
593 s
.Alloc(Strlen(psz
) + string
.Len());
600 wxString
operator+(const char *psz
, const wxString
& string
)
602 wxASSERT( string
.GetStringData()->IsValid() );
605 s
.Alloc(Strlen(psz
) + string
.Len());
612 // ===========================================================================
613 // other common string functions
614 // ===========================================================================
616 // ---------------------------------------------------------------------------
617 // simple sub-string extraction
618 // ---------------------------------------------------------------------------
620 // helper function: clone the data attached to this string
621 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
623 if ( nCopyLen
== 0 ) {
627 dest
.AllocBuffer(nCopyLen
);
628 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
632 // extract string of length nCount starting at nFirst
633 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
635 wxStringData
*pData
= GetStringData();
636 size_t nLen
= pData
->nDataLength
;
638 // default value of nCount is STRING_MAXLEN and means "till the end"
639 if ( nCount
== STRING_MAXLEN
)
641 nCount
= nLen
- nFirst
;
644 // out-of-bounds requests return sensible things
645 if ( nFirst
+ nCount
> nLen
)
647 nCount
= nLen
- nFirst
;
652 // AllocCopy() will return empty string
657 AllocCopy(dest
, nCount
, nFirst
);
662 // extract nCount last (rightmost) characters
663 wxString
wxString::Right(size_t nCount
) const
665 if ( nCount
> (size_t)GetStringData()->nDataLength
)
666 nCount
= GetStringData()->nDataLength
;
669 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
673 // get all characters after the last occurence of ch
674 // (returns the whole string if ch not found)
675 wxString
wxString::Right(char ch
) const
678 int iPos
= Find(ch
, TRUE
);
679 if ( iPos
== NOT_FOUND
)
682 str
= c_str() + iPos
+ 1;
687 // extract nCount first (leftmost) characters
688 wxString
wxString::Left(size_t nCount
) const
690 if ( nCount
> (size_t)GetStringData()->nDataLength
)
691 nCount
= GetStringData()->nDataLength
;
694 AllocCopy(dest
, nCount
, 0);
698 // get all characters before the first occurence of ch
699 // (returns the whole string if ch not found)
700 wxString
wxString::Left(char ch
) const
703 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
709 /// get all characters before the last occurence of ch
710 /// (returns empty string if ch not found)
711 wxString
wxString::Before(char ch
) const
714 int iPos
= Find(ch
, TRUE
);
715 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
716 str
= wxString(c_str(), iPos
);
721 /// get all characters after the first occurence of ch
722 /// (returns empty string if ch not found)
723 wxString
wxString::After(char ch
) const
727 if ( iPos
!= NOT_FOUND
)
728 str
= c_str() + iPos
+ 1;
733 // replace first (or all) occurences of some substring with another one
734 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
736 size_t uiCount
= 0; // count of replacements made
738 size_t uiOldLen
= Strlen(szOld
);
741 const char *pCurrent
= m_pchData
;
743 while ( *pCurrent
!= '\0' ) {
744 pSubstr
= strstr(pCurrent
, szOld
);
745 if ( pSubstr
== NULL
) {
746 // strTemp is unused if no replacements were made, so avoid the copy
750 strTemp
+= pCurrent
; // copy the rest
751 break; // exit the loop
754 // take chars before match
755 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
757 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
762 if ( !bReplaceAll
) {
763 strTemp
+= pCurrent
; // copy the rest
764 break; // exit the loop
769 // only done if there were replacements, otherwise would have returned above
775 bool wxString::IsAscii() const
777 const char *s
= (const char*) *this;
779 if(!isascii(*s
)) return(FALSE
);
785 bool wxString::IsWord() const
787 const char *s
= (const char*) *this;
789 if(!isalpha(*s
)) return(FALSE
);
795 bool wxString::IsNumber() const
797 const char *s
= (const char*) *this;
799 if(!isdigit(*s
)) return(FALSE
);
805 wxString
wxString::Strip(stripType w
) const
808 if ( w
& leading
) s
.Trim(FALSE
);
809 if ( w
& trailing
) s
.Trim(TRUE
);
813 // ---------------------------------------------------------------------------
815 // ---------------------------------------------------------------------------
817 wxString
& wxString::MakeUpper()
821 for ( char *p
= m_pchData
; *p
; p
++ )
822 *p
= (char)toupper(*p
);
827 wxString
& wxString::MakeLower()
831 for ( char *p
= m_pchData
; *p
; p
++ )
832 *p
= (char)tolower(*p
);
837 // ---------------------------------------------------------------------------
838 // trimming and padding
839 // ---------------------------------------------------------------------------
841 // trims spaces (in the sense of isspace) from left or right side
842 wxString
& wxString::Trim(bool bFromRight
)
844 // first check if we're going to modify the string at all
847 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
848 (!bFromRight
&& isspace(GetChar(0u)))
852 // ok, there is at least one space to trim
857 // find last non-space character
858 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
859 while ( isspace(*psz
) && (psz
>= m_pchData
) )
862 // truncate at trailing space start
864 GetStringData()->nDataLength
= psz
- m_pchData
;
868 // find first non-space character
869 const char *psz
= m_pchData
;
870 while ( isspace(*psz
) )
873 // fix up data and length
874 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
875 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
876 GetStringData()->nDataLength
= nDataLength
;
883 // adds nCount characters chPad to the string from either side
884 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
886 wxString
s(chPad
, nCount
);
899 // truncate the string
900 wxString
& wxString::Truncate(size_t uiLen
)
902 *(m_pchData
+ uiLen
) = '\0';
903 GetStringData()->nDataLength
= uiLen
;
908 // ---------------------------------------------------------------------------
909 // finding (return NOT_FOUND if not found and index otherwise)
910 // ---------------------------------------------------------------------------
913 int wxString::Find(char ch
, bool bFromEnd
) const
915 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
917 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
920 // find a sub-string (like strstr)
921 int wxString::Find(const char *pszSub
) const
923 const char *psz
= strstr(m_pchData
, pszSub
);
925 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
928 // ---------------------------------------------------------------------------
929 // stream-like operators
930 // ---------------------------------------------------------------------------
931 wxString
& wxString::operator<<(int i
)
936 return (*this) << res
;
939 wxString
& wxString::operator<<(float f
)
944 return (*this) << res
;
947 wxString
& wxString::operator<<(double d
)
952 return (*this) << res
;
955 // ---------------------------------------------------------------------------
957 // ---------------------------------------------------------------------------
958 int wxString::Printf(const char *pszFormat
, ...)
961 va_start(argptr
, pszFormat
);
963 int iLen
= PrintfV(pszFormat
, argptr
);
970 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
972 // static buffer to avoid dynamic memory allocation each time
973 static char s_szScratch
[1024];
975 // NB: wxVsprintf() may return either less than the buffer size or -1 if there
976 // is not enough place depending on implementation
977 int iLen
= wxVsprintf(s_szScratch
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
979 if ( iLen
< (int)WXSIZEOF(s_szScratch
) ) {
980 buffer
= s_szScratch
;
983 int size
= WXSIZEOF(s_szScratch
) * 2;
984 buffer
= (char *)malloc(size
);
985 while ( buffer
!= NULL
) {
986 iLen
= wxVsprintf(buffer
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
988 // ok, there was enough space
992 // still not enough, double it again
993 buffer
= (char *)realloc(buffer
, size
*= 2);
1002 AllocBeforeWrite(iLen
);
1003 strcpy(m_pchData
, buffer
);
1005 if ( buffer
!= s_szScratch
)
1011 // ----------------------------------------------------------------------------
1012 // misc other operations
1013 // ----------------------------------------------------------------------------
1014 bool wxString::Matches(const char *pszMask
) const
1016 // check char by char
1018 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
1019 switch ( *pszMask
) {
1021 if ( *pszTxt
== '\0' )
1030 // ignore special chars immediately following this one
1031 while ( *pszMask
== '*' || *pszMask
== '?' )
1034 // if there is nothing more, match
1035 if ( *pszMask
== '\0' )
1038 // are there any other metacharacters in the mask?
1040 const char *pEndMask
= strpbrk(pszMask
, "*?");
1042 if ( pEndMask
!= NULL
) {
1043 // we have to match the string between two metachars
1044 uiLenMask
= pEndMask
- pszMask
;
1047 // we have to match the remainder of the string
1048 uiLenMask
= strlen(pszMask
);
1051 wxString
strToMatch(pszMask
, uiLenMask
);
1052 const char* pMatch
= strstr(pszTxt
, strToMatch
);
1053 if ( pMatch
== NULL
)
1056 // -1 to compensate "++" in the loop
1057 pszTxt
= pMatch
+ uiLenMask
- 1;
1058 pszMask
+= uiLenMask
- 1;
1063 if ( *pszMask
!= *pszTxt
)
1069 // match only if nothing left
1070 return *pszTxt
== '\0';
1073 // ---------------------------------------------------------------------------
1074 // standard C++ library string functions
1075 // ---------------------------------------------------------------------------
1076 #ifdef STD_STRING_COMPATIBILITY
1078 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1080 wxASSERT( str
.GetStringData()->IsValid() );
1081 wxASSERT( nPos
<= Len() );
1083 if ( !str
.IsEmpty() ) {
1085 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1086 strncpy(pc
, c_str(), nPos
);
1087 strcpy(pc
+ nPos
, str
);
1088 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1089 strTmp
.UngetWriteBuf();
1096 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1098 wxASSERT( str
.GetStringData()->IsValid() );
1099 wxASSERT( nStart
<= Len() );
1101 const char *p
= strstr(c_str() + nStart
, str
);
1103 return p
== NULL
? npos
: p
- c_str();
1106 // VC++ 1.5 can't cope with the default argument in the header.
1107 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1108 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1110 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1114 size_t wxString::find(char ch
, size_t nStart
) const
1116 wxASSERT( nStart
<= Len() );
1118 const char *p
= strchr(c_str() + nStart
, ch
);
1120 return p
== NULL
? npos
: p
- c_str();
1123 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1125 wxASSERT( str
.GetStringData()->IsValid() );
1126 wxASSERT( nStart
<= Len() );
1128 // # could be quicker than that
1129 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1130 while ( p
>= c_str() + str
.Len() ) {
1131 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1132 return p
- str
.Len() - c_str();
1139 // VC++ 1.5 can't cope with the default argument in the header.
1140 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1141 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1143 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1146 size_t wxString::rfind(char ch
, size_t nStart
) const
1148 wxASSERT( nStart
<= Len() );
1150 const char *p
= strrchr(c_str() + nStart
, ch
);
1152 return p
== NULL
? npos
: p
- c_str();
1156 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1158 // npos means 'take all'
1162 wxASSERT( nStart
+ nLen
<= Len() );
1164 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1167 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1169 wxString
strTmp(c_str(), nStart
);
1170 if ( nLen
!= npos
) {
1171 wxASSERT( nStart
+ nLen
<= Len() );
1173 strTmp
.append(c_str() + nStart
+ nLen
);
1180 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1182 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1186 strTmp
.append(c_str(), nStart
);
1188 strTmp
.append(c_str() + nStart
+ nLen
);
1194 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1196 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1199 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1200 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1202 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1205 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1206 const char* sz
, size_t nCount
)
1208 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1211 #endif //std::string compatibility
1213 // ============================================================================
1215 // ============================================================================
1217 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1218 #define ARRAY_MAXSIZE_INCREMENT 4096
1219 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1220 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1223 #define STRING(p) ((wxString *)(&(p)))
1226 wxArrayString::wxArrayString()
1230 m_pItems
= (char **) NULL
;
1234 wxArrayString::wxArrayString(const wxArrayString
& src
)
1238 m_pItems
= (char **) NULL
;
1243 // assignment operator
1244 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1249 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1250 Alloc(src
.m_nCount
);
1252 // we can't just copy the pointers here because otherwise we would share
1253 // the strings with another array
1254 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1257 if ( m_nCount
!= 0 )
1258 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1264 void wxArrayString::Grow()
1266 // only do it if no more place
1267 if( m_nCount
== m_nSize
) {
1268 if( m_nSize
== 0 ) {
1269 // was empty, alloc some memory
1270 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1271 m_pItems
= new char *[m_nSize
];
1274 // otherwise when it's called for the first time, nIncrement would be 0
1275 // and the array would never be expanded
1276 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1278 // add 50% but not too much
1279 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1280 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1281 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1282 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1283 m_nSize
+= nIncrement
;
1284 char **pNew
= new char *[m_nSize
];
1286 // copy data to new location
1287 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1289 // delete old memory (but do not release the strings!)
1290 wxDELETEA(m_pItems
);
1297 void wxArrayString::Free()
1299 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1300 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1304 // deletes all the strings from the list
1305 void wxArrayString::Empty()
1312 // as Empty, but also frees memory
1313 void wxArrayString::Clear()
1320 wxDELETEA(m_pItems
);
1324 wxArrayString::~wxArrayString()
1328 wxDELETEA(m_pItems
);
1331 // pre-allocates memory (frees the previous data!)
1332 void wxArrayString::Alloc(size_t nSize
)
1334 wxASSERT( nSize
> 0 );
1336 // only if old buffer was not big enough
1337 if ( nSize
> m_nSize
) {
1339 wxDELETEA(m_pItems
);
1340 m_pItems
= new char *[nSize
];
1347 // searches the array for an item (forward or backwards)
1348 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1351 if ( m_nCount
> 0 ) {
1352 size_t ui
= m_nCount
;
1354 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1361 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1362 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1370 // add item at the end
1371 void wxArrayString::Add(const wxString
& str
)
1373 wxASSERT( str
.GetStringData()->IsValid() );
1377 // the string data must not be deleted!
1378 str
.GetStringData()->Lock();
1379 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1382 // add item at the given position
1383 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1385 wxASSERT( str
.GetStringData()->IsValid() );
1387 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1391 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1392 (m_nCount
- nIndex
)*sizeof(char *));
1394 str
.GetStringData()->Lock();
1395 m_pItems
[nIndex
] = (char *)str
.c_str();
1400 // removes item from array (by index)
1401 void wxArrayString::Remove(size_t nIndex
)
1403 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1406 Item(nIndex
).GetStringData()->Unlock();
1408 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1409 (m_nCount
- nIndex
- 1)*sizeof(char *));
1413 // removes item from array (by value)
1414 void wxArrayString::Remove(const char *sz
)
1416 int iIndex
= Index(sz
);
1418 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1419 _("removing inexistent element in wxArrayString::Remove") );
1424 // sort array elements using passed comparaison function
1426 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1429 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);