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"
43 #ifdef WXSTRING_IS_WXOBJECT
44 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
45 #endif //WXSTRING_IS_WXOBJECT
47 // allocating extra space for each string consumes more memory but speeds up
48 // the concatenation operations (nLen is the current string's length)
49 #define EXTRA_ALLOC 16
51 // ---------------------------------------------------------------------------
52 // static class variables definition
53 // ---------------------------------------------------------------------------
55 #ifdef STD_STRING_COMPATIBILITY
56 const size_t wxString::npos
= STRING_MAXLEN
;
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // for an empty string, GetStringData() will return this address
64 static int g_strEmpty
[] = { -1, // ref count (locked)
66 0, // allocated memory
68 // empty C style string: points to 'string data' byte of g_strEmpty
69 extern const char *g_szNul
= (const char *)(&g_strEmpty
[3]);
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 #ifdef STD_STRING_COMPATIBILITY
77 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
80 // ATTN: you can _not_ use both of these in the same program!
83 #define NAMESPACE std::
89 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
94 NAMESPACE streambuf
*sb
= is
.rdbuf();
97 int ch
= sb
->sbumpc ();
99 is
.setstate(NAMESPACE
ios::eofbit
);
102 else if ( isspace(ch
) ) {
114 if ( str
.length() == 0 )
115 is
.setstate(NAMESPACE
ios::failbit
);
120 #endif //std::string compatibility
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 // this small class is used to gather statistics for performance tuning
127 //#define WXSTRING_STATISTICS
128 #ifdef WXSTRING_STATISTICS
132 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
134 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
136 void Add(uint n
) { m_nTotal
+= n
; m_nCount
++; }
139 uint m_nCount
, m_nTotal
;
141 } g_averageLength("allocation size"),
142 g_averageSummandLength("summand length"),
143 g_averageConcatHit("hit probability in concat"),
144 g_averageInitialLength("initial string length");
146 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
148 #define STATISTICS_ADD(av, val)
149 #endif // WXSTRING_STATISTICS
151 // ===========================================================================
152 // wxString class core
153 // ===========================================================================
155 // ---------------------------------------------------------------------------
157 // ---------------------------------------------------------------------------
159 // constructs string of <nLength> copies of character <ch>
160 wxString::wxString(char ch
, size_t nLength
)
165 AllocBuffer(nLength
);
167 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
169 memset(m_pchData
, ch
, nLength
);
173 // takes nLength elements of psz starting at nPos
174 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
178 wxASSERT( nPos
<= Strlen(psz
) );
180 if ( nLength
== STRING_MAXLEN
)
181 nLength
= Strlen(psz
+ nPos
);
183 STATISTICS_ADD(InitialLength
, nLength
);
186 // trailing '\0' is written in AllocBuffer()
187 AllocBuffer(nLength
);
188 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
192 // the same as previous constructor, but for compilers using unsigned char
193 wxString::wxString(const unsigned char* psz
, size_t nLength
)
195 InitWith((const char *)psz
, 0, nLength
);
198 #ifdef STD_STRING_COMPATIBILITY
200 // poor man's iterators are "void *" pointers
201 wxString::wxString(const void *pStart
, const void *pEnd
)
203 InitWith((const char *)pStart
, 0,
204 (const char *)pEnd
- (const char *)pStart
);
207 #endif //std::string compatibility
210 wxString::wxString(const wchar_t *pwz
)
212 // first get necessary size
213 size_t nLen
= wcstombs(NULL
, pwz
, 0);
218 wcstombs(m_pchData
, pwz
, nLen
);
225 // ---------------------------------------------------------------------------
227 // ---------------------------------------------------------------------------
229 // allocates memory needed to store a C string of length nLen
230 void wxString::AllocBuffer(size_t nLen
)
232 wxASSERT( nLen
> 0 ); //
233 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
235 STATISTICS_ADD(Length
, nLen
);
238 // 1) one extra character for '\0' termination
239 // 2) sizeof(wxStringData) for housekeeping info
240 wxStringData
* pData
= (wxStringData
*)
241 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
243 pData
->nDataLength
= nLen
;
244 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
245 m_pchData
= pData
->data(); // data starts after wxStringData
246 m_pchData
[nLen
] = '\0';
249 // must be called before changing this string
250 void wxString::CopyBeforeWrite()
252 wxStringData
* pData
= GetStringData();
254 if ( pData
->IsShared() ) {
255 pData
->Unlock(); // memory not freed because shared
256 uint nLen
= pData
->nDataLength
;
258 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
261 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
264 // must be called before replacing contents of this string
265 void wxString::AllocBeforeWrite(size_t nLen
)
267 wxASSERT( nLen
!= 0 ); // doesn't make any sense
269 // must not share string and must have enough space
270 wxStringData
* pData
= GetStringData();
271 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
272 // can't work with old buffer, get new one
277 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
280 // allocate enough memory for nLen characters
281 void wxString::Alloc(uint nLen
)
283 wxStringData
*pData
= GetStringData();
284 if ( pData
->nAllocLength
<= nLen
) {
285 if ( pData
->IsEmpty() ) {
288 wxStringData
* pData
= (wxStringData
*)
289 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
291 pData
->nDataLength
= 0;
292 pData
->nAllocLength
= nLen
;
293 m_pchData
= pData
->data(); // data starts after wxStringData
294 m_pchData
[0u] = '\0';
296 else if ( pData
->IsShared() ) {
297 pData
->Unlock(); // memory not freed because shared
298 uint nOldLen
= pData
->nDataLength
;
300 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
305 wxStringData
*p
= (wxStringData
*)
306 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
309 // @@@ what to do on memory error?
313 // it's not important if the pointer changed or not (the check for this
314 // is not faster than assigning to m_pchData in all cases)
315 p
->nAllocLength
= nLen
;
316 m_pchData
= p
->data();
319 //else: we've already got enough
322 // shrink to minimal size (releasing extra memory)
323 void wxString::Shrink()
325 wxStringData
*pData
= GetStringData();
327 // this variable is unused in release build, so avoid the compiler warning by
328 // just not declaring it
332 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
334 wxASSERT( p
!= NULL
); // can't free memory?
335 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
338 // get the pointer to writable buffer of (at least) nLen bytes
339 char *wxString::GetWriteBuf(uint nLen
)
341 AllocBeforeWrite(nLen
);
343 wxASSERT( GetStringData()->nRefs
== 1 );
344 GetStringData()->Validate(FALSE
);
349 // put string back in a reasonable state after GetWriteBuf
350 void wxString::UngetWriteBuf()
352 GetStringData()->nDataLength
= strlen(m_pchData
);
353 GetStringData()->Validate(TRUE
);
356 // ---------------------------------------------------------------------------
358 // ---------------------------------------------------------------------------
360 // all functions are inline in string.h
362 // ---------------------------------------------------------------------------
363 // assignment operators
364 // ---------------------------------------------------------------------------
366 // helper function: does real copy
367 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
369 if ( nSrcLen
== 0 ) {
373 AllocBeforeWrite(nSrcLen
);
374 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
375 GetStringData()->nDataLength
= nSrcLen
;
376 m_pchData
[nSrcLen
] = '\0';
380 // assigns one string to another
381 wxString
& wxString::operator=(const wxString
& stringSrc
)
383 wxASSERT( stringSrc
.GetStringData()->IsValid() );
385 // don't copy string over itself
386 if ( m_pchData
!= stringSrc
.m_pchData
) {
387 if ( stringSrc
.GetStringData()->IsEmpty() ) {
392 GetStringData()->Unlock();
393 m_pchData
= stringSrc
.m_pchData
;
394 GetStringData()->Lock();
401 // assigns a single character
402 wxString
& wxString::operator=(char ch
)
409 wxString
& wxString::operator=(const char *psz
)
411 AssignCopy(Strlen(psz
), psz
);
415 // same as 'signed char' variant
416 wxString
& wxString::operator=(const unsigned char* psz
)
418 *this = (const char *)psz
;
422 wxString
& wxString::operator=(const wchar_t *pwz
)
429 // ---------------------------------------------------------------------------
430 // string concatenation
431 // ---------------------------------------------------------------------------
433 // add something to this string
434 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
436 STATISTICS_ADD(SummandLength
, nSrcLen
);
438 // concatenating an empty string is a NOP, but it happens quite rarely,
439 // so we don't waste our time checking for it
440 // if ( nSrcLen > 0 )
441 wxStringData
*pData
= GetStringData();
442 uint nLen
= pData
->nDataLength
;
443 uint nNewLen
= nLen
+ nSrcLen
;
445 // alloc new buffer if current is too small
446 if ( pData
->IsShared() ) {
447 STATISTICS_ADD(ConcatHit
, 0);
449 // we have to allocate another buffer
450 wxStringData
* pOldData
= GetStringData();
451 AllocBuffer(nNewLen
);
452 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
455 else if ( nNewLen
> pData
->nAllocLength
) {
456 STATISTICS_ADD(ConcatHit
, 0);
458 // we have to grow the buffer
462 STATISTICS_ADD(ConcatHit
, 1);
464 // the buffer is already big enough
467 // should be enough space
468 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
470 // fast concatenation - all is done in our buffer
471 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
473 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
474 GetStringData()->nDataLength
= nNewLen
; // and fix the length
478 * concatenation functions come in 5 flavours:
480 * char + string and string + char
481 * C str + string and string + C str
484 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
486 wxASSERT( string1
.GetStringData()->IsValid() );
487 wxASSERT( string2
.GetStringData()->IsValid() );
489 wxString s
= string1
;
495 wxString
operator+(const wxString
& string
, char ch
)
497 wxASSERT( string
.GetStringData()->IsValid() );
505 wxString
operator+(char ch
, const wxString
& string
)
507 wxASSERT( string
.GetStringData()->IsValid() );
515 wxString
operator+(const wxString
& string
, const char *psz
)
517 wxASSERT( string
.GetStringData()->IsValid() );
520 s
.Alloc(Strlen(psz
) + string
.Len());
527 wxString
operator+(const char *psz
, const wxString
& string
)
529 wxASSERT( string
.GetStringData()->IsValid() );
532 s
.Alloc(Strlen(psz
) + string
.Len());
539 // ===========================================================================
540 // other common string functions
541 // ===========================================================================
543 // ---------------------------------------------------------------------------
544 // simple sub-string extraction
545 // ---------------------------------------------------------------------------
547 // helper function: clone the data attached to this string
548 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
550 if ( nCopyLen
== 0 ) {
554 dest
.AllocBuffer(nCopyLen
);
555 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
559 // extract string of length nCount starting at nFirst
560 // default value of nCount is 0 and means "till the end"
561 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
563 // out-of-bounds requests return sensible things
565 nCount
= GetStringData()->nDataLength
- nFirst
;
567 if ( nFirst
+ nCount
> (size_t)GetStringData()->nDataLength
)
568 nCount
= GetStringData()->nDataLength
- nFirst
;
569 if ( nFirst
> (size_t)GetStringData()->nDataLength
)
573 AllocCopy(dest
, nCount
, nFirst
);
577 // extract nCount last (rightmost) characters
578 wxString
wxString::Right(size_t nCount
) const
580 if ( nCount
> (size_t)GetStringData()->nDataLength
)
581 nCount
= GetStringData()->nDataLength
;
584 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
588 // get all characters after the last occurence of ch
589 // (returns the whole string if ch not found)
590 wxString
wxString::Right(char ch
) const
593 int iPos
= Find(ch
, TRUE
);
594 if ( iPos
== NOT_FOUND
)
597 str
= c_str() + iPos
+ 1;
602 // extract nCount first (leftmost) characters
603 wxString
wxString::Left(size_t nCount
) const
605 if ( nCount
> (size_t)GetStringData()->nDataLength
)
606 nCount
= GetStringData()->nDataLength
;
609 AllocCopy(dest
, nCount
, 0);
613 // get all characters before the first occurence of ch
614 // (returns the whole string if ch not found)
615 wxString
wxString::Left(char ch
) const
618 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
624 /// get all characters before the last occurence of ch
625 /// (returns empty string if ch not found)
626 wxString
wxString::Before(char ch
) const
629 int iPos
= Find(ch
, TRUE
);
630 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
631 str
= wxString(c_str(), iPos
);
636 /// get all characters after the first occurence of ch
637 /// (returns empty string if ch not found)
638 wxString
wxString::After(char ch
) const
642 if ( iPos
!= NOT_FOUND
)
643 str
= c_str() + iPos
+ 1;
648 // replace first (or all) occurences of some substring with another one
649 uint
wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
651 uint uiCount
= 0; // count of replacements made
653 uint uiOldLen
= Strlen(szOld
);
656 const char *pCurrent
= m_pchData
;
658 while ( *pCurrent
!= '\0' ) {
659 pSubstr
= strstr(pCurrent
, szOld
);
660 if ( pSubstr
== NULL
) {
661 // strTemp is unused if no replacements were made, so avoid the copy
665 strTemp
+= pCurrent
; // copy the rest
666 break; // exit the loop
669 // take chars before match
670 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
672 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
677 if ( !bReplaceAll
) {
678 strTemp
+= pCurrent
; // copy the rest
679 break; // exit the loop
684 // only done if there were replacements, otherwise would have returned above
690 bool wxString::IsAscii() const
692 const char *s
= (const char*) *this;
694 if(!isascii(*s
)) return(FALSE
);
700 bool wxString::IsWord() const
702 const char *s
= (const char*) *this;
704 if(!isalpha(*s
)) return(FALSE
);
710 bool wxString::IsNumber() const
712 const char *s
= (const char*) *this;
714 if(!isdigit(*s
)) return(FALSE
);
720 wxString
wxString::Strip(stripType w
) const
723 if ( w
& leading
) s
.Trim(FALSE
);
724 if ( w
& trailing
) s
.Trim(TRUE
);
728 // ---------------------------------------------------------------------------
730 // ---------------------------------------------------------------------------
732 wxString
& wxString::MakeUpper()
736 for ( char *p
= m_pchData
; *p
; p
++ )
737 *p
= (char)toupper(*p
);
742 wxString
& wxString::MakeLower()
746 for ( char *p
= m_pchData
; *p
; p
++ )
747 *p
= (char)tolower(*p
);
752 // ---------------------------------------------------------------------------
753 // trimming and padding
754 // ---------------------------------------------------------------------------
756 // trims spaces (in the sense of isspace) from left or right side
757 wxString
& wxString::Trim(bool bFromRight
)
763 // find last non-space character
764 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
765 while ( isspace(*psz
) && (psz
>= m_pchData
) )
768 // truncate at trailing space start
770 GetStringData()->nDataLength
= psz
- m_pchData
;
774 // find first non-space character
775 const char *psz
= m_pchData
;
776 while ( isspace(*psz
) )
779 // fix up data and length
780 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
781 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
782 GetStringData()->nDataLength
= nDataLength
;
788 // adds nCount characters chPad to the string from either side
789 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
791 wxString
s(chPad
, nCount
);
804 // truncate the string
805 wxString
& wxString::Truncate(size_t uiLen
)
807 *(m_pchData
+ uiLen
) = '\0';
808 GetStringData()->nDataLength
= uiLen
;
813 // ---------------------------------------------------------------------------
814 // finding (return NOT_FOUND if not found and index otherwise)
815 // ---------------------------------------------------------------------------
818 int wxString::Find(char ch
, bool bFromEnd
) const
820 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
822 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
825 // find a sub-string (like strstr)
826 int wxString::Find(const char *pszSub
) const
828 const char *psz
= strstr(m_pchData
, pszSub
);
830 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
833 // ---------------------------------------------------------------------------
835 // ---------------------------------------------------------------------------
836 int wxString::Printf(const char *pszFormat
, ...)
839 va_start(argptr
, pszFormat
);
841 int iLen
= PrintfV(pszFormat
, argptr
);
848 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
850 static char s_szScratch
[1024];
852 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
853 AllocBeforeWrite(iLen
);
854 strcpy(m_pchData
, s_szScratch
);
859 // ----------------------------------------------------------------------------
860 // misc other operations
861 // ----------------------------------------------------------------------------
862 bool wxString::Matches(const char *pszMask
) const
864 // check char by char
866 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
867 switch ( *pszMask
) {
869 if ( *pszTxt
== '\0' )
878 // ignore special chars immediately following this one
879 while ( *pszMask
== '*' || *pszMask
== '?' )
882 // if there is nothing more, match
883 if ( *pszMask
== '\0' )
886 // are there any other metacharacters in the mask?
888 const char *pEndMask
= strpbrk(pszMask
, "*?");
890 if ( pEndMask
!= NULL
) {
891 // we have to match the string between two metachars
892 uiLenMask
= pEndMask
- pszMask
;
895 // we have to match the remainder of the string
896 uiLenMask
= strlen(pszMask
);
899 wxString
strToMatch(pszMask
, uiLenMask
);
900 const char* pMatch
= strstr(pszTxt
, strToMatch
);
901 if ( pMatch
== NULL
)
904 // -1 to compensate "++" in the loop
905 pszTxt
= pMatch
+ uiLenMask
- 1;
906 pszMask
+= uiLenMask
- 1;
911 if ( *pszMask
!= *pszTxt
)
917 // match only if nothing left
918 return *pszTxt
== '\0';
921 // ---------------------------------------------------------------------------
922 // standard C++ library string functions
923 // ---------------------------------------------------------------------------
924 #ifdef STD_STRING_COMPATIBILITY
926 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
928 wxASSERT( str
.GetStringData()->IsValid() );
929 wxASSERT( nPos
<= Len() );
932 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
933 strncpy(pc
, c_str(), nPos
);
934 strcpy(pc
+ nPos
, str
);
935 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
936 strTmp
.UngetWriteBuf();
942 size_t wxString::find(const wxString
& str
, size_t nStart
) const
944 wxASSERT( str
.GetStringData()->IsValid() );
945 wxASSERT( nStart
<= Len() );
947 const char *p
= strstr(c_str() + nStart
, str
);
949 return p
== NULL
? npos
: p
- c_str();
952 // VC++ 1.5 can't cope with the default argument in the header.
953 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
954 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
956 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
960 size_t wxString::find(char ch
, size_t nStart
) const
962 wxASSERT( nStart
<= Len() );
964 const char *p
= strchr(c_str() + nStart
, ch
);
966 return p
== NULL
? npos
: p
- c_str();
969 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
971 wxASSERT( str
.GetStringData()->IsValid() );
972 wxASSERT( nStart
<= Len() );
974 // # could be quicker than that
975 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
976 while ( p
>= c_str() + str
.Len() ) {
977 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
978 return p
- str
.Len() - c_str();
985 // VC++ 1.5 can't cope with the default argument in the header.
986 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
987 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
989 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
992 size_t wxString::rfind(char ch
, size_t nStart
) const
994 wxASSERT( nStart
<= Len() );
996 const char *p
= strrchr(c_str() + nStart
, ch
);
998 return p
== NULL
? npos
: p
- c_str();
1002 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1004 // npos means 'take all'
1008 wxASSERT( nStart
+ nLen
<= Len() );
1010 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1013 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1015 wxString
strTmp(c_str(), nStart
);
1016 if ( nLen
!= npos
) {
1017 wxASSERT( nStart
+ nLen
<= Len() );
1019 strTmp
.append(c_str() + nStart
+ nLen
);
1026 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1028 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1032 strTmp
.append(c_str(), nStart
);
1034 strTmp
.append(c_str() + nStart
+ nLen
);
1040 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1042 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1045 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1046 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1048 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1051 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1052 const char* sz
, size_t nCount
)
1054 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1057 #endif //std::string compatibility
1059 // ============================================================================
1061 // ============================================================================
1063 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1064 #define ARRAY_MAXSIZE_INCREMENT 4096
1065 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1066 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1069 #define STRING(p) ((wxString *)(&(p)))
1072 wxArrayString::wxArrayString()
1080 wxArrayString::wxArrayString(const wxArrayString
& src
)
1089 // assignment operator
1090 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1095 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1096 Alloc(src
.m_nCount
);
1098 // we can't just copy the pointers here because otherwise we would share
1099 // the strings with another array
1100 for ( uint n
= 0; n
< src
.m_nCount
; n
++ )
1103 if ( m_nCount
!= 0 )
1104 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1110 void wxArrayString::Grow()
1112 // only do it if no more place
1113 if( m_nCount
== m_nSize
) {
1114 if( m_nSize
== 0 ) {
1115 // was empty, alloc some memory
1116 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1117 m_pItems
= new char *[m_nSize
];
1120 // otherwise when it's called for the first time, nIncrement would be 0
1121 // and the array would never be expanded
1122 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1124 // add 50% but not too much
1125 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1126 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1127 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1128 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1129 m_nSize
+= nIncrement
;
1130 char **pNew
= new char *[m_nSize
];
1132 // copy data to new location
1133 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1135 // delete old memory (but do not release the strings!)
1143 void wxArrayString::Free()
1145 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1146 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1150 // deletes all the strings from the list
1151 void wxArrayString::Empty()
1158 // as Empty, but also frees memory
1159 void wxArrayString::Clear()
1171 wxArrayString::~wxArrayString()
1178 // pre-allocates memory (frees the previous data!)
1179 void wxArrayString::Alloc(size_t nSize
)
1181 wxASSERT( nSize
> 0 );
1183 // only if old buffer was not big enough
1184 if ( nSize
> m_nSize
) {
1187 m_pItems
= new char *[nSize
];
1194 // searches the array for an item (forward or backwards)
1195 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1198 if ( m_nCount
> 0 ) {
1201 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1208 for( uint ui
= 0; ui
< m_nCount
; ui
++ ) {
1209 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1217 // add item at the end
1218 void wxArrayString::Add(const wxString
& str
)
1220 wxASSERT( str
.GetStringData()->IsValid() );
1224 // the string data must not be deleted!
1225 str
.GetStringData()->Lock();
1226 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1229 // add item at the given position
1230 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1232 wxASSERT( str
.GetStringData()->IsValid() );
1234 wxCHECK_RET( nIndex
<= m_nCount
, "bad index in wxArrayString::Insert" );
1238 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1239 (m_nCount
- nIndex
)*sizeof(char *));
1241 str
.GetStringData()->Lock();
1242 m_pItems
[nIndex
] = (char *)str
.c_str();
1247 // removes item from array (by index)
1248 void wxArrayString::Remove(size_t nIndex
)
1250 wxCHECK_RET( nIndex
<= m_nCount
, "bad index in wxArrayString::Remove" );
1253 Item(nIndex
).GetStringData()->Unlock();
1255 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1256 (m_nCount
- nIndex
- 1)*sizeof(char *));
1260 // removes item from array (by value)
1261 void wxArrayString::Remove(const char *sz
)
1263 int iIndex
= Index(sz
);
1265 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1266 "removing inexistent element in wxArrayString::Remove" );
1268 Remove((size_t)iIndex
);
1271 // sort array elements using passed comparaison function
1273 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1276 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);