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 #define EXTRA_ALLOC 16
52 // ---------------------------------------------------------------------------
53 // static class variables definition
54 // ---------------------------------------------------------------------------
56 #ifdef STD_STRING_COMPATIBILITY
57 const size_t wxString::npos
= STRING_MAXLEN
;
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // for an empty string, GetStringData() will return this address
65 static int g_strEmpty
[] = { -1, // ref count (locked)
67 0, // allocated memory
69 // empty C style string: points to 'string data' byte of g_strEmpty
70 extern const char *g_szNul
= (const char *)(&g_strEmpty
[3]);
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 #ifdef STD_STRING_COMPATIBILITY
78 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
81 // ATTN: you can _not_ use both of these in the same program!
84 #define NAMESPACE std::
90 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
95 NAMESPACE streambuf
*sb
= is
.rdbuf();
98 int ch
= sb
->sbumpc ();
100 is
.setstate(NAMESPACE
ios::eofbit
);
103 else if ( isspace(ch
) ) {
115 if ( str
.length() == 0 )
116 is
.setstate(NAMESPACE
ios::failbit
);
121 #endif //std::string compatibility
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // this small class is used to gather statistics for performance tuning
128 //#define WXSTRING_STATISTICS
129 #ifdef WXSTRING_STATISTICS
133 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
135 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
137 void Add(uint n
) { m_nTotal
+= n
; m_nCount
++; }
140 uint m_nCount
, m_nTotal
;
142 } g_averageLength("allocation size"),
143 g_averageSummandLength("summand length"),
144 g_averageConcatHit("hit probability in concat"),
145 g_averageInitialLength("initial string length");
147 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
149 #define STATISTICS_ADD(av, val)
150 #endif // WXSTRING_STATISTICS
152 // ===========================================================================
153 // wxString class core
154 // ===========================================================================
156 // ---------------------------------------------------------------------------
158 // ---------------------------------------------------------------------------
160 // constructs string of <nLength> copies of character <ch>
161 wxString::wxString(char ch
, size_t nLength
)
166 AllocBuffer(nLength
);
168 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
170 memset(m_pchData
, ch
, nLength
);
174 // takes nLength elements of psz starting at nPos
175 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
179 wxASSERT( nPos
<= Strlen(psz
) );
181 if ( nLength
== STRING_MAXLEN
)
182 nLength
= Strlen(psz
+ nPos
);
184 STATISTICS_ADD(InitialLength
, nLength
);
187 // trailing '\0' is written in AllocBuffer()
188 AllocBuffer(nLength
);
189 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
193 // the same as previous constructor, but for compilers using unsigned char
194 wxString::wxString(const unsigned char* psz
, size_t nLength
)
196 InitWith((const char *)psz
, 0, nLength
);
199 #ifdef STD_STRING_COMPATIBILITY
201 // poor man's iterators are "void *" pointers
202 wxString::wxString(const void *pStart
, const void *pEnd
)
204 InitWith((const char *)pStart
, 0,
205 (const char *)pEnd
- (const char *)pStart
);
208 #endif //std::string compatibility
211 wxString::wxString(const wchar_t *pwz
)
213 // first get necessary size
214 size_t nLen
= wcstombs(NULL
, pwz
, 0);
219 wcstombs(m_pchData
, pwz
, nLen
);
226 // ---------------------------------------------------------------------------
228 // ---------------------------------------------------------------------------
230 // allocates memory needed to store a C string of length nLen
231 void wxString::AllocBuffer(size_t nLen
)
233 wxASSERT( nLen
> 0 ); //
234 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
236 STATISTICS_ADD(Length
, nLen
);
239 // 1) one extra character for '\0' termination
240 // 2) sizeof(wxStringData) for housekeeping info
241 wxStringData
* pData
= (wxStringData
*)
242 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
244 pData
->nDataLength
= nLen
;
245 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
246 m_pchData
= pData
->data(); // data starts after wxStringData
247 m_pchData
[nLen
] = '\0';
250 // must be called before changing this string
251 void wxString::CopyBeforeWrite()
253 wxStringData
* pData
= GetStringData();
255 if ( pData
->IsShared() ) {
256 pData
->Unlock(); // memory not freed because shared
257 uint nLen
= pData
->nDataLength
;
259 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
262 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
265 // must be called before replacing contents of this string
266 void wxString::AllocBeforeWrite(size_t nLen
)
268 wxASSERT( nLen
!= 0 ); // doesn't make any sense
270 // must not share string and must have enough space
271 wxStringData
* pData
= GetStringData();
272 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
273 // can't work with old buffer, get new one
278 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
281 // allocate enough memory for nLen characters
282 void wxString::Alloc(uint nLen
)
284 wxStringData
*pData
= GetStringData();
285 if ( pData
->nAllocLength
<= nLen
) {
286 if ( pData
->IsEmpty() ) {
289 wxStringData
* pData
= (wxStringData
*)
290 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
292 pData
->nDataLength
= 0;
293 pData
->nAllocLength
= nLen
;
294 m_pchData
= pData
->data(); // data starts after wxStringData
295 m_pchData
[0u] = '\0';
297 else if ( pData
->IsShared() ) {
298 pData
->Unlock(); // memory not freed because shared
299 uint nOldLen
= pData
->nDataLength
;
301 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
306 wxStringData
*p
= (wxStringData
*)
307 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
310 // @@@ what to do on memory error?
314 // it's not important if the pointer changed or not (the check for this
315 // is not faster than assigning to m_pchData in all cases)
316 p
->nAllocLength
= nLen
;
317 m_pchData
= p
->data();
320 //else: we've already got enough
323 // shrink to minimal size (releasing extra memory)
324 void wxString::Shrink()
326 wxStringData
*pData
= GetStringData();
328 // this variable is unused in release build, so avoid the compiler warning by
329 // just not declaring it
333 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
335 wxASSERT( p
!= NULL
); // can't free memory?
336 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
339 // get the pointer to writable buffer of (at least) nLen bytes
340 char *wxString::GetWriteBuf(uint nLen
)
342 AllocBeforeWrite(nLen
);
344 wxASSERT( GetStringData()->nRefs
== 1 );
345 GetStringData()->Validate(FALSE
);
350 // put string back in a reasonable state after GetWriteBuf
351 void wxString::UngetWriteBuf()
353 GetStringData()->nDataLength
= strlen(m_pchData
);
354 GetStringData()->Validate(TRUE
);
357 // ---------------------------------------------------------------------------
359 // ---------------------------------------------------------------------------
361 // all functions are inline in string.h
363 // ---------------------------------------------------------------------------
364 // assignment operators
365 // ---------------------------------------------------------------------------
367 // helper function: does real copy
368 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
370 if ( nSrcLen
== 0 ) {
374 AllocBeforeWrite(nSrcLen
);
375 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
376 GetStringData()->nDataLength
= nSrcLen
;
377 m_pchData
[nSrcLen
] = '\0';
381 // assigns one string to another
382 wxString
& wxString::operator=(const wxString
& stringSrc
)
384 wxASSERT( stringSrc
.GetStringData()->IsValid() );
386 // don't copy string over itself
387 if ( m_pchData
!= stringSrc
.m_pchData
) {
388 if ( stringSrc
.GetStringData()->IsEmpty() ) {
393 GetStringData()->Unlock();
394 m_pchData
= stringSrc
.m_pchData
;
395 GetStringData()->Lock();
402 // assigns a single character
403 wxString
& wxString::operator=(char ch
)
410 wxString
& wxString::operator=(const char *psz
)
412 AssignCopy(Strlen(psz
), psz
);
416 // same as 'signed char' variant
417 wxString
& wxString::operator=(const unsigned char* psz
)
419 *this = (const char *)psz
;
423 wxString
& wxString::operator=(const wchar_t *pwz
)
430 // ---------------------------------------------------------------------------
431 // string concatenation
432 // ---------------------------------------------------------------------------
434 // add something to this string
435 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
437 STATISTICS_ADD(SummandLength
, nSrcLen
);
439 // concatenating an empty string is a NOP, but it happens quite rarely,
440 // so we don't waste our time checking for it
441 // if ( nSrcLen > 0 )
442 wxStringData
*pData
= GetStringData();
443 uint nLen
= pData
->nDataLength
;
444 uint nNewLen
= nLen
+ nSrcLen
;
446 // alloc new buffer if current is too small
447 if ( pData
->IsShared() ) {
448 STATISTICS_ADD(ConcatHit
, 0);
450 // we have to allocate another buffer
451 wxStringData
* pOldData
= GetStringData();
452 AllocBuffer(nNewLen
);
453 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
456 else if ( nNewLen
> pData
->nAllocLength
) {
457 STATISTICS_ADD(ConcatHit
, 0);
459 // we have to grow the buffer
463 STATISTICS_ADD(ConcatHit
, 1);
465 // the buffer is already big enough
468 // should be enough space
469 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
471 // fast concatenation - all is done in our buffer
472 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
474 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
475 GetStringData()->nDataLength
= nNewLen
; // and fix the length
479 * concatenation functions come in 5 flavours:
481 * char + string and string + char
482 * C str + string and string + C str
485 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
487 wxASSERT( string1
.GetStringData()->IsValid() );
488 wxASSERT( string2
.GetStringData()->IsValid() );
490 wxString s
= string1
;
496 wxString
operator+(const wxString
& string
, char ch
)
498 wxASSERT( string
.GetStringData()->IsValid() );
506 wxString
operator+(char ch
, const wxString
& string
)
508 wxASSERT( string
.GetStringData()->IsValid() );
516 wxString
operator+(const wxString
& string
, const char *psz
)
518 wxASSERT( string
.GetStringData()->IsValid() );
521 s
.Alloc(Strlen(psz
) + string
.Len());
528 wxString
operator+(const char *psz
, const wxString
& string
)
530 wxASSERT( string
.GetStringData()->IsValid() );
533 s
.Alloc(Strlen(psz
) + string
.Len());
540 // ===========================================================================
541 // other common string functions
542 // ===========================================================================
544 // ---------------------------------------------------------------------------
545 // simple sub-string extraction
546 // ---------------------------------------------------------------------------
548 // helper function: clone the data attached to this string
549 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
551 if ( nCopyLen
== 0 ) {
555 dest
.AllocBuffer(nCopyLen
);
556 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
560 // extract string of length nCount starting at nFirst
561 // default value of nCount is 0 and means "till the end"
562 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
564 // out-of-bounds requests return sensible things
566 nCount
= GetStringData()->nDataLength
- nFirst
;
568 if ( nFirst
+ nCount
> (size_t)GetStringData()->nDataLength
)
569 nCount
= GetStringData()->nDataLength
- nFirst
;
570 if ( nFirst
> (size_t)GetStringData()->nDataLength
)
574 AllocCopy(dest
, nCount
, nFirst
);
578 // extract nCount last (rightmost) characters
579 wxString
wxString::Right(size_t nCount
) const
581 if ( nCount
> (size_t)GetStringData()->nDataLength
)
582 nCount
= GetStringData()->nDataLength
;
585 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
589 // get all characters after the last occurence of ch
590 // (returns the whole string if ch not found)
591 wxString
wxString::Right(char ch
) const
594 int iPos
= Find(ch
, TRUE
);
595 if ( iPos
== NOT_FOUND
)
598 str
= c_str() + iPos
+ 1;
603 // extract nCount first (leftmost) characters
604 wxString
wxString::Left(size_t nCount
) const
606 if ( nCount
> (size_t)GetStringData()->nDataLength
)
607 nCount
= GetStringData()->nDataLength
;
610 AllocCopy(dest
, nCount
, 0);
614 // get all characters before the first occurence of ch
615 // (returns the whole string if ch not found)
616 wxString
wxString::Left(char ch
) const
619 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
625 /// get all characters before the last occurence of ch
626 /// (returns empty string if ch not found)
627 wxString
wxString::Before(char ch
) const
630 int iPos
= Find(ch
, TRUE
);
631 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
632 str
= wxString(c_str(), iPos
);
637 /// get all characters after the first occurence of ch
638 /// (returns empty string if ch not found)
639 wxString
wxString::After(char ch
) const
643 if ( iPos
!= NOT_FOUND
)
644 str
= c_str() + iPos
+ 1;
649 // replace first (or all) occurences of some substring with another one
650 uint
wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
652 uint uiCount
= 0; // count of replacements made
654 uint uiOldLen
= Strlen(szOld
);
657 const char *pCurrent
= m_pchData
;
659 while ( *pCurrent
!= '\0' ) {
660 pSubstr
= strstr(pCurrent
, szOld
);
661 if ( pSubstr
== NULL
) {
662 // strTemp is unused if no replacements were made, so avoid the copy
666 strTemp
+= pCurrent
; // copy the rest
667 break; // exit the loop
670 // take chars before match
671 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
673 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
678 if ( !bReplaceAll
) {
679 strTemp
+= pCurrent
; // copy the rest
680 break; // exit the loop
685 // only done if there were replacements, otherwise would have returned above
691 bool wxString::IsAscii() const
693 const char *s
= (const char*) *this;
695 if(!isascii(*s
)) return(FALSE
);
701 bool wxString::IsWord() const
703 const char *s
= (const char*) *this;
705 if(!isalpha(*s
)) return(FALSE
);
711 bool wxString::IsNumber() const
713 const char *s
= (const char*) *this;
715 if(!isdigit(*s
)) return(FALSE
);
721 wxString
wxString::Strip(stripType w
) const
724 if ( w
& leading
) s
.Trim(FALSE
);
725 if ( w
& trailing
) s
.Trim(TRUE
);
729 // ---------------------------------------------------------------------------
731 // ---------------------------------------------------------------------------
733 wxString
& wxString::MakeUpper()
737 for ( char *p
= m_pchData
; *p
; p
++ )
738 *p
= (char)toupper(*p
);
743 wxString
& wxString::MakeLower()
747 for ( char *p
= m_pchData
; *p
; p
++ )
748 *p
= (char)tolower(*p
);
753 // ---------------------------------------------------------------------------
754 // trimming and padding
755 // ---------------------------------------------------------------------------
757 // trims spaces (in the sense of isspace) from left or right side
758 wxString
& wxString::Trim(bool bFromRight
)
764 // find last non-space character
765 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
766 while ( isspace(*psz
) && (psz
>= m_pchData
) )
769 // truncate at trailing space start
771 GetStringData()->nDataLength
= psz
- m_pchData
;
775 // find first non-space character
776 const char *psz
= m_pchData
;
777 while ( isspace(*psz
) )
780 // fix up data and length
781 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
782 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
783 GetStringData()->nDataLength
= nDataLength
;
789 // adds nCount characters chPad to the string from either side
790 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
792 wxString
s(chPad
, nCount
);
805 // truncate the string
806 wxString
& wxString::Truncate(size_t uiLen
)
808 *(m_pchData
+ uiLen
) = '\0';
809 GetStringData()->nDataLength
= uiLen
;
814 // ---------------------------------------------------------------------------
815 // finding (return NOT_FOUND if not found and index otherwise)
816 // ---------------------------------------------------------------------------
819 int wxString::Find(char ch
, bool bFromEnd
) const
821 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
823 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
826 // find a sub-string (like strstr)
827 int wxString::Find(const char *pszSub
) const
829 const char *psz
= strstr(m_pchData
, pszSub
);
831 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
834 // ---------------------------------------------------------------------------
836 // ---------------------------------------------------------------------------
837 int wxString::Printf(const char *pszFormat
, ...)
840 va_start(argptr
, pszFormat
);
842 int iLen
= PrintfV(pszFormat
, argptr
);
849 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
851 static char s_szScratch
[1024];
853 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
854 AllocBeforeWrite(iLen
);
855 strcpy(m_pchData
, s_szScratch
);
860 // ----------------------------------------------------------------------------
861 // misc other operations
862 // ----------------------------------------------------------------------------
863 bool wxString::Matches(const char *pszMask
) const
865 // check char by char
867 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
868 switch ( *pszMask
) {
870 if ( *pszTxt
== '\0' )
879 // ignore special chars immediately following this one
880 while ( *pszMask
== '*' || *pszMask
== '?' )
883 // if there is nothing more, match
884 if ( *pszMask
== '\0' )
887 // are there any other metacharacters in the mask?
889 const char *pEndMask
= strpbrk(pszMask
, "*?");
891 if ( pEndMask
!= NULL
) {
892 // we have to match the string between two metachars
893 uiLenMask
= pEndMask
- pszMask
;
896 // we have to match the remainder of the string
897 uiLenMask
= strlen(pszMask
);
900 wxString
strToMatch(pszMask
, uiLenMask
);
901 const char* pMatch
= strstr(pszTxt
, strToMatch
);
902 if ( pMatch
== NULL
)
905 // -1 to compensate "++" in the loop
906 pszTxt
= pMatch
+ uiLenMask
- 1;
907 pszMask
+= uiLenMask
- 1;
912 if ( *pszMask
!= *pszTxt
)
918 // match only if nothing left
919 return *pszTxt
== '\0';
922 // ---------------------------------------------------------------------------
923 // standard C++ library string functions
924 // ---------------------------------------------------------------------------
925 #ifdef STD_STRING_COMPATIBILITY
927 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
929 wxASSERT( str
.GetStringData()->IsValid() );
930 wxASSERT( nPos
<= Len() );
933 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
934 strncpy(pc
, c_str(), nPos
);
935 strcpy(pc
+ nPos
, str
);
936 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
937 strTmp
.UngetWriteBuf();
943 size_t wxString::find(const wxString
& str
, size_t nStart
) const
945 wxASSERT( str
.GetStringData()->IsValid() );
946 wxASSERT( nStart
<= Len() );
948 const char *p
= strstr(c_str() + nStart
, str
);
950 return p
== NULL
? npos
: p
- c_str();
953 // VC++ 1.5 can't cope with the default argument in the header.
954 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
955 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
957 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
961 size_t wxString::find(char ch
, size_t nStart
) const
963 wxASSERT( nStart
<= Len() );
965 const char *p
= strchr(c_str() + nStart
, ch
);
967 return p
== NULL
? npos
: p
- c_str();
970 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
972 wxASSERT( str
.GetStringData()->IsValid() );
973 wxASSERT( nStart
<= Len() );
975 // # could be quicker than that
976 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
977 while ( p
>= c_str() + str
.Len() ) {
978 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
979 return p
- str
.Len() - c_str();
986 // VC++ 1.5 can't cope with the default argument in the header.
987 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
988 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
990 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
993 size_t wxString::rfind(char ch
, size_t nStart
) const
995 wxASSERT( nStart
<= Len() );
997 const char *p
= strrchr(c_str() + nStart
, ch
);
999 return p
== NULL
? npos
: p
- c_str();
1003 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1005 // npos means 'take all'
1009 wxASSERT( nStart
+ nLen
<= Len() );
1011 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1014 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1016 wxString
strTmp(c_str(), nStart
);
1017 if ( nLen
!= npos
) {
1018 wxASSERT( nStart
+ nLen
<= Len() );
1020 strTmp
.append(c_str() + nStart
+ nLen
);
1027 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1029 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1033 strTmp
.append(c_str(), nStart
);
1035 strTmp
.append(c_str() + nStart
+ nLen
);
1041 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1043 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1046 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1047 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1049 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1052 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1053 const char* sz
, size_t nCount
)
1055 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1058 #endif //std::string compatibility
1060 // ============================================================================
1062 // ============================================================================
1064 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1065 #define ARRAY_MAXSIZE_INCREMENT 4096
1066 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1067 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1070 #define STRING(p) ((wxString *)(&(p)))
1073 wxArrayString::wxArrayString()
1081 wxArrayString::wxArrayString(const wxArrayString
& src
)
1090 // assignment operator
1091 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1096 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1097 Alloc(src
.m_nCount
);
1099 // we can't just copy the pointers here because otherwise we would share
1100 // the strings with another array
1101 for ( uint n
= 0; n
< src
.m_nCount
; n
++ )
1104 if ( m_nCount
!= 0 )
1105 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1111 void wxArrayString::Grow()
1113 // only do it if no more place
1114 if( m_nCount
== m_nSize
) {
1115 if( m_nSize
== 0 ) {
1116 // was empty, alloc some memory
1117 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1118 m_pItems
= new char *[m_nSize
];
1121 // otherwise when it's called for the first time, nIncrement would be 0
1122 // and the array would never be expanded
1123 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1125 // add 50% but not too much
1126 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1127 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1128 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1129 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1130 m_nSize
+= nIncrement
;
1131 char **pNew
= new char *[m_nSize
];
1133 // copy data to new location
1134 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1136 // delete old memory (but do not release the strings!)
1137 wxDELETEA(m_pItems
);
1144 void wxArrayString::Free()
1146 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1147 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1151 // deletes all the strings from the list
1152 void wxArrayString::Empty()
1159 // as Empty, but also frees memory
1160 void wxArrayString::Clear()
1167 wxDELETEA(m_pItems
);
1171 wxArrayString::~wxArrayString()
1175 wxDELETEA(m_pItems
);
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
) {
1186 wxDELETEA(m_pItems
);
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);