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() ) {
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();
326 void *p
= realloc(pData
, sizeof(wxStringData
) +
327 (pData
->nDataLength
+ 1)*sizeof(char));
328 wxASSERT( p
!= NULL
); // can't free memory?
329 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
332 // get the pointer to writable buffer of (at least) nLen bytes
333 char *wxString::GetWriteBuf(uint nLen
)
335 AllocBeforeWrite(nLen
);
337 wxASSERT( GetStringData()->nRefs
== 1 );
338 GetStringData()->Validate(FALSE
);
343 // put string back in a reasonable state after GetWriteBuf
344 void wxString::UngetWriteBuf()
346 GetStringData()->nDataLength
= strlen(m_pchData
);
347 GetStringData()->Validate(TRUE
);
350 // ---------------------------------------------------------------------------
352 // ---------------------------------------------------------------------------
354 // all functions are inline in string.h
356 // ---------------------------------------------------------------------------
357 // assignment operators
358 // ---------------------------------------------------------------------------
360 // helper function: does real copy
361 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
363 if ( nSrcLen
== 0 ) {
367 AllocBeforeWrite(nSrcLen
);
368 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
369 GetStringData()->nDataLength
= nSrcLen
;
370 m_pchData
[nSrcLen
] = '\0';
374 // assigns one string to another
375 wxString
& wxString::operator=(const wxString
& stringSrc
)
377 wxASSERT( stringSrc
.GetStringData()->IsValid() );
379 // don't copy string over itself
380 if ( m_pchData
!= stringSrc
.m_pchData
) {
381 if ( stringSrc
.GetStringData()->IsEmpty() ) {
386 GetStringData()->Unlock();
387 m_pchData
= stringSrc
.m_pchData
;
388 GetStringData()->Lock();
395 // assigns a single character
396 wxString
& wxString::operator=(char ch
)
403 wxString
& wxString::operator=(const char *psz
)
405 AssignCopy(Strlen(psz
), psz
);
409 // same as 'signed char' variant
410 wxString
& wxString::operator=(const unsigned char* psz
)
412 *this = (const char *)psz
;
416 wxString
& wxString::operator=(const wchar_t *pwz
)
423 // ---------------------------------------------------------------------------
424 // string concatenation
425 // ---------------------------------------------------------------------------
427 // add something to this string
428 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
430 STATISTICS_ADD(SummandLength
, nSrcLen
);
432 // concatenating an empty string is a NOP, but it happens quite rarely,
433 // so we don't waste our time checking for it
434 // if ( nSrcLen > 0 )
435 wxStringData
*pData
= GetStringData();
436 uint nLen
= pData
->nDataLength
;
437 uint nNewLen
= nLen
+ nSrcLen
;
439 // alloc new buffer if current is too small
440 if ( pData
->IsShared() ) {
441 STATISTICS_ADD(ConcatHit
, 0);
443 // we have to allocate another buffer
444 wxStringData
* pOldData
= GetStringData();
445 AllocBuffer(nNewLen
);
446 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
449 else if ( nNewLen
> pData
->nAllocLength
) {
450 STATISTICS_ADD(ConcatHit
, 0);
452 // we have to grow the buffer
456 STATISTICS_ADD(ConcatHit
, 1);
458 // the buffer is already big enough
461 // should be enough space
462 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
464 // fast concatenation - all is done in our buffer
465 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
467 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
468 GetStringData()->nDataLength
= nNewLen
; // and fix the length
472 * concatenation functions come in 5 flavours:
474 * char + string and string + char
475 * C str + string and string + C str
478 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
480 wxASSERT( string1
.GetStringData()->IsValid() );
481 wxASSERT( string2
.GetStringData()->IsValid() );
483 wxString s
= string1
;
489 wxString
operator+(const wxString
& string
, char ch
)
491 wxASSERT( string
.GetStringData()->IsValid() );
499 wxString
operator+(char ch
, const wxString
& string
)
501 wxASSERT( string
.GetStringData()->IsValid() );
509 wxString
operator+(const wxString
& string
, const char *psz
)
511 wxASSERT( string
.GetStringData()->IsValid() );
514 s
.Alloc(Strlen(psz
) + string
.Len());
521 wxString
operator+(const char *psz
, const wxString
& string
)
523 wxASSERT( string
.GetStringData()->IsValid() );
526 s
.Alloc(Strlen(psz
) + string
.Len());
533 // ===========================================================================
534 // other common string functions
535 // ===========================================================================
537 // ---------------------------------------------------------------------------
538 // simple sub-string extraction
539 // ---------------------------------------------------------------------------
541 // helper function: clone the data attached to this string
542 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
544 if ( nCopyLen
== 0 ) {
548 dest
.AllocBuffer(nCopyLen
);
549 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
553 // extract string of length nCount starting at nFirst
554 // default value of nCount is 0 and means "till the end"
555 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
557 // out-of-bounds requests return sensible things
559 nCount
= GetStringData()->nDataLength
- nFirst
;
561 if ( nFirst
+ nCount
> (size_t)GetStringData()->nDataLength
)
562 nCount
= GetStringData()->nDataLength
- nFirst
;
563 if ( nFirst
> (size_t)GetStringData()->nDataLength
)
567 AllocCopy(dest
, nCount
, nFirst
);
571 // extract nCount last (rightmost) characters
572 wxString
wxString::Right(size_t nCount
) const
574 if ( nCount
> (size_t)GetStringData()->nDataLength
)
575 nCount
= GetStringData()->nDataLength
;
578 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
582 // get all characters after the last occurence of ch
583 // (returns the whole string if ch not found)
584 wxString
wxString::Right(char ch
) const
587 int iPos
= Find(ch
, TRUE
);
588 if ( iPos
== NOT_FOUND
)
591 str
= c_str() + iPos
+ 1;
596 // extract nCount first (leftmost) characters
597 wxString
wxString::Left(size_t nCount
) const
599 if ( nCount
> (size_t)GetStringData()->nDataLength
)
600 nCount
= GetStringData()->nDataLength
;
603 AllocCopy(dest
, nCount
, 0);
607 // get all characters before the first occurence of ch
608 // (returns the whole string if ch not found)
609 wxString
wxString::Left(char ch
) const
612 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
618 /// get all characters before the last occurence of ch
619 /// (returns empty string if ch not found)
620 wxString
wxString::Before(char ch
) const
623 int iPos
= Find(ch
, TRUE
);
624 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
625 str
= wxString(c_str(), iPos
);
630 /// get all characters after the first occurence of ch
631 /// (returns empty string if ch not found)
632 wxString
wxString::After(char ch
) const
636 if ( iPos
!= NOT_FOUND
)
637 str
= c_str() + iPos
+ 1;
642 // replace first (or all) occurences of some substring with another one
643 uint
wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
645 uint uiCount
= 0; // count of replacements made
647 uint uiOldLen
= Strlen(szOld
);
650 const char *pCurrent
= m_pchData
;
652 while ( *pCurrent
!= '\0' ) {
653 pSubstr
= strstr(pCurrent
, szOld
);
654 if ( pSubstr
== NULL
) {
655 // strTemp is unused if no replacements were made, so avoid the copy
659 strTemp
+= pCurrent
; // copy the rest
660 break; // exit the loop
663 // take chars before match
664 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
666 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
671 if ( !bReplaceAll
) {
672 strTemp
+= pCurrent
; // copy the rest
673 break; // exit the loop
678 // only done if there were replacements, otherwise would have returned above
684 bool wxString::IsAscii() const
686 const char *s
= (const char*) *this;
688 if(!isascii(*s
)) return(FALSE
);
694 bool wxString::IsWord() const
696 const char *s
= (const char*) *this;
698 if(!isalpha(*s
)) return(FALSE
);
704 bool wxString::IsNumber() const
706 const char *s
= (const char*) *this;
708 if(!isdigit(*s
)) return(FALSE
);
714 wxString
wxString::Strip(stripType w
) const
717 if ( w
& leading
) s
.Trim(FALSE
);
718 if ( w
& trailing
) s
.Trim(TRUE
);
722 // ---------------------------------------------------------------------------
724 // ---------------------------------------------------------------------------
726 wxString
& wxString::MakeUpper()
730 for ( char *p
= m_pchData
; *p
; p
++ )
731 *p
= (char)toupper(*p
);
736 wxString
& wxString::MakeLower()
740 for ( char *p
= m_pchData
; *p
; p
++ )
741 *p
= (char)tolower(*p
);
746 // ---------------------------------------------------------------------------
747 // trimming and padding
748 // ---------------------------------------------------------------------------
750 // trims spaces (in the sense of isspace) from left or right side
751 wxString
& wxString::Trim(bool bFromRight
)
757 // find last non-space character
758 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
759 while ( isspace(*psz
) && (psz
>= m_pchData
) )
762 // truncate at trailing space start
764 GetStringData()->nDataLength
= psz
- m_pchData
;
768 // find first non-space character
769 const char *psz
= m_pchData
;
770 while ( isspace(*psz
) )
773 // fix up data and length
774 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
775 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
776 GetStringData()->nDataLength
= nDataLength
;
782 // adds nCount characters chPad to the string from either side
783 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
785 wxString
s(chPad
, nCount
);
798 // truncate the string
799 wxString
& wxString::Truncate(size_t uiLen
)
801 *(m_pchData
+ uiLen
) = '\0';
802 GetStringData()->nDataLength
= uiLen
;
807 // ---------------------------------------------------------------------------
808 // finding (return NOT_FOUND if not found and index otherwise)
809 // ---------------------------------------------------------------------------
812 int wxString::Find(char ch
, bool bFromEnd
) const
814 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
816 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
819 // find a sub-string (like strstr)
820 int wxString::Find(const char *pszSub
) const
822 const char *psz
= strstr(m_pchData
, pszSub
);
824 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
827 // ---------------------------------------------------------------------------
829 // ---------------------------------------------------------------------------
830 int wxString::Printf(const char *pszFormat
, ...)
833 va_start(argptr
, pszFormat
);
835 int iLen
= PrintfV(pszFormat
, argptr
);
842 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
844 static char s_szScratch
[1024];
846 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
847 AllocBeforeWrite(iLen
);
848 strcpy(m_pchData
, s_szScratch
);
853 // ----------------------------------------------------------------------------
854 // misc other operations
855 // ----------------------------------------------------------------------------
856 bool wxString::Matches(const char *pszMask
) const
858 // check char by char
860 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
861 switch ( *pszMask
) {
863 if ( *pszTxt
== '\0' )
872 // ignore special chars immediately following this one
873 while ( *pszMask
== '*' || *pszMask
== '?' )
876 // if there is nothing more, match
877 if ( *pszMask
== '\0' )
880 // are there any other metacharacters in the mask?
882 const char *pEndMask
= strpbrk(pszMask
, "*?");
884 if ( pEndMask
!= NULL
) {
885 // we have to match the string between two metachars
886 uiLenMask
= pEndMask
- pszMask
;
889 // we have to match the remainder of the string
890 uiLenMask
= strlen(pszMask
);
893 wxString
strToMatch(pszMask
, uiLenMask
);
894 const char* pMatch
= strstr(pszTxt
, strToMatch
);
895 if ( pMatch
== NULL
)
898 // -1 to compensate "++" in the loop
899 pszTxt
= pMatch
+ uiLenMask
- 1;
900 pszMask
+= uiLenMask
- 1;
905 if ( *pszMask
!= *pszTxt
)
911 // match only if nothing left
912 return *pszTxt
== '\0';
915 // ---------------------------------------------------------------------------
916 // standard C++ library string functions
917 // ---------------------------------------------------------------------------
918 #ifdef STD_STRING_COMPATIBILITY
920 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
922 wxASSERT( str
.GetStringData()->IsValid() );
923 wxASSERT( nPos
<= Len() );
926 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
927 strncpy(pc
, c_str(), nPos
);
928 strcpy(pc
+ nPos
, str
);
929 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
930 strTmp
.UngetWriteBuf();
936 size_t wxString::find(const wxString
& str
, size_t nStart
) const
938 wxASSERT( str
.GetStringData()->IsValid() );
939 wxASSERT( nStart
<= Len() );
941 const char *p
= strstr(c_str() + nStart
, str
);
943 return p
== NULL
? npos
: p
- c_str();
946 // VC++ 1.5 can't cope with the default argument in the header.
947 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
948 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
950 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
954 size_t wxString::find(char ch
, size_t nStart
) const
956 wxASSERT( nStart
<= Len() );
958 const char *p
= strchr(c_str() + nStart
, ch
);
960 return p
== NULL
? npos
: p
- c_str();
963 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
965 wxASSERT( str
.GetStringData()->IsValid() );
966 wxASSERT( nStart
<= Len() );
968 // # could be quicker than that
969 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
970 while ( p
>= c_str() + str
.Len() ) {
971 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
972 return p
- str
.Len() - c_str();
979 // VC++ 1.5 can't cope with the default argument in the header.
980 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
981 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
983 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
986 size_t wxString::rfind(char ch
, size_t nStart
) const
988 wxASSERT( nStart
<= Len() );
990 const char *p
= strrchr(c_str() + nStart
, ch
);
992 return p
== NULL
? npos
: p
- c_str();
996 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
998 // npos means 'take all'
1002 wxASSERT( nStart
+ nLen
<= Len() );
1004 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1007 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1009 wxString
strTmp(c_str(), nStart
);
1010 if ( nLen
!= npos
) {
1011 wxASSERT( nStart
+ nLen
<= Len() );
1013 strTmp
.append(c_str() + nStart
+ nLen
);
1020 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1022 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1026 strTmp
.append(c_str(), nStart
);
1028 strTmp
.append(c_str() + nStart
+ nLen
);
1034 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1036 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1039 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1040 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1042 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1045 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1046 const char* sz
, size_t nCount
)
1048 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1051 #endif //std::string compatibility
1053 // ============================================================================
1055 // ============================================================================
1057 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1058 #define ARRAY_MAXSIZE_INCREMENT 4096
1059 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1060 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1063 #define STRING(p) ((wxString *)(&(p)))
1066 wxArrayString::wxArrayString()
1074 wxArrayString::wxArrayString(const wxArrayString
& src
)
1081 // assignment operator
1082 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1087 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1088 Alloc(src
.m_nCount
);
1090 // we can't just copy the pointers here because otherwise we would share
1091 // the strings with another array
1092 for ( uint n
= 0; n
< src
.m_nCount
; n
++ )
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
< ARRAY_DEFAULT_INITIAL_SIZE
1111 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1112 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1113 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1114 m_nSize
+= nIncrement
;
1115 char **pNew
= new char *[m_nSize
];
1117 // copy data to new location
1118 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1120 // delete old memory (but do not release the strings!)
1128 void wxArrayString::Free()
1130 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1131 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1135 // deletes all the strings from the list
1136 void wxArrayString::Empty()
1143 // as Empty, but also frees memory
1144 void wxArrayString::Clear()
1156 wxArrayString::~wxArrayString()
1163 // pre-allocates memory (frees the previous data!)
1164 void wxArrayString::Alloc(size_t nSize
)
1166 wxASSERT( nSize
> 0 );
1168 // only if old buffer was not big enough
1169 if ( nSize
> m_nSize
) {
1172 m_pItems
= new char *[nSize
];
1179 // searches the array for an item (forward or backwards)
1180 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1183 if ( m_nCount
> 0 ) {
1186 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1193 for( uint ui
= 0; ui
< m_nCount
; ui
++ ) {
1194 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1202 // add item at the end
1203 void wxArrayString::Add(const wxString
& str
)
1205 wxASSERT( str
.GetStringData()->IsValid() );
1209 // the string data must not be deleted!
1210 str
.GetStringData()->Lock();
1211 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1214 // add item at the given position
1215 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1217 wxASSERT( str
.GetStringData()->IsValid() );
1219 wxCHECK_RET( nIndex
<= m_nCount
, "bad index in wxArrayString::Insert" );
1223 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1224 (m_nCount
- nIndex
)*sizeof(char *));
1226 str
.GetStringData()->Lock();
1227 m_pItems
[nIndex
] = (char *)str
.c_str();
1232 // removes item from array (by index)
1233 void wxArrayString::Remove(size_t nIndex
)
1235 wxCHECK_RET( nIndex
<= m_nCount
, "bad index in wxArrayString::Remove" );
1238 Item(nIndex
).GetStringData()->Unlock();
1240 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1241 (m_nCount
- nIndex
- 1)*sizeof(char *));
1245 // removes item from array (by value)
1246 void wxArrayString::Remove(const char *sz
)
1248 int iIndex
= Index(sz
);
1250 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1251 "removing inexistent element in wxArrayString::Remove" );
1253 Remove((size_t)iIndex
);
1256 // sort array elements using passed comparaison function
1258 void wxArrayString::Sort(bool bCase
, bool bReverse
)
1261 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);