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::
105 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
110 NAMESPACE streambuf
*sb
= is
.rdbuf();
113 int ch
= sb
->sbumpc ();
115 is
.setstate(NAMESPACE
ios::eofbit
);
118 else if ( isspace(ch
) ) {
130 if ( str
.length() == 0 )
131 is
.setstate(NAMESPACE
ios::failbit
);
136 #endif //std::string compatibility
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 // this small class is used to gather statistics for performance tuning
143 //#define WXSTRING_STATISTICS
144 #ifdef WXSTRING_STATISTICS
148 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
150 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
152 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
155 size_t m_nCount
, m_nTotal
;
157 } g_averageLength("allocation size"),
158 g_averageSummandLength("summand length"),
159 g_averageConcatHit("hit probability in concat"),
160 g_averageInitialLength("initial string length");
162 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
164 #define STATISTICS_ADD(av, val)
165 #endif // WXSTRING_STATISTICS
167 // ===========================================================================
168 // wxString class core
169 // ===========================================================================
171 // ---------------------------------------------------------------------------
173 // ---------------------------------------------------------------------------
175 // constructs string of <nLength> copies of character <ch>
176 wxString::wxString(char ch
, size_t nLength
)
181 AllocBuffer(nLength
);
183 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
185 memset(m_pchData
, ch
, nLength
);
189 // takes nLength elements of psz starting at nPos
190 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
194 wxASSERT( nPos
<= Strlen(psz
) );
196 if ( nLength
== STRING_MAXLEN
)
197 nLength
= Strlen(psz
+ nPos
);
199 STATISTICS_ADD(InitialLength
, nLength
);
202 // trailing '\0' is written in AllocBuffer()
203 AllocBuffer(nLength
);
204 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
208 // the same as previous constructor, but for compilers using unsigned char
209 wxString::wxString(const unsigned char* psz
, size_t nLength
)
211 InitWith((const char *)psz
, 0, nLength
);
214 #ifdef STD_STRING_COMPATIBILITY
216 // poor man's iterators are "void *" pointers
217 wxString::wxString(const void *pStart
, const void *pEnd
)
219 InitWith((const char *)pStart
, 0,
220 (const char *)pEnd
- (const char *)pStart
);
223 #endif //std::string compatibility
226 wxString::wxString(const wchar_t *pwz
)
228 // first get necessary size
230 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
231 // honor the 3rd parameter, thus it will happily crash here).
232 #ifdef wxUSE_WCSRTOMBS
233 // don't know if it's really needed (or if we can pass NULL), but better safe
236 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
238 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
244 wcstombs(m_pchData
, pwz
, nLen
);
251 // ---------------------------------------------------------------------------
253 // ---------------------------------------------------------------------------
255 // allocates memory needed to store a C string of length nLen
256 void wxString::AllocBuffer(size_t nLen
)
258 wxASSERT( nLen
> 0 ); //
259 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
261 STATISTICS_ADD(Length
, nLen
);
264 // 1) one extra character for '\0' termination
265 // 2) sizeof(wxStringData) for housekeeping info
266 wxStringData
* pData
= (wxStringData
*)
267 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
269 pData
->nDataLength
= nLen
;
270 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
271 m_pchData
= pData
->data(); // data starts after wxStringData
272 m_pchData
[nLen
] = '\0';
275 // must be called before changing this string
276 void wxString::CopyBeforeWrite()
278 wxStringData
* pData
= GetStringData();
280 if ( pData
->IsShared() ) {
281 pData
->Unlock(); // memory not freed because shared
282 size_t nLen
= pData
->nDataLength
;
284 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
287 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
290 // must be called before replacing contents of this string
291 void wxString::AllocBeforeWrite(size_t nLen
)
293 wxASSERT( nLen
!= 0 ); // doesn't make any sense
295 // must not share string and must have enough space
296 wxStringData
* pData
= GetStringData();
297 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
298 // can't work with old buffer, get new one
303 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
306 // allocate enough memory for nLen characters
307 void wxString::Alloc(size_t nLen
)
309 wxStringData
*pData
= GetStringData();
310 if ( pData
->nAllocLength
<= nLen
) {
311 if ( pData
->IsEmpty() ) {
314 wxStringData
* pData
= (wxStringData
*)
315 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
317 pData
->nDataLength
= 0;
318 pData
->nAllocLength
= nLen
;
319 m_pchData
= pData
->data(); // data starts after wxStringData
320 m_pchData
[0u] = '\0';
322 else if ( pData
->IsShared() ) {
323 pData
->Unlock(); // memory not freed because shared
324 size_t nOldLen
= pData
->nDataLength
;
326 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
331 wxStringData
*p
= (wxStringData
*)
332 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
335 // @@@ what to do on memory error?
339 // it's not important if the pointer changed or not (the check for this
340 // is not faster than assigning to m_pchData in all cases)
341 p
->nAllocLength
= nLen
;
342 m_pchData
= p
->data();
345 //else: we've already got enough
348 // shrink to minimal size (releasing extra memory)
349 void wxString::Shrink()
351 wxStringData
*pData
= GetStringData();
353 // this variable is unused in release build, so avoid the compiler warning by
354 // just not declaring it
358 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
360 wxASSERT( p
!= NULL
); // can't free memory?
361 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
364 // get the pointer to writable buffer of (at least) nLen bytes
365 char *wxString::GetWriteBuf(size_t nLen
)
367 AllocBeforeWrite(nLen
);
369 wxASSERT( GetStringData()->nRefs
== 1 );
370 GetStringData()->Validate(FALSE
);
375 // put string back in a reasonable state after GetWriteBuf
376 void wxString::UngetWriteBuf()
378 GetStringData()->nDataLength
= strlen(m_pchData
);
379 GetStringData()->Validate(TRUE
);
382 // ---------------------------------------------------------------------------
384 // ---------------------------------------------------------------------------
386 // all functions are inline in string.h
388 // ---------------------------------------------------------------------------
389 // assignment operators
390 // ---------------------------------------------------------------------------
392 // helper function: does real copy
393 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
395 if ( nSrcLen
== 0 ) {
399 AllocBeforeWrite(nSrcLen
);
400 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
401 GetStringData()->nDataLength
= nSrcLen
;
402 m_pchData
[nSrcLen
] = '\0';
406 // assigns one string to another
407 wxString
& wxString::operator=(const wxString
& stringSrc
)
409 wxASSERT( stringSrc
.GetStringData()->IsValid() );
411 // don't copy string over itself
412 if ( m_pchData
!= stringSrc
.m_pchData
) {
413 if ( stringSrc
.GetStringData()->IsEmpty() ) {
418 GetStringData()->Unlock();
419 m_pchData
= stringSrc
.m_pchData
;
420 GetStringData()->Lock();
427 // assigns a single character
428 wxString
& wxString::operator=(char ch
)
435 wxString
& wxString::operator=(const char *psz
)
437 AssignCopy(Strlen(psz
), psz
);
441 // same as 'signed char' variant
442 wxString
& wxString::operator=(const unsigned char* psz
)
444 *this = (const char *)psz
;
448 wxString
& wxString::operator=(const wchar_t *pwz
)
455 // ---------------------------------------------------------------------------
456 // string concatenation
457 // ---------------------------------------------------------------------------
459 // add something to this string
460 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
462 STATISTICS_ADD(SummandLength
, nSrcLen
);
464 // concatenating an empty string is a NOP
466 wxStringData
*pData
= GetStringData();
467 size_t nLen
= pData
->nDataLength
;
468 size_t nNewLen
= nLen
+ nSrcLen
;
470 // alloc new buffer if current is too small
471 if ( pData
->IsShared() ) {
472 STATISTICS_ADD(ConcatHit
, 0);
474 // we have to allocate another buffer
475 wxStringData
* pOldData
= GetStringData();
476 AllocBuffer(nNewLen
);
477 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
480 else if ( nNewLen
> pData
->nAllocLength
) {
481 STATISTICS_ADD(ConcatHit
, 0);
483 // we have to grow the buffer
487 STATISTICS_ADD(ConcatHit
, 1);
489 // the buffer is already big enough
492 // should be enough space
493 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
495 // fast concatenation - all is done in our buffer
496 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
498 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
499 GetStringData()->nDataLength
= nNewLen
; // and fix the length
501 //else: the string to append was empty
505 * concatenation functions come in 5 flavours:
507 * char + string and string + char
508 * C str + string and string + C str
511 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
513 wxASSERT( string1
.GetStringData()->IsValid() );
514 wxASSERT( string2
.GetStringData()->IsValid() );
516 wxString s
= string1
;
522 wxString
operator+(const wxString
& string
, char ch
)
524 wxASSERT( string
.GetStringData()->IsValid() );
532 wxString
operator+(char ch
, const wxString
& string
)
534 wxASSERT( string
.GetStringData()->IsValid() );
542 wxString
operator+(const wxString
& string
, const char *psz
)
544 wxASSERT( string
.GetStringData()->IsValid() );
547 s
.Alloc(Strlen(psz
) + string
.Len());
554 wxString
operator+(const char *psz
, const wxString
& string
)
556 wxASSERT( string
.GetStringData()->IsValid() );
559 s
.Alloc(Strlen(psz
) + string
.Len());
566 // ===========================================================================
567 // other common string functions
568 // ===========================================================================
570 // ---------------------------------------------------------------------------
571 // simple sub-string extraction
572 // ---------------------------------------------------------------------------
574 // helper function: clone the data attached to this string
575 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
577 if ( nCopyLen
== 0 ) {
581 dest
.AllocBuffer(nCopyLen
);
582 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
586 // extract string of length nCount starting at nFirst
587 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
589 wxStringData
*pData
= GetStringData();
590 size_t nLen
= pData
->nDataLength
;
592 // default value of nCount is STRING_MAXLEN and means "till the end"
593 if ( nCount
== STRING_MAXLEN
)
595 nCount
= nLen
- nFirst
;
598 // out-of-bounds requests return sensible things
599 if ( nFirst
+ nCount
> nLen
)
601 nCount
= nLen
- nFirst
;
606 // AllocCopy() will return empty string
611 AllocCopy(dest
, nCount
, nFirst
);
616 // extract nCount last (rightmost) characters
617 wxString
wxString::Right(size_t nCount
) const
619 if ( nCount
> (size_t)GetStringData()->nDataLength
)
620 nCount
= GetStringData()->nDataLength
;
623 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
627 // get all characters after the last occurence of ch
628 // (returns the whole string if ch not found)
629 wxString
wxString::Right(char ch
) const
632 int iPos
= Find(ch
, TRUE
);
633 if ( iPos
== NOT_FOUND
)
636 str
= c_str() + iPos
+ 1;
641 // extract nCount first (leftmost) characters
642 wxString
wxString::Left(size_t nCount
) const
644 if ( nCount
> (size_t)GetStringData()->nDataLength
)
645 nCount
= GetStringData()->nDataLength
;
648 AllocCopy(dest
, nCount
, 0);
652 // get all characters before the first occurence of ch
653 // (returns the whole string if ch not found)
654 wxString
wxString::Left(char ch
) const
657 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
663 /// get all characters before the last occurence of ch
664 /// (returns empty string if ch not found)
665 wxString
wxString::Before(char ch
) const
668 int iPos
= Find(ch
, TRUE
);
669 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
670 str
= wxString(c_str(), iPos
);
675 /// get all characters after the first occurence of ch
676 /// (returns empty string if ch not found)
677 wxString
wxString::After(char ch
) const
681 if ( iPos
!= NOT_FOUND
)
682 str
= c_str() + iPos
+ 1;
687 // replace first (or all) occurences of some substring with another one
688 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
690 size_t uiCount
= 0; // count of replacements made
692 size_t uiOldLen
= Strlen(szOld
);
695 const char *pCurrent
= m_pchData
;
697 while ( *pCurrent
!= '\0' ) {
698 pSubstr
= strstr(pCurrent
, szOld
);
699 if ( pSubstr
== NULL
) {
700 // strTemp is unused if no replacements were made, so avoid the copy
704 strTemp
+= pCurrent
; // copy the rest
705 break; // exit the loop
708 // take chars before match
709 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
711 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
716 if ( !bReplaceAll
) {
717 strTemp
+= pCurrent
; // copy the rest
718 break; // exit the loop
723 // only done if there were replacements, otherwise would have returned above
729 bool wxString::IsAscii() const
731 const char *s
= (const char*) *this;
733 if(!isascii(*s
)) return(FALSE
);
739 bool wxString::IsWord() const
741 const char *s
= (const char*) *this;
743 if(!isalpha(*s
)) return(FALSE
);
749 bool wxString::IsNumber() const
751 const char *s
= (const char*) *this;
753 if(!isdigit(*s
)) return(FALSE
);
759 wxString
wxString::Strip(stripType w
) const
762 if ( w
& leading
) s
.Trim(FALSE
);
763 if ( w
& trailing
) s
.Trim(TRUE
);
767 // ---------------------------------------------------------------------------
769 // ---------------------------------------------------------------------------
771 wxString
& wxString::MakeUpper()
775 for ( char *p
= m_pchData
; *p
; p
++ )
776 *p
= (char)toupper(*p
);
781 wxString
& wxString::MakeLower()
785 for ( char *p
= m_pchData
; *p
; p
++ )
786 *p
= (char)tolower(*p
);
791 // ---------------------------------------------------------------------------
792 // trimming and padding
793 // ---------------------------------------------------------------------------
795 // trims spaces (in the sense of isspace) from left or right side
796 wxString
& wxString::Trim(bool bFromRight
)
798 // first check if we're going to modify the string at all
801 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
802 (!bFromRight
&& isspace(GetChar(0u)))
806 // ok, there is at least one space to trim
811 // find last non-space character
812 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
813 while ( isspace(*psz
) && (psz
>= m_pchData
) )
816 // truncate at trailing space start
818 GetStringData()->nDataLength
= psz
- m_pchData
;
822 // find first non-space character
823 const char *psz
= m_pchData
;
824 while ( isspace(*psz
) )
827 // fix up data and length
828 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
829 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
830 GetStringData()->nDataLength
= nDataLength
;
837 // adds nCount characters chPad to the string from either side
838 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
840 wxString
s(chPad
, nCount
);
853 // truncate the string
854 wxString
& wxString::Truncate(size_t uiLen
)
856 *(m_pchData
+ uiLen
) = '\0';
857 GetStringData()->nDataLength
= uiLen
;
862 // ---------------------------------------------------------------------------
863 // finding (return NOT_FOUND if not found and index otherwise)
864 // ---------------------------------------------------------------------------
867 int wxString::Find(char ch
, bool bFromEnd
) const
869 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
871 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
874 // find a sub-string (like strstr)
875 int wxString::Find(const char *pszSub
) const
877 const char *psz
= strstr(m_pchData
, pszSub
);
879 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
882 // ---------------------------------------------------------------------------
883 // stream-like operators
884 // ---------------------------------------------------------------------------
885 wxString
& wxString::operator<<(int i
)
890 return (*this) << res
;
893 wxString
& wxString::operator<<(float f
)
898 return (*this) << res
;
901 wxString
& wxString::operator<<(double d
)
906 return (*this) << res
;
909 // ---------------------------------------------------------------------------
911 // ---------------------------------------------------------------------------
912 int wxString::Printf(const char *pszFormat
, ...)
915 va_start(argptr
, pszFormat
);
917 int iLen
= PrintfV(pszFormat
, argptr
);
924 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
928 #define wxVsprintf _vsnprintf
930 #else // guess that any Unix has snprintf() - feel free to insert additional
931 // platform/compiler tests here if this is not the case for you
932 #define wxVsprintf vsnprintf
936 #pragma message("Using sprintf() because no snprintf()-like function defined")
937 #define wxVsprintf vsprintf
940 // static buffer to avoid dynamic memory allocation each time
941 static char s_szScratch
[1024];
943 int iLen
= wxVsprintf(s_szScratch
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
945 if ( (size_t)iLen
< WXSIZEOF(s_szScratch
) ) {
946 buffer
= s_szScratch
;
949 int size
= WXSIZEOF(s_szScratch
) * 2;
950 buffer
= (char *)malloc(size
);
951 while ( buffer
!= NULL
) {
952 iLen
= wxVsprintf(buffer
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
954 // ok, there was enough space
958 // still not enough, double it again
959 buffer
= (char *)realloc(buffer
, size
*= 2);
968 AllocBeforeWrite(iLen
);
969 strcpy(m_pchData
, buffer
);
971 if ( buffer
!= s_szScratch
)
977 // ----------------------------------------------------------------------------
978 // misc other operations
979 // ----------------------------------------------------------------------------
980 bool wxString::Matches(const char *pszMask
) const
982 // check char by char
984 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
985 switch ( *pszMask
) {
987 if ( *pszTxt
== '\0' )
996 // ignore special chars immediately following this one
997 while ( *pszMask
== '*' || *pszMask
== '?' )
1000 // if there is nothing more, match
1001 if ( *pszMask
== '\0' )
1004 // are there any other metacharacters in the mask?
1006 const char *pEndMask
= strpbrk(pszMask
, "*?");
1008 if ( pEndMask
!= NULL
) {
1009 // we have to match the string between two metachars
1010 uiLenMask
= pEndMask
- pszMask
;
1013 // we have to match the remainder of the string
1014 uiLenMask
= strlen(pszMask
);
1017 wxString
strToMatch(pszMask
, uiLenMask
);
1018 const char* pMatch
= strstr(pszTxt
, strToMatch
);
1019 if ( pMatch
== NULL
)
1022 // -1 to compensate "++" in the loop
1023 pszTxt
= pMatch
+ uiLenMask
- 1;
1024 pszMask
+= uiLenMask
- 1;
1029 if ( *pszMask
!= *pszTxt
)
1035 // match only if nothing left
1036 return *pszTxt
== '\0';
1039 // ---------------------------------------------------------------------------
1040 // standard C++ library string functions
1041 // ---------------------------------------------------------------------------
1042 #ifdef STD_STRING_COMPATIBILITY
1044 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1046 wxASSERT( str
.GetStringData()->IsValid() );
1047 wxASSERT( nPos
<= Len() );
1049 if ( !str
.IsEmpty() ) {
1051 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1052 strncpy(pc
, c_str(), nPos
);
1053 strcpy(pc
+ nPos
, str
);
1054 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1055 strTmp
.UngetWriteBuf();
1062 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1064 wxASSERT( str
.GetStringData()->IsValid() );
1065 wxASSERT( nStart
<= Len() );
1067 const char *p
= strstr(c_str() + nStart
, str
);
1069 return p
== NULL
? npos
: p
- c_str();
1072 // VC++ 1.5 can't cope with the default argument in the header.
1073 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1074 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1076 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1080 size_t wxString::find(char ch
, size_t nStart
) const
1082 wxASSERT( nStart
<= Len() );
1084 const char *p
= strchr(c_str() + nStart
, ch
);
1086 return p
== NULL
? npos
: p
- c_str();
1089 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1091 wxASSERT( str
.GetStringData()->IsValid() );
1092 wxASSERT( nStart
<= Len() );
1094 // # could be quicker than that
1095 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1096 while ( p
>= c_str() + str
.Len() ) {
1097 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1098 return p
- str
.Len() - c_str();
1105 // VC++ 1.5 can't cope with the default argument in the header.
1106 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1107 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1109 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1112 size_t wxString::rfind(char ch
, size_t nStart
) const
1114 wxASSERT( nStart
<= Len() );
1116 const char *p
= strrchr(c_str() + nStart
, ch
);
1118 return p
== NULL
? npos
: p
- c_str();
1122 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1124 // npos means 'take all'
1128 wxASSERT( nStart
+ nLen
<= Len() );
1130 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1133 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1135 wxString
strTmp(c_str(), nStart
);
1136 if ( nLen
!= npos
) {
1137 wxASSERT( nStart
+ nLen
<= Len() );
1139 strTmp
.append(c_str() + nStart
+ nLen
);
1146 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1148 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1152 strTmp
.append(c_str(), nStart
);
1154 strTmp
.append(c_str() + nStart
+ nLen
);
1160 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1162 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1165 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1166 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1168 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1171 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1172 const char* sz
, size_t nCount
)
1174 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1177 #endif //std::string compatibility
1179 // ============================================================================
1181 // ============================================================================
1183 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1184 #define ARRAY_MAXSIZE_INCREMENT 4096
1185 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1186 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1189 #define STRING(p) ((wxString *)(&(p)))
1192 wxArrayString::wxArrayString()
1196 m_pItems
= (char **) NULL
;
1200 wxArrayString::wxArrayString(const wxArrayString
& src
)
1204 m_pItems
= (char **) NULL
;
1209 // assignment operator
1210 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1215 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1216 Alloc(src
.m_nCount
);
1218 // we can't just copy the pointers here because otherwise we would share
1219 // the strings with another array
1220 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1223 if ( m_nCount
!= 0 )
1224 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1230 void wxArrayString::Grow()
1232 // only do it if no more place
1233 if( m_nCount
== m_nSize
) {
1234 if( m_nSize
== 0 ) {
1235 // was empty, alloc some memory
1236 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1237 m_pItems
= new char *[m_nSize
];
1240 // otherwise when it's called for the first time, nIncrement would be 0
1241 // and the array would never be expanded
1242 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1244 // add 50% but not too much
1245 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1246 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1247 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1248 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1249 m_nSize
+= nIncrement
;
1250 char **pNew
= new char *[m_nSize
];
1252 // copy data to new location
1253 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1255 // delete old memory (but do not release the strings!)
1256 wxDELETEA(m_pItems
);
1263 void wxArrayString::Free()
1265 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1266 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1270 // deletes all the strings from the list
1271 void wxArrayString::Empty()
1278 // as Empty, but also frees memory
1279 void wxArrayString::Clear()
1286 wxDELETEA(m_pItems
);
1290 wxArrayString::~wxArrayString()
1294 wxDELETEA(m_pItems
);
1297 // pre-allocates memory (frees the previous data!)
1298 void wxArrayString::Alloc(size_t nSize
)
1300 wxASSERT( nSize
> 0 );
1302 // only if old buffer was not big enough
1303 if ( nSize
> m_nSize
) {
1305 wxDELETEA(m_pItems
);
1306 m_pItems
= new char *[nSize
];
1313 // searches the array for an item (forward or backwards)
1314 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1317 if ( m_nCount
> 0 ) {
1318 size_t ui
= m_nCount
;
1320 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1327 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1328 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1336 // add item at the end
1337 void wxArrayString::Add(const wxString
& str
)
1339 wxASSERT( str
.GetStringData()->IsValid() );
1343 // the string data must not be deleted!
1344 str
.GetStringData()->Lock();
1345 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1348 // add item at the given position
1349 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1351 wxASSERT( str
.GetStringData()->IsValid() );
1353 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1357 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1358 (m_nCount
- nIndex
)*sizeof(char *));
1360 str
.GetStringData()->Lock();
1361 m_pItems
[nIndex
] = (char *)str
.c_str();
1366 // removes item from array (by index)
1367 void wxArrayString::Remove(size_t nIndex
)
1369 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1372 Item(nIndex
).GetStringData()->Unlock();
1374 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1375 (m_nCount
- nIndex
- 1)*sizeof(char *));
1379 // removes item from array (by value)
1380 void wxArrayString::Remove(const char *sz
)
1382 int iIndex
= Index(sz
);
1384 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1385 _("removing inexistent element in wxArrayString::Remove") );
1390 // sort array elements using passed comparaison function
1392 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1395 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);