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 #ifdef wxUSE_WCSRTOMBS
45 #include <wchar.h> // for wcsrtombs(), see comments where it's used
48 #ifdef WXSTRING_IS_WXOBJECT
49 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
50 #endif //WXSTRING_IS_WXOBJECT
52 // allocating extra space for each string consumes more memory but speeds up
53 // the concatenation operations (nLen is the current string's length)
54 // NB: EXTRA_ALLOC must be >= 0!
55 #define EXTRA_ALLOC (19 - nLen % 16)
57 // ---------------------------------------------------------------------------
58 // static class variables definition
59 // ---------------------------------------------------------------------------
61 #ifdef STD_STRING_COMPATIBILITY
62 const size_t wxString::npos
= STRING_MAXLEN
;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // for an empty string, GetStringData() will return this address: this
70 // structure has the same layout as wxStringData and it's data() method will
71 // return the empty string (dummy pointer)
76 } g_strEmpty
= { {-1, 0, 0}, '\0' };
78 // empty C style string: points to 'string data' byte of g_strEmpty
79 extern const char *g_szNul
= &g_strEmpty
.dummy
;
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 #ifdef STD_STRING_COMPATIBILITY
87 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
90 // ATTN: you can _not_ use both of these in the same program!
93 #define NAMESPACE std::
100 #include <iostream.h>
107 #define NAMESPACE std::
111 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
116 NAMESPACE streambuf
*sb
= is
.rdbuf();
119 int ch
= sb
->sbumpc ();
121 is
.setstate(NAMESPACE
ios::eofbit
);
124 else if ( isspace(ch
) ) {
136 if ( str
.length() == 0 )
137 is
.setstate(NAMESPACE
ios::failbit
);
142 #endif //std::string compatibility
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 // this small class is used to gather statistics for performance tuning
149 //#define WXSTRING_STATISTICS
150 #ifdef WXSTRING_STATISTICS
154 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
156 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
158 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
161 size_t m_nCount
, m_nTotal
;
163 } g_averageLength("allocation size"),
164 g_averageSummandLength("summand length"),
165 g_averageConcatHit("hit probability in concat"),
166 g_averageInitialLength("initial string length");
168 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
170 #define STATISTICS_ADD(av, val)
171 #endif // WXSTRING_STATISTICS
173 // ===========================================================================
174 // wxString class core
175 // ===========================================================================
177 // ---------------------------------------------------------------------------
179 // ---------------------------------------------------------------------------
181 // constructs string of <nLength> copies of character <ch>
182 wxString::wxString(char ch
, size_t nLength
)
187 AllocBuffer(nLength
);
189 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
191 memset(m_pchData
, ch
, nLength
);
195 // takes nLength elements of psz starting at nPos
196 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
200 wxASSERT( nPos
<= Strlen(psz
) );
202 if ( nLength
== STRING_MAXLEN
)
203 nLength
= Strlen(psz
+ nPos
);
205 STATISTICS_ADD(InitialLength
, nLength
);
208 // trailing '\0' is written in AllocBuffer()
209 AllocBuffer(nLength
);
210 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
214 // the same as previous constructor, but for compilers using unsigned char
215 wxString::wxString(const unsigned char* psz
, size_t nLength
)
217 InitWith((const char *)psz
, 0, nLength
);
220 #ifdef STD_STRING_COMPATIBILITY
222 // poor man's iterators are "void *" pointers
223 wxString::wxString(const void *pStart
, const void *pEnd
)
225 InitWith((const char *)pStart
, 0,
226 (const char *)pEnd
- (const char *)pStart
);
229 #endif //std::string compatibility
232 wxString::wxString(const wchar_t *pwz
)
234 // first get necessary size
236 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
237 // honor the 3rd parameter, thus it will happily crash here).
238 #ifdef wxUSE_WCSRTOMBS
239 // don't know if it's really needed (or if we can pass NULL), but better safe
242 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
244 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
250 wcstombs(m_pchData
, pwz
, nLen
);
257 // ---------------------------------------------------------------------------
259 // ---------------------------------------------------------------------------
261 // allocates memory needed to store a C string of length nLen
262 void wxString::AllocBuffer(size_t nLen
)
264 wxASSERT( nLen
> 0 ); //
265 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
267 STATISTICS_ADD(Length
, nLen
);
270 // 1) one extra character for '\0' termination
271 // 2) sizeof(wxStringData) for housekeeping info
272 wxStringData
* pData
= (wxStringData
*)
273 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
275 pData
->nDataLength
= nLen
;
276 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
277 m_pchData
= pData
->data(); // data starts after wxStringData
278 m_pchData
[nLen
] = '\0';
281 // must be called before changing this string
282 void wxString::CopyBeforeWrite()
284 wxStringData
* pData
= GetStringData();
286 if ( pData
->IsShared() ) {
287 pData
->Unlock(); // memory not freed because shared
288 size_t nLen
= pData
->nDataLength
;
290 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
293 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
296 // must be called before replacing contents of this string
297 void wxString::AllocBeforeWrite(size_t nLen
)
299 wxASSERT( nLen
!= 0 ); // doesn't make any sense
301 // must not share string and must have enough space
302 wxStringData
* pData
= GetStringData();
303 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
304 // can't work with old buffer, get new one
309 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
312 // allocate enough memory for nLen characters
313 void wxString::Alloc(size_t nLen
)
315 wxStringData
*pData
= GetStringData();
316 if ( pData
->nAllocLength
<= nLen
) {
317 if ( pData
->IsEmpty() ) {
320 wxStringData
* pData
= (wxStringData
*)
321 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
323 pData
->nDataLength
= 0;
324 pData
->nAllocLength
= nLen
;
325 m_pchData
= pData
->data(); // data starts after wxStringData
326 m_pchData
[0u] = '\0';
328 else if ( pData
->IsShared() ) {
329 pData
->Unlock(); // memory not freed because shared
330 size_t nOldLen
= pData
->nDataLength
;
332 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
337 wxStringData
*p
= (wxStringData
*)
338 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
341 // @@@ what to do on memory error?
345 // it's not important if the pointer changed or not (the check for this
346 // is not faster than assigning to m_pchData in all cases)
347 p
->nAllocLength
= nLen
;
348 m_pchData
= p
->data();
351 //else: we've already got enough
354 // shrink to minimal size (releasing extra memory)
355 void wxString::Shrink()
357 wxStringData
*pData
= GetStringData();
359 // this variable is unused in release build, so avoid the compiler warning by
360 // just not declaring it
364 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
366 wxASSERT( p
!= NULL
); // can't free memory?
367 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
370 // get the pointer to writable buffer of (at least) nLen bytes
371 char *wxString::GetWriteBuf(size_t nLen
)
373 AllocBeforeWrite(nLen
);
375 wxASSERT( GetStringData()->nRefs
== 1 );
376 GetStringData()->Validate(FALSE
);
381 // put string back in a reasonable state after GetWriteBuf
382 void wxString::UngetWriteBuf()
384 GetStringData()->nDataLength
= strlen(m_pchData
);
385 GetStringData()->Validate(TRUE
);
388 // ---------------------------------------------------------------------------
390 // ---------------------------------------------------------------------------
392 // all functions are inline in string.h
394 // ---------------------------------------------------------------------------
395 // assignment operators
396 // ---------------------------------------------------------------------------
398 // helper function: does real copy
399 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
401 if ( nSrcLen
== 0 ) {
405 AllocBeforeWrite(nSrcLen
);
406 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
407 GetStringData()->nDataLength
= nSrcLen
;
408 m_pchData
[nSrcLen
] = '\0';
412 // assigns one string to another
413 wxString
& wxString::operator=(const wxString
& stringSrc
)
415 wxASSERT( stringSrc
.GetStringData()->IsValid() );
417 // don't copy string over itself
418 if ( m_pchData
!= stringSrc
.m_pchData
) {
419 if ( stringSrc
.GetStringData()->IsEmpty() ) {
424 GetStringData()->Unlock();
425 m_pchData
= stringSrc
.m_pchData
;
426 GetStringData()->Lock();
433 // assigns a single character
434 wxString
& wxString::operator=(char ch
)
441 wxString
& wxString::operator=(const char *psz
)
443 AssignCopy(Strlen(psz
), psz
);
447 // same as 'signed char' variant
448 wxString
& wxString::operator=(const unsigned char* psz
)
450 *this = (const char *)psz
;
454 wxString
& wxString::operator=(const wchar_t *pwz
)
461 // ---------------------------------------------------------------------------
462 // string concatenation
463 // ---------------------------------------------------------------------------
465 // add something to this string
466 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
468 STATISTICS_ADD(SummandLength
, nSrcLen
);
470 // concatenating an empty string is a NOP
472 wxStringData
*pData
= GetStringData();
473 size_t nLen
= pData
->nDataLength
;
474 size_t nNewLen
= nLen
+ nSrcLen
;
476 // alloc new buffer if current is too small
477 if ( pData
->IsShared() ) {
478 STATISTICS_ADD(ConcatHit
, 0);
480 // we have to allocate another buffer
481 wxStringData
* pOldData
= GetStringData();
482 AllocBuffer(nNewLen
);
483 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
486 else if ( nNewLen
> pData
->nAllocLength
) {
487 STATISTICS_ADD(ConcatHit
, 0);
489 // we have to grow the buffer
493 STATISTICS_ADD(ConcatHit
, 1);
495 // the buffer is already big enough
498 // should be enough space
499 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
501 // fast concatenation - all is done in our buffer
502 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
504 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
505 GetStringData()->nDataLength
= nNewLen
; // and fix the length
507 //else: the string to append was empty
511 * concatenation functions come in 5 flavours:
513 * char + string and string + char
514 * C str + string and string + C str
517 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
519 wxASSERT( string1
.GetStringData()->IsValid() );
520 wxASSERT( string2
.GetStringData()->IsValid() );
522 wxString s
= string1
;
528 wxString
operator+(const wxString
& string
, char ch
)
530 wxASSERT( string
.GetStringData()->IsValid() );
538 wxString
operator+(char ch
, const wxString
& string
)
540 wxASSERT( string
.GetStringData()->IsValid() );
548 wxString
operator+(const wxString
& string
, const char *psz
)
550 wxASSERT( string
.GetStringData()->IsValid() );
553 s
.Alloc(Strlen(psz
) + string
.Len());
560 wxString
operator+(const char *psz
, const wxString
& string
)
562 wxASSERT( string
.GetStringData()->IsValid() );
565 s
.Alloc(Strlen(psz
) + string
.Len());
572 // ===========================================================================
573 // other common string functions
574 // ===========================================================================
576 // ---------------------------------------------------------------------------
577 // simple sub-string extraction
578 // ---------------------------------------------------------------------------
580 // helper function: clone the data attached to this string
581 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
583 if ( nCopyLen
== 0 ) {
587 dest
.AllocBuffer(nCopyLen
);
588 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
592 // extract string of length nCount starting at nFirst
593 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
595 wxStringData
*pData
= GetStringData();
596 size_t nLen
= pData
->nDataLength
;
598 // default value of nCount is STRING_MAXLEN and means "till the end"
599 if ( nCount
== STRING_MAXLEN
)
601 nCount
= nLen
- nFirst
;
604 // out-of-bounds requests return sensible things
605 if ( nFirst
+ nCount
> nLen
)
607 nCount
= nLen
- nFirst
;
612 // AllocCopy() will return empty string
617 AllocCopy(dest
, nCount
, nFirst
);
622 // extract nCount last (rightmost) characters
623 wxString
wxString::Right(size_t nCount
) const
625 if ( nCount
> (size_t)GetStringData()->nDataLength
)
626 nCount
= GetStringData()->nDataLength
;
629 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
633 // get all characters after the last occurence of ch
634 // (returns the whole string if ch not found)
635 wxString
wxString::Right(char ch
) const
638 int iPos
= Find(ch
, TRUE
);
639 if ( iPos
== NOT_FOUND
)
642 str
= c_str() + iPos
+ 1;
647 // extract nCount first (leftmost) characters
648 wxString
wxString::Left(size_t nCount
) const
650 if ( nCount
> (size_t)GetStringData()->nDataLength
)
651 nCount
= GetStringData()->nDataLength
;
654 AllocCopy(dest
, nCount
, 0);
658 // get all characters before the first occurence of ch
659 // (returns the whole string if ch not found)
660 wxString
wxString::Left(char ch
) const
663 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
669 /// get all characters before the last occurence of ch
670 /// (returns empty string if ch not found)
671 wxString
wxString::Before(char ch
) const
674 int iPos
= Find(ch
, TRUE
);
675 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
676 str
= wxString(c_str(), iPos
);
681 /// get all characters after the first occurence of ch
682 /// (returns empty string if ch not found)
683 wxString
wxString::After(char ch
) const
687 if ( iPos
!= NOT_FOUND
)
688 str
= c_str() + iPos
+ 1;
693 // replace first (or all) occurences of some substring with another one
694 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
696 size_t uiCount
= 0; // count of replacements made
698 size_t uiOldLen
= Strlen(szOld
);
701 const char *pCurrent
= m_pchData
;
703 while ( *pCurrent
!= '\0' ) {
704 pSubstr
= strstr(pCurrent
, szOld
);
705 if ( pSubstr
== NULL
) {
706 // strTemp is unused if no replacements were made, so avoid the copy
710 strTemp
+= pCurrent
; // copy the rest
711 break; // exit the loop
714 // take chars before match
715 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
717 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
722 if ( !bReplaceAll
) {
723 strTemp
+= pCurrent
; // copy the rest
724 break; // exit the loop
729 // only done if there were replacements, otherwise would have returned above
735 bool wxString::IsAscii() const
737 const char *s
= (const char*) *this;
739 if(!isascii(*s
)) return(FALSE
);
745 bool wxString::IsWord() const
747 const char *s
= (const char*) *this;
749 if(!isalpha(*s
)) return(FALSE
);
755 bool wxString::IsNumber() const
757 const char *s
= (const char*) *this;
759 if(!isdigit(*s
)) return(FALSE
);
765 wxString
wxString::Strip(stripType w
) const
768 if ( w
& leading
) s
.Trim(FALSE
);
769 if ( w
& trailing
) s
.Trim(TRUE
);
773 // ---------------------------------------------------------------------------
775 // ---------------------------------------------------------------------------
777 wxString
& wxString::MakeUpper()
781 for ( char *p
= m_pchData
; *p
; p
++ )
782 *p
= (char)toupper(*p
);
787 wxString
& wxString::MakeLower()
791 for ( char *p
= m_pchData
; *p
; p
++ )
792 *p
= (char)tolower(*p
);
797 // ---------------------------------------------------------------------------
798 // trimming and padding
799 // ---------------------------------------------------------------------------
801 // trims spaces (in the sense of isspace) from left or right side
802 wxString
& wxString::Trim(bool bFromRight
)
804 // first check if we're going to modify the string at all
807 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
808 (!bFromRight
&& isspace(GetChar(0u)))
812 // ok, there is at least one space to trim
817 // find last non-space character
818 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
819 while ( isspace(*psz
) && (psz
>= m_pchData
) )
822 // truncate at trailing space start
824 GetStringData()->nDataLength
= psz
- m_pchData
;
828 // find first non-space character
829 const char *psz
= m_pchData
;
830 while ( isspace(*psz
) )
833 // fix up data and length
834 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
835 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
836 GetStringData()->nDataLength
= nDataLength
;
843 // adds nCount characters chPad to the string from either side
844 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
846 wxString
s(chPad
, nCount
);
859 // truncate the string
860 wxString
& wxString::Truncate(size_t uiLen
)
862 *(m_pchData
+ uiLen
) = '\0';
863 GetStringData()->nDataLength
= uiLen
;
868 // ---------------------------------------------------------------------------
869 // finding (return NOT_FOUND if not found and index otherwise)
870 // ---------------------------------------------------------------------------
873 int wxString::Find(char ch
, bool bFromEnd
) const
875 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
877 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
880 // find a sub-string (like strstr)
881 int wxString::Find(const char *pszSub
) const
883 const char *psz
= strstr(m_pchData
, pszSub
);
885 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
888 // ---------------------------------------------------------------------------
890 // ---------------------------------------------------------------------------
891 int wxString::Printf(const char *pszFormat
, ...)
894 va_start(argptr
, pszFormat
);
896 int iLen
= PrintfV(pszFormat
, argptr
);
903 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
905 static char s_szScratch
[1024];
907 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
908 AllocBeforeWrite(iLen
);
909 strcpy(m_pchData
, s_szScratch
);
914 // ----------------------------------------------------------------------------
915 // misc other operations
916 // ----------------------------------------------------------------------------
917 bool wxString::Matches(const char *pszMask
) const
919 // check char by char
921 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
922 switch ( *pszMask
) {
924 if ( *pszTxt
== '\0' )
933 // ignore special chars immediately following this one
934 while ( *pszMask
== '*' || *pszMask
== '?' )
937 // if there is nothing more, match
938 if ( *pszMask
== '\0' )
941 // are there any other metacharacters in the mask?
943 const char *pEndMask
= strpbrk(pszMask
, "*?");
945 if ( pEndMask
!= NULL
) {
946 // we have to match the string between two metachars
947 uiLenMask
= pEndMask
- pszMask
;
950 // we have to match the remainder of the string
951 uiLenMask
= strlen(pszMask
);
954 wxString
strToMatch(pszMask
, uiLenMask
);
955 const char* pMatch
= strstr(pszTxt
, strToMatch
);
956 if ( pMatch
== NULL
)
959 // -1 to compensate "++" in the loop
960 pszTxt
= pMatch
+ uiLenMask
- 1;
961 pszMask
+= uiLenMask
- 1;
966 if ( *pszMask
!= *pszTxt
)
972 // match only if nothing left
973 return *pszTxt
== '\0';
976 // ---------------------------------------------------------------------------
977 // standard C++ library string functions
978 // ---------------------------------------------------------------------------
979 #ifdef STD_STRING_COMPATIBILITY
981 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
983 wxASSERT( str
.GetStringData()->IsValid() );
984 wxASSERT( nPos
<= Len() );
986 if ( !str
.IsEmpty() ) {
988 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
989 strncpy(pc
, c_str(), nPos
);
990 strcpy(pc
+ nPos
, str
);
991 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
992 strTmp
.UngetWriteBuf();
999 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1001 wxASSERT( str
.GetStringData()->IsValid() );
1002 wxASSERT( nStart
<= Len() );
1004 const char *p
= strstr(c_str() + nStart
, str
);
1006 return p
== NULL
? npos
: p
- c_str();
1009 // VC++ 1.5 can't cope with the default argument in the header.
1010 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1011 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1013 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1017 size_t wxString::find(char ch
, size_t nStart
) const
1019 wxASSERT( nStart
<= Len() );
1021 const char *p
= strchr(c_str() + nStart
, ch
);
1023 return p
== NULL
? npos
: p
- c_str();
1026 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1028 wxASSERT( str
.GetStringData()->IsValid() );
1029 wxASSERT( nStart
<= Len() );
1031 // # could be quicker than that
1032 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1033 while ( p
>= c_str() + str
.Len() ) {
1034 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1035 return p
- str
.Len() - c_str();
1042 // VC++ 1.5 can't cope with the default argument in the header.
1043 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1044 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1046 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1049 size_t wxString::rfind(char ch
, size_t nStart
) const
1051 wxASSERT( nStart
<= Len() );
1053 const char *p
= strrchr(c_str() + nStart
, ch
);
1055 return p
== NULL
? npos
: p
- c_str();
1059 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1061 // npos means 'take all'
1065 wxASSERT( nStart
+ nLen
<= Len() );
1067 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1070 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1072 wxString
strTmp(c_str(), nStart
);
1073 if ( nLen
!= npos
) {
1074 wxASSERT( nStart
+ nLen
<= Len() );
1076 strTmp
.append(c_str() + nStart
+ nLen
);
1083 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1085 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1089 strTmp
.append(c_str(), nStart
);
1091 strTmp
.append(c_str() + nStart
+ nLen
);
1097 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1099 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1102 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1103 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1105 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1108 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1109 const char* sz
, size_t nCount
)
1111 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1114 #endif //std::string compatibility
1116 // ============================================================================
1118 // ============================================================================
1120 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1121 #define ARRAY_MAXSIZE_INCREMENT 4096
1122 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1123 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1126 #define STRING(p) ((wxString *)(&(p)))
1129 wxArrayString::wxArrayString()
1133 m_pItems
= (char **) NULL
;
1137 wxArrayString::wxArrayString(const wxArrayString
& src
)
1141 m_pItems
= (char **) NULL
;
1146 // assignment operator
1147 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1152 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1153 Alloc(src
.m_nCount
);
1155 // we can't just copy the pointers here because otherwise we would share
1156 // the strings with another array
1157 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1160 if ( m_nCount
!= 0 )
1161 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1167 void wxArrayString::Grow()
1169 // only do it if no more place
1170 if( m_nCount
== m_nSize
) {
1171 if( m_nSize
== 0 ) {
1172 // was empty, alloc some memory
1173 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1174 m_pItems
= new char *[m_nSize
];
1177 // otherwise when it's called for the first time, nIncrement would be 0
1178 // and the array would never be expanded
1179 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1181 // add 50% but not too much
1182 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1183 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1184 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1185 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1186 m_nSize
+= nIncrement
;
1187 char **pNew
= new char *[m_nSize
];
1189 // copy data to new location
1190 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1192 // delete old memory (but do not release the strings!)
1193 wxDELETEA(m_pItems
);
1200 void wxArrayString::Free()
1202 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1203 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1207 // deletes all the strings from the list
1208 void wxArrayString::Empty()
1215 // as Empty, but also frees memory
1216 void wxArrayString::Clear()
1223 wxDELETEA(m_pItems
);
1227 wxArrayString::~wxArrayString()
1231 wxDELETEA(m_pItems
);
1234 // pre-allocates memory (frees the previous data!)
1235 void wxArrayString::Alloc(size_t nSize
)
1237 wxASSERT( nSize
> 0 );
1239 // only if old buffer was not big enough
1240 if ( nSize
> m_nSize
) {
1242 wxDELETEA(m_pItems
);
1243 m_pItems
= new char *[nSize
];
1250 // searches the array for an item (forward or backwards)
1251 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1254 if ( m_nCount
> 0 ) {
1255 size_t ui
= m_nCount
;
1257 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1264 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1265 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1273 // add item at the end
1274 void wxArrayString::Add(const wxString
& str
)
1276 wxASSERT( str
.GetStringData()->IsValid() );
1280 // the string data must not be deleted!
1281 str
.GetStringData()->Lock();
1282 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1285 // add item at the given position
1286 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1288 wxASSERT( str
.GetStringData()->IsValid() );
1290 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1294 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1295 (m_nCount
- nIndex
)*sizeof(char *));
1297 str
.GetStringData()->Lock();
1298 m_pItems
[nIndex
] = (char *)str
.c_str();
1303 // removes item from array (by index)
1304 void wxArrayString::Remove(size_t nIndex
)
1306 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1309 Item(nIndex
).GetStringData()->Unlock();
1311 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1312 (m_nCount
- nIndex
- 1)*sizeof(char *));
1316 // removes item from array (by value)
1317 void wxArrayString::Remove(const char *sz
)
1319 int iIndex
= Index(sz
);
1321 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1322 _("removing inexistent element in wxArrayString::Remove") );
1327 // sort array elements using passed comparaison function
1329 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1332 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);