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::
99 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
104 NAMESPACE streambuf
*sb
= is
.rdbuf();
107 int ch
= sb
->sbumpc ();
109 is
.setstate(NAMESPACE
ios::eofbit
);
112 else if ( isspace(ch
) ) {
124 if ( str
.length() == 0 )
125 is
.setstate(NAMESPACE
ios::failbit
);
130 #endif //std::string compatibility
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 // this small class is used to gather statistics for performance tuning
137 //#define WXSTRING_STATISTICS
138 #ifdef WXSTRING_STATISTICS
142 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
144 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
146 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
149 size_t m_nCount
, m_nTotal
;
151 } g_averageLength("allocation size"),
152 g_averageSummandLength("summand length"),
153 g_averageConcatHit("hit probability in concat"),
154 g_averageInitialLength("initial string length");
156 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
158 #define STATISTICS_ADD(av, val)
159 #endif // WXSTRING_STATISTICS
161 // ===========================================================================
162 // wxString class core
163 // ===========================================================================
165 // ---------------------------------------------------------------------------
167 // ---------------------------------------------------------------------------
169 // constructs string of <nLength> copies of character <ch>
170 wxString::wxString(char ch
, size_t nLength
)
175 AllocBuffer(nLength
);
177 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
179 memset(m_pchData
, ch
, nLength
);
183 // takes nLength elements of psz starting at nPos
184 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
188 wxASSERT( nPos
<= Strlen(psz
) );
190 if ( nLength
== STRING_MAXLEN
)
191 nLength
= Strlen(psz
+ nPos
);
193 STATISTICS_ADD(InitialLength
, nLength
);
196 // trailing '\0' is written in AllocBuffer()
197 AllocBuffer(nLength
);
198 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
202 // the same as previous constructor, but for compilers using unsigned char
203 wxString::wxString(const unsigned char* psz
, size_t nLength
)
205 InitWith((const char *)psz
, 0, nLength
);
208 #ifdef STD_STRING_COMPATIBILITY
210 // poor man's iterators are "void *" pointers
211 wxString::wxString(const void *pStart
, const void *pEnd
)
213 InitWith((const char *)pStart
, 0,
214 (const char *)pEnd
- (const char *)pStart
);
217 #endif //std::string compatibility
220 wxString::wxString(const wchar_t *pwz
)
222 // first get necessary size
224 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
225 // honor the 3rd parameter, thus it will happily crash here).
226 #ifdef wxUSE_WCSRTOMBS
227 // don't know if it's really needed (or if we can pass NULL), but better safe
230 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
232 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
238 wcstombs(m_pchData
, pwz
, nLen
);
245 // ---------------------------------------------------------------------------
247 // ---------------------------------------------------------------------------
249 // allocates memory needed to store a C string of length nLen
250 void wxString::AllocBuffer(size_t nLen
)
252 wxASSERT( nLen
> 0 ); //
253 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
255 STATISTICS_ADD(Length
, nLen
);
258 // 1) one extra character for '\0' termination
259 // 2) sizeof(wxStringData) for housekeeping info
260 wxStringData
* pData
= (wxStringData
*)
261 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
263 pData
->nDataLength
= nLen
;
264 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
265 m_pchData
= pData
->data(); // data starts after wxStringData
266 m_pchData
[nLen
] = '\0';
269 // must be called before changing this string
270 void wxString::CopyBeforeWrite()
272 wxStringData
* pData
= GetStringData();
274 if ( pData
->IsShared() ) {
275 pData
->Unlock(); // memory not freed because shared
276 size_t nLen
= pData
->nDataLength
;
278 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
281 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
284 // must be called before replacing contents of this string
285 void wxString::AllocBeforeWrite(size_t nLen
)
287 wxASSERT( nLen
!= 0 ); // doesn't make any sense
289 // must not share string and must have enough space
290 wxStringData
* pData
= GetStringData();
291 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
292 // can't work with old buffer, get new one
297 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
300 // allocate enough memory for nLen characters
301 void wxString::Alloc(size_t nLen
)
303 wxStringData
*pData
= GetStringData();
304 if ( pData
->nAllocLength
<= nLen
) {
305 if ( pData
->IsEmpty() ) {
308 wxStringData
* pData
= (wxStringData
*)
309 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
311 pData
->nDataLength
= 0;
312 pData
->nAllocLength
= nLen
;
313 m_pchData
= pData
->data(); // data starts after wxStringData
314 m_pchData
[0u] = '\0';
316 else if ( pData
->IsShared() ) {
317 pData
->Unlock(); // memory not freed because shared
318 size_t nOldLen
= pData
->nDataLength
;
320 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
325 wxStringData
*p
= (wxStringData
*)
326 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
329 // @@@ what to do on memory error?
333 // it's not important if the pointer changed or not (the check for this
334 // is not faster than assigning to m_pchData in all cases)
335 p
->nAllocLength
= nLen
;
336 m_pchData
= p
->data();
339 //else: we've already got enough
342 // shrink to minimal size (releasing extra memory)
343 void wxString::Shrink()
345 wxStringData
*pData
= GetStringData();
347 // this variable is unused in release build, so avoid the compiler warning by
348 // just not declaring it
352 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
354 wxASSERT( p
!= NULL
); // can't free memory?
355 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
358 // get the pointer to writable buffer of (at least) nLen bytes
359 char *wxString::GetWriteBuf(size_t nLen
)
361 AllocBeforeWrite(nLen
);
363 wxASSERT( GetStringData()->nRefs
== 1 );
364 GetStringData()->Validate(FALSE
);
369 // put string back in a reasonable state after GetWriteBuf
370 void wxString::UngetWriteBuf()
372 GetStringData()->nDataLength
= strlen(m_pchData
);
373 GetStringData()->Validate(TRUE
);
376 // ---------------------------------------------------------------------------
378 // ---------------------------------------------------------------------------
380 // all functions are inline in string.h
382 // ---------------------------------------------------------------------------
383 // assignment operators
384 // ---------------------------------------------------------------------------
386 // helper function: does real copy
387 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
389 if ( nSrcLen
== 0 ) {
393 AllocBeforeWrite(nSrcLen
);
394 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
395 GetStringData()->nDataLength
= nSrcLen
;
396 m_pchData
[nSrcLen
] = '\0';
400 // assigns one string to another
401 wxString
& wxString::operator=(const wxString
& stringSrc
)
403 wxASSERT( stringSrc
.GetStringData()->IsValid() );
405 // don't copy string over itself
406 if ( m_pchData
!= stringSrc
.m_pchData
) {
407 if ( stringSrc
.GetStringData()->IsEmpty() ) {
412 GetStringData()->Unlock();
413 m_pchData
= stringSrc
.m_pchData
;
414 GetStringData()->Lock();
421 // assigns a single character
422 wxString
& wxString::operator=(char ch
)
429 wxString
& wxString::operator=(const char *psz
)
431 AssignCopy(Strlen(psz
), psz
);
435 // same as 'signed char' variant
436 wxString
& wxString::operator=(const unsigned char* psz
)
438 *this = (const char *)psz
;
442 wxString
& wxString::operator=(const wchar_t *pwz
)
449 // ---------------------------------------------------------------------------
450 // string concatenation
451 // ---------------------------------------------------------------------------
453 // add something to this string
454 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
456 STATISTICS_ADD(SummandLength
, nSrcLen
);
458 // concatenating an empty string is a NOP
460 wxStringData
*pData
= GetStringData();
461 size_t nLen
= pData
->nDataLength
;
462 size_t nNewLen
= nLen
+ nSrcLen
;
464 // alloc new buffer if current is too small
465 if ( pData
->IsShared() ) {
466 STATISTICS_ADD(ConcatHit
, 0);
468 // we have to allocate another buffer
469 wxStringData
* pOldData
= GetStringData();
470 AllocBuffer(nNewLen
);
471 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
474 else if ( nNewLen
> pData
->nAllocLength
) {
475 STATISTICS_ADD(ConcatHit
, 0);
477 // we have to grow the buffer
481 STATISTICS_ADD(ConcatHit
, 1);
483 // the buffer is already big enough
486 // should be enough space
487 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
489 // fast concatenation - all is done in our buffer
490 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
492 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
493 GetStringData()->nDataLength
= nNewLen
; // and fix the length
495 //else: the string to append was empty
499 * concatenation functions come in 5 flavours:
501 * char + string and string + char
502 * C str + string and string + C str
505 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
507 wxASSERT( string1
.GetStringData()->IsValid() );
508 wxASSERT( string2
.GetStringData()->IsValid() );
510 wxString s
= string1
;
516 wxString
operator+(const wxString
& string
, char ch
)
518 wxASSERT( string
.GetStringData()->IsValid() );
526 wxString
operator+(char ch
, const wxString
& string
)
528 wxASSERT( string
.GetStringData()->IsValid() );
536 wxString
operator+(const wxString
& string
, const char *psz
)
538 wxASSERT( string
.GetStringData()->IsValid() );
541 s
.Alloc(Strlen(psz
) + string
.Len());
548 wxString
operator+(const char *psz
, const wxString
& string
)
550 wxASSERT( string
.GetStringData()->IsValid() );
553 s
.Alloc(Strlen(psz
) + string
.Len());
560 // ===========================================================================
561 // other common string functions
562 // ===========================================================================
564 // ---------------------------------------------------------------------------
565 // simple sub-string extraction
566 // ---------------------------------------------------------------------------
568 // helper function: clone the data attached to this string
569 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
571 if ( nCopyLen
== 0 ) {
575 dest
.AllocBuffer(nCopyLen
);
576 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
580 // extract string of length nCount starting at nFirst
581 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
583 wxStringData
*pData
= GetStringData();
584 size_t nLen
= pData
->nDataLength
;
586 // default value of nCount is STRING_MAXLEN and means "till the end"
587 if ( nCount
== STRING_MAXLEN
)
589 nCount
= nLen
- nFirst
;
592 // out-of-bounds requests return sensible things
593 if ( nFirst
+ nCount
> nLen
)
595 nCount
= nLen
- nFirst
;
600 // AllocCopy() will return empty string
605 AllocCopy(dest
, nCount
, nFirst
);
610 // extract nCount last (rightmost) characters
611 wxString
wxString::Right(size_t nCount
) const
613 if ( nCount
> (size_t)GetStringData()->nDataLength
)
614 nCount
= GetStringData()->nDataLength
;
617 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
621 // get all characters after the last occurence of ch
622 // (returns the whole string if ch not found)
623 wxString
wxString::Right(char ch
) const
626 int iPos
= Find(ch
, TRUE
);
627 if ( iPos
== NOT_FOUND
)
630 str
= c_str() + iPos
+ 1;
635 // extract nCount first (leftmost) characters
636 wxString
wxString::Left(size_t nCount
) const
638 if ( nCount
> (size_t)GetStringData()->nDataLength
)
639 nCount
= GetStringData()->nDataLength
;
642 AllocCopy(dest
, nCount
, 0);
646 // get all characters before the first occurence of ch
647 // (returns the whole string if ch not found)
648 wxString
wxString::Left(char ch
) const
651 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
657 /// get all characters before the last occurence of ch
658 /// (returns empty string if ch not found)
659 wxString
wxString::Before(char ch
) const
662 int iPos
= Find(ch
, TRUE
);
663 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
664 str
= wxString(c_str(), iPos
);
669 /// get all characters after the first occurence of ch
670 /// (returns empty string if ch not found)
671 wxString
wxString::After(char ch
) const
675 if ( iPos
!= NOT_FOUND
)
676 str
= c_str() + iPos
+ 1;
681 // replace first (or all) occurences of some substring with another one
682 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
684 size_t uiCount
= 0; // count of replacements made
686 size_t uiOldLen
= Strlen(szOld
);
689 const char *pCurrent
= m_pchData
;
691 while ( *pCurrent
!= '\0' ) {
692 pSubstr
= strstr(pCurrent
, szOld
);
693 if ( pSubstr
== NULL
) {
694 // strTemp is unused if no replacements were made, so avoid the copy
698 strTemp
+= pCurrent
; // copy the rest
699 break; // exit the loop
702 // take chars before match
703 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
705 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
710 if ( !bReplaceAll
) {
711 strTemp
+= pCurrent
; // copy the rest
712 break; // exit the loop
717 // only done if there were replacements, otherwise would have returned above
723 bool wxString::IsAscii() const
725 const char *s
= (const char*) *this;
727 if(!isascii(*s
)) return(FALSE
);
733 bool wxString::IsWord() const
735 const char *s
= (const char*) *this;
737 if(!isalpha(*s
)) return(FALSE
);
743 bool wxString::IsNumber() const
745 const char *s
= (const char*) *this;
747 if(!isdigit(*s
)) return(FALSE
);
753 wxString
wxString::Strip(stripType w
) const
756 if ( w
& leading
) s
.Trim(FALSE
);
757 if ( w
& trailing
) s
.Trim(TRUE
);
761 // ---------------------------------------------------------------------------
763 // ---------------------------------------------------------------------------
765 wxString
& wxString::MakeUpper()
769 for ( char *p
= m_pchData
; *p
; p
++ )
770 *p
= (char)toupper(*p
);
775 wxString
& wxString::MakeLower()
779 for ( char *p
= m_pchData
; *p
; p
++ )
780 *p
= (char)tolower(*p
);
785 // ---------------------------------------------------------------------------
786 // trimming and padding
787 // ---------------------------------------------------------------------------
789 // trims spaces (in the sense of isspace) from left or right side
790 wxString
& wxString::Trim(bool bFromRight
)
792 // first check if we're going to modify the string at all
795 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
796 (!bFromRight
&& isspace(GetChar(0u)))
800 // ok, there is at least one space to trim
805 // find last non-space character
806 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
807 while ( isspace(*psz
) && (psz
>= m_pchData
) )
810 // truncate at trailing space start
812 GetStringData()->nDataLength
= psz
- m_pchData
;
816 // find first non-space character
817 const char *psz
= m_pchData
;
818 while ( isspace(*psz
) )
821 // fix up data and length
822 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
823 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
824 GetStringData()->nDataLength
= nDataLength
;
831 // adds nCount characters chPad to the string from either side
832 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
834 wxString
s(chPad
, nCount
);
847 // truncate the string
848 wxString
& wxString::Truncate(size_t uiLen
)
850 *(m_pchData
+ uiLen
) = '\0';
851 GetStringData()->nDataLength
= uiLen
;
856 // ---------------------------------------------------------------------------
857 // finding (return NOT_FOUND if not found and index otherwise)
858 // ---------------------------------------------------------------------------
861 int wxString::Find(char ch
, bool bFromEnd
) const
863 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
865 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
868 // find a sub-string (like strstr)
869 int wxString::Find(const char *pszSub
) const
871 const char *psz
= strstr(m_pchData
, pszSub
);
873 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
876 // ---------------------------------------------------------------------------
878 // ---------------------------------------------------------------------------
879 int wxString::Printf(const char *pszFormat
, ...)
882 va_start(argptr
, pszFormat
);
884 int iLen
= PrintfV(pszFormat
, argptr
);
891 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
893 static char s_szScratch
[1024];
895 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
896 AllocBeforeWrite(iLen
);
897 strcpy(m_pchData
, s_szScratch
);
902 // ----------------------------------------------------------------------------
903 // misc other operations
904 // ----------------------------------------------------------------------------
905 bool wxString::Matches(const char *pszMask
) const
907 // check char by char
909 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
910 switch ( *pszMask
) {
912 if ( *pszTxt
== '\0' )
921 // ignore special chars immediately following this one
922 while ( *pszMask
== '*' || *pszMask
== '?' )
925 // if there is nothing more, match
926 if ( *pszMask
== '\0' )
929 // are there any other metacharacters in the mask?
931 const char *pEndMask
= strpbrk(pszMask
, "*?");
933 if ( pEndMask
!= NULL
) {
934 // we have to match the string between two metachars
935 uiLenMask
= pEndMask
- pszMask
;
938 // we have to match the remainder of the string
939 uiLenMask
= strlen(pszMask
);
942 wxString
strToMatch(pszMask
, uiLenMask
);
943 const char* pMatch
= strstr(pszTxt
, strToMatch
);
944 if ( pMatch
== NULL
)
947 // -1 to compensate "++" in the loop
948 pszTxt
= pMatch
+ uiLenMask
- 1;
949 pszMask
+= uiLenMask
- 1;
954 if ( *pszMask
!= *pszTxt
)
960 // match only if nothing left
961 return *pszTxt
== '\0';
964 // ---------------------------------------------------------------------------
965 // standard C++ library string functions
966 // ---------------------------------------------------------------------------
967 #ifdef STD_STRING_COMPATIBILITY
969 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
971 wxASSERT( str
.GetStringData()->IsValid() );
972 wxASSERT( nPos
<= Len() );
974 if ( !str
.IsEmpty() ) {
976 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
977 strncpy(pc
, c_str(), nPos
);
978 strcpy(pc
+ nPos
, str
);
979 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
980 strTmp
.UngetWriteBuf();
987 size_t wxString::find(const wxString
& str
, size_t nStart
) const
989 wxASSERT( str
.GetStringData()->IsValid() );
990 wxASSERT( nStart
<= Len() );
992 const char *p
= strstr(c_str() + nStart
, str
);
994 return p
== NULL
? npos
: p
- c_str();
997 // VC++ 1.5 can't cope with the default argument in the header.
998 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
999 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1001 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1005 size_t wxString::find(char ch
, size_t nStart
) const
1007 wxASSERT( nStart
<= Len() );
1009 const char *p
= strchr(c_str() + nStart
, ch
);
1011 return p
== NULL
? npos
: p
- c_str();
1014 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1016 wxASSERT( str
.GetStringData()->IsValid() );
1017 wxASSERT( nStart
<= Len() );
1019 // # could be quicker than that
1020 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1021 while ( p
>= c_str() + str
.Len() ) {
1022 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1023 return p
- str
.Len() - c_str();
1030 // VC++ 1.5 can't cope with the default argument in the header.
1031 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1032 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1034 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1037 size_t wxString::rfind(char ch
, size_t nStart
) const
1039 wxASSERT( nStart
<= Len() );
1041 const char *p
= strrchr(c_str() + nStart
, ch
);
1043 return p
== NULL
? npos
: p
- c_str();
1047 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1049 // npos means 'take all'
1053 wxASSERT( nStart
+ nLen
<= Len() );
1055 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1058 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1060 wxString
strTmp(c_str(), nStart
);
1061 if ( nLen
!= npos
) {
1062 wxASSERT( nStart
+ nLen
<= Len() );
1064 strTmp
.append(c_str() + nStart
+ nLen
);
1071 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1073 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1077 strTmp
.append(c_str(), nStart
);
1079 strTmp
.append(c_str() + nStart
+ nLen
);
1085 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1087 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1090 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1091 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1093 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1096 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1097 const char* sz
, size_t nCount
)
1099 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1102 #endif //std::string compatibility
1104 // ============================================================================
1106 // ============================================================================
1108 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1109 #define ARRAY_MAXSIZE_INCREMENT 4096
1110 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1111 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1114 #define STRING(p) ((wxString *)(&(p)))
1117 wxArrayString::wxArrayString()
1121 m_pItems
= (char **) NULL
;
1125 wxArrayString::wxArrayString(const wxArrayString
& src
)
1129 m_pItems
= (char **) NULL
;
1134 // assignment operator
1135 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1140 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1141 Alloc(src
.m_nCount
);
1143 // we can't just copy the pointers here because otherwise we would share
1144 // the strings with another array
1145 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1148 if ( m_nCount
!= 0 )
1149 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1155 void wxArrayString::Grow()
1157 // only do it if no more place
1158 if( m_nCount
== m_nSize
) {
1159 if( m_nSize
== 0 ) {
1160 // was empty, alloc some memory
1161 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1162 m_pItems
= new char *[m_nSize
];
1165 // otherwise when it's called for the first time, nIncrement would be 0
1166 // and the array would never be expanded
1167 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1169 // add 50% but not too much
1170 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1171 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1172 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1173 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1174 m_nSize
+= nIncrement
;
1175 char **pNew
= new char *[m_nSize
];
1177 // copy data to new location
1178 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1180 // delete old memory (but do not release the strings!)
1181 wxDELETEA(m_pItems
);
1188 void wxArrayString::Free()
1190 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1191 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1195 // deletes all the strings from the list
1196 void wxArrayString::Empty()
1203 // as Empty, but also frees memory
1204 void wxArrayString::Clear()
1211 wxDELETEA(m_pItems
);
1215 wxArrayString::~wxArrayString()
1219 wxDELETEA(m_pItems
);
1222 // pre-allocates memory (frees the previous data!)
1223 void wxArrayString::Alloc(size_t nSize
)
1225 wxASSERT( nSize
> 0 );
1227 // only if old buffer was not big enough
1228 if ( nSize
> m_nSize
) {
1230 wxDELETEA(m_pItems
);
1231 m_pItems
= new char *[nSize
];
1238 // searches the array for an item (forward or backwards)
1239 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1242 if ( m_nCount
> 0 ) {
1243 size_t ui
= m_nCount
;
1245 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1252 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1253 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1261 // add item at the end
1262 void wxArrayString::Add(const wxString
& str
)
1264 wxASSERT( str
.GetStringData()->IsValid() );
1268 // the string data must not be deleted!
1269 str
.GetStringData()->Lock();
1270 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1273 // add item at the given position
1274 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1276 wxASSERT( str
.GetStringData()->IsValid() );
1278 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1282 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1283 (m_nCount
- nIndex
)*sizeof(char *));
1285 str
.GetStringData()->Lock();
1286 m_pItems
[nIndex
] = (char *)str
.c_str();
1291 // removes item from array (by index)
1292 void wxArrayString::Remove(size_t nIndex
)
1294 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1297 Item(nIndex
).GetStringData()->Unlock();
1299 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1300 (m_nCount
- nIndex
- 1)*sizeof(char *));
1304 // removes item from array (by value)
1305 void wxArrayString::Remove(const char *sz
)
1307 int iIndex
= Index(sz
);
1309 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1310 _("removing inexistent element in wxArrayString::Remove") );
1315 // sort array elements using passed comparaison function
1317 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1320 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);