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