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( !pData
->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() )
287 else if ( pData
->IsShared() ) {
288 pData
->Unlock(); // memory not freed because shared
289 uint nLen
= pData
->nDataLength
;
291 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
296 wxStringData
*p
= (wxStringData
*)
297 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
300 // @@@ what to do on memory error?
304 // it's not important if the pointer changed or not (the check for this
305 // is not faster than assigning to m_pchData in all cases)
306 p
->nAllocLength
= nLen
;
307 m_pchData
= p
->data();
310 //else: we've already got enough
313 // shrink to minimal size (releasing extra memory)
314 void wxString::Shrink()
316 wxStringData
*pData
= GetStringData();
317 void *p
= realloc(pData
, sizeof(wxStringData
) +
318 (pData
->nDataLength
+ 1)*sizeof(char));
319 wxASSERT( p
!= NULL
); // can't free memory?
320 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
323 // get the pointer to writable buffer of (at least) nLen bytes
324 char *wxString::GetWriteBuf(uint nLen
)
326 AllocBeforeWrite(nLen
);
328 wxASSERT( GetStringData()->nRefs
== 1 );
329 GetStringData()->Validate(FALSE
);
334 // put string back in a reasonable state after GetWriteBuf
335 void wxString::UngetWriteBuf()
337 GetStringData()->nDataLength
= strlen(m_pchData
);
338 GetStringData()->Validate(TRUE
);
341 // ---------------------------------------------------------------------------
343 // ---------------------------------------------------------------------------
345 // all functions are inline in string.h
347 // ---------------------------------------------------------------------------
348 // assignment operators
349 // ---------------------------------------------------------------------------
351 // helper function: does real copy
352 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
354 if ( nSrcLen
== 0 ) {
358 AllocBeforeWrite(nSrcLen
);
359 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
360 GetStringData()->nDataLength
= nSrcLen
;
361 m_pchData
[nSrcLen
] = '\0';
365 // assigns one string to another
366 wxString
& wxString::operator=(const wxString
& stringSrc
)
368 wxASSERT( stringSrc
.GetStringData()->IsValid() );
370 // don't copy string over itself
371 if ( m_pchData
!= stringSrc
.m_pchData
) {
372 if ( stringSrc
.GetStringData()->IsEmpty() ) {
377 GetStringData()->Unlock();
378 m_pchData
= stringSrc
.m_pchData
;
379 GetStringData()->Lock();
386 // assigns a single character
387 wxString
& wxString::operator=(char ch
)
394 wxString
& wxString::operator=(const char *psz
)
396 AssignCopy(Strlen(psz
), psz
);
400 // same as 'signed char' variant
401 wxString
& wxString::operator=(const unsigned char* psz
)
403 *this = (const char *)psz
;
407 wxString
& wxString::operator=(const wchar_t *pwz
)
414 // ---------------------------------------------------------------------------
415 // string concatenation
416 // ---------------------------------------------------------------------------
418 // add something to this string
419 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
421 STATISTICS_ADD(SummandLength
, nSrcLen
);
423 // concatenating an empty string is a NOP, but it happens quite rarely,
424 // so we don't waste our time checking for it
425 // if ( nSrcLen > 0 )
426 wxStringData
*pData
= GetStringData();
427 uint nLen
= pData
->nDataLength
;
428 uint nNewLen
= nLen
+ nSrcLen
;
430 // alloc new buffer if current is too small
431 if ( pData
->IsShared() ) {
432 STATISTICS_ADD(ConcatHit
, 0);
434 // we have to allocate another buffer
435 wxStringData
* pOldData
= GetStringData();
436 AllocBuffer(nNewLen
);
437 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
440 else if ( nNewLen
> pData
->nAllocLength
) {
441 STATISTICS_ADD(ConcatHit
, 0);
443 // we have to grow the buffer
447 STATISTICS_ADD(ConcatHit
, 1);
449 // the buffer is already big enough
452 // should be enough space
453 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
455 // fast concatenation - all is done in our buffer
456 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
458 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
459 GetStringData()->nDataLength
= nNewLen
; // and fix the length
463 * concatenation functions come in 5 flavours:
465 * char + string and string + char
466 * C str + string and string + C str
469 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
471 wxASSERT( string1
.GetStringData()->IsValid() );
472 wxASSERT( string2
.GetStringData()->IsValid() );
474 wxString s
= string1
;
480 wxString
operator+(const wxString
& string
, char ch
)
482 wxASSERT( string
.GetStringData()->IsValid() );
490 wxString
operator+(char ch
, const wxString
& string
)
492 wxASSERT( string
.GetStringData()->IsValid() );
500 wxString
operator+(const wxString
& string
, const char *psz
)
502 wxASSERT( string
.GetStringData()->IsValid() );
505 s
.Alloc(Strlen(psz
) + string
.Len());
512 wxString
operator+(const char *psz
, const wxString
& string
)
514 wxASSERT( string
.GetStringData()->IsValid() );
517 s
.Alloc(Strlen(psz
) + string
.Len());
524 // ===========================================================================
525 // other common string functions
526 // ===========================================================================
528 // ---------------------------------------------------------------------------
529 // simple sub-string extraction
530 // ---------------------------------------------------------------------------
532 // helper function: clone the data attached to this string
533 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
535 if ( nCopyLen
== 0 ) {
539 dest
.AllocBuffer(nCopyLen
);
540 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
544 // extract string of length nCount starting at nFirst
545 // default value of nCount is 0 and means "till the end"
546 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
548 // out-of-bounds requests return sensible things
550 nCount
= GetStringData()->nDataLength
- nFirst
;
552 if ( nFirst
+ nCount
> (size_t)GetStringData()->nDataLength
)
553 nCount
= GetStringData()->nDataLength
- nFirst
;
554 if ( nFirst
> (size_t)GetStringData()->nDataLength
)
558 AllocCopy(dest
, nCount
, nFirst
);
562 // extract nCount last (rightmost) characters
563 wxString
wxString::Right(size_t nCount
) const
565 if ( nCount
> (size_t)GetStringData()->nDataLength
)
566 nCount
= GetStringData()->nDataLength
;
569 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
573 // get all characters after the last occurence of ch
574 // (returns the whole string if ch not found)
575 wxString
wxString::Right(char ch
) const
578 int iPos
= Find(ch
, TRUE
);
579 if ( iPos
== NOT_FOUND
)
582 str
= c_str() + iPos
+ 1;
587 // extract nCount first (leftmost) characters
588 wxString
wxString::Left(size_t nCount
) const
590 if ( nCount
> (size_t)GetStringData()->nDataLength
)
591 nCount
= GetStringData()->nDataLength
;
594 AllocCopy(dest
, nCount
, 0);
598 // get all characters before the first occurence of ch
599 // (returns the whole string if ch not found)
600 wxString
wxString::Left(char ch
) const
603 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
609 /// get all characters before the last occurence of ch
610 /// (returns empty string if ch not found)
611 wxString
wxString::Before(char ch
) const
614 int iPos
= Find(ch
, TRUE
);
615 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
616 str
= wxString(c_str(), iPos
);
621 /// get all characters after the first occurence of ch
622 /// (returns empty string if ch not found)
623 wxString
wxString::After(char ch
) const
627 if ( iPos
!= NOT_FOUND
)
628 str
= c_str() + iPos
+ 1;
633 // replace first (or all) occurences of some substring with another one
634 uint
wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
636 uint uiCount
= 0; // count of replacements made
638 uint uiOldLen
= Strlen(szOld
);
641 const char *pCurrent
= m_pchData
;
643 while ( *pCurrent
!= '\0' ) {
644 pSubstr
= strstr(pCurrent
, szOld
);
645 if ( pSubstr
== NULL
) {
646 // strTemp is unused if no replacements were made, so avoid the copy
650 strTemp
+= pCurrent
; // copy the rest
651 break; // exit the loop
654 // take chars before match
655 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
657 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
662 if ( !bReplaceAll
) {
663 strTemp
+= pCurrent
; // copy the rest
664 break; // exit the loop
669 // only done if there were replacements, otherwise would have returned above
675 bool wxString::IsAscii() const
677 const char *s
= (const char*) *this;
679 if(!isascii(*s
)) return(FALSE
);
685 bool wxString::IsWord() const
687 const char *s
= (const char*) *this;
689 if(!isalpha(*s
)) return(FALSE
);
695 bool wxString::IsNumber() const
697 const char *s
= (const char*) *this;
699 if(!isdigit(*s
)) return(FALSE
);
705 wxString
wxString::Strip(stripType w
) const
708 if ( w
& leading
) s
.Trim(FALSE
);
709 if ( w
& trailing
) s
.Trim(TRUE
);
713 // ---------------------------------------------------------------------------
715 // ---------------------------------------------------------------------------
717 wxString
& wxString::MakeUpper()
721 for ( char *p
= m_pchData
; *p
; p
++ )
722 *p
= (char)toupper(*p
);
727 wxString
& wxString::MakeLower()
731 for ( char *p
= m_pchData
; *p
; p
++ )
732 *p
= (char)tolower(*p
);
737 // ---------------------------------------------------------------------------
738 // trimming and padding
739 // ---------------------------------------------------------------------------
741 // trims spaces (in the sense of isspace) from left or right side
742 wxString
& wxString::Trim(bool bFromRight
)
748 // find last non-space character
749 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
750 while ( isspace(*psz
) && (psz
>= m_pchData
) )
753 // truncate at trailing space start
755 GetStringData()->nDataLength
= psz
- m_pchData
;
759 // find first non-space character
760 const char *psz
= m_pchData
;
761 while ( isspace(*psz
) )
764 // fix up data and length
765 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
766 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
767 GetStringData()->nDataLength
= nDataLength
;
773 // adds nCount characters chPad to the string from either side
774 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
776 wxString
s(chPad
, nCount
);
789 // truncate the string
790 wxString
& wxString::Truncate(size_t uiLen
)
792 *(m_pchData
+ uiLen
) = '\0';
793 GetStringData()->nDataLength
= uiLen
;
798 // ---------------------------------------------------------------------------
799 // finding (return NOT_FOUND if not found and index otherwise)
800 // ---------------------------------------------------------------------------
803 int wxString::Find(char ch
, bool bFromEnd
) const
805 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
807 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
810 // find a sub-string (like strstr)
811 int wxString::Find(const char *pszSub
) const
813 const char *psz
= strstr(m_pchData
, pszSub
);
815 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
818 // ---------------------------------------------------------------------------
820 // ---------------------------------------------------------------------------
821 int wxString::Printf(const char *pszFormat
, ...)
824 va_start(argptr
, pszFormat
);
826 int iLen
= PrintfV(pszFormat
, argptr
);
833 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
835 static char s_szScratch
[1024];
837 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
838 AllocBeforeWrite(iLen
);
839 strcpy(m_pchData
, s_szScratch
);
844 // ----------------------------------------------------------------------------
845 // misc other operations
846 // ----------------------------------------------------------------------------
847 bool wxString::Matches(const char *pszMask
) const
849 // check char by char
851 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
852 switch ( *pszMask
) {
854 if ( *pszTxt
== '\0' )
863 // ignore special chars immediately following this one
864 while ( *pszMask
== '*' || *pszMask
== '?' )
867 // if there is nothing more, match
868 if ( *pszMask
== '\0' )
871 // are there any other metacharacters in the mask?
873 const char *pEndMask
= strpbrk(pszMask
, "*?");
875 if ( pEndMask
!= NULL
) {
876 // we have to match the string between two metachars
877 uiLenMask
= pEndMask
- pszMask
;
880 // we have to match the remainder of the string
881 uiLenMask
= strlen(pszMask
);
884 wxString
strToMatch(pszMask
, uiLenMask
);
885 const char* pMatch
= strstr(pszTxt
, strToMatch
);
886 if ( pMatch
== NULL
)
889 // -1 to compensate "++" in the loop
890 pszTxt
= pMatch
+ uiLenMask
- 1;
891 pszMask
+= uiLenMask
- 1;
896 if ( *pszMask
!= *pszTxt
)
902 // match only if nothing left
903 return *pszTxt
== '\0';
906 // ---------------------------------------------------------------------------
907 // standard C++ library string functions
908 // ---------------------------------------------------------------------------
909 #ifdef STD_STRING_COMPATIBILITY
911 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
913 wxASSERT( str
.GetStringData()->IsValid() );
914 wxASSERT( nPos
<= Len() );
917 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
918 strncpy(pc
, c_str(), nPos
);
919 strcpy(pc
+ nPos
, str
);
920 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
921 strTmp
.UngetWriteBuf();
927 size_t wxString::find(const wxString
& str
, size_t nStart
) const
929 wxASSERT( str
.GetStringData()->IsValid() );
930 wxASSERT( nStart
<= Len() );
932 const char *p
= strstr(c_str() + nStart
, str
);
934 return p
== NULL
? npos
: p
- c_str();
937 // VC++ 1.5 can't cope with the default argument in the header.
938 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
939 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
941 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
945 size_t wxString::find(char ch
, size_t nStart
) const
947 wxASSERT( nStart
<= Len() );
949 const char *p
= strchr(c_str() + nStart
, ch
);
951 return p
== NULL
? npos
: p
- c_str();
954 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
956 wxASSERT( str
.GetStringData()->IsValid() );
957 wxASSERT( nStart
<= Len() );
959 // # could be quicker than that
960 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
961 while ( p
>= c_str() + str
.Len() ) {
962 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
963 return p
- str
.Len() - c_str();
970 // VC++ 1.5 can't cope with the default argument in the header.
971 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
972 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
974 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
977 size_t wxString::rfind(char ch
, size_t nStart
) const
979 wxASSERT( nStart
<= Len() );
981 const char *p
= strrchr(c_str() + nStart
, ch
);
983 return p
== NULL
? npos
: p
- c_str();
987 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
989 // npos means 'take all'
993 wxASSERT( nStart
+ nLen
<= Len() );
995 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
998 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1000 wxString
strTmp(c_str(), nStart
);
1001 if ( nLen
!= npos
) {
1002 wxASSERT( nStart
+ nLen
<= Len() );
1004 strTmp
.append(c_str() + nStart
+ nLen
);
1011 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1013 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1017 strTmp
.append(c_str(), nStart
);
1019 strTmp
.append(c_str() + nStart
+ nLen
);
1025 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1027 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1030 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1031 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1033 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1036 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1037 const char* sz
, size_t nCount
)
1039 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1042 #endif //std::string compatibility
1044 // ============================================================================
1046 // ============================================================================
1048 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1049 #define ARRAY_MAXSIZE_INCREMENT 4096
1050 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1051 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1054 #define STRING(p) ((wxString *)(&(p)))
1057 wxArrayString::wxArrayString()
1065 wxArrayString::wxArrayString(const wxArrayString
& src
)
1067 m_nSize
= src
.m_nSize
;
1068 m_nCount
= src
.m_nCount
;
1071 m_pItems
= new char *[m_nSize
];
1075 if ( m_nCount
!= 0 )
1076 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1080 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1084 m_nSize
= src
.m_nSize
;
1085 m_nCount
= src
.m_nCount
;
1088 m_pItems
= new char *[m_nCount
];
1092 if ( m_nCount
!= 0 )
1093 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1099 void wxArrayString::Grow()
1101 // only do it if no more place
1102 if( m_nCount
== m_nSize
) {
1103 if( m_nSize
== 0 ) {
1104 // was empty, alloc some memory
1105 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1106 m_pItems
= new char *[m_nSize
];
1109 // add 50% but not too much
1110 size_t nIncrement
= m_nSize
>> 1;
1111 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1112 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1113 m_nSize
+= nIncrement
;
1114 char **pNew
= new char *[m_nSize
];
1116 // copy data to new location
1117 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1119 // delete old memory (but do not release the strings!)
1127 void wxArrayString::Free()
1129 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1130 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1134 // deletes all the strings from the list
1135 void wxArrayString::Empty()
1142 // as Empty, but also frees memory
1143 void wxArrayString::Clear()
1155 wxArrayString::~wxArrayString()
1162 // pre-allocates memory (frees the previous data!)
1163 void wxArrayString::Alloc(size_t nSize
)
1165 wxASSERT( nSize
> 0 );
1167 // only if old buffer was not big enough
1168 if ( nSize
> m_nSize
) {
1171 m_pItems
= new char *[nSize
];
1178 // searches the array for an item (forward or backwards)
1179 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1182 if ( m_nCount
> 0 ) {
1185 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1192 for( uint ui
= 0; ui
< m_nCount
; ui
++ ) {
1193 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1201 // add item at the end
1202 void wxArrayString::Add(const wxString
& str
)
1204 wxASSERT( str
.GetStringData()->IsValid() );
1208 // the string data must not be deleted!
1209 str
.GetStringData()->Lock();
1210 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1213 // add item at the given position
1214 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1216 wxASSERT( str
.GetStringData()->IsValid() );
1218 wxCHECK_RET( nIndex
<= m_nCount
, "bad index in wxArrayString::Insert" );
1222 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1223 (m_nCount
- nIndex
)*sizeof(char *));
1225 str
.GetStringData()->Lock();
1226 m_pItems
[nIndex
] = (char *)str
.c_str();
1231 // removes item from array (by index)
1232 void wxArrayString::Remove(size_t nIndex
)
1234 wxCHECK_RET( nIndex
<= m_nCount
, "bad index in wxArrayString::Remove" );
1237 Item(nIndex
).GetStringData()->Unlock();
1239 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1240 (m_nCount
- nIndex
- 1)*sizeof(char *));
1244 // removes item from array (by value)
1245 void wxArrayString::Remove(const char *sz
)
1247 int iIndex
= Index(sz
);
1249 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1250 "removing inexistent element in wxArrayString::Remove" );
1252 Remove((size_t)iIndex
);
1255 // sort array elements using passed comparaison function
1257 void wxArrayString::Sort(bool bCase
, bool bReverse
)
1260 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);