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 // This probably isn't right, what should it be Vadim?
45 // Otherwise we end up with no wxVsprintf defined.
50 #ifdef wxUSE_WCSRTOMBS
51 #include <wchar.h> // for wcsrtombs(), see comments where it's used
54 #ifdef WXSTRING_IS_WXOBJECT
55 IMPLEMENT_DYNAMIC_CLASS(wxString
, wxObject
)
56 #endif //WXSTRING_IS_WXOBJECT
58 // allocating extra space for each string consumes more memory but speeds up
59 // the concatenation operations (nLen is the current string's length)
60 // NB: EXTRA_ALLOC must be >= 0!
61 #define EXTRA_ALLOC (19 - nLen % 16)
63 // ---------------------------------------------------------------------------
64 // static class variables definition
65 // ---------------------------------------------------------------------------
67 #ifdef STD_STRING_COMPATIBILITY
68 const size_t wxString::npos
= STRING_MAXLEN
;
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // for an empty string, GetStringData() will return this address: this
76 // structure has the same layout as wxStringData and it's data() method will
77 // return the empty string (dummy pointer)
82 } g_strEmpty
= { {-1, 0, 0}, '\0' };
84 // empty C style string: points to 'string data' byte of g_strEmpty
85 extern const char *g_szNul
= &g_strEmpty
.dummy
;
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 #ifdef STD_STRING_COMPATIBILITY
93 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
96 // ATTN: you can _not_ use both of these in the same program!
105 // for msvc (bcc50+ also) you don't need these NAMESPACE defines,
106 // using namespace std; takes care of that.
107 #define NAMESPACE std::
112 #define wxVsprintf _vsnprintf
115 #if defined ( HAVE_VSNPRINTF )
116 #define wxVsprintf vsnprintf
121 // vsprintf() is ANSI so we can always use it, but it's unsafe!
122 #define wxVsprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
123 #pragma message("Using sprintf() because no snprintf()-like function defined")
126 NAMESPACE istream
& operator>>(NAMESPACE istream
& is
, wxString
& WXUNUSED(str
))
131 NAMESPACE streambuf
*sb
= is
.rdbuf();
134 int ch
= sb
->sbumpc ();
136 is
.setstate(NAMESPACE
ios::eofbit
);
139 else if ( isspace(ch
) ) {
151 if ( str
.length() == 0 )
152 is
.setstate(NAMESPACE
ios::failbit
);
157 #endif //std::string compatibility
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 // this small class is used to gather statistics for performance tuning
164 //#define WXSTRING_STATISTICS
165 #ifdef WXSTRING_STATISTICS
169 Averager(const char *sz
) { m_sz
= sz
; m_nTotal
= m_nCount
= 0; }
171 { printf("wxString: average %s = %f\n", m_sz
, ((float)m_nTotal
)/m_nCount
); }
173 void Add(size_t n
) { m_nTotal
+= n
; m_nCount
++; }
176 size_t m_nCount
, m_nTotal
;
178 } g_averageLength("allocation size"),
179 g_averageSummandLength("summand length"),
180 g_averageConcatHit("hit probability in concat"),
181 g_averageInitialLength("initial string length");
183 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
185 #define STATISTICS_ADD(av, val)
186 #endif // WXSTRING_STATISTICS
188 // ===========================================================================
189 // wxString class core
190 // ===========================================================================
192 // ---------------------------------------------------------------------------
194 // ---------------------------------------------------------------------------
196 // constructs string of <nLength> copies of character <ch>
197 wxString::wxString(char ch
, size_t nLength
)
202 AllocBuffer(nLength
);
204 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
206 memset(m_pchData
, ch
, nLength
);
210 // takes nLength elements of psz starting at nPos
211 void wxString::InitWith(const char *psz
, size_t nPos
, size_t nLength
)
215 wxASSERT( nPos
<= Strlen(psz
) );
217 if ( nLength
== STRING_MAXLEN
)
218 nLength
= Strlen(psz
+ nPos
);
220 STATISTICS_ADD(InitialLength
, nLength
);
223 // trailing '\0' is written in AllocBuffer()
224 AllocBuffer(nLength
);
225 memcpy(m_pchData
, psz
+ nPos
, nLength
*sizeof(char));
229 // the same as previous constructor, but for compilers using unsigned char
230 wxString::wxString(const unsigned char* psz
, size_t nLength
)
232 InitWith((const char *)psz
, 0, nLength
);
235 #ifdef STD_STRING_COMPATIBILITY
237 // poor man's iterators are "void *" pointers
238 wxString::wxString(const void *pStart
, const void *pEnd
)
240 InitWith((const char *)pStart
, 0,
241 (const char *)pEnd
- (const char *)pStart
);
244 #endif //std::string compatibility
247 wxString::wxString(const wchar_t *pwz
)
249 // first get necessary size
251 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
252 // honor the 3rd parameter, thus it will happily crash here).
253 #ifdef wxUSE_WCSRTOMBS
254 // don't know if it's really needed (or if we can pass NULL), but better safe
257 size_t nLen
= wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
259 size_t nLen
= wcstombs((char *) NULL
, pwz
, 0);
265 wcstombs(m_pchData
, pwz
, nLen
);
272 // ---------------------------------------------------------------------------
274 // ---------------------------------------------------------------------------
276 // allocates memory needed to store a C string of length nLen
277 void wxString::AllocBuffer(size_t nLen
)
279 wxASSERT( nLen
> 0 ); //
280 wxASSERT( nLen
<= INT_MAX
-1 ); // max size (enough room for 1 extra)
282 STATISTICS_ADD(Length
, nLen
);
285 // 1) one extra character for '\0' termination
286 // 2) sizeof(wxStringData) for housekeeping info
287 wxStringData
* pData
= (wxStringData
*)
288 malloc(sizeof(wxStringData
) + (nLen
+ EXTRA_ALLOC
+ 1)*sizeof(char));
290 pData
->nDataLength
= nLen
;
291 pData
->nAllocLength
= nLen
+ EXTRA_ALLOC
;
292 m_pchData
= pData
->data(); // data starts after wxStringData
293 m_pchData
[nLen
] = '\0';
296 // must be called before changing this string
297 void wxString::CopyBeforeWrite()
299 wxStringData
* pData
= GetStringData();
301 if ( pData
->IsShared() ) {
302 pData
->Unlock(); // memory not freed because shared
303 size_t nLen
= pData
->nDataLength
;
305 memcpy(m_pchData
, pData
->data(), nLen
*sizeof(char));
308 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
311 // must be called before replacing contents of this string
312 void wxString::AllocBeforeWrite(size_t nLen
)
314 wxASSERT( nLen
!= 0 ); // doesn't make any sense
316 // must not share string and must have enough space
317 wxStringData
* pData
= GetStringData();
318 if ( pData
->IsShared() || (nLen
> pData
->nAllocLength
) ) {
319 // can't work with old buffer, get new one
324 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
327 // allocate enough memory for nLen characters
328 void wxString::Alloc(size_t nLen
)
330 wxStringData
*pData
= GetStringData();
331 if ( pData
->nAllocLength
<= nLen
) {
332 if ( pData
->IsEmpty() ) {
335 wxStringData
* pData
= (wxStringData
*)
336 malloc(sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
338 pData
->nDataLength
= 0;
339 pData
->nAllocLength
= nLen
;
340 m_pchData
= pData
->data(); // data starts after wxStringData
341 m_pchData
[0u] = '\0';
343 else if ( pData
->IsShared() ) {
344 pData
->Unlock(); // memory not freed because shared
345 size_t nOldLen
= pData
->nDataLength
;
347 memcpy(m_pchData
, pData
->data(), nOldLen
*sizeof(char));
352 wxStringData
*p
= (wxStringData
*)
353 realloc(pData
, sizeof(wxStringData
) + (nLen
+ 1)*sizeof(char));
356 // @@@ what to do on memory error?
360 // it's not important if the pointer changed or not (the check for this
361 // is not faster than assigning to m_pchData in all cases)
362 p
->nAllocLength
= nLen
;
363 m_pchData
= p
->data();
366 //else: we've already got enough
369 // shrink to minimal size (releasing extra memory)
370 void wxString::Shrink()
372 wxStringData
*pData
= GetStringData();
374 // this variable is unused in release build, so avoid the compiler warning by
375 // just not declaring it
379 realloc(pData
, sizeof(wxStringData
) + (pData
->nDataLength
+ 1)*sizeof(char));
381 wxASSERT( p
!= NULL
); // can't free memory?
382 wxASSERT( p
== pData
); // we're decrementing the size - block shouldn't move!
385 // get the pointer to writable buffer of (at least) nLen bytes
386 char *wxString::GetWriteBuf(size_t nLen
)
388 AllocBeforeWrite(nLen
);
390 wxASSERT( GetStringData()->nRefs
== 1 );
391 GetStringData()->Validate(FALSE
);
396 // put string back in a reasonable state after GetWriteBuf
397 void wxString::UngetWriteBuf()
399 GetStringData()->nDataLength
= strlen(m_pchData
);
400 GetStringData()->Validate(TRUE
);
403 // ---------------------------------------------------------------------------
405 // ---------------------------------------------------------------------------
407 // all functions are inline in string.h
409 // ---------------------------------------------------------------------------
410 // assignment operators
411 // ---------------------------------------------------------------------------
413 // helper function: does real copy
414 void wxString::AssignCopy(size_t nSrcLen
, const char *pszSrcData
)
416 if ( nSrcLen
== 0 ) {
420 AllocBeforeWrite(nSrcLen
);
421 memcpy(m_pchData
, pszSrcData
, nSrcLen
*sizeof(char));
422 GetStringData()->nDataLength
= nSrcLen
;
423 m_pchData
[nSrcLen
] = '\0';
427 // assigns one string to another
428 wxString
& wxString::operator=(const wxString
& stringSrc
)
430 wxASSERT( stringSrc
.GetStringData()->IsValid() );
432 // don't copy string over itself
433 if ( m_pchData
!= stringSrc
.m_pchData
) {
434 if ( stringSrc
.GetStringData()->IsEmpty() ) {
439 GetStringData()->Unlock();
440 m_pchData
= stringSrc
.m_pchData
;
441 GetStringData()->Lock();
448 // assigns a single character
449 wxString
& wxString::operator=(char ch
)
456 wxString
& wxString::operator=(const char *psz
)
458 AssignCopy(Strlen(psz
), psz
);
462 // same as 'signed char' variant
463 wxString
& wxString::operator=(const unsigned char* psz
)
465 *this = (const char *)psz
;
469 wxString
& wxString::operator=(const wchar_t *pwz
)
476 // ---------------------------------------------------------------------------
477 // string concatenation
478 // ---------------------------------------------------------------------------
480 // add something to this string
481 void wxString::ConcatSelf(int nSrcLen
, const char *pszSrcData
)
483 STATISTICS_ADD(SummandLength
, nSrcLen
);
485 // concatenating an empty string is a NOP
487 wxStringData
*pData
= GetStringData();
488 size_t nLen
= pData
->nDataLength
;
489 size_t nNewLen
= nLen
+ nSrcLen
;
491 // alloc new buffer if current is too small
492 if ( pData
->IsShared() ) {
493 STATISTICS_ADD(ConcatHit
, 0);
495 // we have to allocate another buffer
496 wxStringData
* pOldData
= GetStringData();
497 AllocBuffer(nNewLen
);
498 memcpy(m_pchData
, pOldData
->data(), nLen
*sizeof(char));
501 else if ( nNewLen
> pData
->nAllocLength
) {
502 STATISTICS_ADD(ConcatHit
, 0);
504 // we have to grow the buffer
508 STATISTICS_ADD(ConcatHit
, 1);
510 // the buffer is already big enough
513 // should be enough space
514 wxASSERT( nNewLen
<= GetStringData()->nAllocLength
);
516 // fast concatenation - all is done in our buffer
517 memcpy(m_pchData
+ nLen
, pszSrcData
, nSrcLen
*sizeof(char));
519 m_pchData
[nNewLen
] = '\0'; // put terminating '\0'
520 GetStringData()->nDataLength
= nNewLen
; // and fix the length
522 //else: the string to append was empty
526 * concatenation functions come in 5 flavours:
528 * char + string and string + char
529 * C str + string and string + C str
532 wxString
operator+(const wxString
& string1
, const wxString
& string2
)
534 wxASSERT( string1
.GetStringData()->IsValid() );
535 wxASSERT( string2
.GetStringData()->IsValid() );
537 wxString s
= string1
;
543 wxString
operator+(const wxString
& string
, char ch
)
545 wxASSERT( string
.GetStringData()->IsValid() );
553 wxString
operator+(char ch
, const wxString
& string
)
555 wxASSERT( string
.GetStringData()->IsValid() );
563 wxString
operator+(const wxString
& string
, const char *psz
)
565 wxASSERT( string
.GetStringData()->IsValid() );
568 s
.Alloc(Strlen(psz
) + string
.Len());
575 wxString
operator+(const char *psz
, const wxString
& string
)
577 wxASSERT( string
.GetStringData()->IsValid() );
580 s
.Alloc(Strlen(psz
) + string
.Len());
587 // ===========================================================================
588 // other common string functions
589 // ===========================================================================
591 // ---------------------------------------------------------------------------
592 // simple sub-string extraction
593 // ---------------------------------------------------------------------------
595 // helper function: clone the data attached to this string
596 void wxString::AllocCopy(wxString
& dest
, int nCopyLen
, int nCopyIndex
) const
598 if ( nCopyLen
== 0 ) {
602 dest
.AllocBuffer(nCopyLen
);
603 memcpy(dest
.m_pchData
, m_pchData
+ nCopyIndex
, nCopyLen
*sizeof(char));
607 // extract string of length nCount starting at nFirst
608 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
610 wxStringData
*pData
= GetStringData();
611 size_t nLen
= pData
->nDataLength
;
613 // default value of nCount is STRING_MAXLEN and means "till the end"
614 if ( nCount
== STRING_MAXLEN
)
616 nCount
= nLen
- nFirst
;
619 // out-of-bounds requests return sensible things
620 if ( nFirst
+ nCount
> nLen
)
622 nCount
= nLen
- nFirst
;
627 // AllocCopy() will return empty string
632 AllocCopy(dest
, nCount
, nFirst
);
637 // extract nCount last (rightmost) characters
638 wxString
wxString::Right(size_t nCount
) const
640 if ( nCount
> (size_t)GetStringData()->nDataLength
)
641 nCount
= GetStringData()->nDataLength
;
644 AllocCopy(dest
, nCount
, GetStringData()->nDataLength
- nCount
);
648 // get all characters after the last occurence of ch
649 // (returns the whole string if ch not found)
650 wxString
wxString::Right(char ch
) const
653 int iPos
= Find(ch
, TRUE
);
654 if ( iPos
== NOT_FOUND
)
657 str
= c_str() + iPos
+ 1;
662 // extract nCount first (leftmost) characters
663 wxString
wxString::Left(size_t nCount
) const
665 if ( nCount
> (size_t)GetStringData()->nDataLength
)
666 nCount
= GetStringData()->nDataLength
;
669 AllocCopy(dest
, nCount
, 0);
673 // get all characters before the first occurence of ch
674 // (returns the whole string if ch not found)
675 wxString
wxString::Left(char ch
) const
678 for ( const char *pc
= m_pchData
; *pc
!= '\0' && *pc
!= ch
; pc
++ )
684 /// get all characters before the last occurence of ch
685 /// (returns empty string if ch not found)
686 wxString
wxString::Before(char ch
) const
689 int iPos
= Find(ch
, TRUE
);
690 if ( iPos
!= NOT_FOUND
&& iPos
!= 0 )
691 str
= wxString(c_str(), iPos
);
696 /// get all characters after the first occurence of ch
697 /// (returns empty string if ch not found)
698 wxString
wxString::After(char ch
) const
702 if ( iPos
!= NOT_FOUND
)
703 str
= c_str() + iPos
+ 1;
708 // replace first (or all) occurences of some substring with another one
709 size_t wxString::Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
)
711 size_t uiCount
= 0; // count of replacements made
713 size_t uiOldLen
= Strlen(szOld
);
716 const char *pCurrent
= m_pchData
;
718 while ( *pCurrent
!= '\0' ) {
719 pSubstr
= strstr(pCurrent
, szOld
);
720 if ( pSubstr
== NULL
) {
721 // strTemp is unused if no replacements were made, so avoid the copy
725 strTemp
+= pCurrent
; // copy the rest
726 break; // exit the loop
729 // take chars before match
730 strTemp
.ConcatSelf(pSubstr
- pCurrent
, pCurrent
);
732 pCurrent
= pSubstr
+ uiOldLen
; // restart after match
737 if ( !bReplaceAll
) {
738 strTemp
+= pCurrent
; // copy the rest
739 break; // exit the loop
744 // only done if there were replacements, otherwise would have returned above
750 bool wxString::IsAscii() const
752 const char *s
= (const char*) *this;
754 if(!isascii(*s
)) return(FALSE
);
760 bool wxString::IsWord() const
762 const char *s
= (const char*) *this;
764 if(!isalpha(*s
)) return(FALSE
);
770 bool wxString::IsNumber() const
772 const char *s
= (const char*) *this;
774 if(!isdigit(*s
)) return(FALSE
);
780 wxString
wxString::Strip(stripType w
) const
783 if ( w
& leading
) s
.Trim(FALSE
);
784 if ( w
& trailing
) s
.Trim(TRUE
);
788 // ---------------------------------------------------------------------------
790 // ---------------------------------------------------------------------------
792 wxString
& wxString::MakeUpper()
796 for ( char *p
= m_pchData
; *p
; p
++ )
797 *p
= (char)toupper(*p
);
802 wxString
& wxString::MakeLower()
806 for ( char *p
= m_pchData
; *p
; p
++ )
807 *p
= (char)tolower(*p
);
812 // ---------------------------------------------------------------------------
813 // trimming and padding
814 // ---------------------------------------------------------------------------
816 // trims spaces (in the sense of isspace) from left or right side
817 wxString
& wxString::Trim(bool bFromRight
)
819 // first check if we're going to modify the string at all
822 (bFromRight
&& isspace(GetChar(Len() - 1))) ||
823 (!bFromRight
&& isspace(GetChar(0u)))
827 // ok, there is at least one space to trim
832 // find last non-space character
833 char *psz
= m_pchData
+ GetStringData()->nDataLength
- 1;
834 while ( isspace(*psz
) && (psz
>= m_pchData
) )
837 // truncate at trailing space start
839 GetStringData()->nDataLength
= psz
- m_pchData
;
843 // find first non-space character
844 const char *psz
= m_pchData
;
845 while ( isspace(*psz
) )
848 // fix up data and length
849 int nDataLength
= GetStringData()->nDataLength
- (psz
- m_pchData
);
850 memmove(m_pchData
, psz
, (nDataLength
+ 1)*sizeof(char));
851 GetStringData()->nDataLength
= nDataLength
;
858 // adds nCount characters chPad to the string from either side
859 wxString
& wxString::Pad(size_t nCount
, char chPad
, bool bFromRight
)
861 wxString
s(chPad
, nCount
);
874 // truncate the string
875 wxString
& wxString::Truncate(size_t uiLen
)
877 *(m_pchData
+ uiLen
) = '\0';
878 GetStringData()->nDataLength
= uiLen
;
883 // ---------------------------------------------------------------------------
884 // finding (return NOT_FOUND if not found and index otherwise)
885 // ---------------------------------------------------------------------------
888 int wxString::Find(char ch
, bool bFromEnd
) const
890 const char *psz
= bFromEnd
? strrchr(m_pchData
, ch
) : strchr(m_pchData
, ch
);
892 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
895 // find a sub-string (like strstr)
896 int wxString::Find(const char *pszSub
) const
898 const char *psz
= strstr(m_pchData
, pszSub
);
900 return (psz
== NULL
) ? NOT_FOUND
: psz
- m_pchData
;
903 // ---------------------------------------------------------------------------
904 // stream-like operators
905 // ---------------------------------------------------------------------------
906 wxString
& wxString::operator<<(int i
)
911 return (*this) << res
;
914 wxString
& wxString::operator<<(float f
)
919 return (*this) << res
;
922 wxString
& wxString::operator<<(double d
)
927 return (*this) << res
;
930 // ---------------------------------------------------------------------------
932 // ---------------------------------------------------------------------------
933 int wxString::Printf(const char *pszFormat
, ...)
936 va_start(argptr
, pszFormat
);
938 int iLen
= PrintfV(pszFormat
, argptr
);
945 int wxString::PrintfV(const char* pszFormat
, va_list argptr
)
947 // static buffer to avoid dynamic memory allocation each time
948 static char s_szScratch
[1024];
950 int iLen
= wxVsprintf(s_szScratch
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
952 if ( (size_t)iLen
< WXSIZEOF(s_szScratch
) ) {
953 buffer
= s_szScratch
;
956 int size
= WXSIZEOF(s_szScratch
) * 2;
957 buffer
= (char *)malloc(size
);
958 while ( buffer
!= NULL
) {
959 iLen
= wxVsprintf(buffer
, WXSIZEOF(s_szScratch
), pszFormat
, argptr
);
961 // ok, there was enough space
965 // still not enough, double it again
966 buffer
= (char *)realloc(buffer
, size
*= 2);
975 AllocBeforeWrite(iLen
);
976 strcpy(m_pchData
, buffer
);
978 if ( buffer
!= s_szScratch
)
984 // ----------------------------------------------------------------------------
985 // misc other operations
986 // ----------------------------------------------------------------------------
987 bool wxString::Matches(const char *pszMask
) const
989 // check char by char
991 for ( pszTxt
= c_str(); *pszMask
!= '\0'; pszMask
++, pszTxt
++ ) {
992 switch ( *pszMask
) {
994 if ( *pszTxt
== '\0' )
1003 // ignore special chars immediately following this one
1004 while ( *pszMask
== '*' || *pszMask
== '?' )
1007 // if there is nothing more, match
1008 if ( *pszMask
== '\0' )
1011 // are there any other metacharacters in the mask?
1013 const char *pEndMask
= strpbrk(pszMask
, "*?");
1015 if ( pEndMask
!= NULL
) {
1016 // we have to match the string between two metachars
1017 uiLenMask
= pEndMask
- pszMask
;
1020 // we have to match the remainder of the string
1021 uiLenMask
= strlen(pszMask
);
1024 wxString
strToMatch(pszMask
, uiLenMask
);
1025 const char* pMatch
= strstr(pszTxt
, strToMatch
);
1026 if ( pMatch
== NULL
)
1029 // -1 to compensate "++" in the loop
1030 pszTxt
= pMatch
+ uiLenMask
- 1;
1031 pszMask
+= uiLenMask
- 1;
1036 if ( *pszMask
!= *pszTxt
)
1042 // match only if nothing left
1043 return *pszTxt
== '\0';
1046 // ---------------------------------------------------------------------------
1047 // standard C++ library string functions
1048 // ---------------------------------------------------------------------------
1049 #ifdef STD_STRING_COMPATIBILITY
1051 wxString
& wxString::insert(size_t nPos
, const wxString
& str
)
1053 wxASSERT( str
.GetStringData()->IsValid() );
1054 wxASSERT( nPos
<= Len() );
1056 if ( !str
.IsEmpty() ) {
1058 char *pc
= strTmp
.GetWriteBuf(Len() + str
.Len());
1059 strncpy(pc
, c_str(), nPos
);
1060 strcpy(pc
+ nPos
, str
);
1061 strcpy(pc
+ nPos
+ str
.Len(), c_str() + nPos
);
1062 strTmp
.UngetWriteBuf();
1069 size_t wxString::find(const wxString
& str
, size_t nStart
) const
1071 wxASSERT( str
.GetStringData()->IsValid() );
1072 wxASSERT( nStart
<= Len() );
1074 const char *p
= strstr(c_str() + nStart
, str
);
1076 return p
== NULL
? npos
: p
- c_str();
1079 // VC++ 1.5 can't cope with the default argument in the header.
1080 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1081 size_t wxString::find(const char* sz
, size_t nStart
, size_t n
) const
1083 return find(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1087 size_t wxString::find(char ch
, size_t nStart
) const
1089 wxASSERT( nStart
<= Len() );
1091 const char *p
= strchr(c_str() + nStart
, ch
);
1093 return p
== NULL
? npos
: p
- c_str();
1096 size_t wxString::rfind(const wxString
& str
, size_t nStart
) const
1098 wxASSERT( str
.GetStringData()->IsValid() );
1099 wxASSERT( nStart
<= Len() );
1101 // # could be quicker than that
1102 const char *p
= c_str() + (nStart
== npos
? Len() : nStart
);
1103 while ( p
>= c_str() + str
.Len() ) {
1104 if ( strncmp(p
- str
.Len(), str
, str
.Len()) == 0 )
1105 return p
- str
.Len() - c_str();
1112 // VC++ 1.5 can't cope with the default argument in the header.
1113 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
1114 size_t wxString::rfind(const char* sz
, size_t nStart
, size_t n
) const
1116 return rfind(wxString(sz
, n
== npos
? 0 : n
), nStart
);
1119 size_t wxString::rfind(char ch
, size_t nStart
) const
1121 wxASSERT( nStart
<= Len() );
1123 const char *p
= strrchr(c_str() + nStart
, ch
);
1125 return p
== NULL
? npos
: p
- c_str();
1129 wxString
wxString::substr(size_t nStart
, size_t nLen
) const
1131 // npos means 'take all'
1135 wxASSERT( nStart
+ nLen
<= Len() );
1137 return wxString(c_str() + nStart
, nLen
== npos
? 0 : nLen
);
1140 wxString
& wxString::erase(size_t nStart
, size_t nLen
)
1142 wxString
strTmp(c_str(), nStart
);
1143 if ( nLen
!= npos
) {
1144 wxASSERT( nStart
+ nLen
<= Len() );
1146 strTmp
.append(c_str() + nStart
+ nLen
);
1153 wxString
& wxString::replace(size_t nStart
, size_t nLen
, const char *sz
)
1155 wxASSERT( nStart
+ nLen
<= Strlen(sz
) );
1159 strTmp
.append(c_str(), nStart
);
1161 strTmp
.append(c_str() + nStart
+ nLen
);
1167 wxString
& wxString::replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
)
1169 return replace(nStart
, nLen
, wxString(ch
, nCount
));
1172 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1173 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1175 return replace(nStart
, nLen
, str
.substr(nStart2
, nLen2
));
1178 wxString
& wxString::replace(size_t nStart
, size_t nLen
,
1179 const char* sz
, size_t nCount
)
1181 return replace(nStart
, nLen
, wxString(sz
, nCount
));
1184 #endif //std::string compatibility
1186 // ============================================================================
1188 // ============================================================================
1190 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1191 #define ARRAY_MAXSIZE_INCREMENT 4096
1192 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1193 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1196 #define STRING(p) ((wxString *)(&(p)))
1199 wxArrayString::wxArrayString()
1203 m_pItems
= (char **) NULL
;
1207 wxArrayString::wxArrayString(const wxArrayString
& src
)
1211 m_pItems
= (char **) NULL
;
1216 // assignment operator
1217 wxArrayString
& wxArrayString::operator=(const wxArrayString
& src
)
1222 if ( src
.m_nCount
> ARRAY_DEFAULT_INITIAL_SIZE
)
1223 Alloc(src
.m_nCount
);
1225 // we can't just copy the pointers here because otherwise we would share
1226 // the strings with another array
1227 for ( size_t n
= 0; n
< src
.m_nCount
; n
++ )
1230 if ( m_nCount
!= 0 )
1231 memcpy(m_pItems
, src
.m_pItems
, m_nCount
*sizeof(char *));
1237 void wxArrayString::Grow()
1239 // only do it if no more place
1240 if( m_nCount
== m_nSize
) {
1241 if( m_nSize
== 0 ) {
1242 // was empty, alloc some memory
1243 m_nSize
= ARRAY_DEFAULT_INITIAL_SIZE
;
1244 m_pItems
= new char *[m_nSize
];
1247 // otherwise when it's called for the first time, nIncrement would be 0
1248 // and the array would never be expanded
1249 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE
!= 0 );
1251 // add 50% but not too much
1252 size_t nIncrement
= m_nSize
< ARRAY_DEFAULT_INITIAL_SIZE
1253 ? ARRAY_DEFAULT_INITIAL_SIZE
: m_nSize
>> 1;
1254 if ( nIncrement
> ARRAY_MAXSIZE_INCREMENT
)
1255 nIncrement
= ARRAY_MAXSIZE_INCREMENT
;
1256 m_nSize
+= nIncrement
;
1257 char **pNew
= new char *[m_nSize
];
1259 // copy data to new location
1260 memcpy(pNew
, m_pItems
, m_nCount
*sizeof(char *));
1262 // delete old memory (but do not release the strings!)
1263 wxDELETEA(m_pItems
);
1270 void wxArrayString::Free()
1272 for ( size_t n
= 0; n
< m_nCount
; n
++ ) {
1273 STRING(m_pItems
[n
])->GetStringData()->Unlock();
1277 // deletes all the strings from the list
1278 void wxArrayString::Empty()
1285 // as Empty, but also frees memory
1286 void wxArrayString::Clear()
1293 wxDELETEA(m_pItems
);
1297 wxArrayString::~wxArrayString()
1301 wxDELETEA(m_pItems
);
1304 // pre-allocates memory (frees the previous data!)
1305 void wxArrayString::Alloc(size_t nSize
)
1307 wxASSERT( nSize
> 0 );
1309 // only if old buffer was not big enough
1310 if ( nSize
> m_nSize
) {
1312 wxDELETEA(m_pItems
);
1313 m_pItems
= new char *[nSize
];
1320 // searches the array for an item (forward or backwards)
1321 int wxArrayString::Index(const char *sz
, bool bCase
, bool bFromEnd
) const
1324 if ( m_nCount
> 0 ) {
1325 size_t ui
= m_nCount
;
1327 if ( STRING(m_pItems
[--ui
])->IsSameAs(sz
, bCase
) )
1334 for( size_t ui
= 0; ui
< m_nCount
; ui
++ ) {
1335 if( STRING(m_pItems
[ui
])->IsSameAs(sz
, bCase
) )
1343 // add item at the end
1344 void wxArrayString::Add(const wxString
& str
)
1346 wxASSERT( str
.GetStringData()->IsValid() );
1350 // the string data must not be deleted!
1351 str
.GetStringData()->Lock();
1352 m_pItems
[m_nCount
++] = (char *)str
.c_str();
1355 // add item at the given position
1356 void wxArrayString::Insert(const wxString
& str
, size_t nIndex
)
1358 wxASSERT( str
.GetStringData()->IsValid() );
1360 wxCHECK_RET( nIndex
<= m_nCount
, ("bad index in wxArrayString::Insert") );
1364 memmove(&m_pItems
[nIndex
+ 1], &m_pItems
[nIndex
],
1365 (m_nCount
- nIndex
)*sizeof(char *));
1367 str
.GetStringData()->Lock();
1368 m_pItems
[nIndex
] = (char *)str
.c_str();
1373 // removes item from array (by index)
1374 void wxArrayString::Remove(size_t nIndex
)
1376 wxCHECK_RET( nIndex
<= m_nCount
, _("bad index in wxArrayString::Remove") );
1379 Item(nIndex
).GetStringData()->Unlock();
1381 memmove(&m_pItems
[nIndex
], &m_pItems
[nIndex
+ 1],
1382 (m_nCount
- nIndex
- 1)*sizeof(char *));
1386 // removes item from array (by value)
1387 void wxArrayString::Remove(const char *sz
)
1389 int iIndex
= Index(sz
);
1391 wxCHECK_RET( iIndex
!= NOT_FOUND
,
1392 _("removing inexistent element in wxArrayString::Remove") );
1397 // sort array elements using passed comparaison function
1399 void wxArrayString::Sort(bool WXUNUSED(bCase
), bool WXUNUSED(bReverse
) )
1402 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);