]> git.saurik.com Git - wxWidgets.git/blob - src/common/string.cpp
Updated makefiles for VisualAge C++ V3.0
[wxWidgets.git] / src / common / string.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: string.cpp
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "string.h"
14 #endif
15
16 /*
17 * About ref counting:
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
21 */
22
23 // ===========================================================================
24 // headers, declarations, constants
25 // ===========================================================================
26
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
29
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif
33
34 #ifndef WX_PRECOMP
35 #include "wx/defs.h"
36 #include "wx/string.h"
37 #include "wx/intl.h"
38 #if wxUSE_THREADS
39 #include <wx/thread.h>
40 #endif
41 #endif
42
43 #include <ctype.h>
44 #include <string.h>
45 #include <stdlib.h>
46
47 #ifdef __SALFORDC__
48 #include <clib.h>
49 #endif
50
51 #if wxUSE_WCSRTOMBS
52 #include <wchar.h> // for wcsrtombs(), see comments where it's used
53 #endif // GNU
54
55 #ifdef WXSTRING_IS_WXOBJECT
56 IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject)
57 #endif //WXSTRING_IS_WXOBJECT
58
59 #if wxUSE_UNICODE
60 #undef wxUSE_EXPERIMENTAL_PRINTF
61 #define wxUSE_EXPERIMENTAL_PRINTF 1
62 #endif
63
64 // allocating extra space for each string consumes more memory but speeds up
65 // the concatenation operations (nLen is the current string's length)
66 // NB: EXTRA_ALLOC must be >= 0!
67 #define EXTRA_ALLOC (19 - nLen % 16)
68
69 // ---------------------------------------------------------------------------
70 // static class variables definition
71 // ---------------------------------------------------------------------------
72
73 #ifdef wxSTD_STRING_COMPATIBILITY
74 const size_t wxString::npos = wxSTRING_MAXLEN;
75 #endif // wxSTD_STRING_COMPATIBILITY
76
77 // ----------------------------------------------------------------------------
78 // static data
79 // ----------------------------------------------------------------------------
80
81 // for an empty string, GetStringData() will return this address: this
82 // structure has the same layout as wxStringData and it's data() method will
83 // return the empty string (dummy pointer)
84 static const struct
85 {
86 wxStringData data;
87 wxChar dummy;
88 } g_strEmpty = { {-1, 0, 0}, _T('\0') };
89
90 // empty C style string: points to 'string data' byte of g_strEmpty
91 extern const wxChar WXDLLEXPORT *g_szNul = &g_strEmpty.dummy;
92
93 // ----------------------------------------------------------------------------
94 // conditional compilation
95 // ----------------------------------------------------------------------------
96
97 #if !defined(__WXSW__) && wxUSE_UNICODE
98 #ifdef wxUSE_EXPERIMENTAL_PRINTF
99 #undef wxUSE_EXPERIMENTAL_PRINTF
100 #endif
101 #define wxUSE_EXPERIMENTAL_PRINTF 1
102 #endif
103
104 // we want to find out if the current platform supports vsnprintf()-like
105 // function: for Unix this is done with configure, for Windows we test the
106 // compiler explicitly.
107 #ifdef __WXMSW__
108 #ifdef __VISUALC__
109 #define wxVsnprintf _vsnprintf
110 #endif
111 #else // !Windows
112 #ifdef HAVE_VSNPRINTF
113 #define wxVsnprintf vsnprintf
114 #endif
115 #endif // Windows/!Windows
116
117 #ifndef wxVsnprintf
118 // in this case we'll use vsprintf() (which is ANSI and thus should be
119 // always available), but it's unsafe because it doesn't check for buffer
120 // size - so give a warning
121 #define wxVsnprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
122
123 #if defined(__VISUALC__)
124 #pragma message("Using sprintf() because no snprintf()-like function defined")
125 #elif defined(__GNUG__) && !defined(__UNIX__)
126 #warning "Using sprintf() because no snprintf()-like function defined"
127 #elif defined(__MWERKS__)
128 #warning "Using sprintf() because no snprintf()-like function defined"
129 #endif //compiler
130 #endif // no vsnprintf
131
132 #ifdef _AIX
133 // AIX has vsnprintf, but there's no prototype in the system headers.
134 extern "C" int vsnprintf(char* str, size_t n, const char* format, va_list ap);
135 #endif
136
137 // ----------------------------------------------------------------------------
138 // global functions
139 // ----------------------------------------------------------------------------
140
141 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
142
143 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
144 // iostream ones.
145 //
146 // ATTN: you can _not_ use both of these in the same program!
147
148 istream& operator>>(istream& is, wxString& WXUNUSED(str))
149 {
150 #if 0
151 int w = is.width(0);
152 if ( is.ipfx(0) ) {
153 streambuf *sb = is.rdbuf();
154 str.erase();
155 while ( true ) {
156 int ch = sb->sbumpc ();
157 if ( ch == EOF ) {
158 is.setstate(ios::eofbit);
159 break;
160 }
161 else if ( isspace(ch) ) {
162 sb->sungetc();
163 break;
164 }
165
166 str += ch;
167 if ( --w == 1 )
168 break;
169 }
170 }
171
172 is.isfx();
173 if ( str.length() == 0 )
174 is.setstate(ios::failbit);
175 #endif
176 return is;
177 }
178
179 #endif //std::string compatibility
180
181 // ----------------------------------------------------------------------------
182 // private classes
183 // ----------------------------------------------------------------------------
184
185 // this small class is used to gather statistics for performance tuning
186 //#define WXSTRING_STATISTICS
187 #ifdef WXSTRING_STATISTICS
188 class Averager
189 {
190 public:
191 Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
192 ~Averager()
193 { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
194
195 void Add(size_t n) { m_nTotal += n; m_nCount++; }
196
197 private:
198 size_t m_nCount, m_nTotal;
199 const char *m_sz;
200 } g_averageLength("allocation size"),
201 g_averageSummandLength("summand length"),
202 g_averageConcatHit("hit probability in concat"),
203 g_averageInitialLength("initial string length");
204
205 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
206 #else
207 #define STATISTICS_ADD(av, val)
208 #endif // WXSTRING_STATISTICS
209
210 // ===========================================================================
211 // wxString class core
212 // ===========================================================================
213
214 // ---------------------------------------------------------------------------
215 // construction
216 // ---------------------------------------------------------------------------
217
218 // constructs string of <nLength> copies of character <ch>
219 wxString::wxString(wxChar ch, size_t nLength)
220 {
221 Init();
222
223 if ( nLength > 0 ) {
224 AllocBuffer(nLength);
225
226 #if wxUSE_UNICODE
227 // memset only works on char
228 for (size_t n=0; n<nLength; n++) m_pchData[n] = ch;
229 #else
230 memset(m_pchData, ch, nLength);
231 #endif
232 }
233 }
234
235 // takes nLength elements of psz starting at nPos
236 void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
237 {
238 Init();
239
240 wxASSERT( nPos <= wxStrlen(psz) );
241
242 if ( nLength == wxSTRING_MAXLEN )
243 nLength = wxStrlen(psz + nPos);
244
245 STATISTICS_ADD(InitialLength, nLength);
246
247 if ( nLength > 0 ) {
248 // trailing '\0' is written in AllocBuffer()
249 AllocBuffer(nLength);
250 memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar));
251 }
252 }
253
254 #ifdef wxSTD_STRING_COMPATIBILITY
255
256 // poor man's iterators are "void *" pointers
257 wxString::wxString(const void *pStart, const void *pEnd)
258 {
259 InitWith((const wxChar *)pStart, 0,
260 (const wxChar *)pEnd - (const wxChar *)pStart);
261 }
262
263 #endif //std::string compatibility
264
265 #if wxUSE_UNICODE
266
267 // from multibyte string
268 wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
269 {
270 // first get necessary size
271 size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
272
273 // nLength is number of *Unicode* characters here!
274 if ((nLen != (size_t)-1) && (nLen > nLength))
275 nLen = nLength;
276
277 // empty?
278 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
279 AllocBuffer(nLen);
280 conv.MB2WC(m_pchData, psz, nLen);
281 }
282 else {
283 Init();
284 }
285 }
286
287 #else
288
289 #if wxUSE_WCHAR_T
290 // from wide string
291 wxString::wxString(const wchar_t *pwz)
292 {
293 // first get necessary size
294 size_t nLen = pwz ? wxWC2MB((char *) NULL, pwz, 0) : 0;
295
296 // empty?
297 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
298 AllocBuffer(nLen);
299 wxWC2MB(m_pchData, pwz, nLen);
300 }
301 else {
302 Init();
303 }
304 }
305 #endif
306
307 #endif
308
309 // ---------------------------------------------------------------------------
310 // memory allocation
311 // ---------------------------------------------------------------------------
312
313 // allocates memory needed to store a C string of length nLen
314 void wxString::AllocBuffer(size_t nLen)
315 {
316 wxASSERT( nLen > 0 ); //
317 wxASSERT( nLen <= INT_MAX-1 ); // max size (enough room for 1 extra)
318
319 STATISTICS_ADD(Length, nLen);
320
321 // allocate memory:
322 // 1) one extra character for '\0' termination
323 // 2) sizeof(wxStringData) for housekeeping info
324 wxStringData* pData = (wxStringData*)
325 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
326 pData->nRefs = 1;
327 pData->nDataLength = nLen;
328 pData->nAllocLength = nLen + EXTRA_ALLOC;
329 m_pchData = pData->data(); // data starts after wxStringData
330 m_pchData[nLen] = _T('\0');
331 }
332
333 // must be called before changing this string
334 void wxString::CopyBeforeWrite()
335 {
336 wxStringData* pData = GetStringData();
337
338 if ( pData->IsShared() ) {
339 pData->Unlock(); // memory not freed because shared
340 size_t nLen = pData->nDataLength;
341 AllocBuffer(nLen);
342 memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar));
343 }
344
345 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
346 }
347
348 // must be called before replacing contents of this string
349 void wxString::AllocBeforeWrite(size_t nLen)
350 {
351 wxASSERT( nLen != 0 ); // doesn't make any sense
352
353 // must not share string and must have enough space
354 wxStringData* pData = GetStringData();
355 if ( pData->IsShared() || (nLen > pData->nAllocLength) ) {
356 // can't work with old buffer, get new one
357 pData->Unlock();
358 AllocBuffer(nLen);
359 }
360 else {
361 // update the string length
362 pData->nDataLength = nLen;
363 }
364
365 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
366 }
367
368 // allocate enough memory for nLen characters
369 void wxString::Alloc(size_t nLen)
370 {
371 wxStringData *pData = GetStringData();
372 if ( pData->nAllocLength <= nLen ) {
373 if ( pData->IsEmpty() ) {
374 nLen += EXTRA_ALLOC;
375
376 wxStringData* pData = (wxStringData*)
377 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
378 pData->nRefs = 1;
379 pData->nDataLength = 0;
380 pData->nAllocLength = nLen;
381 m_pchData = pData->data(); // data starts after wxStringData
382 m_pchData[0u] = _T('\0');
383 }
384 else if ( pData->IsShared() ) {
385 pData->Unlock(); // memory not freed because shared
386 size_t nOldLen = pData->nDataLength;
387 AllocBuffer(nLen);
388 memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar));
389 }
390 else {
391 nLen += EXTRA_ALLOC;
392
393 wxStringData *p = (wxStringData *)
394 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
395
396 if ( p == NULL ) {
397 // @@@ what to do on memory error?
398 return;
399 }
400
401 // it's not important if the pointer changed or not (the check for this
402 // is not faster than assigning to m_pchData in all cases)
403 p->nAllocLength = nLen;
404 m_pchData = p->data();
405 }
406 }
407 //else: we've already got enough
408 }
409
410 // shrink to minimal size (releasing extra memory)
411 void wxString::Shrink()
412 {
413 wxStringData *pData = GetStringData();
414
415 // this variable is unused in release build, so avoid the compiler warning by
416 // just not declaring it
417 #ifdef __WXDEBUG__
418 void *p =
419 #endif
420 realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(wxChar));
421
422 wxASSERT( p != NULL ); // can't free memory?
423 wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move!
424 }
425
426 // get the pointer to writable buffer of (at least) nLen bytes
427 wxChar *wxString::GetWriteBuf(size_t nLen)
428 {
429 AllocBeforeWrite(nLen);
430
431 wxASSERT( GetStringData()->nRefs == 1 );
432 GetStringData()->Validate(FALSE);
433
434 return m_pchData;
435 }
436
437 // put string back in a reasonable state after GetWriteBuf
438 void wxString::UngetWriteBuf()
439 {
440 GetStringData()->nDataLength = wxStrlen(m_pchData);
441 GetStringData()->Validate(TRUE);
442 }
443
444 // ---------------------------------------------------------------------------
445 // data access
446 // ---------------------------------------------------------------------------
447
448 // all functions are inline in string.h
449
450 // ---------------------------------------------------------------------------
451 // assignment operators
452 // ---------------------------------------------------------------------------
453
454 // helper function: does real copy
455 void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
456 {
457 if ( nSrcLen == 0 ) {
458 Reinit();
459 }
460 else {
461 AllocBeforeWrite(nSrcLen);
462 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
463 GetStringData()->nDataLength = nSrcLen;
464 m_pchData[nSrcLen] = _T('\0');
465 }
466 }
467
468 // assigns one string to another
469 wxString& wxString::operator=(const wxString& stringSrc)
470 {
471 wxASSERT( stringSrc.GetStringData()->IsValid() );
472
473 // don't copy string over itself
474 if ( m_pchData != stringSrc.m_pchData ) {
475 if ( stringSrc.GetStringData()->IsEmpty() ) {
476 Reinit();
477 }
478 else {
479 // adjust references
480 GetStringData()->Unlock();
481 m_pchData = stringSrc.m_pchData;
482 GetStringData()->Lock();
483 }
484 }
485
486 return *this;
487 }
488
489 // assigns a single character
490 wxString& wxString::operator=(wxChar ch)
491 {
492 AssignCopy(1, &ch);
493 return *this;
494 }
495
496 // assigns C string
497 wxString& wxString::operator=(const wxChar *psz)
498 {
499 AssignCopy(wxStrlen(psz), psz);
500 return *this;
501 }
502
503 #if !wxUSE_UNICODE
504
505 // same as 'signed char' variant
506 wxString& wxString::operator=(const unsigned char* psz)
507 {
508 *this = (const char *)psz;
509 return *this;
510 }
511
512 #if wxUSE_WCHAR_T
513 wxString& wxString::operator=(const wchar_t *pwz)
514 {
515 wxString str(pwz);
516 *this = str;
517 return *this;
518 }
519 #endif
520
521 #endif
522
523 // ---------------------------------------------------------------------------
524 // string concatenation
525 // ---------------------------------------------------------------------------
526
527 // add something to this string
528 void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData)
529 {
530 STATISTICS_ADD(SummandLength, nSrcLen);
531
532 // concatenating an empty string is a NOP
533 if ( nSrcLen > 0 ) {
534 wxStringData *pData = GetStringData();
535 size_t nLen = pData->nDataLength;
536 size_t nNewLen = nLen + nSrcLen;
537
538 // alloc new buffer if current is too small
539 if ( pData->IsShared() ) {
540 STATISTICS_ADD(ConcatHit, 0);
541
542 // we have to allocate another buffer
543 wxStringData* pOldData = GetStringData();
544 AllocBuffer(nNewLen);
545 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
546 pOldData->Unlock();
547 }
548 else if ( nNewLen > pData->nAllocLength ) {
549 STATISTICS_ADD(ConcatHit, 0);
550
551 // we have to grow the buffer
552 Alloc(nNewLen);
553 }
554 else {
555 STATISTICS_ADD(ConcatHit, 1);
556
557 // the buffer is already big enough
558 }
559
560 // should be enough space
561 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
562
563 // fast concatenation - all is done in our buffer
564 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
565
566 m_pchData[nNewLen] = _T('\0'); // put terminating '\0'
567 GetStringData()->nDataLength = nNewLen; // and fix the length
568 }
569 //else: the string to append was empty
570 }
571
572 /*
573 * concatenation functions come in 5 flavours:
574 * string + string
575 * char + string and string + char
576 * C str + string and string + C str
577 */
578
579 wxString operator+(const wxString& string1, const wxString& string2)
580 {
581 wxASSERT( string1.GetStringData()->IsValid() );
582 wxASSERT( string2.GetStringData()->IsValid() );
583
584 wxString s = string1;
585 s += string2;
586
587 return s;
588 }
589
590 wxString operator+(const wxString& string, wxChar ch)
591 {
592 wxASSERT( string.GetStringData()->IsValid() );
593
594 wxString s = string;
595 s += ch;
596
597 return s;
598 }
599
600 wxString operator+(wxChar ch, const wxString& string)
601 {
602 wxASSERT( string.GetStringData()->IsValid() );
603
604 wxString s = ch;
605 s += string;
606
607 return s;
608 }
609
610 wxString operator+(const wxString& string, const wxChar *psz)
611 {
612 wxASSERT( string.GetStringData()->IsValid() );
613
614 wxString s;
615 s.Alloc(wxStrlen(psz) + string.Len());
616 s = string;
617 s += psz;
618
619 return s;
620 }
621
622 wxString operator+(const wxChar *psz, const wxString& string)
623 {
624 wxASSERT( string.GetStringData()->IsValid() );
625
626 wxString s;
627 s.Alloc(wxStrlen(psz) + string.Len());
628 s = psz;
629 s += string;
630
631 return s;
632 }
633
634 // ===========================================================================
635 // other common string functions
636 // ===========================================================================
637
638 // ---------------------------------------------------------------------------
639 // simple sub-string extraction
640 // ---------------------------------------------------------------------------
641
642 // helper function: clone the data attached to this string
643 void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
644 {
645 if ( nCopyLen == 0 ) {
646 dest.Init();
647 }
648 else {
649 dest.AllocBuffer(nCopyLen);
650 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
651 }
652 }
653
654 // extract string of length nCount starting at nFirst
655 wxString wxString::Mid(size_t nFirst, size_t nCount) const
656 {
657 wxStringData *pData = GetStringData();
658 size_t nLen = pData->nDataLength;
659
660 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
661 if ( nCount == wxSTRING_MAXLEN )
662 {
663 nCount = nLen - nFirst;
664 }
665
666 // out-of-bounds requests return sensible things
667 if ( nFirst + nCount > nLen )
668 {
669 nCount = nLen - nFirst;
670 }
671
672 if ( nFirst > nLen )
673 {
674 // AllocCopy() will return empty string
675 nCount = 0;
676 }
677
678 wxString dest;
679 AllocCopy(dest, nCount, nFirst);
680
681 return dest;
682 }
683
684 // extract nCount last (rightmost) characters
685 wxString wxString::Right(size_t nCount) const
686 {
687 if ( nCount > (size_t)GetStringData()->nDataLength )
688 nCount = GetStringData()->nDataLength;
689
690 wxString dest;
691 AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount);
692 return dest;
693 }
694
695 // get all characters after the last occurence of ch
696 // (returns the whole string if ch not found)
697 wxString wxString::AfterLast(wxChar ch) const
698 {
699 wxString str;
700 int iPos = Find(ch, TRUE);
701 if ( iPos == wxNOT_FOUND )
702 str = *this;
703 else
704 str = c_str() + iPos + 1;
705
706 return str;
707 }
708
709 // extract nCount first (leftmost) characters
710 wxString wxString::Left(size_t nCount) const
711 {
712 if ( nCount > (size_t)GetStringData()->nDataLength )
713 nCount = GetStringData()->nDataLength;
714
715 wxString dest;
716 AllocCopy(dest, nCount, 0);
717 return dest;
718 }
719
720 // get all characters before the first occurence of ch
721 // (returns the whole string if ch not found)
722 wxString wxString::BeforeFirst(wxChar ch) const
723 {
724 wxString str;
725 for ( const wxChar *pc = m_pchData; *pc != _T('\0') && *pc != ch; pc++ )
726 str += *pc;
727
728 return str;
729 }
730
731 /// get all characters before the last occurence of ch
732 /// (returns empty string if ch not found)
733 wxString wxString::BeforeLast(wxChar ch) const
734 {
735 wxString str;
736 int iPos = Find(ch, TRUE);
737 if ( iPos != wxNOT_FOUND && iPos != 0 )
738 str = wxString(c_str(), iPos);
739
740 return str;
741 }
742
743 /// get all characters after the first occurence of ch
744 /// (returns empty string if ch not found)
745 wxString wxString::AfterFirst(wxChar ch) const
746 {
747 wxString str;
748 int iPos = Find(ch);
749 if ( iPos != wxNOT_FOUND )
750 str = c_str() + iPos + 1;
751
752 return str;
753 }
754
755 // replace first (or all) occurences of some substring with another one
756 size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
757 {
758 size_t uiCount = 0; // count of replacements made
759
760 size_t uiOldLen = wxStrlen(szOld);
761
762 wxString strTemp;
763 const wxChar *pCurrent = m_pchData;
764 const wxChar *pSubstr;
765 while ( *pCurrent != _T('\0') ) {
766 pSubstr = wxStrstr(pCurrent, szOld);
767 if ( pSubstr == NULL ) {
768 // strTemp is unused if no replacements were made, so avoid the copy
769 if ( uiCount == 0 )
770 return 0;
771
772 strTemp += pCurrent; // copy the rest
773 break; // exit the loop
774 }
775 else {
776 // take chars before match
777 strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent);
778 strTemp += szNew;
779 pCurrent = pSubstr + uiOldLen; // restart after match
780
781 uiCount++;
782
783 // stop now?
784 if ( !bReplaceAll ) {
785 strTemp += pCurrent; // copy the rest
786 break; // exit the loop
787 }
788 }
789 }
790
791 // only done if there were replacements, otherwise would have returned above
792 *this = strTemp;
793
794 return uiCount;
795 }
796
797 bool wxString::IsAscii() const
798 {
799 const wxChar *s = (const wxChar*) *this;
800 while(*s){
801 if(!isascii(*s)) return(FALSE);
802 s++;
803 }
804 return(TRUE);
805 }
806
807 bool wxString::IsWord() const
808 {
809 const wxChar *s = (const wxChar*) *this;
810 while(*s){
811 if(!wxIsalpha(*s)) return(FALSE);
812 s++;
813 }
814 return(TRUE);
815 }
816
817 bool wxString::IsNumber() const
818 {
819 const wxChar *s = (const wxChar*) *this;
820 while(*s){
821 if(!wxIsdigit(*s)) return(FALSE);
822 s++;
823 }
824 return(TRUE);
825 }
826
827 wxString wxString::Strip(stripType w) const
828 {
829 wxString s = *this;
830 if ( w & leading ) s.Trim(FALSE);
831 if ( w & trailing ) s.Trim(TRUE);
832 return s;
833 }
834
835 // ---------------------------------------------------------------------------
836 // case conversion
837 // ---------------------------------------------------------------------------
838
839 wxString& wxString::MakeUpper()
840 {
841 CopyBeforeWrite();
842
843 for ( wxChar *p = m_pchData; *p; p++ )
844 *p = (wxChar)wxToupper(*p);
845
846 return *this;
847 }
848
849 wxString& wxString::MakeLower()
850 {
851 CopyBeforeWrite();
852
853 for ( wxChar *p = m_pchData; *p; p++ )
854 *p = (wxChar)wxTolower(*p);
855
856 return *this;
857 }
858
859 // ---------------------------------------------------------------------------
860 // trimming and padding
861 // ---------------------------------------------------------------------------
862
863 // trims spaces (in the sense of isspace) from left or right side
864 wxString& wxString::Trim(bool bFromRight)
865 {
866 // first check if we're going to modify the string at all
867 if ( !IsEmpty() &&
868 (
869 (bFromRight && wxIsspace(GetChar(Len() - 1))) ||
870 (!bFromRight && wxIsspace(GetChar(0u)))
871 )
872 )
873 {
874 // ok, there is at least one space to trim
875 CopyBeforeWrite();
876
877 if ( bFromRight )
878 {
879 // find last non-space character
880 wxChar *psz = m_pchData + GetStringData()->nDataLength - 1;
881 while ( wxIsspace(*psz) && (psz >= m_pchData) )
882 psz--;
883
884 // truncate at trailing space start
885 *++psz = _T('\0');
886 GetStringData()->nDataLength = psz - m_pchData;
887 }
888 else
889 {
890 // find first non-space character
891 const wxChar *psz = m_pchData;
892 while ( wxIsspace(*psz) )
893 psz++;
894
895 // fix up data and length
896 int nDataLength = GetStringData()->nDataLength - (psz - (const wxChar*) m_pchData);
897 memmove(m_pchData, psz, (nDataLength + 1)*sizeof(wxChar));
898 GetStringData()->nDataLength = nDataLength;
899 }
900 }
901
902 return *this;
903 }
904
905 // adds nCount characters chPad to the string from either side
906 wxString& wxString::Pad(size_t nCount, wxChar chPad, bool bFromRight)
907 {
908 wxString s(chPad, nCount);
909
910 if ( bFromRight )
911 *this += s;
912 else
913 {
914 s += *this;
915 *this = s;
916 }
917
918 return *this;
919 }
920
921 // truncate the string
922 wxString& wxString::Truncate(size_t uiLen)
923 {
924 if ( uiLen < Len() ) {
925 CopyBeforeWrite();
926
927 *(m_pchData + uiLen) = _T('\0');
928 GetStringData()->nDataLength = uiLen;
929 }
930 //else: nothing to do, string is already short enough
931
932 return *this;
933 }
934
935 // ---------------------------------------------------------------------------
936 // finding (return wxNOT_FOUND if not found and index otherwise)
937 // ---------------------------------------------------------------------------
938
939 // find a character
940 int wxString::Find(wxChar ch, bool bFromEnd) const
941 {
942 const wxChar *psz = bFromEnd ? wxStrrchr(m_pchData, ch) : wxStrchr(m_pchData, ch);
943
944 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
945 }
946
947 // find a sub-string (like strstr)
948 int wxString::Find(const wxChar *pszSub) const
949 {
950 const wxChar *psz = wxStrstr(m_pchData, pszSub);
951
952 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
953 }
954
955 // ---------------------------------------------------------------------------
956 // stream-like operators
957 // ---------------------------------------------------------------------------
958 wxString& wxString::operator<<(int i)
959 {
960 wxString res;
961 res.Printf(_T("%d"), i);
962
963 return (*this) << res;
964 }
965
966 wxString& wxString::operator<<(float f)
967 {
968 wxString res;
969 res.Printf(_T("%f"), f);
970
971 return (*this) << res;
972 }
973
974 wxString& wxString::operator<<(double d)
975 {
976 wxString res;
977 res.Printf(_T("%g"), d);
978
979 return (*this) << res;
980 }
981
982 // ---------------------------------------------------------------------------
983 // formatted output
984 // ---------------------------------------------------------------------------
985 int wxString::Printf(const wxChar *pszFormat, ...)
986 {
987 va_list argptr;
988 va_start(argptr, pszFormat);
989
990 int iLen = PrintfV(pszFormat, argptr);
991
992 va_end(argptr);
993
994 return iLen;
995 }
996
997 int wxString::PrintfV(const wxChar* pszFormat, va_list argptr)
998 {
999 // static buffer to avoid dynamic memory allocation each time
1000 char s_szScratch[1024]; // using static buffer causes internal compiler err
1001 #if 0
1002 #if wxUSE_THREADS
1003 // protect the static buffer
1004 static wxCriticalSection critsect;
1005 wxCriticalSectionLocker lock(critsect);
1006 #endif
1007 #endif
1008
1009 #if wxUSE_EXPERIMENTAL_PRINTF
1010 // the new implementation
1011
1012 Reinit();
1013 for (size_t n = 0; pszFormat[n]; n++)
1014 if (pszFormat[n] == _T('%')) {
1015 static char s_szFlags[256] = "%";
1016 size_t flagofs = 1;
1017 bool adj_left = FALSE, in_prec = FALSE,
1018 prec_dot = FALSE, done = FALSE;
1019 int ilen = 0;
1020 size_t min_width = 0, max_width = wxSTRING_MAXLEN;
1021 do {
1022 #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1023 switch (pszFormat[++n]) {
1024 case _T('\0'):
1025 done = TRUE;
1026 break;
1027 case _T('%'):
1028 *this += _T('%');
1029 done = TRUE;
1030 break;
1031 case _T('#'):
1032 case _T('0'):
1033 case _T(' '):
1034 case _T('+'):
1035 case _T('\''):
1036 CHECK_PREC
1037 s_szFlags[flagofs++] = pszFormat[n];
1038 break;
1039 case _T('-'):
1040 CHECK_PREC
1041 adj_left = TRUE;
1042 s_szFlags[flagofs++] = pszFormat[n];
1043 break;
1044 case _T('.'):
1045 CHECK_PREC
1046 in_prec = TRUE;
1047 prec_dot = FALSE;
1048 max_width = 0;
1049 // dot will be auto-added to s_szFlags if non-negative number follows
1050 break;
1051 case _T('h'):
1052 ilen = -1;
1053 CHECK_PREC
1054 s_szFlags[flagofs++] = pszFormat[n];
1055 break;
1056 case _T('l'):
1057 ilen = 1;
1058 CHECK_PREC
1059 s_szFlags[flagofs++] = pszFormat[n];
1060 break;
1061 case _T('q'):
1062 case _T('L'):
1063 ilen = 2;
1064 CHECK_PREC
1065 s_szFlags[flagofs++] = pszFormat[n];
1066 break;
1067 case _T('Z'):
1068 ilen = 3;
1069 CHECK_PREC
1070 s_szFlags[flagofs++] = pszFormat[n];
1071 break;
1072 case _T('*'):
1073 {
1074 int len = va_arg(argptr, int);
1075 if (in_prec) {
1076 if (len<0) break;
1077 CHECK_PREC
1078 max_width = len;
1079 } else {
1080 if (len<0) {
1081 adj_left = !adj_left;
1082 s_szFlags[flagofs++] = '-';
1083 len = -len;
1084 }
1085 min_width = len;
1086 }
1087 flagofs += ::sprintf(s_szFlags+flagofs,"%d",len);
1088 }
1089 break;
1090 case _T('1'): case _T('2'): case _T('3'):
1091 case _T('4'): case _T('5'): case _T('6'):
1092 case _T('7'): case _T('8'): case _T('9'):
1093 {
1094 int len = 0;
1095 CHECK_PREC
1096 while ((pszFormat[n]>=_T('0')) && (pszFormat[n]<=_T('9'))) {
1097 s_szFlags[flagofs++] = pszFormat[n];
1098 len = len*10 + (pszFormat[n] - _T('0'));
1099 n++;
1100 }
1101 if (in_prec) max_width = len;
1102 else min_width = len;
1103 n--; // the main loop pre-increments n again
1104 }
1105 break;
1106 case _T('d'):
1107 case _T('i'):
1108 case _T('o'):
1109 case _T('u'):
1110 case _T('x'):
1111 case _T('X'):
1112 CHECK_PREC
1113 s_szFlags[flagofs++] = pszFormat[n];
1114 s_szFlags[flagofs] = '\0';
1115 if (ilen == 0 ) {
1116 int val = va_arg(argptr, int);
1117 ::sprintf(s_szScratch, s_szFlags, val);
1118 }
1119 else if (ilen == -1) {
1120 short int val = va_arg(argptr, short int);
1121 ::sprintf(s_szScratch, s_szFlags, val);
1122 }
1123 else if (ilen == 1) {
1124 long int val = va_arg(argptr, long int);
1125 ::sprintf(s_szScratch, s_szFlags, val);
1126 }
1127 else if (ilen == 2) {
1128 #if SIZEOF_LONG_LONG
1129 long long int val = va_arg(argptr, long long int);
1130 ::sprintf(s_szScratch, s_szFlags, val);
1131 #else
1132 long int val = va_arg(argptr, long int);
1133 ::sprintf(s_szScratch, s_szFlags, val);
1134 #endif
1135 }
1136 else if (ilen == 3) {
1137 size_t val = va_arg(argptr, size_t);
1138 ::sprintf(s_szScratch, s_szFlags, val);
1139 }
1140 *this += wxString(s_szScratch);
1141 done = TRUE;
1142 break;
1143 case _T('e'):
1144 case _T('E'):
1145 case _T('f'):
1146 case _T('g'):
1147 case _T('G'):
1148 CHECK_PREC
1149 s_szFlags[flagofs++] = pszFormat[n];
1150 s_szFlags[flagofs] = '\0';
1151 if (ilen == 2) {
1152 long double val = va_arg(argptr, long double);
1153 ::sprintf(s_szScratch, s_szFlags, val);
1154 } else {
1155 double val = va_arg(argptr, double);
1156 ::sprintf(s_szScratch, s_szFlags, val);
1157 }
1158 *this += wxString(s_szScratch);
1159 done = TRUE;
1160 break;
1161 case _T('p'):
1162 {
1163 void *val = va_arg(argptr, void *);
1164 CHECK_PREC
1165 s_szFlags[flagofs++] = pszFormat[n];
1166 s_szFlags[flagofs] = '\0';
1167 ::sprintf(s_szScratch, s_szFlags, val);
1168 *this += wxString(s_szScratch);
1169 done = TRUE;
1170 }
1171 break;
1172 case _T('c'):
1173 {
1174 wxChar val = va_arg(argptr, int);
1175 // we don't need to honor padding here, do we?
1176 *this += val;
1177 done = TRUE;
1178 }
1179 break;
1180 case _T('s'):
1181 if (ilen == -1) {
1182 // wx extension: we'll let %hs mean non-Unicode strings
1183 char *val = va_arg(argptr, char *);
1184 #if wxUSE_UNICODE
1185 // ASCII->Unicode constructor handles max_width right
1186 wxString s(val, wxConvLibc, max_width);
1187 #else
1188 size_t len = wxSTRING_MAXLEN;
1189 if (val) {
1190 for (len = 0; val[len] && (len<max_width); len++);
1191 } else val = _T("(null)");
1192 wxString s(val, len);
1193 #endif
1194 if (s.Len() < min_width)
1195 s.Pad(min_width - s.Len(), _T(' '), adj_left);
1196 *this += s;
1197 } else {
1198 wxChar *val = va_arg(argptr, wxChar *);
1199 size_t len = wxSTRING_MAXLEN;
1200 if (val) {
1201 for (len = 0; val[len] && (len<max_width); len++);
1202 } else val = _T("(null)");
1203 wxString s(val, len);
1204 if (s.Len() < min_width)
1205 s.Pad(min_width - s.Len(), _T(' '), adj_left);
1206 *this += s;
1207 }
1208 done = TRUE;
1209 break;
1210 case _T('n'):
1211 if (ilen == 0) {
1212 int *val = va_arg(argptr, int *);
1213 *val = Len();
1214 }
1215 else if (ilen == -1) {
1216 short int *val = va_arg(argptr, short int *);
1217 *val = Len();
1218 }
1219 else if (ilen >= 1) {
1220 long int *val = va_arg(argptr, long int *);
1221 *val = Len();
1222 }
1223 done = TRUE;
1224 break;
1225 default:
1226 if (wxIsalpha(pszFormat[n]))
1227 // probably some flag not taken care of here yet
1228 s_szFlags[flagofs++] = pszFormat[n];
1229 else {
1230 // bad format
1231 *this += _T('%'); // just to pass the glibc tst-printf.c
1232 n--;
1233 done = TRUE;
1234 }
1235 break;
1236 }
1237 #undef CHECK_PREC
1238 } while (!done);
1239 } else *this += pszFormat[n];
1240
1241 #else
1242 // NB: wxVsnprintf() may return either less than the buffer size or -1 if there
1243 // is not enough place depending on implementation
1244 int iLen = wxVsnprintf(s_szScratch, WXSIZEOF(s_szScratch), pszFormat, argptr);
1245 char *buffer;
1246 if ( iLen < (int)WXSIZEOF(s_szScratch) ) {
1247 buffer = s_szScratch;
1248 }
1249 else {
1250 int size = WXSIZEOF(s_szScratch) * 2;
1251 buffer = (char *)malloc(size);
1252 while ( buffer != NULL ) {
1253 iLen = wxVsnprintf(buffer, WXSIZEOF(s_szScratch), pszFormat, argptr);
1254 if ( iLen < size ) {
1255 // ok, there was enough space
1256 break;
1257 }
1258
1259 // still not enough, double it again
1260 buffer = (char *)realloc(buffer, size *= 2);
1261 }
1262
1263 if ( !buffer ) {
1264 // out of memory
1265 return -1;
1266 }
1267 }
1268
1269 wxString s(buffer);
1270 *this = s;
1271
1272 if ( buffer != s_szScratch )
1273 free(buffer);
1274 #endif
1275
1276 return Len();
1277 }
1278
1279 // ----------------------------------------------------------------------------
1280 // misc other operations
1281 // ----------------------------------------------------------------------------
1282 bool wxString::Matches(const wxChar *pszMask) const
1283 {
1284 // check char by char
1285 const wxChar *pszTxt;
1286 for ( pszTxt = c_str(); *pszMask != _T('\0'); pszMask++, pszTxt++ ) {
1287 switch ( *pszMask ) {
1288 case _T('?'):
1289 if ( *pszTxt == _T('\0') )
1290 return FALSE;
1291
1292 pszTxt++;
1293 pszMask++;
1294 break;
1295
1296 case _T('*'):
1297 {
1298 // ignore special chars immediately following this one
1299 while ( *pszMask == _T('*') || *pszMask == _T('?') )
1300 pszMask++;
1301
1302 // if there is nothing more, match
1303 if ( *pszMask == _T('\0') )
1304 return TRUE;
1305
1306 // are there any other metacharacters in the mask?
1307 size_t uiLenMask;
1308 const wxChar *pEndMask = wxStrpbrk(pszMask, _T("*?"));
1309
1310 if ( pEndMask != NULL ) {
1311 // we have to match the string between two metachars
1312 uiLenMask = pEndMask - pszMask;
1313 }
1314 else {
1315 // we have to match the remainder of the string
1316 uiLenMask = wxStrlen(pszMask);
1317 }
1318
1319 wxString strToMatch(pszMask, uiLenMask);
1320 const wxChar* pMatch = wxStrstr(pszTxt, strToMatch);
1321 if ( pMatch == NULL )
1322 return FALSE;
1323
1324 // -1 to compensate "++" in the loop
1325 pszTxt = pMatch + uiLenMask - 1;
1326 pszMask += uiLenMask - 1;
1327 }
1328 break;
1329
1330 default:
1331 if ( *pszMask != *pszTxt )
1332 return FALSE;
1333 break;
1334 }
1335 }
1336
1337 // match only if nothing left
1338 return *pszTxt == _T('\0');
1339 }
1340
1341 // Count the number of chars
1342 int wxString::Freq(wxChar ch) const
1343 {
1344 int count = 0;
1345 int len = Len();
1346 for (int i = 0; i < len; i++)
1347 {
1348 if (GetChar(i) == ch)
1349 count ++;
1350 }
1351 return count;
1352 }
1353
1354 // convert to upper case, return the copy of the string
1355 wxString wxString::Upper() const
1356 { wxString s(*this); return s.MakeUpper(); }
1357
1358 // convert to lower case, return the copy of the string
1359 wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); }
1360
1361 int wxString::sprintf(const wxChar *pszFormat, ...)
1362 {
1363 va_list argptr;
1364 va_start(argptr, pszFormat);
1365 int iLen = PrintfV(pszFormat, argptr);
1366 va_end(argptr);
1367 return iLen;
1368 }
1369
1370 // ---------------------------------------------------------------------------
1371 // standard C++ library string functions
1372 // ---------------------------------------------------------------------------
1373 #ifdef wxSTD_STRING_COMPATIBILITY
1374
1375 wxString& wxString::insert(size_t nPos, const wxString& str)
1376 {
1377 wxASSERT( str.GetStringData()->IsValid() );
1378 wxASSERT( nPos <= Len() );
1379
1380 if ( !str.IsEmpty() ) {
1381 wxString strTmp;
1382 wxChar *pc = strTmp.GetWriteBuf(Len() + str.Len());
1383 wxStrncpy(pc, c_str(), nPos);
1384 wxStrcpy(pc + nPos, str);
1385 wxStrcpy(pc + nPos + str.Len(), c_str() + nPos);
1386 strTmp.UngetWriteBuf();
1387 *this = strTmp;
1388 }
1389
1390 return *this;
1391 }
1392
1393 size_t wxString::find(const wxString& str, size_t nStart) const
1394 {
1395 wxASSERT( str.GetStringData()->IsValid() );
1396 wxASSERT( nStart <= Len() );
1397
1398 const wxChar *p = wxStrstr(c_str() + nStart, str);
1399
1400 return p == NULL ? npos : p - c_str();
1401 }
1402
1403 // VC++ 1.5 can't cope with the default argument in the header.
1404 #if !defined(__VISUALC__) || defined(__WIN32__)
1405 size_t wxString::find(const wxChar* sz, size_t nStart, size_t n) const
1406 {
1407 return find(wxString(sz, n == npos ? 0 : n), nStart);
1408 }
1409 #endif // VC++ 1.5
1410
1411 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1412 #if !defined(__BORLANDC__)
1413 size_t wxString::find(wxChar ch, size_t nStart) const
1414 {
1415 wxASSERT( nStart <= Len() );
1416
1417 const wxChar *p = wxStrchr(c_str() + nStart, ch);
1418
1419 return p == NULL ? npos : p - c_str();
1420 }
1421 #endif
1422
1423 size_t wxString::rfind(const wxString& str, size_t nStart) const
1424 {
1425 wxASSERT( str.GetStringData()->IsValid() );
1426 wxASSERT( nStart <= Len() );
1427
1428 // TODO could be made much quicker than that
1429 const wxChar *p = c_str() + (nStart == npos ? Len() : nStart);
1430 while ( p >= c_str() + str.Len() ) {
1431 if ( wxStrncmp(p - str.Len(), str, str.Len()) == 0 )
1432 return p - str.Len() - c_str();
1433 p--;
1434 }
1435
1436 return npos;
1437 }
1438
1439 // VC++ 1.5 can't cope with the default argument in the header.
1440 #if !defined(__VISUALC__) || defined(__WIN32__)
1441 size_t wxString::rfind(const wxChar* sz, size_t nStart, size_t n) const
1442 {
1443 return rfind(wxString(sz, n == npos ? 0 : n), nStart);
1444 }
1445
1446 size_t wxString::rfind(wxChar ch, size_t nStart) const
1447 {
1448 if ( nStart == npos )
1449 {
1450 nStart = Len();
1451 }
1452 else
1453 {
1454 wxASSERT( nStart <= Len() );
1455 }
1456
1457 const wxChar *p = wxStrrchr(c_str(), ch);
1458
1459 if ( p == NULL )
1460 return npos;
1461
1462 size_t result = p - c_str();
1463 return ( result > nStart ) ? npos : result;
1464 }
1465 #endif // VC++ 1.5
1466
1467 size_t wxString::find_first_of(const wxChar* sz, size_t nStart) const
1468 {
1469 const wxChar *start = c_str() + nStart;
1470 const wxChar *firstOf = wxStrpbrk(start, sz);
1471 if ( firstOf )
1472 return firstOf - start;
1473 else
1474 return npos;
1475 }
1476
1477 size_t wxString::find_last_of(const wxChar* sz, size_t nStart) const
1478 {
1479 if ( nStart == npos )
1480 {
1481 nStart = Len();
1482 }
1483 else
1484 {
1485 wxASSERT( nStart <= Len() );
1486 }
1487
1488 for ( const wxChar *p = c_str() + length() - 1; p >= c_str(); p-- )
1489 {
1490 if ( wxStrchr(sz, *p) )
1491 return p - c_str();
1492 }
1493
1494 return npos;
1495 }
1496
1497 size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart) const
1498 {
1499 if ( nStart == npos )
1500 {
1501 nStart = Len();
1502 }
1503 else
1504 {
1505 wxASSERT( nStart <= Len() );
1506 }
1507
1508 size_t nAccept = wxStrspn(c_str() + nStart, sz);
1509 if ( nAccept >= length() - nStart )
1510 return npos;
1511 else
1512 return nAccept;
1513 }
1514
1515 size_t wxString::find_first_not_of(wxChar ch, size_t nStart) const
1516 {
1517 wxASSERT( nStart <= Len() );
1518
1519 for ( const wxChar *p = c_str() + nStart; *p; p++ )
1520 {
1521 if ( *p != ch )
1522 return p - c_str();
1523 }
1524
1525 return npos;
1526 }
1527
1528 size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart) const
1529 {
1530 if ( nStart == npos )
1531 {
1532 nStart = Len();
1533 }
1534 else
1535 {
1536 wxASSERT( nStart <= Len() );
1537 }
1538
1539 for ( const wxChar *p = c_str() + nStart - 1; p >= c_str(); p-- )
1540 {
1541 if ( !wxStrchr(sz, *p) )
1542 return p - c_str();
1543 }
1544
1545 return npos;
1546 }
1547
1548 size_t wxString::find_last_not_of(wxChar ch, size_t nStart) const
1549 {
1550 if ( nStart == npos )
1551 {
1552 nStart = Len();
1553 }
1554 else
1555 {
1556 wxASSERT( nStart <= Len() );
1557 }
1558
1559 for ( const wxChar *p = c_str() + nStart - 1; p >= c_str(); p-- )
1560 {
1561 if ( *p != ch )
1562 return p - c_str();
1563 }
1564
1565 return npos;
1566 }
1567
1568 wxString wxString::substr(size_t nStart, size_t nLen) const
1569 {
1570 // npos means 'take all'
1571 if ( nLen == npos )
1572 nLen = 0;
1573
1574 wxASSERT( nStart + nLen <= Len() );
1575
1576 return wxString(c_str() + nStart, nLen == npos ? 0 : nLen);
1577 }
1578
1579 wxString& wxString::erase(size_t nStart, size_t nLen)
1580 {
1581 wxString strTmp(c_str(), nStart);
1582 if ( nLen != npos ) {
1583 wxASSERT( nStart + nLen <= Len() );
1584
1585 strTmp.append(c_str() + nStart + nLen);
1586 }
1587
1588 *this = strTmp;
1589 return *this;
1590 }
1591
1592 wxString& wxString::replace(size_t nStart, size_t nLen, const wxChar *sz)
1593 {
1594 wxASSERT( nStart + nLen <= wxStrlen(sz) );
1595
1596 wxString strTmp;
1597 if ( nStart != 0 )
1598 strTmp.append(c_str(), nStart);
1599 strTmp += sz;
1600 strTmp.append(c_str() + nStart + nLen);
1601
1602 *this = strTmp;
1603 return *this;
1604 }
1605
1606 wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch)
1607 {
1608 return replace(nStart, nLen, wxString(ch, nCount));
1609 }
1610
1611 wxString& wxString::replace(size_t nStart, size_t nLen,
1612 const wxString& str, size_t nStart2, size_t nLen2)
1613 {
1614 return replace(nStart, nLen, str.substr(nStart2, nLen2));
1615 }
1616
1617 wxString& wxString::replace(size_t nStart, size_t nLen,
1618 const wxChar* sz, size_t nCount)
1619 {
1620 return replace(nStart, nLen, wxString(sz, nCount));
1621 }
1622
1623 #endif //std::string compatibility
1624
1625 // ============================================================================
1626 // ArrayString
1627 // ============================================================================
1628
1629 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1630 #define ARRAY_MAXSIZE_INCREMENT 4096
1631 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1632 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1633 #endif
1634
1635 #define STRING(p) ((wxString *)(&(p)))
1636
1637 // ctor
1638 wxArrayString::wxArrayString()
1639 {
1640 m_nSize =
1641 m_nCount = 0;
1642 m_pItems = (wxChar **) NULL;
1643 }
1644
1645 // copy ctor
1646 wxArrayString::wxArrayString(const wxArrayString& src)
1647 {
1648 m_nSize =
1649 m_nCount = 0;
1650 m_pItems = (wxChar **) NULL;
1651
1652 *this = src;
1653 }
1654
1655 // assignment operator
1656 wxArrayString& wxArrayString::operator=(const wxArrayString& src)
1657 {
1658 if ( m_nSize > 0 )
1659 Clear();
1660
1661 if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
1662 Alloc(src.m_nCount);
1663
1664 // we can't just copy the pointers here because otherwise we would share
1665 // the strings with another array
1666 for ( size_t n = 0; n < src.m_nCount; n++ )
1667 Add(src[n]);
1668
1669 if ( m_nCount != 0 )
1670 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(wxChar *));
1671
1672 return *this;
1673 }
1674
1675 // grow the array
1676 void wxArrayString::Grow()
1677 {
1678 // only do it if no more place
1679 if( m_nCount == m_nSize ) {
1680 if( m_nSize == 0 ) {
1681 // was empty, alloc some memory
1682 m_nSize = ARRAY_DEFAULT_INITIAL_SIZE;
1683 m_pItems = new wxChar *[m_nSize];
1684 }
1685 else {
1686 // otherwise when it's called for the first time, nIncrement would be 0
1687 // and the array would never be expanded
1688 #if defined(__VISAGECPP__)
1689 int array_size = ARRAY_DEFAULT_INITIAL_SIZE;
1690 wxASSERT( array_size != 0 );
1691 #else
1692 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE != 0 );
1693 #endif
1694
1695 // add 50% but not too much
1696 size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
1697 ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
1698 if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
1699 nIncrement = ARRAY_MAXSIZE_INCREMENT;
1700 m_nSize += nIncrement;
1701 wxChar **pNew = new wxChar *[m_nSize];
1702
1703 // copy data to new location
1704 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
1705
1706 // delete old memory (but do not release the strings!)
1707 wxDELETEA(m_pItems);
1708
1709 m_pItems = pNew;
1710 }
1711 }
1712 }
1713
1714 void wxArrayString::Free()
1715 {
1716 for ( size_t n = 0; n < m_nCount; n++ ) {
1717 STRING(m_pItems[n])->GetStringData()->Unlock();
1718 }
1719 }
1720
1721 // deletes all the strings from the list
1722 void wxArrayString::Empty()
1723 {
1724 Free();
1725
1726 m_nCount = 0;
1727 }
1728
1729 // as Empty, but also frees memory
1730 void wxArrayString::Clear()
1731 {
1732 Free();
1733
1734 m_nSize =
1735 m_nCount = 0;
1736
1737 wxDELETEA(m_pItems);
1738 }
1739
1740 // dtor
1741 wxArrayString::~wxArrayString()
1742 {
1743 Free();
1744
1745 wxDELETEA(m_pItems);
1746 }
1747
1748 // pre-allocates memory (frees the previous data!)
1749 void wxArrayString::Alloc(size_t nSize)
1750 {
1751 wxASSERT( nSize > 0 );
1752
1753 // only if old buffer was not big enough
1754 if ( nSize > m_nSize ) {
1755 Free();
1756 wxDELETEA(m_pItems);
1757 m_pItems = new wxChar *[nSize];
1758 m_nSize = nSize;
1759 }
1760
1761 m_nCount = 0;
1762 }
1763
1764 // minimizes the memory usage by freeing unused memory
1765 void wxArrayString::Shrink()
1766 {
1767 // only do it if we have some memory to free
1768 if( m_nCount < m_nSize ) {
1769 // allocates exactly as much memory as we need
1770 wxChar **pNew = new wxChar *[m_nCount];
1771
1772 // copy data to new location
1773 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
1774 delete [] m_pItems;
1775 m_pItems = pNew;
1776 }
1777 }
1778
1779 // searches the array for an item (forward or backwards)
1780 int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const
1781 {
1782 if ( bFromEnd ) {
1783 if ( m_nCount > 0 ) {
1784 size_t ui = m_nCount;
1785 do {
1786 if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) )
1787 return ui;
1788 }
1789 while ( ui != 0 );
1790 }
1791 }
1792 else {
1793 for( size_t ui = 0; ui < m_nCount; ui++ ) {
1794 if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) )
1795 return ui;
1796 }
1797 }
1798
1799 return wxNOT_FOUND;
1800 }
1801
1802 // add item at the end
1803 void wxArrayString::Add(const wxString& str)
1804 {
1805 wxASSERT( str.GetStringData()->IsValid() );
1806
1807 Grow();
1808
1809 // the string data must not be deleted!
1810 str.GetStringData()->Lock();
1811 m_pItems[m_nCount++] = (wxChar *)str.c_str();
1812 }
1813
1814 // add item at the given position
1815 void wxArrayString::Insert(const wxString& str, size_t nIndex)
1816 {
1817 wxASSERT( str.GetStringData()->IsValid() );
1818
1819 wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Insert") );
1820
1821 Grow();
1822
1823 memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
1824 (m_nCount - nIndex)*sizeof(wxChar *));
1825
1826 str.GetStringData()->Lock();
1827 m_pItems[nIndex] = (wxChar *)str.c_str();
1828
1829 m_nCount++;
1830 }
1831
1832 // removes item from array (by index)
1833 void wxArrayString::Remove(size_t nIndex)
1834 {
1835 wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Remove") );
1836
1837 // release our lock
1838 Item(nIndex).GetStringData()->Unlock();
1839
1840 memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
1841 (m_nCount - nIndex - 1)*sizeof(wxChar *));
1842 m_nCount--;
1843 }
1844
1845 // removes item from array (by value)
1846 void wxArrayString::Remove(const wxChar *sz)
1847 {
1848 int iIndex = Index(sz);
1849
1850 wxCHECK_RET( iIndex != wxNOT_FOUND,
1851 _("removing inexistent element in wxArrayString::Remove") );
1852
1853 Remove(iIndex);
1854 }
1855
1856 // ----------------------------------------------------------------------------
1857 // sorting
1858 // ----------------------------------------------------------------------------
1859
1860 // we can only sort one array at a time with the quick-sort based
1861 // implementation
1862 #if wxUSE_THREADS
1863 // need a critical section to protect access to gs_compareFunction and
1864 // gs_sortAscending variables
1865 static wxCriticalSection *gs_critsectStringSort = NULL;
1866
1867 // call this before the value of the global sort vars is changed/after
1868 // you're finished with them
1869 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
1870 gs_critsectStringSort = new wxCriticalSection; \
1871 gs_critsectStringSort->Enter()
1872 #define END_SORT() gs_critsectStringSort->Leave(); \
1873 delete gs_critsectStringSort; \
1874 gs_critsectStringSort = NULL
1875 #else // !threads
1876 #define START_SORT()
1877 #define END_SORT()
1878 #endif // wxUSE_THREADS
1879
1880 // function to use for string comparaison
1881 static wxArrayString::CompareFunction gs_compareFunction = NULL;
1882
1883 // if we don't use the compare function, this flag tells us if we sort the
1884 // array in ascending or descending order
1885 static bool gs_sortAscending = TRUE;
1886
1887 // function which is called by quick sort
1888 static int LINKAGEMODE wxStringCompareFunction(const void *first, const void *second)
1889 {
1890 wxString *strFirst = (wxString *)first;
1891 wxString *strSecond = (wxString *)second;
1892
1893 if ( gs_compareFunction ) {
1894 return gs_compareFunction(*strFirst, *strSecond);
1895 }
1896 else {
1897 // maybe we should use wxStrcoll
1898 int result = wxStrcmp(strFirst->c_str(), strSecond->c_str());
1899
1900 return gs_sortAscending ? result : -result;
1901 }
1902 }
1903
1904 // sort array elements using passed comparaison function
1905 void wxArrayString::Sort(CompareFunction compareFunction)
1906 {
1907 START_SORT();
1908
1909 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
1910 gs_compareFunction = compareFunction;
1911
1912 DoSort();
1913
1914 END_SORT();
1915 }
1916
1917 void wxArrayString::Sort(bool reverseOrder)
1918 {
1919 START_SORT();
1920
1921 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
1922 gs_sortAscending = !reverseOrder;
1923
1924 DoSort();
1925
1926 END_SORT();
1927 }
1928
1929 void wxArrayString::DoSort()
1930 {
1931 // just sort the pointers using qsort() - of course it only works because
1932 // wxString() *is* a pointer to its data
1933 qsort(m_pItems, m_nCount, sizeof(wxChar *), wxStringCompareFunction);
1934 }
1935