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 wxUSE_WCSRTOMBS
45 #include <wchar.h> // for wcsrtombs(), see comments where it's used
48 #ifdef WXSTRING_IS_WXOBJECT
49 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
50 #endif //WXSTRING_IS_WXOBJECT
52 // allocating extra space for each string consumes more memory but speeds up
53 // the concatenation operations (nLen is the current string's length)
54 // NB: EXTRA_ALLOC must be >= 0!
55 #define EXTRA_ALLOC (19 - nLen % 16)
57 // ---------------------------------------------------------------------------
58 // static class variables definition
59 // ---------------------------------------------------------------------------
61 #ifdef STD_STRING_COMPATIBILITY
62 const size_t wxString::npos
= STRING_MAXLEN
;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // for an empty string, GetStringData() will return this address: this
70 // structure has the same layout as wxStringData and it's data() method will
71 // return the empty string (dummy pointer)
76 } g_strEmpty
= { {-1, 0, 0}, '\0' };
78 // empty C style string: points to 'string data' byte of g_strEmpty
79 extern const char *g_szNul
= &g_strEmpty
.dummy
;
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 #ifdef STD_STRING_COMPATIBILITY
87 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
90 // ATTN: you can _not_ use both of these in the same program!
99 // for msvc (bcc50+ also) you don't need these NAMESPACE defines,
100 // using namespace std; takes care of that.
101 #define NAMESPACE std::
106 #define wxVsprintf _vsnprintf
109 #if defined ( HAVE_VSNPRINTF )
110 #define wxVsprintf vsnprintf
115 // vsprintf() is ANSI so we can always use it, but it's unsafe!
116 #define wxVsprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
117 #pragma message("Using sprintf() because no snprintf()-like function defined")
120 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
125 NAMESPACE streambuf
*sb
= is
.rdbuf();
128 int ch
= sb
->sbumpc ();
130 is
.setstate(NAMESPACE
ios::eofbit
);
133 else if ( isspace(ch
) ) {
145 if ( str
.length() == 0 )
146 is
.setstate(NAMESPACE
ios::failbit
);
151 #endif //std::string compatibility
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 // this small class is used to gather statistics for performance tuning
158 //#define WXSTRING_STATISTICS
159 #ifdef WXSTRING_STATISTICS
163 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
165 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
167 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
170 size_t m_nCount
, m_nTotal
;
172 } g_averageLength("allocation size"),
173 g_averageSummandLength("summand length"),
174 g_averageConcatHit("hit probability in concat"),
175 g_averageInitialLength("initial string length");
177 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
179 #define STATISTICS_ADD(av, val)
180 #endif // WXSTRING_STATISTICS
182 // ===========================================================================
183 // wxString class core
184 // ===========================================================================
186 // ---------------------------------------------------------------------------
188 // ---------------------------------------------------------------------------
190 // constructs string of <nLength> copies of character <ch>
191 wxString::wxString(char ch
, size_t nLength
)
196 AllocBuffer(nLength
);
198 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
200 memset(m_pchData
, ch
, nLength
);
204 // takes nLength elements of psz starting at nPos
205 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
209 wxASSERT( nPos
<= Strlen(psz
) );
211 if ( nLength
== STRING_MAXLEN
)
212 nLength
= Strlen(psz
+ nPos
);
214 STATISTICS_ADD(InitialLength
, nLength
);
217 // trailing '\0' is written in AllocBuffer()
218 AllocBuffer(nLength
);
219 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
223 // the same as previous constructor, but for compilers using unsigned char
224 wxString::wxString(const unsigned char* psz
, size_t nLength
)
226 InitWith((const char *)psz
, 0, nLength
);
229 #ifdef STD_STRING_COMPATIBILITY
231 // poor man's iterators are "void *" pointers
232 wxString::wxString(const void *pStart
, const void *pEnd
)
234 InitWith((const char *)pStart
, 0,
235 (const char *)pEnd
- (const char *)pStart
);
238 #endif //std::string compatibility
241 wxString::wxString(const wchar_t *pwz
)
243 // first get necessary size
245 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
246 // honor the 3rd parameter, thus it will happily crash here).
247 #ifdef wxUSE_WCSRTOMBS
248 // don't know if it's really needed (or if we can pass NULL), but better safe
251 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
253 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
259 wcstombs(m_pchData
, pwz
, nLen
);
266 // ---------------------------------------------------------------------------
268 // ---------------------------------------------------------------------------
270 // allocates memory needed to store a C string of length nLen
271 void wxString::AllocBuffer(size_t nLen
)
273 wxASSERT( nLen
> 0 ); //
274 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
276 STATISTICS_ADD(Length
, nLen
);
279 // 1) one extra character for '\0' termination
280 // 2) sizeof(wxStringData) for housekeeping info
281 wxStringData
* pData
= (wxStringData
*)
282 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
284 pData
->nDataLength
= nLen
;
285 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
286 m_pchData
= pData
->data(); // data starts after wxStringData
287 m_pchData
[nLen
] = '\0';
290 // must be called before changing this string
291 void wxString::CopyBeforeWrite()
293 wxStringData
* pData
= GetStringData();
295 if ( pData
->IsShared() ) {
296 pData
->Unlock(); // memory not freed because shared
297 size_t nLen
= pData
->nDataLength
;
299 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
302 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
305 // must be called before replacing contents of this string
306 void wxString::AllocBeforeWrite(size_t nLen
)
308 wxASSERT( nLen
!= 0 ); // doesn't make any sense
310 // must not share string and must have enough space
311 wxStringData
* pData
= GetStringData();
312 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
313 // can't work with old buffer, get new one
318 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
321 // allocate enough memory for nLen characters
322 void wxString::Alloc(size_t nLen
)
324 wxStringData
*pData
= GetStringData();
325 if ( pData
->nAllocLength
<= nLen
) {
326 if ( pData
->IsEmpty() ) {
329 wxStringData
* pData
= (wxStringData
*)
330 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
332 pData
->nDataLength
= 0;
333 pData
->nAllocLength
= nLen
;
334 m_pchData
= pData
->data(); // data starts after wxStringData
335 m_pchData
[0u] = '\0';
337 else if ( pData
->IsShared() ) {
338 pData
->Unlock(); // memory not freed because shared
339 size_t nOldLen
= pData
->nDataLength
;
341 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
346 wxStringData
*p
= (wxStringData
*)
347 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
350 // @@@ what to do on memory error?
354 // it's not important if the pointer changed or not (the check for this
355 // is not faster than assigning to m_pchData in all cases)
356 p
->nAllocLength
= nLen
;
357 m_pchData
= p
->data();
360 //else: we've already got enough
363 // shrink to minimal size (releasing extra memory)
364 void wxString::Shrink()
366 wxStringData
*pData
= GetStringData();
368 // this variable is unused in release build, so avoid the compiler warning by
369 // just not declaring it
373 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
375 wxASSERT( p
!= NULL
); // can't free memory?
376 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
379 // get the pointer to writable buffer of (at least) nLen bytes
380 char *wxString::GetWriteBuf(size_t nLen
)
382 AllocBeforeWrite(nLen
);
384 wxASSERT( GetStringData()->nRefs
== 1 );
385 GetStringData()->Validate(FALSE
);
390 // put string back in a reasonable state after GetWriteBuf
391 void wxString::UngetWriteBuf()
393 GetStringData()->nDataLength
= strlen(m_pchData
);
394 GetStringData()->Validate(TRUE
);
397 // ---------------------------------------------------------------------------
399 // ---------------------------------------------------------------------------
401 // all functions are inline in string.h
403 // ---------------------------------------------------------------------------
404 // assignment operators
405 // ---------------------------------------------------------------------------
407 // helper function: does real copy
408 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
410 if ( nSrcLen
== 0 ) {
414 AllocBeforeWrite(nSrcLen
);
415 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
416 GetStringData()->nDataLength
= nSrcLen
;
417 m_pchData
[nSrcLen
] = '\0';
421 // assigns one string to another
422 wxString
& wxString::operator=(const wxString
& stringSrc
)
424 wxASSERT( stringSrc
.GetStringData()->IsValid() );
426 // don't copy string over itself
427 if ( m_pchData
!= stringSrc
.m_pchData
) {
428 if ( stringSrc
.GetStringData()->IsEmpty() ) {
433 GetStringData()->Unlock();
434 m_pchData
= stringSrc
.m_pchData
;
435 GetStringData()->Lock();
442 // assigns a single character
443 wxString
& wxString::operator=(char ch
)
450 wxString
& wxString::operator=(const char *psz
)
452 AssignCopy(Strlen(psz
), psz
);
456 // same as 'signed char' variant
457 wxString
& wxString::operator=(const unsigned char* psz
)
459 *this = (const char *)psz
;
463 wxString
& wxString::operator=(const wchar_t *pwz
)
470 // ---------------------------------------------------------------------------
471 // string concatenation
472 // ---------------------------------------------------------------------------
474 // add something to this string
475 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
477 STATISTICS_ADD(SummandLength
, nSrcLen
);
479 // concatenating an empty string is a NOP
481 wxStringData
*pData
= GetStringData();
482 size_t nLen
= pData
->nDataLength
;
483 size_t nNewLen
= nLen
+ nSrcLen
;
485 // alloc new buffer if current is too small
486 if ( pData
->IsShared() ) {
487 STATISTICS_ADD(ConcatHit
, 0);
489 // we have to allocate another buffer
490 wxStringData
* pOldData
= GetStringData();
491 AllocBuffer(nNewLen
);
492 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
495 else if ( nNewLen
> pData
->nAllocLength
) {
496 STATISTICS_ADD(ConcatHit
, 0);
498 // we have to grow the buffer
502 STATISTICS_ADD(ConcatHit
, 1);
504 // the buffer is already big enough
507 // should be enough space
508 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
510 // fast concatenation - all is done in our buffer
511 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
513 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
514 GetStringData()->nDataLength
= nNewLen
; // and fix the length
516 //else: the string to append was empty
520 * concatenation functions come in 5 flavours:
522 * char + string and string + char
523 * C str + string and string + C str
526 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
528 wxASSERT( string1
.GetStringData()->IsValid() );
529 wxASSERT( string2
.GetStringData()->IsValid() );
531 wxString s
= string1
;
537 wxString
operator+(const wxString
& string
, char ch
)
539 wxASSERT( string
.GetStringData()->IsValid() );
547 wxString
operator+(char ch
, const wxString
& string
)
549 wxASSERT( string
.GetStringData()->IsValid() );
557 wxString
operator+(const wxString
& string
, const char *psz
)
559 wxASSERT( string
.GetStringData()->IsValid() );
562 s
.Alloc(Strlen(psz
) + string
.Len());
569 wxString
operator+(const char *psz
, const wxString
& string
)
571 wxASSERT( string
.GetStringData()->IsValid() );
574 s
.Alloc(Strlen(psz
) + string
.Len());
581 // ===========================================================================
582 // other common string functions
583 // ===========================================================================
585 // ---------------------------------------------------------------------------
586 // simple sub-string extraction
587 // ---------------------------------------------------------------------------
589 // helper function: clone the data attached to this string
590 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
592 if ( nCopyLen
== 0 ) {
596 dest
.AllocBuffer(nCopyLen
);
597 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
601 // extract string of length nCount starting at nFirst
602 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
604 wxStringData
*pData
= GetStringData();
605 size_t nLen
= pData
->nDataLength
;
607 // default value of nCount is STRING_MAXLEN and means "till the end"
608 if ( nCount
== STRING_MAXLEN
)
610 nCount
= nLen
- nFirst
;
613 // out-of-bounds requests return sensible things
614 if ( nFirst
+ nCount
> nLen
)
616 nCount
= nLen
- nFirst
;
621 // AllocCopy() will return empty string
626 AllocCopy(dest
, nCount
, nFirst
);
631 // extract nCount last (rightmost) characters
632 wxString
wxString::Right(size_t nCount
) const
634 if ( nCount
> (size_t)GetStringData()->nDataLength
)
635 nCount
= GetStringData()->nDataLength
;
638 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
642 // get all characters after the last occurence of ch
643 // (returns the whole string if ch not found)
644 wxString
wxString::Right(char ch
) const
647 int iPos
= Find(ch
, TRUE
);
648 if ( iPos
== NOT_FOUND
)
651 str
= c_str() + iPos
+ 1;
656 // extract nCount first (leftmost) characters
657 wxString
wxString::Left(size_t nCount
) const
659 if ( nCount
> (size_t)GetStringData()->nDataLength
)
660 nCount
= GetStringData()->nDataLength
;
663 AllocCopy(dest
, nCount
, 0);
667 // get all characters before the first occurence of ch
668 // (returns the whole string if ch not found)
669 wxString
wxString::Left(char ch
) const
672 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
678 /// get all characters before the last occurence of ch
679 /// (returns empty string if ch not found)
680 wxString
wxString::Before(char ch
) const
683 int iPos
= Find(ch
, TRUE
);
684 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
685 str
= wxString(c_str(), iPos
);
690 /// get all characters after the first occurence of ch
691 /// (returns empty string if ch not found)
692 wxString
wxString::After(char ch
) const
696 if ( iPos
!= NOT_FOUND
)
697 str
= c_str() + iPos
+ 1;
702 // replace first (or all) occurences of some substring with another one
703 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
705 size_t uiCount
= 0; // count of replacements made
707 size_t uiOldLen
= Strlen(szOld
);
710 const char *pCurrent
= m_pchData
;
712 while ( *pCurrent
!= '\0' ) {
713 pSubstr
= strstr(pCurrent
, szOld
);
714 if ( pSubstr
== NULL
) {
715 // strTemp is unused if no replacements were made, so avoid the copy
719 strTemp
+= pCurrent
; // copy the rest
720 break; // exit the loop
723 // take chars before match
724 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
726 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
731 if ( !bReplaceAll
) {
732 strTemp
+= pCurrent
; // copy the rest
733 break; // exit the loop
738 // only done if there were replacements, otherwise would have returned above
744 bool wxString::IsAscii() const
746 const char *s
= (const char*) *this;
748 if(!isascii(*s
)) return(FALSE
);
754 bool wxString::IsWord() const
756 const char *s
= (const char*) *this;
758 if(!isalpha(*s
)) return(FALSE
);
764 bool wxString::IsNumber() const
766 const char *s
= (const char*) *this;
768 if(!isdigit(*s
)) return(FALSE
);
774 wxString
wxString::Strip(stripType w
) const
777 if ( w
& leading
) s
.Trim(FALSE
);
778 if ( w
& trailing
) s
.Trim(TRUE
);
782 // ---------------------------------------------------------------------------
784 // ---------------------------------------------------------------------------
786 wxString
& wxString::MakeUpper()
790 for ( char *p
= m_pchData
; *p
; p
++ )
791 *p
= (char)toupper(*p
);
796 wxString
& wxString::MakeLower()
800 for ( char *p
= m_pchData
; *p
; p
++ )
801 *p
= (char)tolower(*p
);
806 // ---------------------------------------------------------------------------
807 // trimming and padding
808 // ---------------------------------------------------------------------------
810 // trims spaces (in the sense of isspace) from left or right side
811 wxString
& wxString::Trim(bool bFromRight
)
813 // first check if we're going to modify the string at all
816 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
817 (!bFromRight
&& isspace(GetChar(0u)))
821 // ok, there is at least one space to trim
826 // find last non-space character
827 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
828 while ( isspace(*psz
) && (psz
>= m_pchData
) )
831 // truncate at trailing space start
833 GetStringData()->nDataLength
= psz
- m_pchData
;
837 // find first non-space character
838 const char *psz
= m_pchData
;
839 while ( isspace(*psz
) )
842 // fix up data and length
843 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
844 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
845 GetStringData()->nDataLength
= nDataLength
;
852 // adds nCount characters chPad to the string from either side
853 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
855 wxString
s(chPad
, nCount
);
868 // truncate the string
869 wxString
& wxString::Truncate(size_t uiLen
)
871 *(m_pchData
+ uiLen
) = '\0';
872 GetStringData()->nDataLength
= uiLen
;
877 // ---------------------------------------------------------------------------
878 // finding (return NOT_FOUND if not found and index otherwise)
879 // ---------------------------------------------------------------------------
882 int wxString::Find(char ch
, bool bFromEnd
) const
884 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
886 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
889 // find a sub-string (like strstr)
890 int wxString::Find(const char *pszSub
) const
892 const char *psz
= strstr(m_pchData
, pszSub
);
894 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
897 // ---------------------------------------------------------------------------
898 // stream-like operators
899 // ---------------------------------------------------------------------------
900 wxString
& wxString::operator<<(int i
)
905 return (*this) << res
;
908 wxString
& wxString::operator<<(float f
)
913 return (*this) << res
;
916 wxString
& wxString::operator<<(double d
)
921 return (*this) << res
;
924 // ---------------------------------------------------------------------------
926 // ---------------------------------------------------------------------------
927 int wxString::Printf(const char *pszFormat
, ...)
930 va_start(argptr
, pszFormat
);
932 int iLen
= PrintfV(pszFormat
, argptr
);
939 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
941 // static buffer to avoid dynamic memory allocation each time
942 static char s_szScratch
[1024];
944 int iLen
= wxVsprintf(s_szScratch
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
946 if ( (size_t)iLen
< WXSIZEOF(s_szScratch
) ) {
947 buffer
= s_szScratch
;
950 int size
= WXSIZEOF(s_szScratch
) * 2;
951 buffer
= (char *)malloc(size
);
952 while ( buffer
!= NULL
) {
953 iLen
= wxVsprintf(buffer
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
955 // ok, there was enough space
959 // still not enough, double it again
960 buffer
= (char *)realloc(buffer
, size
*= 2);
969 AllocBeforeWrite(iLen
);
970 strcpy(m_pchData
, buffer
);
972 if ( buffer
!= s_szScratch
)
978 // ----------------------------------------------------------------------------
979 // misc other operations
980 // ----------------------------------------------------------------------------
981 bool wxString::Matches(const char *pszMask
) const
983 // check char by char
985 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
986 switch ( *pszMask
) {
988 if ( *pszTxt
== '\0' )
997 // ignore special chars immediately following this one
998 while ( *pszMask
== '*' || *pszMask
== '?' )
1001 // if there is nothing more, match
1002 if ( *pszMask
== '\0' )
1005 // are there any other metacharacters in the mask?
1007 const char *pEndMask
= strpbrk(pszMask
, "*?");
1009 if ( pEndMask
!= NULL
) {
1010 // we have to match the string between two metachars
1011 uiLenMask
= pEndMask
- pszMask
;
1014 // we have to match the remainder of the string
1015 uiLenMask
= strlen(pszMask
);
1018 wxString
strToMatch(pszMask
, uiLenMask
);
1019 const char* pMatch
= strstr(pszTxt
, strToMatch
);
1020 if ( pMatch
== NULL
)
1023 // -1 to compensate "++" in the loop
1024 pszTxt
= pMatch
+ uiLenMask
- 1;
1025 pszMask
+= uiLenMask
- 1;
1030 if ( *pszMask
!= *pszTxt
)
1036 // match only if nothing left
1037 return *pszTxt
== '\0';
1040 // ---------------------------------------------------------------------------
1041 // standard C++ library string functions
1042 // ---------------------------------------------------------------------------
1043 #ifdef STD_STRING_COMPATIBILITY
1045 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1047 wxASSERT( str
.GetStringData()->IsValid() );
1048 wxASSERT( nPos
<= Len() );
1050 if ( !str
.IsEmpty() ) {
1052 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1053 strncpy(pc
, c_str(), nPos
);
1054 strcpy(pc
+ nPos
, str
);
1055 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1056 strTmp
.UngetWriteBuf();
1063 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1065 wxASSERT( str
.GetStringData()->IsValid() );
1066 wxASSERT( nStart
<= Len() );
1068 const char *p
= strstr(c_str() + nStart
, str
);
1070 return p
== NULL
? npos
: p
- c_str();
1073 // VC++ 1.5 can't cope with the default argument in the header.
1074 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1075 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1077 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1081 size_t wxString::find(char ch
, size_t nStart
) const
1083 wxASSERT( nStart
<= Len() );
1085 const char *p
= strchr(c_str() + nStart
, ch
);
1087 return p
== NULL
? npos
: p
- c_str();
1090 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1092 wxASSERT( str
.GetStringData()->IsValid() );
1093 wxASSERT( nStart
<= Len() );
1095 // # could be quicker than that
1096 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1097 while ( p
>= c_str() + str
.Len() ) {
1098 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1099 return p
- str
.Len() - c_str();
1106 // VC++ 1.5 can't cope with the default argument in the header.
1107 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1108 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1110 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1113 size_t wxString::rfind(char ch
, size_t nStart
) const
1115 wxASSERT( nStart
<= Len() );
1117 const char *p
= strrchr(c_str() + nStart
, ch
);
1119 return p
== NULL
? npos
: p
- c_str();
1123 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1125 // npos means 'take all'
1129 wxASSERT( nStart
+ nLen
<= Len() );
1131 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1134 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1136 wxString
strTmp(c_str(), nStart
);
1137 if ( nLen
!= npos
) {
1138 wxASSERT( nStart
+ nLen
<= Len() );
1140 strTmp
.append(c_str() + nStart
+ nLen
);
1147 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1149 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1153 strTmp
.append(c_str(), nStart
);
1155 strTmp
.append(c_str() + nStart
+ nLen
);
1161 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1163 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1166 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1167 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1169 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1172 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1173 const char* sz
, size_t nCount
)
1175 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1178 #endif //std::string compatibility
1180 // ============================================================================
1182 // ============================================================================
1184 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1185 #define ARRAY_MAXSIZE_INCREMENT 4096
1186 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1187 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1190 #define STRING(p) ((wxString *)(&(p)))
1193 wxArrayString::wxArrayString()
1197 m_pItems
= (char **) NULL
;
1201 wxArrayString::wxArrayString(const wxArrayString
& src
)
1205 m_pItems
= (char **) NULL
;
1210 // assignment operator
1211 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1216 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1217 Alloc(src
.m_nCount
);
1219 // we can't just copy the pointers here because otherwise we would share
1220 // the strings with another array
1221 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1224 if ( m_nCount
!= 0 )
1225 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1231 void wxArrayString::Grow()
1233 // only do it if no more place
1234 if( m_nCount
== m_nSize
) {
1235 if( m_nSize
== 0 ) {
1236 // was empty, alloc some memory
1237 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1238 m_pItems
= new char *[m_nSize
];
1241 // otherwise when it's called for the first time, nIncrement would be 0
1242 // and the array would never be expanded
1243 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1245 // add 50% but not too much
1246 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1247 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1248 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1249 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1250 m_nSize
+= nIncrement
;
1251 char **pNew
= new char *[m_nSize
];
1253 // copy data to new location
1254 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1256 // delete old memory (but do not release the strings!)
1257 wxDELETEA(m_pItems
);
1264 void wxArrayString::Free()
1266 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1267 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1271 // deletes all the strings from the list
1272 void wxArrayString::Empty()
1279 // as Empty, but also frees memory
1280 void wxArrayString::Clear()
1287 wxDELETEA(m_pItems
);
1291 wxArrayString::~wxArrayString()
1295 wxDELETEA(m_pItems
);
1298 // pre-allocates memory (frees the previous data!)
1299 void wxArrayString::Alloc(size_t nSize
)
1301 wxASSERT( nSize
> 0 );
1303 // only if old buffer was not big enough
1304 if ( nSize
> m_nSize
) {
1306 wxDELETEA(m_pItems
);
1307 m_pItems
= new char *[nSize
];
1314 // searches the array for an item (forward or backwards)
1315 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1318 if ( m_nCount
> 0 ) {
1319 size_t ui
= m_nCount
;
1321 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1328 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1329 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1337 // add item at the end
1338 void wxArrayString::Add(const wxString
& str
)
1340 wxASSERT( str
.GetStringData()->IsValid() );
1344 // the string data must not be deleted!
1345 str
.GetStringData()->Lock();
1346 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1349 // add item at the given position
1350 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1352 wxASSERT( str
.GetStringData()->IsValid() );
1354 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1358 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1359 (m_nCount
- nIndex
)*sizeof(char *));
1361 str
.GetStringData()->Lock();
1362 m_pItems
[nIndex
] = (char *)str
.c_str();
1367 // removes item from array (by index)
1368 void wxArrayString::Remove(size_t nIndex
)
1370 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1373 Item(nIndex
).GetStringData()->Unlock();
1375 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1376 (m_nCount
- nIndex
- 1)*sizeof(char *));
1380 // removes item from array (by value)
1381 void wxArrayString::Remove(const char *sz
)
1383 int iIndex
= Index(sz
);
1385 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1386 _("removing inexistent element in wxArrayString::Remove") );
1391 // sort array elements using passed comparaison function
1393 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1396 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);