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
= wxSTRING_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)
107 #if defined(__VISUALC__)
108 #pragma message("Using sprintf() because no snprintf()-like function defined")
109 #elif defined(__GNUG__)
110 #warning "Using sprintf() because no snprintf()-like function defined"
111 #elif defined(__MWERKS__)
112 #warning "Using sprintf() because no snprintf()-like function defined"
113 #elif defined(__SUNCC__)
114 // nothing -- I don't know about "#warning" for Sun's CC
116 // change this to some analogue of '#warning' for your compiler
117 #error "Using sprintf() because no snprintf()-like function defined"
120 #endif // no vsnprintf
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 #ifdef wxSTD_STRING_COMPATIBILITY
128 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
131 // ATTN: you can _not_ use both of these in the same program!
133 istream
& operator>>(istream
& is
, wxString
& WXUNUSED(str
))
138 streambuf
*sb
= is
.rdbuf();
141 int ch
= sb
->sbumpc ();
143 is
.setstate(ios::eofbit
);
146 else if ( isspace(ch
) ) {
158 if ( str
.length() == 0 )
159 is
.setstate(ios::failbit
);
164 #endif //std::string compatibility
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 // this small class is used to gather statistics for performance tuning
171 //#define WXSTRING_STATISTICS
172 #ifdef WXSTRING_STATISTICS
176 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
178 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
180 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
183 size_t m_nCount
, m_nTotal
;
185 } g_averageLength("allocation size"),
186 g_averageSummandLength("summand length"),
187 g_averageConcatHit("hit probability in concat"),
188 g_averageInitialLength("initial string length");
190 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
192 #define STATISTICS_ADD(av, val)
193 #endif // WXSTRING_STATISTICS
195 // ===========================================================================
196 // wxString class core
197 // ===========================================================================
199 // ---------------------------------------------------------------------------
201 // ---------------------------------------------------------------------------
203 // constructs string of <nLength> copies of character <ch>
204 wxString::wxString(char ch
, size_t nLength
)
209 AllocBuffer(nLength
);
211 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
213 memset(m_pchData
, ch
, nLength
);
217 // takes nLength elements of psz starting at nPos
218 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
222 wxASSERT( nPos
<= Strlen(psz
) );
224 if ( nLength
== wxSTRING_MAXLEN
)
225 nLength
= Strlen(psz
+ nPos
);
227 STATISTICS_ADD(InitialLength
, nLength
);
230 // trailing '\0' is written in AllocBuffer()
231 AllocBuffer(nLength
);
232 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
236 // the same as previous constructor, but for compilers using unsigned char
237 wxString::wxString(const unsigned char* psz
, size_t nLength
)
239 InitWith((const char *)psz
, 0, nLength
);
242 #ifdef wxSTD_STRING_COMPATIBILITY
244 // poor man's iterators are "void *" pointers
245 wxString::wxString(const void *pStart
, const void *pEnd
)
247 InitWith((const char *)pStart
, 0,
248 (const char *)pEnd
- (const char *)pStart
);
251 #endif //std::string compatibility
254 wxString::wxString(const wchar_t *pwz
)
256 // first get necessary size
258 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
259 // honor the 3rd parameter, thus it will happily crash here).
261 // don't know if it's really needed (or if we can pass NULL), but better safe
264 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
266 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
272 wcstombs(m_pchData
, pwz
, nLen
);
279 // ---------------------------------------------------------------------------
281 // ---------------------------------------------------------------------------
283 // allocates memory needed to store a C string of length nLen
284 void wxString::AllocBuffer(size_t nLen
)
286 wxASSERT( nLen
> 0 ); //
287 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
289 STATISTICS_ADD(Length
, nLen
);
292 // 1) one extra character for '\0' termination
293 // 2) sizeof(wxStringData) for housekeeping info
294 wxStringData
* pData
= (wxStringData
*)
295 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
297 pData
->nDataLength
= nLen
;
298 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
299 m_pchData
= pData
->data(); // data starts after wxStringData
300 m_pchData
[nLen
] = '\0';
303 // must be called before changing this string
304 void wxString::CopyBeforeWrite()
306 wxStringData
* pData
= GetStringData();
308 if ( pData
->IsShared() ) {
309 pData
->Unlock(); // memory not freed because shared
310 size_t nLen
= pData
->nDataLength
;
312 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
315 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
318 // must be called before replacing contents of this string
319 void wxString::AllocBeforeWrite(size_t nLen
)
321 wxASSERT( nLen
!= 0 ); // doesn't make any sense
323 // must not share string and must have enough space
324 wxStringData
* pData
= GetStringData();
325 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
326 // can't work with old buffer, get new one
331 // update the string length
332 pData
->nDataLength
= nLen
;
335 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
338 // allocate enough memory for nLen characters
339 void wxString::Alloc(size_t nLen
)
341 wxStringData
*pData
= GetStringData();
342 if ( pData
->nAllocLength
<= nLen
) {
343 if ( pData
->IsEmpty() ) {
346 wxStringData
* pData
= (wxStringData
*)
347 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
349 pData
->nDataLength
= 0;
350 pData
->nAllocLength
= nLen
;
351 m_pchData
= pData
->data(); // data starts after wxStringData
352 m_pchData
[0u] = '\0';
354 else if ( pData
->IsShared() ) {
355 pData
->Unlock(); // memory not freed because shared
356 size_t nOldLen
= pData
->nDataLength
;
358 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
363 wxStringData
*p
= (wxStringData
*)
364 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
367 // @@@ what to do on memory error?
371 // it's not important if the pointer changed or not (the check for this
372 // is not faster than assigning to m_pchData in all cases)
373 p
->nAllocLength
= nLen
;
374 m_pchData
= p
->data();
377 //else: we've already got enough
380 // shrink to minimal size (releasing extra memory)
381 void wxString::Shrink()
383 wxStringData
*pData
= GetStringData();
385 // this variable is unused in release build, so avoid the compiler warning by
386 // just not declaring it
390 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
392 wxASSERT( p
!= NULL
); // can't free memory?
393 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
396 // get the pointer to writable buffer of (at least) nLen bytes
397 char *wxString::GetWriteBuf(size_t nLen
)
399 AllocBeforeWrite(nLen
);
401 wxASSERT( GetStringData()->nRefs
== 1 );
402 GetStringData()->Validate(FALSE
);
407 // put string back in a reasonable state after GetWriteBuf
408 void wxString::UngetWriteBuf()
410 GetStringData()->nDataLength
= strlen(m_pchData
);
411 GetStringData()->Validate(TRUE
);
414 // ---------------------------------------------------------------------------
416 // ---------------------------------------------------------------------------
418 // all functions are inline in string.h
420 // ---------------------------------------------------------------------------
421 // assignment operators
422 // ---------------------------------------------------------------------------
424 // helper function: does real copy
425 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
427 if ( nSrcLen
== 0 ) {
431 AllocBeforeWrite(nSrcLen
);
432 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
433 GetStringData()->nDataLength
= nSrcLen
;
434 m_pchData
[nSrcLen
] = '\0';
438 // assigns one string to another
439 wxString
& wxString::operator=(const wxString
& stringSrc
)
441 wxASSERT( stringSrc
.GetStringData()->IsValid() );
443 // don't copy string over itself
444 if ( m_pchData
!= stringSrc
.m_pchData
) {
445 if ( stringSrc
.GetStringData()->IsEmpty() ) {
450 GetStringData()->Unlock();
451 m_pchData
= stringSrc
.m_pchData
;
452 GetStringData()->Lock();
459 // assigns a single character
460 wxString
& wxString::operator=(char ch
)
467 wxString
& wxString::operator=(const char *psz
)
469 AssignCopy(Strlen(psz
), psz
);
473 // same as 'signed char' variant
474 wxString
& wxString::operator=(const unsigned char* psz
)
476 *this = (const char *)psz
;
480 wxString
& wxString::operator=(const wchar_t *pwz
)
487 // ---------------------------------------------------------------------------
488 // string concatenation
489 // ---------------------------------------------------------------------------
491 // add something to this string
492 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
494 STATISTICS_ADD(SummandLength
, nSrcLen
);
496 // concatenating an empty string is a NOP
498 wxStringData
*pData
= GetStringData();
499 size_t nLen
= pData
->nDataLength
;
500 size_t nNewLen
= nLen
+ nSrcLen
;
502 // alloc new buffer if current is too small
503 if ( pData
->IsShared() ) {
504 STATISTICS_ADD(ConcatHit
, 0);
506 // we have to allocate another buffer
507 wxStringData
* pOldData
= GetStringData();
508 AllocBuffer(nNewLen
);
509 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
512 else if ( nNewLen
> pData
->nAllocLength
) {
513 STATISTICS_ADD(ConcatHit
, 0);
515 // we have to grow the buffer
519 STATISTICS_ADD(ConcatHit
, 1);
521 // the buffer is already big enough
524 // should be enough space
525 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
527 // fast concatenation - all is done in our buffer
528 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
530 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
531 GetStringData()->nDataLength
= nNewLen
; // and fix the length
533 //else: the string to append was empty
537 * concatenation functions come in 5 flavours:
539 * char + string and string + char
540 * C str + string and string + C str
543 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
545 wxASSERT( string1
.GetStringData()->IsValid() );
546 wxASSERT( string2
.GetStringData()->IsValid() );
548 wxString s
= string1
;
554 wxString
operator+(const wxString
& string
, char ch
)
556 wxASSERT( string
.GetStringData()->IsValid() );
564 wxString
operator+(char ch
, const wxString
& string
)
566 wxASSERT( string
.GetStringData()->IsValid() );
574 wxString
operator+(const wxString
& string
, const char *psz
)
576 wxASSERT( string
.GetStringData()->IsValid() );
579 s
.Alloc(Strlen(psz
) + string
.Len());
586 wxString
operator+(const char *psz
, const wxString
& string
)
588 wxASSERT( string
.GetStringData()->IsValid() );
591 s
.Alloc(Strlen(psz
) + string
.Len());
598 // ===========================================================================
599 // other common string functions
600 // ===========================================================================
602 // ---------------------------------------------------------------------------
603 // simple sub-string extraction
604 // ---------------------------------------------------------------------------
606 // helper function: clone the data attached to this string
607 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
609 if ( nCopyLen
== 0 ) {
613 dest
.AllocBuffer(nCopyLen
);
614 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
618 // extract string of length nCount starting at nFirst
619 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
621 wxStringData
*pData
= GetStringData();
622 size_t nLen
= pData
->nDataLength
;
624 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
625 if ( nCount
== wxSTRING_MAXLEN
)
627 nCount
= nLen
- nFirst
;
630 // out-of-bounds requests return sensible things
631 if ( nFirst
+ nCount
> nLen
)
633 nCount
= nLen
- nFirst
;
638 // AllocCopy() will return empty string
643 AllocCopy(dest
, nCount
, nFirst
);
648 // extract nCount last (rightmost) characters
649 wxString
wxString::Right(size_t nCount
) const
651 if ( nCount
> (size_t)GetStringData()->nDataLength
)
652 nCount
= GetStringData()->nDataLength
;
655 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
659 // get all characters after the last occurence of ch
660 // (returns the whole string if ch not found)
661 wxString
wxString::AfterLast(char ch
) const
664 int iPos
= Find(ch
, TRUE
);
665 if ( iPos
== wxNOT_FOUND
)
668 str
= c_str() + iPos
+ 1;
673 // extract nCount first (leftmost) characters
674 wxString
wxString::Left(size_t nCount
) const
676 if ( nCount
> (size_t)GetStringData()->nDataLength
)
677 nCount
= GetStringData()->nDataLength
;
680 AllocCopy(dest
, nCount
, 0);
684 // get all characters before the first occurence of ch
685 // (returns the whole string if ch not found)
686 wxString
wxString::BeforeFirst(char ch
) const
689 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
695 /// get all characters before the last occurence of ch
696 /// (returns empty string if ch not found)
697 wxString
wxString::BeforeLast(char ch
) const
700 int iPos
= Find(ch
, TRUE
);
701 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
702 str
= wxString(c_str(), iPos
);
707 /// get all characters after the first occurence of ch
708 /// (returns empty string if ch not found)
709 wxString
wxString::AfterFirst(char ch
) const
713 if ( iPos
!= wxNOT_FOUND
)
714 str
= c_str() + iPos
+ 1;
719 // replace first (or all) occurences of some substring with another one
720 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
722 size_t uiCount
= 0; // count of replacements made
724 size_t uiOldLen
= Strlen(szOld
);
727 const char *pCurrent
= m_pchData
;
729 while ( *pCurrent
!= '\0' ) {
730 pSubstr
= strstr(pCurrent
, szOld
);
731 if ( pSubstr
== NULL
) {
732 // strTemp is unused if no replacements were made, so avoid the copy
736 strTemp
+= pCurrent
; // copy the rest
737 break; // exit the loop
740 // take chars before match
741 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
743 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
748 if ( !bReplaceAll
) {
749 strTemp
+= pCurrent
; // copy the rest
750 break; // exit the loop
755 // only done if there were replacements, otherwise would have returned above
761 bool wxString::IsAscii() const
763 const char *s
= (const char*) *this;
765 if(!isascii(*s
)) return(FALSE
);
771 bool wxString::IsWord() const
773 const char *s
= (const char*) *this;
775 if(!isalpha(*s
)) return(FALSE
);
781 bool wxString::IsNumber() const
783 const char *s
= (const char*) *this;
785 if(!isdigit(*s
)) return(FALSE
);
791 wxString
wxString::Strip(stripType w
) const
794 if ( w
& leading
) s
.Trim(FALSE
);
795 if ( w
& trailing
) s
.Trim(TRUE
);
799 // ---------------------------------------------------------------------------
801 // ---------------------------------------------------------------------------
803 wxString
& wxString::MakeUpper()
807 for ( char *p
= m_pchData
; *p
; p
++ )
808 *p
= (char)toupper(*p
);
813 wxString
& wxString::MakeLower()
817 for ( char *p
= m_pchData
; *p
; p
++ )
818 *p
= (char)tolower(*p
);
823 // ---------------------------------------------------------------------------
824 // trimming and padding
825 // ---------------------------------------------------------------------------
827 // trims spaces (in the sense of isspace) from left or right side
828 wxString
& wxString::Trim(bool bFromRight
)
830 // first check if we're going to modify the string at all
833 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
834 (!bFromRight
&& isspace(GetChar(0u)))
838 // ok, there is at least one space to trim
843 // find last non-space character
844 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
845 while ( isspace(*psz
) && (psz
>= m_pchData
) )
848 // truncate at trailing space start
850 GetStringData()->nDataLength
= psz
- m_pchData
;
854 // find first non-space character
855 const char *psz
= m_pchData
;
856 while ( isspace(*psz
) )
859 // fix up data and length
860 int nDataLength
= GetStringData()->nDataLength
- (psz
- (const char*) m_pchData
);
861 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
862 GetStringData()->nDataLength
= nDataLength
;
869 // adds nCount characters chPad to the string from either side
870 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
872 wxString
s(chPad
, nCount
);
885 // truncate the string
886 wxString
& wxString::Truncate(size_t uiLen
)
888 if ( uiLen
< Len() ) {
891 *(m_pchData
+ uiLen
) = '\0';
892 GetStringData()->nDataLength
= uiLen
;
894 //else: nothing to do, string is already short enough
899 // ---------------------------------------------------------------------------
900 // finding (return wxNOT_FOUND if not found and index otherwise)
901 // ---------------------------------------------------------------------------
904 int wxString::Find(char ch
, bool bFromEnd
) const
906 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
908 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const char*) m_pchData
;
911 // find a sub-string (like strstr)
912 int wxString::Find(const char *pszSub
) const
914 const char *psz
= strstr(m_pchData
, pszSub
);
916 return (psz
== NULL
) ? wxNOT_FOUND
: psz
- (const char*) m_pchData
;
919 // ---------------------------------------------------------------------------
920 // stream-like operators
921 // ---------------------------------------------------------------------------
922 wxString
& wxString::operator<<(int i
)
927 return (*this) << res
;
930 wxString
& wxString::operator<<(float f
)
935 return (*this) << res
;
938 wxString
& wxString::operator<<(double d
)
943 return (*this) << res
;
946 // ---------------------------------------------------------------------------
948 // ---------------------------------------------------------------------------
949 int wxString::Printf(const char *pszFormat
, ...)
952 va_start(argptr
, pszFormat
);
954 int iLen
= PrintfV(pszFormat
, argptr
);
961 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
963 // static buffer to avoid dynamic memory allocation each time
964 static char s_szScratch
[1024];
966 // NB: wxVsprintf() may return either less than the buffer size or -1 if there
967 // is not enough place depending on implementation
968 int iLen
= wxVsprintf(s_szScratch
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
970 if ( iLen
< (int)WXSIZEOF(s_szScratch
) ) {
971 buffer
= s_szScratch
;
974 int size
= WXSIZEOF(s_szScratch
) * 2;
975 buffer
= (char *)malloc(size
);
976 while ( buffer
!= NULL
) {
977 iLen
= wxVsprintf(buffer
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
979 // ok, there was enough space
983 // still not enough, double it again
984 buffer
= (char *)realloc(buffer
, size
*= 2);
993 AllocBeforeWrite(iLen
);
994 strcpy(m_pchData
, buffer
);
996 if ( buffer
!= s_szScratch
)
1002 // ----------------------------------------------------------------------------
1003 // misc other operations
1004 // ----------------------------------------------------------------------------
1005 bool wxString::Matches(const char *pszMask
) const
1007 // check char by char
1009 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
1010 switch ( *pszMask
) {
1012 if ( *pszTxt
== '\0' )
1021 // ignore special chars immediately following this one
1022 while ( *pszMask
== '*' || *pszMask
== '?' )
1025 // if there is nothing more, match
1026 if ( *pszMask
== '\0' )
1029 // are there any other metacharacters in the mask?
1031 const char *pEndMask
= strpbrk(pszMask
, "*?");
1033 if ( pEndMask
!= NULL
) {
1034 // we have to match the string between two metachars
1035 uiLenMask
= pEndMask
- pszMask
;
1038 // we have to match the remainder of the string
1039 uiLenMask
= strlen(pszMask
);
1042 wxString
strToMatch(pszMask
, uiLenMask
);
1043 const char* pMatch
= strstr(pszTxt
, strToMatch
);
1044 if ( pMatch
== NULL
)
1047 // -1 to compensate "++" in the loop
1048 pszTxt
= pMatch
+ uiLenMask
- 1;
1049 pszMask
+= uiLenMask
- 1;
1054 if ( *pszMask
!= *pszTxt
)
1060 // match only if nothing left
1061 return *pszTxt
== '\0';
1064 // Count the number of chars
1065 int wxString::Freq(char ch
) const
1069 for (int i
= 0; i
< len
; i
++)
1071 if (GetChar(i
) == ch
)
1077 // convert to upper case, return the copy of the string
1078 wxString
wxString::Upper() const
1079 { wxString
s(*this); return s
.MakeUpper(); }
1081 // convert to lower case, return the copy of the string
1082 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }
1084 int wxString::sprintf(const char *pszFormat
, ...)
1087 va_start(argptr
, pszFormat
);
1088 int iLen
= PrintfV(pszFormat
, argptr
);
1093 // ---------------------------------------------------------------------------
1094 // standard C++ library string functions
1095 // ---------------------------------------------------------------------------
1096 #ifdef wxSTD_STRING_COMPATIBILITY
1098 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1100 wxASSERT( str
.GetStringData()->IsValid() );
1101 wxASSERT( nPos
<= Len() );
1103 if ( !str
.IsEmpty() ) {
1105 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1106 strncpy(pc
, c_str(), nPos
);
1107 strcpy(pc
+ nPos
, str
);
1108 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1109 strTmp
.UngetWriteBuf();
1116 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1118 wxASSERT( str
.GetStringData()->IsValid() );
1119 wxASSERT( nStart
<= Len() );
1121 const char *p
= strstr(c_str() + nStart
, str
);
1123 return p
== NULL
? npos
: p
- c_str();
1126 // VC++ 1.5 can't cope with the default argument in the header.
1127 #if !defined(__VISUALC__) || defined(__WIN32__)
1128 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1130 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1134 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1135 #if !defined(__BORLANDC__)
1136 size_t wxString::find(char ch
, size_t nStart
) const
1138 wxASSERT( nStart
<= Len() );
1140 const char *p
= strchr(c_str() + nStart
, ch
);
1142 return p
== NULL
? npos
: p
- c_str();
1146 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1148 wxASSERT( str
.GetStringData()->IsValid() );
1149 wxASSERT( nStart
<= Len() );
1151 // # could be quicker than that
1152 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1153 while ( p
>= c_str() + str
.Len() ) {
1154 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1155 return p
- str
.Len() - c_str();
1162 // VC++ 1.5 can't cope with the default argument in the header.
1163 #if !defined(__VISUALC__) || defined(__WIN32__)
1164 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1166 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1169 size_t wxString::rfind(char ch
, size_t nStart
) const
1171 wxASSERT( nStart
<= Len() );
1173 const char *p
= strrchr(c_str() + nStart
, ch
);
1175 return p
== NULL
? npos
: p
- c_str();
1179 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1181 // npos means 'take all'
1185 wxASSERT( nStart
+ nLen
<= Len() );
1187 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1190 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1192 wxString
strTmp(c_str(), nStart
);
1193 if ( nLen
!= npos
) {
1194 wxASSERT( nStart
+ nLen
<= Len() );
1196 strTmp
.append(c_str() + nStart
+ nLen
);
1203 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1205 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1209 strTmp
.append(c_str(), nStart
);
1211 strTmp
.append(c_str() + nStart
+ nLen
);
1217 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1219 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1222 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1223 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1225 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1228 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1229 const char* sz
, size_t nCount
)
1231 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1234 #endif //std::string compatibility
1236 // ============================================================================
1238 // ============================================================================
1240 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1241 #define ARRAY_MAXSIZE_INCREMENT 4096
1242 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1243 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1246 #define STRING(p) ((wxString *)(&(p)))
1249 wxArrayString::wxArrayString()
1253 m_pItems
= (char **) NULL
;
1257 wxArrayString::wxArrayString(const wxArrayString
& src
)
1261 m_pItems
= (char **) NULL
;
1266 // assignment operator
1267 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1272 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1273 Alloc(src
.m_nCount
);
1275 // we can't just copy the pointers here because otherwise we would share
1276 // the strings with another array
1277 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1280 if ( m_nCount
!= 0 )
1281 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1287 void wxArrayString::Grow()
1289 // only do it if no more place
1290 if( m_nCount
== m_nSize
) {
1291 if( m_nSize
== 0 ) {
1292 // was empty, alloc some memory
1293 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1294 m_pItems
= new char *[m_nSize
];
1297 // otherwise when it's called for the first time, nIncrement would be 0
1298 // and the array would never be expanded
1299 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1301 // add 50% but not too much
1302 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1303 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1304 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1305 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1306 m_nSize
+= nIncrement
;
1307 char **pNew
= new char *[m_nSize
];
1309 // copy data to new location
1310 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1312 // delete old memory (but do not release the strings!)
1313 wxDELETEA(m_pItems
);
1320 void wxArrayString::Free()
1322 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1323 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1327 // deletes all the strings from the list
1328 void wxArrayString::Empty()
1335 // as Empty, but also frees memory
1336 void wxArrayString::Clear()
1343 wxDELETEA(m_pItems
);
1347 wxArrayString::~wxArrayString()
1351 wxDELETEA(m_pItems
);
1354 // pre-allocates memory (frees the previous data!)
1355 void wxArrayString::Alloc(size_t nSize
)
1357 wxASSERT( nSize
> 0 );
1359 // only if old buffer was not big enough
1360 if ( nSize
> m_nSize
) {
1362 wxDELETEA(m_pItems
);
1363 m_pItems
= new char *[nSize
];
1370 // searches the array for an item (forward or backwards)
1371 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1374 if ( m_nCount
> 0 ) {
1375 size_t ui
= m_nCount
;
1377 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1384 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1385 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1393 // add item at the end
1394 void wxArrayString::Add(const wxString
& str
)
1396 wxASSERT( str
.GetStringData()->IsValid() );
1400 // the string data must not be deleted!
1401 str
.GetStringData()->Lock();
1402 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1405 // add item at the given position
1406 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1408 wxASSERT( str
.GetStringData()->IsValid() );
1410 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1414 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1415 (m_nCount
- nIndex
)*sizeof(char *));
1417 str
.GetStringData()->Lock();
1418 m_pItems
[nIndex
] = (char *)str
.c_str();
1423 // removes item from array (by index)
1424 void wxArrayString::Remove(size_t nIndex
)
1426 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1429 Item(nIndex
).GetStringData()->Unlock();
1431 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1432 (m_nCount
- nIndex
- 1)*sizeof(char *));
1436 // removes item from array (by value)
1437 void wxArrayString::Remove(const char *sz
)
1439 int iIndex
= Index(sz
);
1441 wxCHECK_RET( iIndex
!= wxNOT_FOUND
,
1442 _("removing inexistent element in wxArrayString::Remove") );
1447 // sort array elements using passed comparaison function
1449 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1452 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);