1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "string.h"
18 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
19 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
20 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
23 // ===========================================================================
24 // headers, declarations, constants
25 // ===========================================================================
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
36 #include "wx/string.h"
44 #ifdef WXSTRING_IS_WXOBJECT
45 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
46 #endif //WXSTRING_IS_WXOBJECT
48 // allocating extra space for each string consumes more memory but speeds up
49 // the concatenation operations (nLen is the current string's length)
50 // NB: EXTRA_ALLOC must be >= 0!
51 #define EXTRA_ALLOC (19 - nLen % 16)
53 // ---------------------------------------------------------------------------
54 // static class variables definition
55 // ---------------------------------------------------------------------------
57 #ifdef STD_STRING_COMPATIBILITY
58 const size_t wxString::npos
= STRING_MAXLEN
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // for an empty string, GetStringData() will return this address
66 static int g_strEmpty
[] = { -1, // ref count (locked)
68 0, // allocated memory
70 // empty C style string: points to 'string data' byte of g_strEmpty
71 extern const char *g_szNul
= (const char *)(&g_strEmpty
[3]);
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 #ifdef STD_STRING_COMPATIBILITY
79 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
82 // ATTN: you can _not_ use both of these in the same program!
85 #define NAMESPACE std::
91 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
96 NAMESPACE streambuf
*sb
= is
.rdbuf();
99 int ch
= sb
->sbumpc ();
101 is
.setstate(NAMESPACE
ios::eofbit
);
104 else if ( isspace(ch
) ) {
116 if ( str
.length() == 0 )
117 is
.setstate(NAMESPACE
ios::failbit
);
122 #endif //std::string compatibility
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // this small class is used to gather statistics for performance tuning
129 //#define WXSTRING_STATISTICS
130 #ifdef WXSTRING_STATISTICS
134 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
136 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
138 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
141 size_t m_nCount
, m_nTotal
;
143 } g_averageLength("allocation size"),
144 g_averageSummandLength("summand length"),
145 g_averageConcatHit("hit probability in concat"),
146 g_averageInitialLength("initial string length");
148 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
150 #define STATISTICS_ADD(av, val)
151 #endif // WXSTRING_STATISTICS
153 // ===========================================================================
154 // wxString class core
155 // ===========================================================================
157 // ---------------------------------------------------------------------------
159 // ---------------------------------------------------------------------------
161 // constructs string of <nLength> copies of character <ch>
162 wxString::wxString(char ch
, size_t nLength
)
167 AllocBuffer(nLength
);
169 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
171 memset(m_pchData
, ch
, nLength
);
175 // takes nLength elements of psz starting at nPos
176 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
180 wxASSERT( nPos
<= Strlen(psz
) );
182 if ( nLength
== STRING_MAXLEN
)
183 nLength
= Strlen(psz
+ nPos
);
185 STATISTICS_ADD(InitialLength
, nLength
);
188 // trailing '\0' is written in AllocBuffer()
189 AllocBuffer(nLength
);
190 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
194 // the same as previous constructor, but for compilers using unsigned char
195 wxString::wxString(const unsigned char* psz
, size_t nLength
)
197 InitWith((const char *)psz
, 0, nLength
);
200 #ifdef STD_STRING_COMPATIBILITY
202 // poor man's iterators are "void *" pointers
203 wxString::wxString(const void *pStart
, const void *pEnd
)
205 InitWith((const char *)pStart
, 0,
206 (const char *)pEnd
- (const char *)pStart
);
209 #endif //std::string compatibility
212 wxString::wxString(const wchar_t *pwz
)
214 // first get necessary size
215 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
220 wcstombs(m_pchData
, pwz
, nLen
);
227 // ---------------------------------------------------------------------------
229 // ---------------------------------------------------------------------------
231 // allocates memory needed to store a C string of length nLen
232 void wxString::AllocBuffer(size_t nLen
)
234 wxASSERT( nLen
> 0 ); //
235 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
237 STATISTICS_ADD(Length
, nLen
);
240 // 1) one extra character for '\0' termination
241 // 2) sizeof(wxStringData) for housekeeping info
242 wxStringData
* pData
= (wxStringData
*)
243 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
245 pData
->nDataLength
= nLen
;
246 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
247 m_pchData
= pData
->data(); // data starts after wxStringData
248 m_pchData
[nLen
] = '\0';
251 // must be called before changing this string
252 void wxString::CopyBeforeWrite()
254 wxStringData
* pData
= GetStringData();
256 if ( pData
->IsShared() ) {
257 pData
->Unlock(); // memory not freed because shared
258 size_t nLen
= pData
->nDataLength
;
260 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
263 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
266 // must be called before replacing contents of this string
267 void wxString::AllocBeforeWrite(size_t nLen
)
269 wxASSERT( nLen
!= 0 ); // doesn't make any sense
271 // must not share string and must have enough space
272 wxStringData
* pData
= GetStringData();
273 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
274 // can't work with old buffer, get new one
279 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
282 // allocate enough memory for nLen characters
283 void wxString::Alloc(size_t nLen
)
285 wxStringData
*pData
= GetStringData();
286 if ( pData
->nAllocLength
<= nLen
) {
287 if ( pData
->IsEmpty() ) {
290 wxStringData
* pData
= (wxStringData
*)
291 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
293 pData
->nDataLength
= 0;
294 pData
->nAllocLength
= nLen
;
295 m_pchData
= pData
->data(); // data starts after wxStringData
296 m_pchData
[0u] = '\0';
298 else if ( pData
->IsShared() ) {
299 pData
->Unlock(); // memory not freed because shared
300 size_t nOldLen
= pData
->nDataLength
;
302 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
307 wxStringData
*p
= (wxStringData
*)
308 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
311 // @@@ what to do on memory error?
315 // it's not important if the pointer changed or not (the check for this
316 // is not faster than assigning to m_pchData in all cases)
317 p
->nAllocLength
= nLen
;
318 m_pchData
= p
->data();
321 //else: we've already got enough
324 // shrink to minimal size (releasing extra memory)
325 void wxString::Shrink()
327 wxStringData
*pData
= GetStringData();
329 // this variable is unused in release build, so avoid the compiler warning by
330 // just not declaring it
334 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
336 wxASSERT( p
!= NULL
); // can't free memory?
337 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
340 // get the pointer to writable buffer of (at least) nLen bytes
341 char *wxString::GetWriteBuf(size_t nLen
)
343 AllocBeforeWrite(nLen
);
345 wxASSERT( GetStringData()->nRefs
== 1 );
346 GetStringData()->Validate(FALSE
);
351 // put string back in a reasonable state after GetWriteBuf
352 void wxString::UngetWriteBuf()
354 GetStringData()->nDataLength
= strlen(m_pchData
);
355 GetStringData()->Validate(TRUE
);
358 // ---------------------------------------------------------------------------
360 // ---------------------------------------------------------------------------
362 // all functions are inline in string.h
364 // ---------------------------------------------------------------------------
365 // assignment operators
366 // ---------------------------------------------------------------------------
368 // helper function: does real copy
369 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
371 if ( nSrcLen
== 0 ) {
375 AllocBeforeWrite(nSrcLen
);
376 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
377 GetStringData()->nDataLength
= nSrcLen
;
378 m_pchData
[nSrcLen
] = '\0';
382 // assigns one string to another
383 wxString
& wxString::operator=(const wxString
& stringSrc
)
385 wxASSERT( stringSrc
.GetStringData()->IsValid() );
387 // don't copy string over itself
388 if ( m_pchData
!= stringSrc
.m_pchData
) {
389 if ( stringSrc
.GetStringData()->IsEmpty() ) {
394 GetStringData()->Unlock();
395 m_pchData
= stringSrc
.m_pchData
;
396 GetStringData()->Lock();
403 // assigns a single character
404 wxString
& wxString::operator=(char ch
)
411 wxString
& wxString::operator=(const char *psz
)
413 AssignCopy(Strlen(psz
), psz
);
417 // same as 'signed char' variant
418 wxString
& wxString::operator=(const unsigned char* psz
)
420 *this = (const char *)psz
;
424 wxString
& wxString::operator=(const wchar_t *pwz
)
431 // ---------------------------------------------------------------------------
432 // string concatenation
433 // ---------------------------------------------------------------------------
435 // add something to this string
436 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
438 STATISTICS_ADD(SummandLength
, nSrcLen
);
440 // concatenating an empty string is a NOP, but it happens quite rarely,
441 // so we don't waste our time checking for it
442 // if ( nSrcLen > 0 )
443 wxStringData
*pData
= GetStringData();
444 size_t nLen
= pData
->nDataLength
;
445 size_t nNewLen
= nLen
+ nSrcLen
;
447 // alloc new buffer if current is too small
448 if ( pData
->IsShared() ) {
449 STATISTICS_ADD(ConcatHit
, 0);
451 // we have to allocate another buffer
452 wxStringData
* pOldData
= GetStringData();
453 AllocBuffer(nNewLen
);
454 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
457 else if ( nNewLen
> pData
->nAllocLength
) {
458 STATISTICS_ADD(ConcatHit
, 0);
460 // we have to grow the buffer
464 STATISTICS_ADD(ConcatHit
, 1);
466 // the buffer is already big enough
469 // should be enough space
470 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
472 // fast concatenation - all is done in our buffer
473 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
475 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
476 GetStringData()->nDataLength
= nNewLen
; // and fix the length
480 * concatenation functions come in 5 flavours:
482 * char + string and string + char
483 * C str + string and string + C str
486 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
488 wxASSERT( string1
.GetStringData()->IsValid() );
489 wxASSERT( string2
.GetStringData()->IsValid() );
491 wxString s
= string1
;
497 wxString
operator+(const wxString
& string
, char ch
)
499 wxASSERT( string
.GetStringData()->IsValid() );
507 wxString
operator+(char ch
, const wxString
& string
)
509 wxASSERT( string
.GetStringData()->IsValid() );
517 wxString
operator+(const wxString
& string
, const char *psz
)
519 wxASSERT( string
.GetStringData()->IsValid() );
522 s
.Alloc(Strlen(psz
) + string
.Len());
529 wxString
operator+(const char *psz
, const wxString
& string
)
531 wxASSERT( string
.GetStringData()->IsValid() );
534 s
.Alloc(Strlen(psz
) + string
.Len());
541 // ===========================================================================
542 // other common string functions
543 // ===========================================================================
545 // ---------------------------------------------------------------------------
546 // simple sub-string extraction
547 // ---------------------------------------------------------------------------
549 // helper function: clone the data attached to this string
550 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
552 if ( nCopyLen
== 0 ) {
556 dest
.AllocBuffer(nCopyLen
);
557 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
561 // extract string of length nCount starting at nFirst
562 // default value of nCount is 0 and means "till the end"
563 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
565 // out-of-bounds requests return sensible things
567 nCount
= GetStringData()->nDataLength
- nFirst
;
569 if ( nFirst
+ nCount
> (size_t)GetStringData()->nDataLength
)
570 nCount
= GetStringData()->nDataLength
- nFirst
;
571 if ( nFirst
> (size_t)GetStringData()->nDataLength
)
575 AllocCopy(dest
, nCount
, nFirst
);
579 // extract nCount last (rightmost) characters
580 wxString
wxString::Right(size_t nCount
) const
582 if ( nCount
> (size_t)GetStringData()->nDataLength
)
583 nCount
= GetStringData()->nDataLength
;
586 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
590 // get all characters after the last occurence of ch
591 // (returns the whole string if ch not found)
592 wxString
wxString::Right(char ch
) const
595 int iPos
= Find(ch
, TRUE
);
596 if ( iPos
== NOT_FOUND
)
599 str
= c_str() + iPos
+ 1;
604 // extract nCount first (leftmost) characters
605 wxString
wxString::Left(size_t nCount
) const
607 if ( nCount
> (size_t)GetStringData()->nDataLength
)
608 nCount
= GetStringData()->nDataLength
;
611 AllocCopy(dest
, nCount
, 0);
615 // get all characters before the first occurence of ch
616 // (returns the whole string if ch not found)
617 wxString
wxString::Left(char ch
) const
620 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
626 /// get all characters before the last occurence of ch
627 /// (returns empty string if ch not found)
628 wxString
wxString::Before(char ch
) const
631 int iPos
= Find(ch
, TRUE
);
632 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
633 str
= wxString(c_str(), iPos
);
638 /// get all characters after the first occurence of ch
639 /// (returns empty string if ch not found)
640 wxString
wxString::After(char ch
) const
644 if ( iPos
!= NOT_FOUND
)
645 str
= c_str() + iPos
+ 1;
650 // replace first (or all) occurences of some substring with another one
651 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
653 size_t uiCount
= 0; // count of replacements made
655 size_t uiOldLen
= Strlen(szOld
);
658 const char *pCurrent
= m_pchData
;
660 while ( *pCurrent
!= '\0' ) {
661 pSubstr
= strstr(pCurrent
, szOld
);
662 if ( pSubstr
== NULL
) {
663 // strTemp is unused if no replacements were made, so avoid the copy
667 strTemp
+= pCurrent
; // copy the rest
668 break; // exit the loop
671 // take chars before match
672 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
674 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
679 if ( !bReplaceAll
) {
680 strTemp
+= pCurrent
; // copy the rest
681 break; // exit the loop
686 // only done if there were replacements, otherwise would have returned above
692 bool wxString::IsAscii() const
694 const char *s
= (const char*) *this;
696 if(!isascii(*s
)) return(FALSE
);
702 bool wxString::IsWord() const
704 const char *s
= (const char*) *this;
706 if(!isalpha(*s
)) return(FALSE
);
712 bool wxString::IsNumber() const
714 const char *s
= (const char*) *this;
716 if(!isdigit(*s
)) return(FALSE
);
722 wxString
wxString::Strip(stripType w
) const
725 if ( w
& leading
) s
.Trim(FALSE
);
726 if ( w
& trailing
) s
.Trim(TRUE
);
730 // ---------------------------------------------------------------------------
732 // ---------------------------------------------------------------------------
734 wxString
& wxString::MakeUpper()
738 for ( char *p
= m_pchData
; *p
; p
++ )
739 *p
= (char)toupper(*p
);
744 wxString
& wxString::MakeLower()
748 for ( char *p
= m_pchData
; *p
; p
++ )
749 *p
= (char)tolower(*p
);
754 // ---------------------------------------------------------------------------
755 // trimming and padding
756 // ---------------------------------------------------------------------------
758 // trims spaces (in the sense of isspace) from left or right side
759 wxString
& wxString::Trim(bool bFromRight
)
765 // find last non-space character
766 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
767 while ( isspace(*psz
) && (psz
>= m_pchData
) )
770 // truncate at trailing space start
772 GetStringData()->nDataLength
= psz
- m_pchData
;
776 // find first non-space character
777 const char *psz
= m_pchData
;
778 while ( isspace(*psz
) )
781 // fix up data and length
782 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
783 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
784 GetStringData()->nDataLength
= nDataLength
;
790 // adds nCount characters chPad to the string from either side
791 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
793 wxString
s(chPad
, nCount
);
806 // truncate the string
807 wxString
& wxString::Truncate(size_t uiLen
)
809 *(m_pchData
+ uiLen
) = '\0';
810 GetStringData()->nDataLength
= uiLen
;
815 // ---------------------------------------------------------------------------
816 // finding (return NOT_FOUND if not found and index otherwise)
817 // ---------------------------------------------------------------------------
820 int wxString::Find(char ch
, bool bFromEnd
) const
822 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
824 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
827 // find a sub-string (like strstr)
828 int wxString::Find(const char *pszSub
) const
830 const char *psz
= strstr(m_pchData
, pszSub
);
832 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
835 // ---------------------------------------------------------------------------
837 // ---------------------------------------------------------------------------
838 int wxString::Printf(const char *pszFormat
, ...)
841 va_start(argptr
, pszFormat
);
843 int iLen
= PrintfV(pszFormat
, argptr
);
850 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
852 static char s_szScratch
[1024];
854 int iLen
= vsprintf(s_szScratch
, pszFormat
, argptr
);
855 AllocBeforeWrite(iLen
);
856 strcpy(m_pchData
, s_szScratch
);
861 // ----------------------------------------------------------------------------
862 // misc other operations
863 // ----------------------------------------------------------------------------
864 bool wxString::Matches(const char *pszMask
) const
866 // check char by char
868 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
869 switch ( *pszMask
) {
871 if ( *pszTxt
== '\0' )
880 // ignore special chars immediately following this one
881 while ( *pszMask
== '*' || *pszMask
== '?' )
884 // if there is nothing more, match
885 if ( *pszMask
== '\0' )
888 // are there any other metacharacters in the mask?
890 const char *pEndMask
= strpbrk(pszMask
, "*?");
892 if ( pEndMask
!= NULL
) {
893 // we have to match the string between two metachars
894 uiLenMask
= pEndMask
- pszMask
;
897 // we have to match the remainder of the string
898 uiLenMask
= strlen(pszMask
);
901 wxString
strToMatch(pszMask
, uiLenMask
);
902 const char* pMatch
= strstr(pszTxt
, strToMatch
);
903 if ( pMatch
== NULL
)
906 // -1 to compensate "++" in the loop
907 pszTxt
= pMatch
+ uiLenMask
- 1;
908 pszMask
+= uiLenMask
- 1;
913 if ( *pszMask
!= *pszTxt
)
919 // match only if nothing left
920 return *pszTxt
== '\0';
923 // ---------------------------------------------------------------------------
924 // standard C++ library string functions
925 // ---------------------------------------------------------------------------
926 #ifdef STD_STRING_COMPATIBILITY
928 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
930 wxASSERT( str
.GetStringData()->IsValid() );
931 wxASSERT( nPos
<= Len() );
933 if ( !str
.IsEmpty() ) {
935 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
936 strncpy(pc
, c_str(), nPos
);
937 strcpy(pc
+ nPos
, str
);
938 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
939 strTmp
.UngetWriteBuf();
946 size_t wxString::find(const wxString
& str
, size_t nStart
) const
948 wxASSERT( str
.GetStringData()->IsValid() );
949 wxASSERT( nStart
<= Len() );
951 const char *p
= strstr(c_str() + nStart
, str
);
953 return p
== NULL
? npos
: p
- c_str();
956 // VC++ 1.5 can't cope with the default argument in the header.
957 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
958 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
960 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
964 size_t wxString::find(char ch
, size_t nStart
) const
966 wxASSERT( nStart
<= Len() );
968 const char *p
= strchr(c_str() + nStart
, ch
);
970 return p
== NULL
? npos
: p
- c_str();
973 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
975 wxASSERT( str
.GetStringData()->IsValid() );
976 wxASSERT( nStart
<= Len() );
978 // # could be quicker than that
979 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
980 while ( p
>= c_str() + str
.Len() ) {
981 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
982 return p
- str
.Len() - c_str();
989 // VC++ 1.5 can't cope with the default argument in the header.
990 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
991 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
993 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
996 size_t wxString::rfind(char ch
, size_t nStart
) const
998 wxASSERT( nStart
<= Len() );
1000 const char *p
= strrchr(c_str() + nStart
, ch
);
1002 return p
== NULL
? npos
: p
- c_str();
1006 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1008 // npos means 'take all'
1012 wxASSERT( nStart
+ nLen
<= Len() );
1014 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1017 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1019 wxString
strTmp(c_str(), nStart
);
1020 if ( nLen
!= npos
) {
1021 wxASSERT( nStart
+ nLen
<= Len() );
1023 strTmp
.append(c_str() + nStart
+ nLen
);
1030 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1032 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1036 strTmp
.append(c_str(), nStart
);
1038 strTmp
.append(c_str() + nStart
+ nLen
);
1044 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1046 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1049 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1050 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1052 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1055 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1056 const char* sz
, size_t nCount
)
1058 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1061 #endif //std::string compatibility
1063 // ============================================================================
1065 // ============================================================================
1067 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1068 #define ARRAY_MAXSIZE_INCREMENT 4096
1069 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1070 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1073 #define STRING(p) ((wxString *)(&(p)))
1076 wxArrayString::wxArrayString()
1080 m_pItems
= (char **) NULL
;
1084 wxArrayString::wxArrayString(const wxArrayString
& src
)
1088 m_pItems
= (char **) NULL
;
1093 // assignment operator
1094 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1099 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1100 Alloc(src
.m_nCount
);
1102 // we can't just copy the pointers here because otherwise we would share
1103 // the strings with another array
1104 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1107 if ( m_nCount
!= 0 )
1108 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1114 void wxArrayString::Grow()
1116 // only do it if no more place
1117 if( m_nCount
== m_nSize
) {
1118 if( m_nSize
== 0 ) {
1119 // was empty, alloc some memory
1120 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1121 m_pItems
= new char *[m_nSize
];
1124 // otherwise when it's called for the first time, nIncrement would be 0
1125 // and the array would never be expanded
1126 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1128 // add 50% but not too much
1129 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1130 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1131 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1132 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1133 m_nSize
+= nIncrement
;
1134 char **pNew
= new char *[m_nSize
];
1136 // copy data to new location
1137 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1139 // delete old memory (but do not release the strings!)
1140 wxDELETEA(m_pItems
);
1147 void wxArrayString::Free()
1149 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1150 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1154 // deletes all the strings from the list
1155 void wxArrayString::Empty()
1162 // as Empty, but also frees memory
1163 void wxArrayString::Clear()
1170 wxDELETEA(m_pItems
);
1174 wxArrayString::~wxArrayString()
1178 wxDELETEA(m_pItems
);
1181 // pre-allocates memory (frees the previous data!)
1182 void wxArrayString::Alloc(size_t nSize
)
1184 wxASSERT( nSize
> 0 );
1186 // only if old buffer was not big enough
1187 if ( nSize
> m_nSize
) {
1189 wxDELETEA(m_pItems
);
1190 m_pItems
= new char *[nSize
];
1197 // searches the array for an item (forward or backwards)
1198 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1201 if ( m_nCount
> 0 ) {
1202 size_t ui
= m_nCount
;
1204 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1211 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1212 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1220 // add item at the end
1221 void wxArrayString::Add(const wxString
& str
)
1223 wxASSERT( str
.GetStringData()->IsValid() );
1227 // the string data must not be deleted!
1228 str
.GetStringData()->Lock();
1229 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1232 // add item at the given position
1233 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1235 wxASSERT( str
.GetStringData()->IsValid() );
1237 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1241 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1242 (m_nCount
- nIndex
)*sizeof(char *));
1244 str
.GetStringData()->Lock();
1245 m_pItems
[nIndex
] = (char *)str
.c_str();
1250 // removes item from array (by index)
1251 void wxArrayString::Remove(size_t nIndex
)
1253 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1256 Item(nIndex
).GetStringData()->Unlock();
1258 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1259 (m_nCount
- nIndex
- 1)*sizeof(char *));
1263 // removes item from array (by value)
1264 void wxArrayString::Remove(const char *sz
)
1266 int iIndex
= Index(sz
);
1268 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1269 _("removing inexistent element in wxArrayString::Remove") );
1274 // sort array elements using passed comparaison function
1276 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1279 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);