]> git.saurik.com Git - wxWidgets.git/blame - src/common/stringimpl.cpp
non-pch build fix
[wxWidgets.git] / src / common / stringimpl.cpp
CommitLineData
a7ea63e2 1/////////////////////////////////////////////////////////////////////////////
81727065 2// Name: src/common/stringimpl.cpp
a7ea63e2
VS
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/stringimpl.h"
d2384936 33 #include "wx/wxcrt.h"
a7ea63e2
VS
34#endif
35
36#include <ctype.h>
37
38#ifndef __WXWINCE__
39 #include <errno.h>
40#endif
41
42#include <string.h>
43#include <stdlib.h>
44
45#ifdef __SALFORDC__
46 #include <clib.h>
47#endif
48
49// allocating extra space for each string consumes more memory but speeds up
50// the concatenation operations (nLen is the current string's length)
51// NB: EXTRA_ALLOC must be >= 0!
52#define EXTRA_ALLOC (19 - nLen % 16)
53
54
55// string handling functions used by wxString:
56#if wxUSE_UNICODE_UTF8
57 #define wxStringMemcpy memcpy
58 #define wxStringMemcmp memcmp
59 #define wxStringMemchr memchr
a7ea63e2
VS
60#else
61 #define wxStringMemcpy wxTmemcpy
62 #define wxStringMemcmp wxTmemcmp
63 #define wxStringMemchr wxTmemchr
a7ea63e2
VS
64#endif
65
66
67// ---------------------------------------------------------------------------
68// static class variables definition
69// ---------------------------------------------------------------------------
70
71#if !wxUSE_STL_BASED_WXSTRING
72//According to STL _must_ be a -1 size_t
73const size_t wxStringImpl::npos = (size_t) -1;
74#endif
75
76// ----------------------------------------------------------------------------
77// static data
78// ----------------------------------------------------------------------------
79
80#if wxUSE_STL_BASED_WXSTRING
81
81727065
VS
82// FIXME-UTF8: get rid of this, have only one wxEmptyString
83#if wxUSE_UNICODE_UTF8
84extern const wxStringCharType WXDLLIMPEXP_BASE *wxEmptyStringImpl = "";
85#endif
a7ea63e2
VS
86extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T("");
87
88#else
89
90// for an empty string, GetStringData() will return this address: this
91// structure has the same layout as wxStringData and it's data() method will
92// return the empty string (dummy pointer)
93static const struct
94{
95 wxStringData data;
81727065 96 wxStringCharType dummy;
a7ea63e2
VS
97} g_strEmpty = { {-1, 0, 0}, wxT('\0') };
98
99// empty C style string: points to 'string data' byte of g_strEmpty
81727065
VS
100#if wxUSE_UNICODE_UTF8
101// FIXME-UTF8: get rid of this, have only one wxEmptyString
102extern const wxStringCharType WXDLLIMPEXP_BASE *wxEmptyStringImpl = &g_strEmpty.dummy;
103extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T("");
104#else
105extern const wxStringCharType WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy;
106#endif
a7ea63e2
VS
107
108#endif
109
110
111#if !wxUSE_STL_BASED_WXSTRING
112
113// ----------------------------------------------------------------------------
114// private classes
115// ----------------------------------------------------------------------------
116
117// this small class is used to gather statistics for performance tuning
118//#define WXSTRING_STATISTICS
119#ifdef WXSTRING_STATISTICS
120 class Averager
121 {
122 public:
81727065 123 Averager(const wxStringCharType *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
a7ea63e2
VS
124 ~Averager()
125 { wxPrintf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
126
127 void Add(size_t n) { m_nTotal += n; m_nCount++; }
128
129 private:
130 size_t m_nCount, m_nTotal;
81727065 131 const wxStringCharType *m_sz;
a7ea63e2
VS
132 } g_averageLength("allocation size"),
133 g_averageSummandLength("summand length"),
134 g_averageConcatHit("hit probability in concat"),
135 g_averageInitialLength("initial string length");
136
137 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
138#else
139 #define STATISTICS_ADD(av, val)
140#endif // WXSTRING_STATISTICS
141
142// ===========================================================================
143// wxStringData class deallocation
144// ===========================================================================
145
146#if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
147# pragma message (__FILE__ ": building with Multithreaded non DLL runtime has a performance impact on wxString!")
148void wxStringData::Free()
149{
150 free(this);
151}
152#endif
153
154// ===========================================================================
155// wxStringImpl
156// ===========================================================================
157
158// takes nLength elements of psz starting at nPos
81727065
VS
159void wxStringImpl::InitWith(const wxStringCharType *psz,
160 size_t nPos, size_t nLength)
a7ea63e2
VS
161{
162 Init();
163
164 // if the length is not given, assume the string to be NUL terminated
165 if ( nLength == npos ) {
52de37c7 166 wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") );
a7ea63e2 167
52de37c7 168 nLength = wxStrlen(psz + nPos);
a7ea63e2
VS
169 }
170
171 STATISTICS_ADD(InitialLength, nLength);
172
173 if ( nLength > 0 ) {
174 // trailing '\0' is written in AllocBuffer()
175 if ( !AllocBuffer(nLength) ) {
176 wxFAIL_MSG( _T("out of memory in wxStringImpl::InitWith") );
177 return;
178 }
179 wxStringMemcpy(m_pchData, psz + nPos, nLength);
180 }
181}
182
f2a1b1bd 183wxStringImpl::wxStringImpl(const_iterator first, const_iterator last)
a7ea63e2 184{
f2a1b1bd 185 if ( last >= first )
a7ea63e2 186 {
f2a1b1bd 187 InitWith(first, 0, last - first);
a7ea63e2
VS
188 }
189 else
190 {
f2a1b1bd 191 wxFAIL_MSG( _T("first must be before last") );
a7ea63e2
VS
192 Init();
193 }
194}
195
196wxStringImpl::wxStringImpl(size_type n, wxStringCharType ch)
197{
198 Init();
199 append(n, ch);
200}
201
202// ---------------------------------------------------------------------------
203// memory allocation
204// ---------------------------------------------------------------------------
205
206// allocates memory needed to store a C string of length nLen
207bool wxStringImpl::AllocBuffer(size_t nLen)
208{
209 // allocating 0 sized buffer doesn't make sense, all empty strings should
210 // reuse g_strEmpty
211 wxASSERT( nLen > 0 );
212
213 // make sure that we don't overflow
81727065 214 wxASSERT( nLen < (INT_MAX / sizeof(wxStringCharType)) -
a7ea63e2
VS
215 (sizeof(wxStringData) + EXTRA_ALLOC + 1) );
216
217 STATISTICS_ADD(Length, nLen);
218
219 // allocate memory:
220 // 1) one extra character for '\0' termination
221 // 2) sizeof(wxStringData) for housekeeping info
222 wxStringData* pData = (wxStringData*)
81727065 223 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxStringCharType));
a7ea63e2
VS
224
225 if ( pData == NULL ) {
226 // allocation failures are handled by the caller
227 return false;
228 }
229
230 pData->nRefs = 1;
231 pData->nDataLength = nLen;
232 pData->nAllocLength = nLen + EXTRA_ALLOC;
233 m_pchData = pData->data(); // data starts after wxStringData
234 m_pchData[nLen] = wxT('\0');
235 return true;
236}
237
238// must be called before changing this string
239bool wxStringImpl::CopyBeforeWrite()
240{
241 wxStringData* pData = GetStringData();
242
243 if ( pData->IsShared() ) {
244 pData->Unlock(); // memory not freed because shared
245 size_t nLen = pData->nDataLength;
246 if ( !AllocBuffer(nLen) ) {
247 // allocation failures are handled by the caller
248 return false;
249 }
250 wxStringMemcpy(m_pchData, pData->data(), nLen);
251 }
252
253 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
254
255 return true;
256}
257
258// must be called before replacing contents of this string
259bool wxStringImpl::AllocBeforeWrite(size_t nLen)
260{
261 wxASSERT( nLen != 0 ); // doesn't make any sense
262
263 // must not share string and must have enough space
264 wxStringData* pData = GetStringData();
265 if ( pData->IsShared() || pData->IsEmpty() ) {
266 // can't work with old buffer, get new one
267 pData->Unlock();
268 if ( !AllocBuffer(nLen) ) {
269 // allocation failures are handled by the caller
270 return false;
271 }
272 }
273 else {
274 if ( nLen > pData->nAllocLength ) {
275 // realloc the buffer instead of calling malloc() again, this is more
276 // efficient
277 STATISTICS_ADD(Length, nLen);
278
279 nLen += EXTRA_ALLOC;
280
281 pData = (wxStringData*)
81727065
VS
282 realloc(pData,
283 sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType));
a7ea63e2
VS
284
285 if ( pData == NULL ) {
286 // allocation failures are handled by the caller
287 // keep previous data since reallocation failed
288 return false;
289 }
290
291 pData->nAllocLength = nLen;
292 m_pchData = pData->data();
293 }
294 }
295
296 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
297
298 // it doesn't really matter what the string length is as it's going to be
299 // overwritten later but, for extra safety, set it to 0 for now as we may
300 // have some junk in m_pchData
301 GetStringData()->nDataLength = 0;
302
303 return true;
304}
305
306wxStringImpl& wxStringImpl::append(size_t n, wxStringCharType ch)
307{
308 size_type len = length();
309
310 if ( !Alloc(len + n) || !CopyBeforeWrite() ) {
311 wxFAIL_MSG( _T("out of memory in wxStringImpl::append") );
6859e6fc 312 return *this;
a7ea63e2
VS
313 }
314 GetStringData()->nDataLength = len + n;
315 m_pchData[len + n] = '\0';
316 for ( size_t i = 0; i < n; ++i )
317 m_pchData[len + i] = ch;
318 return *this;
319}
320
321void wxStringImpl::resize(size_t nSize, wxStringCharType ch)
322{
323 size_t len = length();
324
325 if ( nSize < len )
326 {
327 erase(begin() + nSize, end());
328 }
329 else if ( nSize > len )
330 {
331 append(nSize - len, ch);
332 }
333 //else: we have exactly the specified length, nothing to do
334}
335
336// allocate enough memory for nLen characters
337bool wxStringImpl::Alloc(size_t nLen)
338{
339 wxStringData *pData = GetStringData();
340 if ( pData->nAllocLength <= nLen ) {
341 if ( pData->IsEmpty() ) {
342 nLen += EXTRA_ALLOC;
343
344 pData = (wxStringData *)
81727065 345 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType));
a7ea63e2
VS
346
347 if ( pData == NULL ) {
348 // allocation failure handled by caller
349 return false;
350 }
351
352 pData->nRefs = 1;
353 pData->nDataLength = 0;
354 pData->nAllocLength = nLen;
355 m_pchData = pData->data(); // data starts after wxStringData
356 m_pchData[0u] = wxT('\0');
357 }
358 else if ( pData->IsShared() ) {
359 pData->Unlock(); // memory not freed because shared
360 size_t nOldLen = pData->nDataLength;
361 if ( !AllocBuffer(nLen) ) {
362 // allocation failure handled by caller
363 return false;
364 }
365 // +1 to copy the terminator, too
81727065 366 memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxStringCharType));
a7ea63e2
VS
367 GetStringData()->nDataLength = nOldLen;
368 }
369 else {
370 nLen += EXTRA_ALLOC;
371
372 pData = (wxStringData *)
81727065 373 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType));
a7ea63e2
VS
374
375 if ( pData == NULL ) {
376 // allocation failure handled by caller
377 // keep previous data since reallocation failed
378 return false;
379 }
380
381 // it's not important if the pointer changed or not (the check for this
382 // is not faster than assigning to m_pchData in all cases)
383 pData->nAllocLength = nLen;
384 m_pchData = pData->data();
385 }
386 }
387 //else: we've already got enough
388 return true;
389}
390
391wxStringImpl::iterator wxStringImpl::begin()
392{
393 if (length() > 0)
394 CopyBeforeWrite();
395 return m_pchData;
396}
397
398wxStringImpl::iterator wxStringImpl::end()
399{
400 if (length() > 0)
401 CopyBeforeWrite();
402 return m_pchData + length();
403}
404
405wxStringImpl::iterator wxStringImpl::erase(iterator it)
406{
407 size_type idx = it - begin();
408 erase(idx, 1);
409 return begin() + idx;
410}
411
412wxStringImpl& wxStringImpl::erase(size_t nStart, size_t nLen)
413{
414 wxASSERT(nStart <= length());
415 size_t strLen = length() - nStart;
416 // delete nLen or up to the end of the string characters
417 nLen = strLen < nLen ? strLen : nLen;
418 wxStringImpl strTmp(c_str(), nStart);
419 strTmp.append(c_str() + nStart + nLen, length() - nStart - nLen);
420
421 swap(strTmp);
422 return *this;
423}
424
81727065
VS
425wxStringImpl& wxStringImpl::insert(size_t nPos,
426 const wxStringCharType *sz, size_t n)
a7ea63e2
VS
427{
428 wxASSERT( nPos <= length() );
429
52de37c7 430 if ( n == npos ) n = wxStrlen(sz);
a7ea63e2
VS
431 if ( n == 0 ) return *this;
432
433 if ( !Alloc(length() + n) || !CopyBeforeWrite() ) {
434 wxFAIL_MSG( _T("out of memory in wxStringImpl::insert") );
6859e6fc 435 return *this;
a7ea63e2
VS
436 }
437
438 memmove(m_pchData + nPos + n, m_pchData + nPos,
81727065
VS
439 (length() - nPos) * sizeof(wxStringCharType));
440 memcpy(m_pchData + nPos, sz, n * sizeof(wxStringCharType));
a7ea63e2
VS
441 GetStringData()->nDataLength = length() + n;
442 m_pchData[length()] = '\0';
443
444 return *this;
445}
446
447void wxStringImpl::swap(wxStringImpl& str)
448{
449 wxStringCharType* tmp = str.m_pchData;
450 str.m_pchData = m_pchData;
451 m_pchData = tmp;
452}
453
454size_t wxStringImpl::find(const wxStringImpl& str, size_t nStart) const
455{
456 // deal with the special case of empty string first
457 const size_t nLen = length();
458 const size_t nLenOther = str.length();
459
460 if ( !nLenOther )
461 {
462 // empty string is a substring of anything
463 return 0;
464 }
465
466 if ( !nLen )
467 {
468 // the other string is non empty so can't be our substring
469 return npos;
470 }
471
472 wxASSERT( str.GetStringData()->IsValid() );
473 wxASSERT( nStart <= nLen );
474
475 const wxStringCharType * const other = str.c_str();
476
477 // anchor
478 const wxStringCharType* p =
479 (const wxStringCharType*)wxStringMemchr(c_str() + nStart,
480 *other,
481 nLen - nStart);
482
483 if ( !p )
484 return npos;
485
486 while ( p - c_str() + nLenOther <= nLen &&
487 wxStringMemcmp(p, other, nLenOther) )
488 {
489 p++;
490
491 // anchor again
492 p = (const wxStringCharType*)
493 wxStringMemchr(p, *other, nLen - (p - c_str()));
494
495 if ( !p )
496 return npos;
497 }
498
499 return p - c_str() + nLenOther <= nLen ? p - c_str() : npos;
500}
501
81727065
VS
502size_t wxStringImpl::find(const wxStringCharType* sz,
503 size_t nStart, size_t n) const
a7ea63e2
VS
504{
505 return find(wxStringImpl(sz, n), nStart);
506}
507
508size_t wxStringImpl::find(wxStringCharType ch, size_t nStart) const
509{
510 wxASSERT( nStart <= length() );
511
512 const wxStringCharType *p = (const wxStringCharType*)
513 wxStringMemchr(c_str() + nStart, ch, length() - nStart);
514
515 return p == NULL ? npos : p - c_str();
516}
517
518size_t wxStringImpl::rfind(const wxStringImpl& str, size_t nStart) const
519{
520 wxASSERT( str.GetStringData()->IsValid() );
521 wxASSERT( nStart == npos || nStart <= length() );
522
523 if ( length() >= str.length() )
524 {
525 // avoids a corner case later
526 if ( length() == 0 && str.length() == 0 )
527 return 0;
528
529 // "top" is the point where search starts from
530 size_t top = length() - str.length();
531
532 if ( nStart == npos )
533 nStart = length() - 1;
534 if ( nStart < top )
535 top = nStart;
536
537 const wxStringCharType *cursor = c_str() + top;
538 do
539 {
540 if ( wxStringMemcmp(cursor, str.c_str(), str.length()) == 0 )
541 {
542 return cursor - c_str();
543 }
544 } while ( cursor-- > c_str() );
545 }
546
547 return npos;
548}
549
81727065
VS
550size_t wxStringImpl::rfind(const wxStringCharType* sz,
551 size_t nStart, size_t n) const
a7ea63e2
VS
552{
553 return rfind(wxStringImpl(sz, n), nStart);
554}
555
556size_t wxStringImpl::rfind(wxStringCharType ch, size_t nStart) const
557{
558 if ( nStart == npos )
559 {
560 nStart = length();
561 }
562 else
563 {
564 wxASSERT( nStart <= length() );
565 }
566
567 const wxStringCharType *actual;
568 for ( actual = c_str() + ( nStart == npos ? length() : nStart + 1 );
569 actual > c_str(); --actual )
570 {
571 if ( *(actual - 1) == ch )
572 return (actual - 1) - c_str();
573 }
574
575 return npos;
576}
577
578wxStringImpl& wxStringImpl::replace(size_t nStart, size_t nLen,
d545bded 579 const wxStringCharType *sz, size_t nCount)
a7ea63e2 580{
d545bded
VZ
581 // check and adjust parameters
582 const size_t lenOld = length();
a7ea63e2 583
d545bded
VZ
584 wxASSERT_MSG( nStart <= lenOld,
585 _T("index out of bounds in wxStringImpl::replace") );
586 size_t nEnd = nStart + nLen;
dfaae3e6 587 if ( nLen > lenOld - nStart )
d545bded
VZ
588 {
589 // nLen may be out of range, as it can be npos, just clump it down
590 nLen = lenOld - nStart;
591 nEnd = lenOld;
592 }
a7ea63e2 593
d545bded
VZ
594 if ( nCount == npos )
595 nCount = wxStrlen(sz);
a7ea63e2 596
d545bded
VZ
597 // build the new string from 3 pieces: part of this string before nStart,
598 // the new substring and the part of this string after nStart+nLen
599 wxStringImpl tmp;
600 const size_t lenNew = lenOld + nCount - nLen;
601 if ( lenNew )
602 {
603 tmp.AllocBuffer(lenOld + nCount - nLen);
604
605 wxStringCharType *dst = tmp.m_pchData;
606 memcpy(dst, m_pchData, nStart*sizeof(wxStringCharType));
607 dst += nStart;
608
609 memcpy(dst, sz, nCount*sizeof(wxStringCharType));
610 dst += nCount;
611
612 memcpy(dst, m_pchData + nEnd, (lenOld - nEnd)*sizeof(wxStringCharType));
613 }
614
615 // and replace this string contents with the new one
616 swap(tmp);
617 return *this;
a7ea63e2
VS
618}
619
620wxStringImpl wxStringImpl::substr(size_t nStart, size_t nLen) const
621{
622 if ( nLen == npos )
623 nLen = length() - nStart;
624 return wxStringImpl(*this, nStart, nLen);
625}
626
627// assigns one string to another
628wxStringImpl& wxStringImpl::operator=(const wxStringImpl& stringSrc)
629{
630 wxASSERT( stringSrc.GetStringData()->IsValid() );
631
632 // don't copy string over itself
633 if ( m_pchData != stringSrc.m_pchData ) {
634 if ( stringSrc.GetStringData()->IsEmpty() ) {
635 Reinit();
636 }
637 else {
638 // adjust references
639 GetStringData()->Unlock();
640 m_pchData = stringSrc.m_pchData;
641 GetStringData()->Lock();
642 }
643 }
644
645 return *this;
646}
647
648// assigns a single character
649wxStringImpl& wxStringImpl::operator=(wxStringCharType ch)
650{
81727065 651 wxStringCharType c(ch);
a7ea63e2 652 if ( !AssignCopy(1, &c) ) {
81727065 653 wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(wxStringCharType)") );
a7ea63e2
VS
654 }
655 return *this;
656}
657
658// assigns C string
81727065 659wxStringImpl& wxStringImpl::operator=(const wxStringCharType *psz)
a7ea63e2 660{
52de37c7 661 if ( !AssignCopy(wxStrlen(psz), psz) ) {
81727065 662 wxFAIL_MSG( _T("out of memory in wxStringImpl::operator=(const wxStringCharType *)") );
a7ea63e2
VS
663 }
664 return *this;
665}
666
667// helper function: does real copy
81727065
VS
668bool wxStringImpl::AssignCopy(size_t nSrcLen,
669 const wxStringCharType *pszSrcData)
a7ea63e2
VS
670{
671 if ( nSrcLen == 0 ) {
672 Reinit();
673 }
674 else {
675 if ( !AllocBeforeWrite(nSrcLen) ) {
676 // allocation failure handled by caller
677 return false;
678 }
81727065 679 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxStringCharType));
a7ea63e2
VS
680 GetStringData()->nDataLength = nSrcLen;
681 m_pchData[nSrcLen] = wxT('\0');
682 }
683 return true;
684}
685
686// ---------------------------------------------------------------------------
687// string concatenation
688// ---------------------------------------------------------------------------
689
690// add something to this string
81727065
VS
691bool wxStringImpl::ConcatSelf(size_t nSrcLen,
692 const wxStringCharType *pszSrcData,
a7ea63e2
VS
693 size_t nMaxLen)
694{
695 STATISTICS_ADD(SummandLength, nSrcLen);
696
697 nSrcLen = nSrcLen < nMaxLen ? nSrcLen : nMaxLen;
698
699 // concatenating an empty string is a NOP
700 if ( nSrcLen > 0 ) {
701 wxStringData *pData = GetStringData();
702 size_t nLen = pData->nDataLength;
d545bded
VZ
703
704 // take special care when appending part of this string to itself: the code
705 // below reallocates our buffer and this invalidates pszSrcData pointer so
706 // we have to copy it in another temporary string in this case (but avoid
707 // doing this unnecessarily)
708 if ( pszSrcData >= m_pchData && pszSrcData < m_pchData + nLen )
709 {
710 wxStringImpl tmp(pszSrcData, nSrcLen);
711 return ConcatSelf(nSrcLen, tmp.m_pchData, nSrcLen);
712 }
713
a7ea63e2
VS
714 size_t nNewLen = nLen + nSrcLen;
715
716 // alloc new buffer if current is too small
717 if ( pData->IsShared() ) {
718 STATISTICS_ADD(ConcatHit, 0);
719
720 // we have to allocate another buffer
721 wxStringData* pOldData = GetStringData();
722 if ( !AllocBuffer(nNewLen) ) {
723 // allocation failure handled by caller
724 return false;
725 }
81727065 726 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxStringCharType));
a7ea63e2
VS
727 pOldData->Unlock();
728 }
729 else if ( nNewLen > pData->nAllocLength ) {
730 STATISTICS_ADD(ConcatHit, 0);
731
732 reserve(nNewLen);
733 // we have to grow the buffer
734 if ( capacity() < nNewLen ) {
735 // allocation failure handled by caller
736 return false;
737 }
738 }
739 else {
740 STATISTICS_ADD(ConcatHit, 1);
741
742 // the buffer is already big enough
743 }
744
745 // should be enough space
746 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
747
748 // fast concatenation - all is done in our buffer
81727065 749 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxStringCharType));
a7ea63e2
VS
750
751 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
752 GetStringData()->nDataLength = nNewLen; // and fix the length
753 }
754 //else: the string to append was empty
755 return true;
756}
757
a7ea63e2 758// get the pointer to writable buffer of (at least) nLen bytes
c87a0bc8 759wxStringCharType *wxStringImpl::DoGetWriteBuf(size_t nLen)
a7ea63e2
VS
760{
761 if ( !AllocBeforeWrite(nLen) ) {
762 // allocation failure handled by caller
763 return NULL;
764 }
765
766 wxASSERT( GetStringData()->nRefs == 1 );
767 GetStringData()->Validate(false);
768
769 return m_pchData;
770}
771
772// put string back in a reasonable state after GetWriteBuf
773void wxStringImpl::DoUngetWriteBuf()
774{
52de37c7 775 DoUngetWriteBuf(wxStrlen(m_pchData));
a7ea63e2
VS
776}
777
778void wxStringImpl::DoUngetWriteBuf(size_t nLen)
779{
780 wxStringData * const pData = GetStringData();
781
782 wxASSERT_MSG( nLen < pData->nAllocLength, _T("buffer overrun") );
783
784 // the strings we store are always NUL-terminated
785 pData->data()[nLen] = _T('\0');
786 pData->nDataLength = nLen;
787 pData->Validate(true);
788}
a7ea63e2
VS
789
790#endif // !wxUSE_STL_BASED_WXSTRING