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 WXSTRING_IS_WXOBJECT
45 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
46 #endif //WXSTRING_IS_WXOBJECT
48 // allocating extra space for each string consumes more memory but speeds up
49 // the concatenation operations (nLen is the current string's length)
50 // NB: EXTRA_ALLOC must be >= 0!
51 #define EXTRA_ALLOC (19 - nLen % 16)
53 // ---------------------------------------------------------------------------
54 // static class variables definition
55 // ---------------------------------------------------------------------------
57 #ifdef STD_STRING_COMPATIBILITY
58 const size_t wxString::npos
= STRING_MAXLEN
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // for an empty string, GetStringData() will return this address: this
66 // structure has the same layout as wxStringData and it's data() method will
67 // return the empty string (dummy pointer)
72 } g_strEmpty
= { {-1, 0, 0}, '\0' };
74 // empty C style string: points to 'string data' byte of g_strEmpty
75 extern const char *g_szNul
= &g_strEmpty
.dummy
;
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 #ifdef STD_STRING_COMPATIBILITY
83 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
86 // ATTN: you can _not_ use both of these in the same program!
89 #define NAMESPACE std::
95 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
100 NAMESPACE streambuf
*sb
= is
.rdbuf();
103 int ch
= sb
->sbumpc ();
105 is
.setstate(NAMESPACE
ios::eofbit
);
108 else if ( isspace(ch
) ) {
120 if ( str
.length() == 0 )
121 is
.setstate(NAMESPACE
ios::failbit
);
126 #endif //std::string compatibility
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 // this small class is used to gather statistics for performance tuning
133 //#define WXSTRING_STATISTICS
134 #ifdef WXSTRING_STATISTICS
138 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
140 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
142 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
145 size_t m_nCount
, m_nTotal
;
147 } g_averageLength("allocation size"),
148 g_averageSummandLength("summand length"),
149 g_averageConcatHit("hit probability in concat"),
150 g_averageInitialLength("initial string length");
152 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
154 #define STATISTICS_ADD(av, val)
155 #endif // WXSTRING_STATISTICS
157 // ===========================================================================
158 // wxString class core
159 // ===========================================================================
161 // ---------------------------------------------------------------------------
163 // ---------------------------------------------------------------------------
165 // constructs string of <nLength> copies of character <ch>
166 wxString::wxString(char ch
, size_t nLength
)
171 AllocBuffer(nLength
);
173 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
175 memset(m_pchData
, ch
, nLength
);
179 // takes nLength elements of psz starting at nPos
180 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
184 wxASSERT( nPos
<= Strlen(psz
) );
186 if ( nLength
== STRING_MAXLEN
)
187 nLength
= Strlen(psz
+ nPos
);
189 STATISTICS_ADD(InitialLength
, nLength
);
192 // trailing '\0' is written in AllocBuffer()
193 AllocBuffer(nLength
);
194 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
198 // the same as previous constructor, but for compilers using unsigned char
199 wxString::wxString(const unsigned char* psz
, size_t nLength
)
201 InitWith((const char *)psz
, 0, nLength
);
204 #ifdef STD_STRING_COMPATIBILITY
206 // poor man's iterators are "void *" pointers
207 wxString::wxString(const void *pStart
, const void *pEnd
)
209 InitWith((const char *)pStart
, 0,
210 (const char *)pEnd
- (const char *)pStart
);
213 #endif //std::string compatibility
216 wxString::wxString(const wchar_t *pwz
)
218 // first get necessary size
219 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
224 wcstombs(m_pchData
, pwz
, nLen
);
231 // ---------------------------------------------------------------------------
233 // ---------------------------------------------------------------------------
235 // allocates memory needed to store a C string of length nLen
236 void wxString::AllocBuffer(size_t nLen
)
238 wxASSERT( nLen
> 0 ); //
239 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
241 STATISTICS_ADD(Length
, nLen
);
244 // 1) one extra character for '\0' termination
245 // 2) sizeof(wxStringData) for housekeeping info
246 wxStringData
* pData
= (wxStringData
*)
247 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
249 pData
->nDataLength
= nLen
;
250 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
251 m_pchData
= pData
->data(); // data starts after wxStringData
252 m_pchData
[nLen
] = '\0';
255 // must be called before changing this string
256 void wxString::CopyBeforeWrite()
258 wxStringData
* pData
= GetStringData();
260 if ( pData
->IsShared() ) {
261 pData
->Unlock(); // memory not freed because shared
262 size_t nLen
= pData
->nDataLength
;
264 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
267 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
270 // must be called before replacing contents of this string
271 void wxString::AllocBeforeWrite(size_t nLen
)
273 wxASSERT( nLen
!= 0 ); // doesn't make any sense
275 // must not share string and must have enough space
276 wxStringData
* pData
= GetStringData();
277 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
278 // can't work with old buffer, get new one
283 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
286 // allocate enough memory for nLen characters
287 void wxString::Alloc(size_t nLen
)
289 wxStringData
*pData
= GetStringData();
290 if ( pData
->nAllocLength
<= nLen
) {
291 if ( pData
->IsEmpty() ) {
294 wxStringData
* pData
= (wxStringData
*)
295 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
297 pData
->nDataLength
= 0;
298 pData
->nAllocLength
= nLen
;
299 m_pchData
= pData
->data(); // data starts after wxStringData
300 m_pchData
[0u] = '\0';
302 else if ( pData
->IsShared() ) {
303 pData
->Unlock(); // memory not freed because shared
304 size_t nOldLen
= pData
->nDataLength
;
306 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
311 wxStringData
*p
= (wxStringData
*)
312 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
315 // @@@ what to do on memory error?
319 // it's not important if the pointer changed or not (the check for this
320 // is not faster than assigning to m_pchData in all cases)
321 p
->nAllocLength
= nLen
;
322 m_pchData
= p
->data();
325 //else: we've already got enough
328 // shrink to minimal size (releasing extra memory)
329 void wxString::Shrink()
331 wxStringData
*pData
= GetStringData();
333 // this variable is unused in release build, so avoid the compiler warning by
334 // just not declaring it
338 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
340 wxASSERT( p
!= NULL
); // can't free memory?
341 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
344 // get the pointer to writable buffer of (at least) nLen bytes
345 char *wxString::GetWriteBuf(size_t nLen
)
347 AllocBeforeWrite(nLen
);
349 wxASSERT( GetStringData()->nRefs
== 1 );
350 GetStringData()->Validate(FALSE
);
355 // put string back in a reasonable state after GetWriteBuf
356 void wxString::UngetWriteBuf()
358 GetStringData()->nDataLength
= strlen(m_pchData
);
359 GetStringData()->Validate(TRUE
);
362 // ---------------------------------------------------------------------------
364 // ---------------------------------------------------------------------------
366 // all functions are inline in string.h
368 // ---------------------------------------------------------------------------
369 // assignment operators
370 // ---------------------------------------------------------------------------
372 // helper function: does real copy
373 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
375 if ( nSrcLen
== 0 ) {
379 AllocBeforeWrite(nSrcLen
);
380 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
381 GetStringData()->nDataLength
= nSrcLen
;
382 m_pchData
[nSrcLen
] = '\0';
386 // assigns one string to another
387 wxString
& wxString::operator=(const wxString
& stringSrc
)
389 wxASSERT( stringSrc
.GetStringData()->IsValid() );
391 // don't copy string over itself
392 if ( m_pchData
!= stringSrc
.m_pchData
) {
393 if ( stringSrc
.GetStringData()->IsEmpty() ) {
398 GetStringData()->Unlock();
399 m_pchData
= stringSrc
.m_pchData
;
400 GetStringData()->Lock();
407 // assigns a single character
408 wxString
& wxString::operator=(char ch
)
415 wxString
& wxString::operator=(const char *psz
)
417 AssignCopy(Strlen(psz
), psz
);
421 // same as 'signed char' variant
422 wxString
& wxString::operator=(const unsigned char* psz
)
424 *this = (const char *)psz
;
428 wxString
& wxString::operator=(const wchar_t *pwz
)
435 // ---------------------------------------------------------------------------
436 // string concatenation
437 // ---------------------------------------------------------------------------
439 // add something to this string
440 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
442 STATISTICS_ADD(SummandLength
, nSrcLen
);
444 // concatenating an empty string is a NOP
446 wxStringData
*pData
= GetStringData();
447 size_t nLen
= pData
->nDataLength
;
448 size_t nNewLen
= nLen
+ nSrcLen
;
450 // alloc new buffer if current is too small
451 if ( pData
->IsShared() ) {
452 STATISTICS_ADD(ConcatHit
, 0);
454 // we have to allocate another buffer
455 wxStringData
* pOldData
= GetStringData();
456 AllocBuffer(nNewLen
);
457 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
460 else if ( nNewLen
> pData
->nAllocLength
) {
461 STATISTICS_ADD(ConcatHit
, 0);
463 // we have to grow the buffer
467 STATISTICS_ADD(ConcatHit
, 1);
469 // the buffer is already big enough
472 // should be enough space
473 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
475 // fast concatenation - all is done in our buffer
476 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
478 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
479 GetStringData()->nDataLength
= nNewLen
; // and fix the length
481 //else: the string to append was empty
485 * concatenation functions come in 5 flavours:
487 * char + string and string + char
488 * C str + string and string + C str
491 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
493 wxASSERT( string1
.GetStringData()->IsValid() );
494 wxASSERT( string2
.GetStringData()->IsValid() );
496 wxString s
= string1
;
502 wxString
operator+(const wxString
& string
, char ch
)
504 wxASSERT( string
.GetStringData()->IsValid() );
512 wxString
operator+(char ch
, const wxString
& string
)
514 wxASSERT( string
.GetStringData()->IsValid() );
522 wxString
operator+(const wxString
& string
, const char *psz
)
524 wxASSERT( string
.GetStringData()->IsValid() );
527 s
.Alloc(Strlen(psz
) + string
.Len());
534 wxString
operator+(const char *psz
, const wxString
& string
)
536 wxASSERT( string
.GetStringData()->IsValid() );
539 s
.Alloc(Strlen(psz
) + string
.Len());
546 // ===========================================================================
547 // other common string functions
548 // ===========================================================================
550 // ---------------------------------------------------------------------------
551 // simple sub-string extraction
552 // ---------------------------------------------------------------------------
554 // helper function: clone the data attached to this string
555 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
557 if ( nCopyLen
== 0 ) {
561 dest
.AllocBuffer(nCopyLen
);
562 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
566 // extract string of length nCount starting at nFirst
567 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
569 wxStringData
*pData
= GetStringData();
570 size_t nLen
= pData
->nDataLength
;
572 // default value of nCount is STRING_MAXLEN and means "till the end"
573 if ( nCount
== STRING_MAXLEN
)
575 nCount
= nLen
- nFirst
;
578 // out-of-bounds requests return sensible things
579 if ( nFirst
+ nCount
> nLen
)
581 nCount
= nLen
- nFirst
;
586 // AllocCopy() will return empty string
591 AllocCopy(dest
, nCount
, nFirst
);
596 // extract nCount last (rightmost) characters
597 wxString
wxString::Right(size_t nCount
) const
599 if ( nCount
> (size_t)GetStringData()->nDataLength
)
600 nCount
= GetStringData()->nDataLength
;
603 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
607 // get all characters after the last occurence of ch
608 // (returns the whole string if ch not found)
609 wxString
wxString::Right(char ch
) const
612 int iPos
= Find(ch
, TRUE
);
613 if ( iPos
== NOT_FOUND
)
616 str
= c_str() + iPos
+ 1;
621 // extract nCount first (leftmost) characters
622 wxString
wxString::Left(size_t nCount
) const
624 if ( nCount
> (size_t)GetStringData()->nDataLength
)
625 nCount
= GetStringData()->nDataLength
;
628 AllocCopy(dest
, nCount
, 0);
632 // get all characters before the first occurence of ch
633 // (returns the whole string if ch not found)
634 wxString
wxString::Left(char ch
) const
637 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
643 /// get all characters before the last occurence of ch
644 /// (returns empty string if ch not found)
645 wxString
wxString::Before(char ch
) const
648 int iPos
= Find(ch
, TRUE
);
649 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
650 str
= wxString(c_str(), iPos
);
655 /// get all characters after the first occurence of ch
656 /// (returns empty string if ch not found)
657 wxString
wxString::After(char ch
) const
661 if ( iPos
!= NOT_FOUND
)
662 str
= c_str() + iPos
+ 1;
667 // replace first (or all) occurences of some substring with another one
668 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
670 size_t uiCount
= 0; // count of replacements made
672 size_t uiOldLen
= Strlen(szOld
);
675 const char *pCurrent
= m_pchData
;
677 while ( *pCurrent
!= '\0' ) {
678 pSubstr
= strstr(pCurrent
, szOld
);
679 if ( pSubstr
== NULL
) {
680 // strTemp is unused if no replacements were made, so avoid the copy
684 strTemp
+= pCurrent
; // copy the rest
685 break; // exit the loop
688 // take chars before match
689 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
691 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
696 if ( !bReplaceAll
) {
697 strTemp
+= pCurrent
; // copy the rest
698 break; // exit the loop
703 // only done if there were replacements, otherwise would have returned above
709 bool wxString::IsAscii() const
711 const char *s
= (const char*) *this;
713 if(!isascii(*s
)) return(FALSE
);
719 bool wxString::IsWord() const
721 const char *s
= (const char*) *this;
723 if(!isalpha(*s
)) return(FALSE
);
729 bool wxString::IsNumber() const
731 const char *s
= (const char*) *this;
733 if(!isdigit(*s
)) return(FALSE
);
739 wxString
wxString::Strip(stripType w
) const
742 if ( w
& leading
) s
.Trim(FALSE
);
743 if ( w
& trailing
) s
.Trim(TRUE
);
747 // ---------------------------------------------------------------------------
749 // ---------------------------------------------------------------------------
751 wxString
& wxString::MakeUpper()
755 for ( char *p
= m_pchData
; *p
; p
++ )
756 *p
= (char)toupper(*p
);
761 wxString
& wxString::MakeLower()
765 for ( char *p
= m_pchData
; *p
; p
++ )
766 *p
= (char)tolower(*p
);
771 // ---------------------------------------------------------------------------
772 // trimming and padding
773 // ---------------------------------------------------------------------------
775 // trims spaces (in the sense of isspace) from left or right side
776 wxString
& wxString::Trim(bool bFromRight
)
782 // find last non-space character
783 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
784 while ( isspace(*psz
) && (psz
>= m_pchData
) )
787 // truncate at trailing space start
789 GetStringData()->nDataLength
= psz
- m_pchData
;
793 // find first non-space character
794 const char *psz
= m_pchData
;
795 while ( isspace(*psz
) )
798 // fix up data and length
799 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
800 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
801 GetStringData()->nDataLength
= nDataLength
;
807 // adds nCount characters chPad to the string from either side
808 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
810 wxString
s(chPad
, nCount
);
823 // truncate the string
824 wxString
& wxString::Truncate(size_t uiLen
)
826 *(m_pchData
+ uiLen
) = '\0';
827 GetStringData()->nDataLength
= uiLen
;
832 // ---------------------------------------------------------------------------
833 // finding (return NOT_FOUND if not found and index otherwise)
834 // ---------------------------------------------------------------------------
837 int wxString::Find(char ch
, bool bFromEnd
) const
839 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
841 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
844 // find a sub-string (like strstr)
845 int wxString::Find(const char *pszSub
) const
847 const char *psz
= strstr(m_pchData
, pszSub
);
849 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
852 // ---------------------------------------------------------------------------
854 // ---------------------------------------------------------------------------
855 int wxString::Printf(const char *pszFormat
, ...)
858 va_start(argptr
, pszFormat
);
860 int iLen
= PrintfV(pszFormat
, argptr
);
867 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
869 static char s_szScratch
[1024];
871 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
872 AllocBeforeWrite(iLen
);
873 strcpy(m_pchData
, s_szScratch
);
878 // ----------------------------------------------------------------------------
879 // misc other operations
880 // ----------------------------------------------------------------------------
881 bool wxString::Matches(const char *pszMask
) const
883 // check char by char
885 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
886 switch ( *pszMask
) {
888 if ( *pszTxt
== '\0' )
897 // ignore special chars immediately following this one
898 while ( *pszMask
== '*' || *pszMask
== '?' )
901 // if there is nothing more, match
902 if ( *pszMask
== '\0' )
905 // are there any other metacharacters in the mask?
907 const char *pEndMask
= strpbrk(pszMask
, "*?");
909 if ( pEndMask
!= NULL
) {
910 // we have to match the string between two metachars
911 uiLenMask
= pEndMask
- pszMask
;
914 // we have to match the remainder of the string
915 uiLenMask
= strlen(pszMask
);
918 wxString
strToMatch(pszMask
, uiLenMask
);
919 const char* pMatch
= strstr(pszTxt
, strToMatch
);
920 if ( pMatch
== NULL
)
923 // -1 to compensate "++" in the loop
924 pszTxt
= pMatch
+ uiLenMask
- 1;
925 pszMask
+= uiLenMask
- 1;
930 if ( *pszMask
!= *pszTxt
)
936 // match only if nothing left
937 return *pszTxt
== '\0';
940 // ---------------------------------------------------------------------------
941 // standard C++ library string functions
942 // ---------------------------------------------------------------------------
943 #ifdef STD_STRING_COMPATIBILITY
945 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
947 wxASSERT( str
.GetStringData()->IsValid() );
948 wxASSERT( nPos
<= Len() );
950 if ( !str
.IsEmpty() ) {
952 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
953 strncpy(pc
, c_str(), nPos
);
954 strcpy(pc
+ nPos
, str
);
955 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
956 strTmp
.UngetWriteBuf();
963 size_t wxString::find(const wxString
& str
, size_t nStart
) const
965 wxASSERT( str
.GetStringData()->IsValid() );
966 wxASSERT( nStart
<= Len() );
968 const char *p
= strstr(c_str() + nStart
, str
);
970 return p
== NULL
? npos
: p
- c_str();
973 // VC++ 1.5 can't cope with the default argument in the header.
974 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
975 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
977 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
981 size_t wxString::find(char ch
, size_t nStart
) const
983 wxASSERT( nStart
<= Len() );
985 const char *p
= strchr(c_str() + nStart
, ch
);
987 return p
== NULL
? npos
: p
- c_str();
990 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
992 wxASSERT( str
.GetStringData()->IsValid() );
993 wxASSERT( nStart
<= Len() );
995 // # could be quicker than that
996 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
997 while ( p
>= c_str() + str
.Len() ) {
998 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
999 return p
- str
.Len() - c_str();
1006 // VC++ 1.5 can't cope with the default argument in the header.
1007 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1008 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1010 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1013 size_t wxString::rfind(char ch
, size_t nStart
) const
1015 wxASSERT( nStart
<= Len() );
1017 const char *p
= strrchr(c_str() + nStart
, ch
);
1019 return p
== NULL
? npos
: p
- c_str();
1023 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1025 // npos means 'take all'
1029 wxASSERT( nStart
+ nLen
<= Len() );
1031 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1034 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1036 wxString
strTmp(c_str(), nStart
);
1037 if ( nLen
!= npos
) {
1038 wxASSERT( nStart
+ nLen
<= Len() );
1040 strTmp
.append(c_str() + nStart
+ nLen
);
1047 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1049 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1053 strTmp
.append(c_str(), nStart
);
1055 strTmp
.append(c_str() + nStart
+ nLen
);
1061 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1063 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1066 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1067 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1069 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1072 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1073 const char* sz
, size_t nCount
)
1075 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1078 #endif //std::string compatibility
1080 // ============================================================================
1082 // ============================================================================
1084 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1085 #define ARRAY_MAXSIZE_INCREMENT 4096
1086 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1087 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1090 #define STRING(p) ((wxString *)(&(p)))
1093 wxArrayString::wxArrayString()
1097 m_pItems
= (char **) NULL
;
1101 wxArrayString::wxArrayString(const wxArrayString
& src
)
1105 m_pItems
= (char **) NULL
;
1110 // assignment operator
1111 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1116 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1117 Alloc(src
.m_nCount
);
1119 // we can't just copy the pointers here because otherwise we would share
1120 // the strings with another array
1121 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1124 if ( m_nCount
!= 0 )
1125 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1131 void wxArrayString::Grow()
1133 // only do it if no more place
1134 if( m_nCount
== m_nSize
) {
1135 if( m_nSize
== 0 ) {
1136 // was empty, alloc some memory
1137 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1138 m_pItems
= new char *[m_nSize
];
1141 // otherwise when it's called for the first time, nIncrement would be 0
1142 // and the array would never be expanded
1143 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1145 // add 50% but not too much
1146 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1147 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1148 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1149 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1150 m_nSize
+= nIncrement
;
1151 char **pNew
= new char *[m_nSize
];
1153 // copy data to new location
1154 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1156 // delete old memory (but do not release the strings!)
1157 wxDELETEA(m_pItems
);
1164 void wxArrayString::Free()
1166 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1167 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1171 // deletes all the strings from the list
1172 void wxArrayString::Empty()
1179 // as Empty, but also frees memory
1180 void wxArrayString::Clear()
1187 wxDELETEA(m_pItems
);
1191 wxArrayString::~wxArrayString()
1195 wxDELETEA(m_pItems
);
1198 // pre-allocates memory (frees the previous data!)
1199 void wxArrayString::Alloc(size_t nSize
)
1201 wxASSERT( nSize
> 0 );
1203 // only if old buffer was not big enough
1204 if ( nSize
> m_nSize
) {
1206 wxDELETEA(m_pItems
);
1207 m_pItems
= new char *[nSize
];
1214 // searches the array for an item (forward or backwards)
1215 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1218 if ( m_nCount
> 0 ) {
1219 size_t ui
= m_nCount
;
1221 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1228 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1229 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1237 // add item at the end
1238 void wxArrayString::Add(const wxString
& str
)
1240 wxASSERT( str
.GetStringData()->IsValid() );
1244 // the string data must not be deleted!
1245 str
.GetStringData()->Lock();
1246 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1249 // add item at the given position
1250 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1252 wxASSERT( str
.GetStringData()->IsValid() );
1254 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1258 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1259 (m_nCount
- nIndex
)*sizeof(char *));
1261 str
.GetStringData()->Lock();
1262 m_pItems
[nIndex
] = (char *)str
.c_str();
1267 // removes item from array (by index)
1268 void wxArrayString::Remove(size_t nIndex
)
1270 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1273 Item(nIndex
).GetStringData()->Unlock();
1275 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1276 (m_nCount
- nIndex
- 1)*sizeof(char *));
1280 // removes item from array (by value)
1281 void wxArrayString::Remove(const char *sz
)
1283 int iIndex
= Index(sz
);
1285 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1286 _("removing inexistent element in wxArrayString::Remove") );
1291 // sort array elements using passed comparaison function
1293 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1296 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);