]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/string.cpp
in case of an faulty event this might not get initialized
[wxWidgets.git] / src / common / string.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: string.cpp
3// Purpose: wxString class
4// Author: Vadim Zeitlin, Ryan Norton
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// (c) 2004 Ryan Norton <wxprojects@comcast.net>
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13/*
14 * About ref counting:
15 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
16 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
17 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
18 */
19
20// ===========================================================================
21// headers, declarations, constants
22// ===========================================================================
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/defs.h"
33 #include "wx/string.h"
34 #include "wx/intl.h"
35 #include "wx/thread.h"
36#endif
37
38#include <ctype.h>
39#include <string.h>
40#include <stdlib.h>
41
42#ifdef __SALFORDC__
43 #include <clib.h>
44#endif
45
46// allocating extra space for each string consumes more memory but speeds up
47// the concatenation operations (nLen is the current string's length)
48// NB: EXTRA_ALLOC must be >= 0!
49#define EXTRA_ALLOC (19 - nLen % 16)
50
51// ---------------------------------------------------------------------------
52// static class variables definition
53// ---------------------------------------------------------------------------
54
55#if !wxUSE_STL
56 //According to STL _must_ be a -1 size_t
57 const size_t wxStringBase::npos = (size_t) -1;
58#endif
59
60// ----------------------------------------------------------------------------
61// static data
62// ----------------------------------------------------------------------------
63
64#if wxUSE_STL
65
66extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T("");
67
68#else
69
70// for an empty string, GetStringData() will return this address: this
71// structure has the same layout as wxStringData and it's data() method will
72// return the empty string (dummy pointer)
73static const struct
74{
75 wxStringData data;
76 wxChar dummy;
77} g_strEmpty = { {-1, 0, 0}, wxT('\0') };
78
79// empty C style string: points to 'string data' byte of g_strEmpty
80extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy;
81
82#endif
83
84// ----------------------------------------------------------------------------
85// global functions
86// ----------------------------------------------------------------------------
87
88#if wxUSE_STD_IOSTREAM
89
90// MS Visual C++ version 5.0 provides the new STL headers as well as the old
91// iostream ones.
92//
93// ATTN: you can _not_ use both of these in the same program!
94
95#include <iostream>
96
97wxSTD istream& operator>>(wxSTD istream& is, wxString& WXUNUSED(str))
98{
99#if 0
100 int w = is.width(0);
101 if ( is.ipfx(0) ) {
102 streambuf *sb = is.rdbuf();
103 str.erase();
104 while ( true ) {
105 int ch = sb->sbumpc ();
106 if ( ch == EOF ) {
107 is.setstate(ios::eofbit);
108 break;
109 }
110 else if ( isspace(ch) ) {
111 sb->sungetc();
112 break;
113 }
114
115 str += ch;
116 if ( --w == 1 )
117 break;
118 }
119 }
120
121 is.isfx();
122 if ( str.length() == 0 )
123 is.setstate(ios::failbit);
124#endif
125 return is;
126}
127
128wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
129{
130#ifdef __BORLANDC__
131 os << str.mb_str();
132#else
133 os << str.c_str();
134#endif
135 return os;
136}
137
138#endif // wxUSE_STD_IOSTREAM
139
140// ----------------------------------------------------------------------------
141// private classes
142// ----------------------------------------------------------------------------
143
144// this small class is used to gather statistics for performance tuning
145//#define WXSTRING_STATISTICS
146#ifdef WXSTRING_STATISTICS
147 class Averager
148 {
149 public:
150 Averager(const wxChar *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
151 ~Averager()
152 { wxPrintf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
153
154 void Add(size_t n) { m_nTotal += n; m_nCount++; }
155
156 private:
157 size_t m_nCount, m_nTotal;
158 const wxChar *m_sz;
159 } g_averageLength("allocation size"),
160 g_averageSummandLength("summand length"),
161 g_averageConcatHit("hit probability in concat"),
162 g_averageInitialLength("initial string length");
163
164 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
165#else
166 #define STATISTICS_ADD(av, val)
167#endif // WXSTRING_STATISTICS
168
169#if !wxUSE_STL
170
171// ===========================================================================
172// wxStringData class deallocation
173// ===========================================================================
174
175#if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
176# pragma message (__FILE__ ": building with Multithreaded non DLL runtime has a performance impact on wxString!")
177void wxStringData::Free()
178{
179 free(this);
180}
181#endif
182
183// ===========================================================================
184// wxStringBase
185// ===========================================================================
186
187// takes nLength elements of psz starting at nPos
188void wxStringBase::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
189{
190 Init();
191
192 // if the length is not given, assume the string to be NUL terminated
193 if ( nLength == npos ) {
194 wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") );
195
196 nLength = wxStrlen(psz + nPos);
197 }
198
199 STATISTICS_ADD(InitialLength, nLength);
200
201 if ( nLength > 0 ) {
202 // trailing '\0' is written in AllocBuffer()
203 if ( !AllocBuffer(nLength) ) {
204 wxFAIL_MSG( _T("out of memory in wxStringBase::InitWith") );
205 return;
206 }
207 wxTmemcpy(m_pchData, psz + nPos, nLength);
208 }
209}
210
211// poor man's iterators are "void *" pointers
212wxStringBase::wxStringBase(const void *pStart, const void *pEnd)
213{
214 InitWith((const wxChar *)pStart, 0,
215 (const wxChar *)pEnd - (const wxChar *)pStart);
216}
217
218wxStringBase::wxStringBase(size_type n, wxChar ch)
219{
220 Init();
221 append(n, ch);
222}
223
224// ---------------------------------------------------------------------------
225// memory allocation
226// ---------------------------------------------------------------------------
227
228// allocates memory needed to store a C string of length nLen
229bool wxStringBase::AllocBuffer(size_t nLen)
230{
231 // allocating 0 sized buffer doesn't make sense, all empty strings should
232 // reuse g_strEmpty
233 wxASSERT( nLen > 0 );
234
235 // make sure that we don't overflow
236 wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) -
237 (sizeof(wxStringData) + EXTRA_ALLOC + 1) );
238
239 STATISTICS_ADD(Length, nLen);
240
241 // allocate memory:
242 // 1) one extra character for '\0' termination
243 // 2) sizeof(wxStringData) for housekeeping info
244 wxStringData* pData = (wxStringData*)
245 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
246
247 if ( pData == NULL ) {
248 // allocation failures are handled by the caller
249 return false;
250 }
251
252 pData->nRefs = 1;
253 pData->nDataLength = nLen;
254 pData->nAllocLength = nLen + EXTRA_ALLOC;
255 m_pchData = pData->data(); // data starts after wxStringData
256 m_pchData[nLen] = wxT('\0');
257 return true;
258}
259
260// must be called before changing this string
261bool wxStringBase::CopyBeforeWrite()
262{
263 wxStringData* pData = GetStringData();
264
265 if ( pData->IsShared() ) {
266 pData->Unlock(); // memory not freed because shared
267 size_t nLen = pData->nDataLength;
268 if ( !AllocBuffer(nLen) ) {
269 // allocation failures are handled by the caller
270 return false;
271 }
272 wxTmemcpy(m_pchData, pData->data(), nLen);
273 }
274
275 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
276
277 return true;
278}
279
280// must be called before replacing contents of this string
281bool wxStringBase::AllocBeforeWrite(size_t nLen)
282{
283 wxASSERT( nLen != 0 ); // doesn't make any sense
284
285 // must not share string and must have enough space
286 wxStringData* pData = GetStringData();
287 if ( pData->IsShared() || pData->IsEmpty() ) {
288 // can't work with old buffer, get new one
289 pData->Unlock();
290 if ( !AllocBuffer(nLen) ) {
291 // allocation failures are handled by the caller
292 return false;
293 }
294 }
295 else {
296 if ( nLen > pData->nAllocLength ) {
297 // realloc the buffer instead of calling malloc() again, this is more
298 // efficient
299 STATISTICS_ADD(Length, nLen);
300
301 nLen += EXTRA_ALLOC;
302
303 pData = (wxStringData*)
304 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
305
306 if ( pData == NULL ) {
307 // allocation failures are handled by the caller
308 // keep previous data since reallocation failed
309 return false;
310 }
311
312 pData->nAllocLength = nLen;
313 m_pchData = pData->data();
314 }
315 }
316
317 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
318
319 // it doesn't really matter what the string length is as it's going to be
320 // overwritten later but, for extra safety, set it to 0 for now as we may
321 // have some junk in m_pchData
322 GetStringData()->nDataLength = 0;
323
324 return true;
325}
326
327wxStringBase& wxStringBase::append(size_t n, wxChar ch)
328{
329 size_type len = length();
330
331 if ( !Alloc(len + n) || !CopyBeforeWrite() ) {
332 wxFAIL_MSG( _T("out of memory in wxStringBase::append") );
333 }
334 GetStringData()->nDataLength = len + n;
335 m_pchData[len + n] = '\0';
336 for ( size_t i = 0; i < n; ++i )
337 m_pchData[len + i] = ch;
338 return *this;
339}
340
341void wxStringBase::resize(size_t nSize, wxChar ch)
342{
343 size_t len = length();
344
345 if ( nSize < len )
346 {
347 erase(begin() + nSize, end());
348 }
349 else if ( nSize > len )
350 {
351 append(nSize - len, ch);
352 }
353 //else: we have exactly the specified length, nothing to do
354}
355
356// allocate enough memory for nLen characters
357bool wxStringBase::Alloc(size_t nLen)
358{
359 wxStringData *pData = GetStringData();
360 if ( pData->nAllocLength <= nLen ) {
361 if ( pData->IsEmpty() ) {
362 nLen += EXTRA_ALLOC;
363
364 pData = (wxStringData *)
365 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
366
367 if ( pData == NULL ) {
368 // allocation failure handled by caller
369 return false;
370 }
371
372 pData->nRefs = 1;
373 pData->nDataLength = 0;
374 pData->nAllocLength = nLen;
375 m_pchData = pData->data(); // data starts after wxStringData
376 m_pchData[0u] = wxT('\0');
377 }
378 else if ( pData->IsShared() ) {
379 pData->Unlock(); // memory not freed because shared
380 size_t nOldLen = pData->nDataLength;
381 if ( !AllocBuffer(nLen) ) {
382 // allocation failure handled by caller
383 return false;
384 }
385 // +1 to copy the terminator, too
386 memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxChar));
387 GetStringData()->nDataLength = nOldLen;
388 }
389 else {
390 nLen += EXTRA_ALLOC;
391
392 pData = (wxStringData *)
393 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
394
395 if ( pData == NULL ) {
396 // allocation failure handled by caller
397 // keep previous data since reallocation failed
398 return false;
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 pData->nAllocLength = nLen;
404 m_pchData = pData->data();
405 }
406 }
407 //else: we've already got enough
408 return true;
409}
410
411wxStringBase::iterator wxStringBase::begin()
412{
413 if (length() > 0)
414 CopyBeforeWrite();
415 return m_pchData;
416}
417
418wxStringBase::iterator wxStringBase::end()
419{
420 if (length() > 0)
421 CopyBeforeWrite();
422 return m_pchData + length();
423}
424
425wxStringBase::iterator wxStringBase::erase(iterator it)
426{
427 size_type idx = it - begin();
428 erase(idx, 1);
429 return begin() + idx;
430}
431
432wxStringBase& wxStringBase::erase(size_t nStart, size_t nLen)
433{
434 wxASSERT(nStart <= length());
435 size_t strLen = length() - nStart;
436 // delete nLen or up to the end of the string characters
437 nLen = strLen < nLen ? strLen : nLen;
438 wxString strTmp(c_str(), nStart);
439 strTmp.append(c_str() + nStart + nLen, length() - nStart - nLen);
440
441 swap(strTmp);
442 return *this;
443}
444
445wxStringBase& wxStringBase::insert(size_t nPos, const wxChar *sz, size_t n)
446{
447 wxASSERT( nPos <= length() );
448
449 if ( n == npos ) n = wxStrlen(sz);
450 if ( n == 0 ) return *this;
451
452 if ( !Alloc(length() + n) || !CopyBeforeWrite() ) {
453 wxFAIL_MSG( _T("out of memory in wxStringBase::insert") );
454 }
455
456 memmove(m_pchData + nPos + n, m_pchData + nPos,
457 (length() - nPos) * sizeof(wxChar));
458 memcpy(m_pchData + nPos, sz, n * sizeof(wxChar));
459 GetStringData()->nDataLength = length() + n;
460 m_pchData[length()] = '\0';
461
462 return *this;
463}
464
465void wxStringBase::swap(wxStringBase& str)
466{
467 wxChar* tmp = str.m_pchData;
468 str.m_pchData = m_pchData;
469 m_pchData = tmp;
470}
471
472size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const
473{
474 wxASSERT( str.GetStringData()->IsValid() );
475 wxASSERT( nStart <= length() );
476
477 //anchor
478 const wxChar* p = (const wxChar*)wxTmemchr(c_str() + nStart,
479 str.c_str()[0],
480 length() - nStart);
481
482 if(!p)
483 return npos;
484
485 while(p - c_str() + str.length() <= length() &&
486 wxTmemcmp(p, str.c_str(), str.length()) )
487 {
488 //Previosly passed as the first argument to wxTmemchr,
489 //but C/C++ standard does not specify evaluation order
490 //of arguments to functions -
491 //http://embedded.com/showArticle.jhtml?articleID=9900607
492 ++p;
493
494 //anchor again
495 p = (const wxChar*)wxTmemchr(p,
496 str.c_str()[0],
497 length() - (p - c_str()));
498
499 if(!p)
500 return npos;
501 }
502
503 return (p - c_str() + str.length() <= length()) ? p - c_str() : npos;
504}
505
506size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const
507{
508 return find(wxStringBase(sz, n), nStart);
509}
510
511size_t wxStringBase::find(wxChar ch, size_t nStart) const
512{
513 wxASSERT( nStart <= length() );
514
515 const wxChar *p = (const wxChar*)wxTmemchr(c_str() + nStart, ch, length() - nStart);
516
517 return p == NULL ? npos : p - c_str();
518}
519
520size_t wxStringBase::rfind(const wxStringBase& str, size_t nStart) const
521{
522 wxASSERT( str.GetStringData()->IsValid() );
523 wxASSERT( nStart == npos || nStart <= length() );
524
525 if ( length() >= str.length() )
526 {
527 // avoids a corner case later
528 if ( length() == 0 && str.length() == 0 )
529 return 0;
530
531 // "top" is the point where search starts from
532 size_t top = length() - str.length();
533
534 if ( nStart == npos )
535 nStart = length() - 1;
536 if ( nStart < top )
537 top = nStart;
538
539 const wxChar *cursor = c_str() + top;
540 do
541 {
542 if ( wxTmemcmp(cursor, str.c_str(),
543 str.length()) == 0 )
544 {
545 return cursor - c_str();
546 }
547 } while ( cursor-- > c_str() );
548 }
549
550 return npos;
551}
552
553size_t wxStringBase::rfind(const wxChar* sz, size_t nStart, size_t n) const
554{
555 return rfind(wxStringBase(sz, n), nStart);
556}
557
558size_t wxStringBase::rfind(wxChar ch, size_t nStart) const
559{
560 if ( nStart == npos )
561 {
562 nStart = length();
563 }
564 else
565 {
566 wxASSERT( nStart <= length() );
567 }
568
569 const wxChar *actual;
570 for ( actual = c_str() + ( nStart == npos ? length() : nStart + 1 );
571 actual > c_str(); --actual )
572 {
573 if ( *(actual - 1) == ch )
574 return (actual - 1) - c_str();
575 }
576
577 return npos;
578}
579
580size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart) const
581{
582 wxASSERT(nStart <= length());
583
584 size_t len = wxStrlen(sz);
585
586 size_t i;
587 for(i = nStart; i < this->length(); ++i)
588 {
589 if (wxTmemchr(sz, *(c_str() + i), len))
590 break;
591 }
592
593 if(i == this->length())
594 return npos;
595 else
596 return i;
597}
598
599size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart,
600 size_t n) const
601{
602 return find_first_of(wxStringBase(sz, n), nStart);
603}
604
605size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart) const
606{
607 if ( nStart == npos )
608 {
609 nStart = length() - 1;
610 }
611 else
612 {
613 wxASSERT_MSG( nStart <= length(),
614 _T("invalid index in find_last_of()") );
615 }
616
617 size_t len = wxStrlen(sz);
618
619 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
620 {
621 if ( wxTmemchr(sz, *p, len) )
622 return p - c_str();
623 }
624
625 return npos;
626}
627
628size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart,
629 size_t n) const
630{
631 return find_last_of(wxStringBase(sz, n), nStart);
632}
633
634size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart) const
635{
636 if ( nStart == npos )
637 {
638 nStart = length();
639 }
640 else
641 {
642 wxASSERT( nStart <= length() );
643 }
644
645 size_t len = wxStrlen(sz);
646
647 size_t i;
648 for(i = nStart; i < this->length(); ++i)
649 {
650 if (!wxTmemchr(sz, *(c_str() + i), len))
651 break;
652 }
653
654 if(i == this->length())
655 return npos;
656 else
657 return i;
658}
659
660size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart,
661 size_t n) const
662{
663 return find_first_not_of(wxStringBase(sz, n), nStart);
664}
665
666size_t wxStringBase::find_first_not_of(wxChar ch, size_t nStart) const
667{
668 wxASSERT( nStart <= length() );
669
670 for ( const wxChar *p = c_str() + nStart; *p; p++ )
671 {
672 if ( *p != ch )
673 return p - c_str();
674 }
675
676 return npos;
677}
678
679size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart) const
680{
681 if ( nStart == npos )
682 {
683 nStart = length() - 1;
684 }
685 else
686 {
687 wxASSERT( nStart <= length() );
688 }
689
690 size_t len = wxStrlen(sz);
691
692 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
693 {
694 if ( !wxTmemchr(sz, *p,len) )
695 return p - c_str();
696 }
697
698 return npos;
699}
700
701size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart,
702 size_t n) const
703{
704 return find_last_not_of(wxStringBase(sz, n), nStart);
705}
706
707size_t wxStringBase::find_last_not_of(wxChar ch, size_t nStart) const
708{
709 if ( nStart == npos )
710 {
711 nStart = length() - 1;
712 }
713 else
714 {
715 wxASSERT( nStart <= length() );
716 }
717
718 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
719 {
720 if ( *p != ch )
721 return p - c_str();
722 }
723
724 return npos;
725}
726
727wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
728 const wxChar *sz)
729{
730 wxASSERT_MSG( nStart <= length(),
731 _T("index out of bounds in wxStringBase::replace") );
732 size_t strLen = length() - nStart;
733 nLen = strLen < nLen ? strLen : nLen;
734
735 wxStringBase strTmp;
736 strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs
737
738 //This is kind of inefficient, but its pretty good considering...
739 //we don't want to use character access operators here because on STL
740 //it will freeze the reference count of strTmp, which means a deep copy
741 //at the end when swap is called
742 //
743 //Also, we can't use append with the full character pointer and must
744 //do it manually because this string can contain null characters
745 for(size_t i1 = 0; i1 < nStart; ++i1)
746 strTmp.append(1, this->c_str()[i1]);
747
748 //its safe to do the full version here because
749 //sz must be a normal c string
750 strTmp.append(sz);
751
752 for(size_t i2 = nStart + nLen; i2 < length(); ++i2)
753 strTmp.append(1, this->c_str()[i2]);
754
755 swap(strTmp);
756 return *this;
757}
758
759wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
760 size_t nCount, wxChar ch)
761{
762 return replace(nStart, nLen, wxStringBase(nCount, ch).c_str());
763}
764
765wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
766 const wxStringBase& str,
767 size_t nStart2, size_t nLen2)
768{
769 return replace(nStart, nLen, str.substr(nStart2, nLen2));
770}
771
772wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
773 const wxChar* sz, size_t nCount)
774{
775 return replace(nStart, nLen, wxStringBase(sz, nCount).c_str());
776}
777
778wxStringBase wxStringBase::substr(size_t nStart, size_t nLen) const
779{
780 if ( nLen == npos )
781 nLen = length() - nStart;
782 return wxStringBase(*this, nStart, nLen);
783}
784
785// assigns one string to another
786wxStringBase& wxStringBase::operator=(const wxStringBase& stringSrc)
787{
788 wxASSERT( stringSrc.GetStringData()->IsValid() );
789
790 // don't copy string over itself
791 if ( m_pchData != stringSrc.m_pchData ) {
792 if ( stringSrc.GetStringData()->IsEmpty() ) {
793 Reinit();
794 }
795 else {
796 // adjust references
797 GetStringData()->Unlock();
798 m_pchData = stringSrc.m_pchData;
799 GetStringData()->Lock();
800 }
801 }
802
803 return *this;
804}
805
806// assigns a single character
807wxStringBase& wxStringBase::operator=(wxChar ch)
808{
809 if ( !AssignCopy(1, &ch) ) {
810 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(wxChar)") );
811 }
812 return *this;
813}
814
815// assigns C string
816wxStringBase& wxStringBase::operator=(const wxChar *psz)
817{
818 if ( !AssignCopy(wxStrlen(psz), psz) ) {
819 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(const wxChar *)") );
820 }
821 return *this;
822}
823
824// helper function: does real copy
825bool wxStringBase::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
826{
827 if ( nSrcLen == 0 ) {
828 Reinit();
829 }
830 else {
831 if ( !AllocBeforeWrite(nSrcLen) ) {
832 // allocation failure handled by caller
833 return false;
834 }
835 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
836 GetStringData()->nDataLength = nSrcLen;
837 m_pchData[nSrcLen] = wxT('\0');
838 }
839 return true;
840}
841
842// ---------------------------------------------------------------------------
843// string concatenation
844// ---------------------------------------------------------------------------
845
846// add something to this string
847bool wxStringBase::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData,
848 size_t nMaxLen)
849{
850 STATISTICS_ADD(SummandLength, nSrcLen);
851
852 nSrcLen = nSrcLen < nMaxLen ? nSrcLen : nMaxLen;
853
854 // concatenating an empty string is a NOP
855 if ( nSrcLen > 0 ) {
856 wxStringData *pData = GetStringData();
857 size_t nLen = pData->nDataLength;
858 size_t nNewLen = nLen + nSrcLen;
859
860 // alloc new buffer if current is too small
861 if ( pData->IsShared() ) {
862 STATISTICS_ADD(ConcatHit, 0);
863
864 // we have to allocate another buffer
865 wxStringData* pOldData = GetStringData();
866 if ( !AllocBuffer(nNewLen) ) {
867 // allocation failure handled by caller
868 return false;
869 }
870 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
871 pOldData->Unlock();
872 }
873 else if ( nNewLen > pData->nAllocLength ) {
874 STATISTICS_ADD(ConcatHit, 0);
875
876 reserve(nNewLen);
877 // we have to grow the buffer
878 if ( capacity() < nNewLen ) {
879 // allocation failure handled by caller
880 return false;
881 }
882 }
883 else {
884 STATISTICS_ADD(ConcatHit, 1);
885
886 // the buffer is already big enough
887 }
888
889 // should be enough space
890 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
891
892 // fast concatenation - all is done in our buffer
893 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
894
895 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
896 GetStringData()->nDataLength = nNewLen; // and fix the length
897 }
898 //else: the string to append was empty
899 return true;
900}
901
902// ---------------------------------------------------------------------------
903// simple sub-string extraction
904// ---------------------------------------------------------------------------
905
906// helper function: clone the data attached to this string
907bool wxStringBase::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
908{
909 if ( nCopyLen == 0 ) {
910 dest.Init();
911 }
912 else {
913 if ( !dest.AllocBuffer(nCopyLen) ) {
914 // allocation failure handled by caller
915 return false;
916 }
917 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
918 }
919 return true;
920}
921
922#endif // !wxUSE_STL
923
924#if !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
925
926#if !wxUSE_STL
927 #define STRINGCLASS wxStringBase
928#else
929 #define STRINGCLASS wxString
930#endif
931
932static inline int wxDoCmp(const wxChar* s1, size_t l1,
933 const wxChar* s2, size_t l2)
934{
935 if( l1 == l2 )
936 return wxTmemcmp(s1, s2, l1);
937 else if( l1 < l2 )
938 {
939 int ret = wxTmemcmp(s1, s2, l1);
940 return ret == 0 ? -1 : ret;
941 }
942 else
943 {
944 int ret = wxTmemcmp(s1, s2, l2);
945 return ret == 0 ? +1 : ret;
946 }
947}
948
949int STRINGCLASS::compare(const wxStringBase& str) const
950{
951 return ::wxDoCmp(data(), length(), str.data(), str.length());
952}
953
954int STRINGCLASS::compare(size_t nStart, size_t nLen,
955 const wxStringBase& str) const
956{
957 wxASSERT(nStart <= length());
958 size_type strLen = length() - nStart;
959 nLen = strLen < nLen ? strLen : nLen;
960 return ::wxDoCmp(data() + nStart, nLen, str.data(), str.length());
961}
962
963int STRINGCLASS::compare(size_t nStart, size_t nLen,
964 const wxStringBase& str,
965 size_t nStart2, size_t nLen2) const
966{
967 wxASSERT(nStart <= length());
968 wxASSERT(nStart2 <= str.length());
969 size_type strLen = length() - nStart,
970 strLen2 = str.length() - nStart2;
971 nLen = strLen < nLen ? strLen : nLen;
972 nLen2 = strLen2 < nLen2 ? strLen2 : nLen2;
973 return ::wxDoCmp(data() + nStart, nLen, str.data() + nStart2, nLen2);
974}
975
976int STRINGCLASS::compare(const wxChar* sz) const
977{
978 size_t nLen = wxStrlen(sz);
979 return ::wxDoCmp(data(), length(), sz, nLen);
980}
981
982int STRINGCLASS::compare(size_t nStart, size_t nLen,
983 const wxChar* sz, size_t nCount) const
984{
985 wxASSERT(nStart <= length());
986 size_type strLen = length() - nStart;
987 nLen = strLen < nLen ? strLen : nLen;
988 if( nCount == npos )
989 nCount = wxStrlen(sz);
990
991 return ::wxDoCmp(data() + nStart, nLen, sz, nCount);
992}
993
994#undef STRINGCLASS
995
996#endif // !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
997
998// ===========================================================================
999// wxString class core
1000// ===========================================================================
1001
1002// ---------------------------------------------------------------------------
1003// construction and conversion
1004// ---------------------------------------------------------------------------
1005
1006#if wxUSE_UNICODE
1007
1008// from multibyte string
1009wxString::wxString(const char *psz, const wxMBConv& conv, size_t nLength)
1010{
1011 // anything to do?
1012 if ( psz && nLength != 0 )
1013 {
1014 if ( nLength == npos )
1015 {
1016 nLength = wxNO_LEN;
1017 }
1018
1019 size_t nLenWide;
1020 wxWCharBuffer wbuf = conv.cMB2WC(psz, nLength, &nLenWide);
1021
1022 if ( nLenWide )
1023 assign(wbuf, nLenWide);
1024 }
1025}
1026
1027//Convert wxString in Unicode mode to a multi-byte string
1028const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const
1029{
1030 return conv.cWC2MB(c_str(), length() + 1 /* size, not length */, NULL);
1031}
1032
1033#else // ANSI
1034
1035#if wxUSE_WCHAR_T
1036
1037// from wide string
1038wxString::wxString(const wchar_t *pwz, const wxMBConv& conv, size_t nLength)
1039{
1040 // anything to do?
1041 if ( pwz && nLength != 0 )
1042 {
1043 if ( nLength == npos )
1044 {
1045 nLength = wxNO_LEN;
1046 }
1047
1048 size_t nLenMB;
1049 wxCharBuffer buf = conv.cWC2MB(pwz, nLength, &nLenMB);
1050
1051 if ( nLenMB )
1052 assign(buf, nLenMB);
1053 }
1054}
1055
1056//Converts this string to a wide character string if unicode
1057//mode is not enabled and wxUSE_WCHAR_T is enabled
1058const wxWCharBuffer wxString::wc_str(const wxMBConv& conv) const
1059{
1060 return conv.cMB2WC(c_str(), length() + 1 /* size, not length */, NULL);
1061}
1062
1063#endif // wxUSE_WCHAR_T
1064
1065#endif // Unicode/ANSI
1066
1067// shrink to minimal size (releasing extra memory)
1068bool wxString::Shrink()
1069{
1070 wxString tmp(begin(), end());
1071 swap(tmp);
1072 return tmp.length() == length();
1073}
1074
1075#if !wxUSE_STL
1076// get the pointer to writable buffer of (at least) nLen bytes
1077wxChar *wxString::GetWriteBuf(size_t nLen)
1078{
1079 if ( !AllocBeforeWrite(nLen) ) {
1080 // allocation failure handled by caller
1081 return NULL;
1082 }
1083
1084 wxASSERT( GetStringData()->nRefs == 1 );
1085 GetStringData()->Validate(false);
1086
1087 return m_pchData;
1088}
1089
1090// put string back in a reasonable state after GetWriteBuf
1091void wxString::UngetWriteBuf()
1092{
1093 GetStringData()->nDataLength = wxStrlen(m_pchData);
1094 GetStringData()->Validate(true);
1095}
1096
1097void wxString::UngetWriteBuf(size_t nLen)
1098{
1099 GetStringData()->nDataLength = nLen;
1100 GetStringData()->Validate(true);
1101}
1102#endif
1103
1104// ---------------------------------------------------------------------------
1105// data access
1106// ---------------------------------------------------------------------------
1107
1108// all functions are inline in string.h
1109
1110// ---------------------------------------------------------------------------
1111// assignment operators
1112// ---------------------------------------------------------------------------
1113
1114#if !wxUSE_UNICODE
1115
1116// same as 'signed char' variant
1117wxString& wxString::operator=(const unsigned char* psz)
1118{
1119 *this = (const char *)psz;
1120 return *this;
1121}
1122
1123#if wxUSE_WCHAR_T
1124wxString& wxString::operator=(const wchar_t *pwz)
1125{
1126 wxString str(pwz);
1127 swap(str);
1128 return *this;
1129}
1130#endif
1131
1132#endif
1133
1134/*
1135 * concatenation functions come in 5 flavours:
1136 * string + string
1137 * char + string and string + char
1138 * C str + string and string + C str
1139 */
1140
1141wxString operator+(const wxString& str1, const wxString& str2)
1142{
1143#if !wxUSE_STL
1144 wxASSERT( str1.GetStringData()->IsValid() );
1145 wxASSERT( str2.GetStringData()->IsValid() );
1146#endif
1147
1148 wxString s = str1;
1149 s += str2;
1150
1151 return s;
1152}
1153
1154wxString operator+(const wxString& str, wxChar ch)
1155{
1156#if !wxUSE_STL
1157 wxASSERT( str.GetStringData()->IsValid() );
1158#endif
1159
1160 wxString s = str;
1161 s += ch;
1162
1163 return s;
1164}
1165
1166wxString operator+(wxChar ch, const wxString& str)
1167{
1168#if !wxUSE_STL
1169 wxASSERT( str.GetStringData()->IsValid() );
1170#endif
1171
1172 wxString s = ch;
1173 s += str;
1174
1175 return s;
1176}
1177
1178wxString operator+(const wxString& str, const wxChar *psz)
1179{
1180#if !wxUSE_STL
1181 wxASSERT( str.GetStringData()->IsValid() );
1182#endif
1183
1184 wxString s;
1185 if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) {
1186 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1187 }
1188 s += str;
1189 s += psz;
1190
1191 return s;
1192}
1193
1194wxString operator+(const wxChar *psz, const wxString& str)
1195{
1196#if !wxUSE_STL
1197 wxASSERT( str.GetStringData()->IsValid() );
1198#endif
1199
1200 wxString s;
1201 if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) {
1202 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1203 }
1204 s = psz;
1205 s += str;
1206
1207 return s;
1208}
1209
1210// ===========================================================================
1211// other common string functions
1212// ===========================================================================
1213
1214int wxString::Cmp(const wxString& s) const
1215{
1216 return compare(s);
1217}
1218
1219int wxString::Cmp(const wxChar* psz) const
1220{
1221 return compare(psz);
1222}
1223
1224static inline int wxDoCmpNoCase(const wxChar* s1, size_t l1,
1225 const wxChar* s2, size_t l2)
1226{
1227 size_t i;
1228
1229 if( l1 == l2 )
1230 {
1231 for(i = 0; i < l1; ++i)
1232 {
1233 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1234 break;
1235 }
1236 return i == l1 ? 0 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
1237 }
1238 else if( l1 < l2 )
1239 {
1240 for(i = 0; i < l1; ++i)
1241 {
1242 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1243 break;
1244 }
1245 return i == l1 ? -1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
1246 }
1247 else
1248 {
1249 for(i = 0; i < l2; ++i)
1250 {
1251 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1252 break;
1253 }
1254 return i == l2 ? 1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
1255 }
1256}
1257
1258int wxString::CmpNoCase(const wxString& s) const
1259{
1260 return wxDoCmpNoCase(data(), length(), s.data(), s.length());
1261}
1262
1263int wxString::CmpNoCase(const wxChar* psz) const
1264{
1265 int nLen = wxStrlen(psz);
1266
1267 return wxDoCmpNoCase(data(), length(), psz, nLen);
1268}
1269
1270
1271#if wxUSE_UNICODE
1272
1273#ifdef __MWERKS__
1274#ifndef __SCHAR_MAX__
1275#define __SCHAR_MAX__ 127
1276#endif
1277#endif
1278
1279wxString wxString::FromAscii(const char *ascii)
1280{
1281 if (!ascii)
1282 return wxEmptyString;
1283
1284 size_t len = strlen( ascii );
1285 wxString res;
1286
1287 if ( len )
1288 {
1289 wxStringBuffer buf(res, len);
1290
1291 wchar_t *dest = buf;
1292
1293 for ( ;; )
1294 {
1295 if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' )
1296 break;
1297 }
1298 }
1299
1300 return res;
1301}
1302
1303wxString wxString::FromAscii(const char ascii)
1304{
1305 // What do we do with '\0' ?
1306
1307 wxString res;
1308 res += (wchar_t)(unsigned char) ascii;
1309
1310 return res;
1311}
1312
1313const wxCharBuffer wxString::ToAscii() const
1314{
1315 // this will allocate enough space for the terminating NUL too
1316 wxCharBuffer buffer(length());
1317
1318
1319 char *dest = buffer.data();
1320
1321 const wchar_t *pwc = c_str();
1322 for ( ;; )
1323 {
1324 *dest++ = (char)(*pwc > SCHAR_MAX ? wxT('_') : *pwc);
1325
1326 // the output string can't have embedded NULs anyhow, so we can safely
1327 // stop at first of them even if we do have any
1328 if ( !*pwc++ )
1329 break;
1330 }
1331
1332 return buffer;
1333}
1334
1335#endif // Unicode
1336
1337// extract string of length nCount starting at nFirst
1338wxString wxString::Mid(size_t nFirst, size_t nCount) const
1339{
1340 size_t nLen = length();
1341
1342 // default value of nCount is npos and means "till the end"
1343 if ( nCount == npos )
1344 {
1345 nCount = nLen - nFirst;
1346 }
1347
1348 // out-of-bounds requests return sensible things
1349 if ( nFirst + nCount > nLen )
1350 {
1351 nCount = nLen - nFirst;
1352 }
1353
1354 if ( nFirst > nLen )
1355 {
1356 // AllocCopy() will return empty string
1357 return wxEmptyString;
1358 }
1359
1360 wxString dest(*this, nFirst, nCount);
1361 if ( dest.length() != nCount )
1362 {
1363 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
1364 }
1365
1366 return dest;
1367}
1368
1369// check that the string starts with prefix and return the rest of the string
1370// in the provided pointer if it is not NULL, otherwise return false
1371bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const
1372{
1373 wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") );
1374
1375 // first check if the beginning of the string matches the prefix: note
1376 // that we don't have to check that we don't run out of this string as
1377 // when we reach the terminating NUL, either prefix string ends too (and
1378 // then it's ok) or we break out of the loop because there is no match
1379 const wxChar *p = c_str();
1380 while ( *prefix )
1381 {
1382 if ( *prefix++ != *p++ )
1383 {
1384 // no match
1385 return false;
1386 }
1387 }
1388
1389 if ( rest )
1390 {
1391 // put the rest of the string into provided pointer
1392 *rest = p;
1393 }
1394
1395 return true;
1396}
1397
1398// extract nCount last (rightmost) characters
1399wxString wxString::Right(size_t nCount) const
1400{
1401 if ( nCount > length() )
1402 nCount = length();
1403
1404 wxString dest(*this, length() - nCount, nCount);
1405 if ( dest.length() != nCount ) {
1406 wxFAIL_MSG( _T("out of memory in wxString::Right") );
1407 }
1408 return dest;
1409}
1410
1411// get all characters after the last occurence of ch
1412// (returns the whole string if ch not found)
1413wxString wxString::AfterLast(wxChar ch) const
1414{
1415 wxString str;
1416 int iPos = Find(ch, true);
1417 if ( iPos == wxNOT_FOUND )
1418 str = *this;
1419 else
1420 str = c_str() + iPos + 1;
1421
1422 return str;
1423}
1424
1425// extract nCount first (leftmost) characters
1426wxString wxString::Left(size_t nCount) const
1427{
1428 if ( nCount > length() )
1429 nCount = length();
1430
1431 wxString dest(*this, 0, nCount);
1432 if ( dest.length() != nCount ) {
1433 wxFAIL_MSG( _T("out of memory in wxString::Left") );
1434 }
1435 return dest;
1436}
1437
1438// get all characters before the first occurence of ch
1439// (returns the whole string if ch not found)
1440wxString wxString::BeforeFirst(wxChar ch) const
1441{
1442 int iPos = Find(ch);
1443 if ( iPos == wxNOT_FOUND ) iPos = length();
1444 return wxString(*this, 0, iPos);
1445}
1446
1447/// get all characters before the last occurence of ch
1448/// (returns empty string if ch not found)
1449wxString wxString::BeforeLast(wxChar ch) const
1450{
1451 wxString str;
1452 int iPos = Find(ch, true);
1453 if ( iPos != wxNOT_FOUND && iPos != 0 )
1454 str = wxString(c_str(), iPos);
1455
1456 return str;
1457}
1458
1459/// get all characters after the first occurence of ch
1460/// (returns empty string if ch not found)
1461wxString wxString::AfterFirst(wxChar ch) const
1462{
1463 wxString str;
1464 int iPos = Find(ch);
1465 if ( iPos != wxNOT_FOUND )
1466 str = c_str() + iPos + 1;
1467
1468 return str;
1469}
1470
1471// replace first (or all) occurences of some substring with another one
1472size_t wxString::Replace(const wxChar *szOld,
1473 const wxChar *szNew, bool bReplaceAll)
1474{
1475 // if we tried to replace an empty string we'd enter an infinite loop below
1476 wxCHECK_MSG( szOld && *szOld && szNew, 0,
1477 _T("wxString::Replace(): invalid parameter") );
1478
1479 size_t uiCount = 0; // count of replacements made
1480
1481 size_t uiOldLen = wxStrlen(szOld);
1482 size_t uiNewLen = wxStrlen(szNew);
1483
1484 size_t dwPos = 0;
1485
1486 while ( this->c_str()[dwPos] != wxT('\0') )
1487 {
1488 //DO NOT USE STRSTR HERE
1489 //this string can contain embedded null characters,
1490 //so strstr will function incorrectly
1491 dwPos = find(szOld, dwPos);
1492 if ( dwPos == npos )
1493 break; // exit the loop
1494 else
1495 {
1496 //replace this occurance of the old string with the new one
1497 replace(dwPos, uiOldLen, szNew, uiNewLen);
1498
1499 //move up pos past the string that was replaced
1500 dwPos += uiNewLen;
1501
1502 //increase replace count
1503 ++uiCount;
1504
1505 // stop now?
1506 if ( !bReplaceAll )
1507 break; // exit the loop
1508 }
1509 }
1510
1511 return uiCount;
1512}
1513
1514bool wxString::IsAscii() const
1515{
1516 const wxChar *s = (const wxChar*) *this;
1517 while(*s){
1518 if(!isascii(*s)) return(false);
1519 s++;
1520 }
1521 return(true);
1522}
1523
1524bool wxString::IsWord() const
1525{
1526 const wxChar *s = (const wxChar*) *this;
1527 while(*s){
1528 if(!wxIsalpha(*s)) return(false);
1529 s++;
1530 }
1531 return(true);
1532}
1533
1534bool wxString::IsNumber() const
1535{
1536 const wxChar *s = (const wxChar*) *this;
1537 if (wxStrlen(s))
1538 if ((s[0] == wxT('-')) || (s[0] == wxT('+'))) s++;
1539 while(*s){
1540 if(!wxIsdigit(*s)) return(false);
1541 s++;
1542 }
1543 return(true);
1544}
1545
1546wxString wxString::Strip(stripType w) const
1547{
1548 wxString s = *this;
1549 if ( w & leading ) s.Trim(false);
1550 if ( w & trailing ) s.Trim(true);
1551 return s;
1552}
1553
1554// ---------------------------------------------------------------------------
1555// case conversion
1556// ---------------------------------------------------------------------------
1557
1558wxString& wxString::MakeUpper()
1559{
1560 for ( iterator it = begin(), en = end(); it != en; ++it )
1561 *it = (wxChar)wxToupper(*it);
1562
1563 return *this;
1564}
1565
1566wxString& wxString::MakeLower()
1567{
1568 for ( iterator it = begin(), en = end(); it != en; ++it )
1569 *it = (wxChar)wxTolower(*it);
1570
1571 return *this;
1572}
1573
1574// ---------------------------------------------------------------------------
1575// trimming and padding
1576// ---------------------------------------------------------------------------
1577
1578// some compilers (VC++ 6.0 not to name them) return true for a call to
1579