]> git.saurik.com Git - wxWidgets.git/blame - src/common/string.cpp
resource loader structure added for loading menu bar, apple menu will not get multipl...
[wxWidgets.git] / src / common / string.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: string.cpp
3// Purpose: wxString class
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
dd1eaa89 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
30b21f9a 13 #pragma implementation "string.h"
c801d85f
KB
14#endif
15
16/*
17 * About ref counting:
18 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
19 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
20 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
21 */
22
23// ===========================================================================
24// headers, declarations, constants
25// ===========================================================================
26
27// For compilers that support precompilation, includes "wx.h".
28#include "wx/wxprec.h"
29
30#ifdef __BORLANDC__
30b21f9a 31 #pragma hdrstop
c801d85f
KB
32#endif
33
34#ifndef WX_PRECOMP
3c024cc2
VZ
35 #include "wx/defs.h"
36 #include "wx/string.h"
37 #include "wx/intl.h"
3096bd2f 38 #include "wx/thread.h"
6b769f3d 39#endif
c801d85f
KB
40
41#include <ctype.h>
42#include <string.h>
43#include <stdlib.h>
44
ce3ed50d 45#ifdef __SALFORDC__
30b21f9a 46 #include <clib.h>
ce3ed50d
JS
47#endif
48
c801d85f
KB
49#ifdef WXSTRING_IS_WXOBJECT
50 IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject)
51#endif //WXSTRING_IS_WXOBJECT
52
ec2ba3aa
OK
53#if wxUSE_UNICODE
54#undef wxUSE_EXPERIMENTAL_PRINTF
55#define wxUSE_EXPERIMENTAL_PRINTF 1
56#endif
57
3168a13f
VZ
58// allocating extra space for each string consumes more memory but speeds up
59// the concatenation operations (nLen is the current string's length)
77ca46e7
VZ
60// NB: EXTRA_ALLOC must be >= 0!
61#define EXTRA_ALLOC (19 - nLen % 16)
3168a13f 62
c801d85f
KB
63// ---------------------------------------------------------------------------
64// static class variables definition
65// ---------------------------------------------------------------------------
66
75ad8feb
DW
67#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
68// must define this static for VA or else you get multiply defined symbols
69// everywhere
70const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100;
71#endif // Visual Age
72
8de2e39c 73#ifdef wxSTD_STRING_COMPATIBILITY
566b84d2 74 const size_t wxString::npos = wxSTRING_MAXLEN;
8de2e39c 75#endif // wxSTD_STRING_COMPATIBILITY
c801d85f 76
3168a13f
VZ
77// ----------------------------------------------------------------------------
78// static data
79// ----------------------------------------------------------------------------
c801d85f 80
3c024cc2
VZ
81// for an empty string, GetStringData() will return this address: this
82// structure has the same layout as wxStringData and it's data() method will
83// return the empty string (dummy pointer)
84static const struct
85{
86 wxStringData data;
2bb67b80 87 wxChar dummy;
223d09f6 88} g_strEmpty = { {-1, 0, 0}, wxT('\0') };
3c024cc2 89
c801d85f 90// empty C style string: points to 'string data' byte of g_strEmpty
e90c1d2a 91extern const wxChar WXDLLEXPORT *wxEmptyString = &g_strEmpty.dummy;
c801d85f 92
89b892a2
VZ
93// ----------------------------------------------------------------------------
94// conditional compilation
95// ----------------------------------------------------------------------------
96
dcf924a3
RR
97#if !defined(__WXSW__) && wxUSE_UNICODE
98 #ifdef wxUSE_EXPERIMENTAL_PRINTF
99 #undef wxUSE_EXPERIMENTAL_PRINTF
100 #endif
101 #define wxUSE_EXPERIMENTAL_PRINTF 1
102#endif
103
89b892a2
VZ
104// we want to find out if the current platform supports vsnprintf()-like
105// function: for Unix this is done with configure, for Windows we test the
106// compiler explicitly.
378b05f7
VZ
107//
108// FIXME currently, this is only for ANSI (!Unicode) strings, so we call this
109// function wxVsnprintfA (A for ANSI), should also find one for Unicode
110// strings in Unicode build
89b892a2 111#ifdef __WXMSW__
012286eb 112 #if defined(__VISUALC__) || (defined(__MINGW32__) && wxUSE_NORLANDER_HEADERS)
378b05f7 113 #define wxVsnprintfA _vsnprintf
89b892a2 114 #endif
03e11df5
GD
115#elif defined(__WXMAC__)
116 #define wxVsnprintfA vsnprintf
89b892a2
VZ
117#else // !Windows
118 #ifdef HAVE_VSNPRINTF
378b05f7 119 #define wxVsnprintfA vsnprintf
89b892a2
VZ
120 #endif
121#endif // Windows/!Windows
122
378b05f7 123#ifndef wxVsnprintfA
89b892a2
VZ
124 // in this case we'll use vsprintf() (which is ANSI and thus should be
125 // always available), but it's unsafe because it doesn't check for buffer
126 // size - so give a warning
378b05f7 127 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
566b84d2 128
57493f9f
VZ
129 #if defined(__VISUALC__)
130 #pragma message("Using sprintf() because no snprintf()-like function defined")
03e11df5 131 #elif defined(__GNUG__)
378b05f7 132 #warning "Using sprintf() because no snprintf()-like function defined"
57493f9f 133 #endif //compiler
3f4a0c5b 134#endif // no vsnprintf
89b892a2 135
783fab59 136#if defined(_AIX)
227b5cd7
VZ
137 // AIX has vsnprintf, but there's no prototype in the system headers.
138 extern "C" int vsnprintf(char* str, size_t n, const char* format, va_list ap);
139#endif
140
3168a13f 141// ----------------------------------------------------------------------------
c801d85f 142// global functions
3168a13f 143// ----------------------------------------------------------------------------
c801d85f 144
a533f5c1 145#if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
c801d85f
KB
146
147// MS Visual C++ version 5.0 provides the new STL headers as well as the old
148// iostream ones.
149//
150// ATTN: you can _not_ use both of these in the same program!
a38b83c3 151
dd107c50 152wxSTD istream& operator>>(wxSTD istream& is, wxString& WXUNUSED(str))
c801d85f
KB
153{
154#if 0
155 int w = is.width(0);
156 if ( is.ipfx(0) ) {
3f4a0c5b 157 streambuf *sb = is.rdbuf();
c801d85f
KB
158 str.erase();
159 while ( true ) {
160 int ch = sb->sbumpc ();
161 if ( ch == EOF ) {
3f4a0c5b 162 is.setstate(ios::eofbit);
c801d85f
KB
163 break;
164 }
165 else if ( isspace(ch) ) {
166 sb->sungetc();
167 break;
168 }
dd1eaa89 169
c801d85f
KB
170 str += ch;
171 if ( --w == 1 )
172 break;
173 }
174 }
175
176 is.isfx();
177 if ( str.length() == 0 )
3f4a0c5b 178 is.setstate(ios::failbit);
c801d85f
KB
179#endif
180 return is;
181}
182
dd107c50 183wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
825ba8f0
SB
184{
185 os << str.c_str();
186 return os;
187}
188
c801d85f
KB
189#endif //std::string compatibility
190
378b05f7
VZ
191extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len,
192 const wxChar *format, va_list argptr)
193{
194#if wxUSE_UNICODE
195 // FIXME should use wvsnprintf() or whatever if it's available
196 wxString s;
197 int iLen = s.PrintfV(format, argptr);
198 if ( iLen != -1 )
199 {
7f017c64
VZ
200 wxStrncpy(buf, s.c_str(), len);
201 buf[len-1] = wxT('\0');
378b05f7
VZ
202 }
203
204 return iLen;
205#else // ANSI
b568d04f
VZ
206 // vsnprintf() will not terminate the string with '\0' if there is not
207 // enough place, but we want the string to always be NUL terminated
208 int rc = wxVsnprintfA(buf, len - 1, format, argptr);
2f02cb89
VZ
209 if ( rc == -1 )
210 {
211 buf[len] = 0;
212 }
b568d04f
VZ
213
214 return rc;
378b05f7
VZ
215#endif // Unicode/ANSI
216}
217
218extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len,
219 const wxChar *format, ...)
220{
221 va_list argptr;
222 va_start(argptr, format);
223
224 int iLen = wxVsnprintf(buf, len, format, argptr);
225
226 va_end(argptr);
227
228 return iLen;
229}
230
3168a13f
VZ
231// ----------------------------------------------------------------------------
232// private classes
233// ----------------------------------------------------------------------------
234
235// this small class is used to gather statistics for performance tuning
236//#define WXSTRING_STATISTICS
237#ifdef WXSTRING_STATISTICS
238 class Averager
239 {
240 public:
241 Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
2c3b684c 242 ~Averager()
3168a13f
VZ
243 { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
244
c86f1403 245 void Add(size_t n) { m_nTotal += n; m_nCount++; }
3168a13f
VZ
246
247 private:
c86f1403 248 size_t m_nCount, m_nTotal;
3168a13f
VZ
249 const char *m_sz;
250 } g_averageLength("allocation size"),
251 g_averageSummandLength("summand length"),
252 g_averageConcatHit("hit probability in concat"),
253 g_averageInitialLength("initial string length");
254
255 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
256#else
257 #define STATISTICS_ADD(av, val)
258#endif // WXSTRING_STATISTICS
259
c801d85f
KB
260// ===========================================================================
261// wxString class core
262// ===========================================================================
263
264// ---------------------------------------------------------------------------
265// construction
266// ---------------------------------------------------------------------------
267
c801d85f 268// constructs string of <nLength> copies of character <ch>
2bb67b80 269wxString::wxString(wxChar ch, size_t nLength)
c801d85f
KB
270{
271 Init();
272
273 if ( nLength > 0 ) {
274 AllocBuffer(nLength);
f1da2f03 275
2bb67b80
OK
276#if wxUSE_UNICODE
277 // memset only works on char
278 for (size_t n=0; n<nLength; n++) m_pchData[n] = ch;
279#else
c801d85f 280 memset(m_pchData, ch, nLength);
2bb67b80 281#endif
c801d85f
KB
282 }
283}
284
285// takes nLength elements of psz starting at nPos
2bb67b80 286void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
c801d85f
KB
287{
288 Init();
289
f6bcfd97
BP
290 // if the length is not given, assume the string to be NUL terminated
291 if ( nLength == wxSTRING_MAXLEN ) {
292 wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") );
c801d85f 293
f6bcfd97
BP
294 nLength = wxStrlen(psz + nPos);
295 }
6c68273f 296
3168a13f
VZ
297 STATISTICS_ADD(InitialLength, nLength);
298
c801d85f
KB
299 if ( nLength > 0 ) {
300 // trailing '\0' is written in AllocBuffer()
301 AllocBuffer(nLength);
2bb67b80 302 memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar));
c801d85f
KB
303 }
304}
dd1eaa89 305
8de2e39c 306#ifdef wxSTD_STRING_COMPATIBILITY
c801d85f 307
c801d85f
KB
308// poor man's iterators are "void *" pointers
309wxString::wxString(const void *pStart, const void *pEnd)
310{
2bb67b80
OK
311 InitWith((const wxChar *)pStart, 0,
312 (const wxChar *)pEnd - (const wxChar *)pStart);
c801d85f
KB
313}
314
315#endif //std::string compatibility
316
2bb67b80
OK
317#if wxUSE_UNICODE
318
319// from multibyte string
cf2f341a 320wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
2bb67b80
OK
321{
322 // first get necessary size
435595e0 323 size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
2bb67b80
OK
324
325 // nLength is number of *Unicode* characters here!
eea4f86a 326 if ((nLen != (size_t)-1) && (nLen > nLength))
2bb67b80
OK
327 nLen = nLength;
328
329 // empty?
eea4f86a 330 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
2bb67b80
OK
331 AllocBuffer(nLen);
332 conv.MB2WC(m_pchData, psz, nLen);
333 }
334 else {
335 Init();
336 }
337}
338
e90c1d2a 339#else // ANSI
2bb67b80 340
0f3e3e0c 341#if wxUSE_WCHAR_T
c801d85f 342// from wide string
f6bcfd97 343wxString::wxString(const wchar_t *pwz, wxMBConv& conv)
c801d85f
KB
344{
345 // first get necessary size
f6bcfd97 346 size_t nLen = pwz ? conv.WC2MB((char *) NULL, pwz, 0) : 0;
c801d85f
KB
347
348 // empty?
eea4f86a 349 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
c801d85f 350 AllocBuffer(nLen);
f6bcfd97 351 conv.WC2MB(m_pchData, pwz, nLen);
c801d85f
KB
352 }
353 else {
354 Init();
355 }
356}
e90c1d2a 357#endif // wxUSE_WCHAR_T
c801d85f 358
e90c1d2a 359#endif // Unicode/ANSI
2bb67b80 360
c801d85f
KB
361// ---------------------------------------------------------------------------
362// memory allocation
363// ---------------------------------------------------------------------------
364
365// allocates memory needed to store a C string of length nLen
366void wxString::AllocBuffer(size_t nLen)
367{
13111b2a
VZ
368 // allocating 0 sized buffer doesn't make sense, all empty strings should
369 // reuse g_strEmpty
370 wxASSERT( nLen > 0 );
371
372 // make sure that we don't overflow
373 wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) -
374 (sizeof(wxStringData) + EXTRA_ALLOC + 1) );
c801d85f 375
3168a13f
VZ
376 STATISTICS_ADD(Length, nLen);
377
c801d85f
KB
378 // allocate memory:
379 // 1) one extra character for '\0' termination
380 // 2) sizeof(wxStringData) for housekeeping info
3168a13f 381 wxStringData* pData = (wxStringData*)
2bb67b80 382 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
c801d85f 383 pData->nRefs = 1;
c801d85f 384 pData->nDataLength = nLen;
3168a13f 385 pData->nAllocLength = nLen + EXTRA_ALLOC;
c801d85f 386 m_pchData = pData->data(); // data starts after wxStringData
223d09f6 387 m_pchData[nLen] = wxT('\0');
c801d85f
KB
388}
389
c801d85f
KB
390// must be called before changing this string
391void wxString::CopyBeforeWrite()
392{
393 wxStringData* pData = GetStringData();
394
395 if ( pData->IsShared() ) {
396 pData->Unlock(); // memory not freed because shared
c86f1403 397 size_t nLen = pData->nDataLength;
3168a13f 398 AllocBuffer(nLen);
2bb67b80 399 memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar));
c801d85f
KB
400 }
401
3bbb630a 402 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
c801d85f
KB
403}
404
405// must be called before replacing contents of this string
406void wxString::AllocBeforeWrite(size_t nLen)
407{
408 wxASSERT( nLen != 0 ); // doesn't make any sense
409
410 // must not share string and must have enough space
3168a13f 411 wxStringData* pData = GetStringData();
fbf0c83d 412 if ( pData->IsShared() || pData->IsEmpty() ) {
c801d85f
KB
413 // can't work with old buffer, get new one
414 pData->Unlock();
415 AllocBuffer(nLen);
416 }
471aebdd 417 else {
fbf0c83d
VZ
418 if ( nLen > pData->nAllocLength ) {
419 // realloc the buffer instead of calling malloc() again, this is more
420 // efficient
421 STATISTICS_ADD(Length, nLen);
422
423 nLen += EXTRA_ALLOC;
424
425 wxStringData *pDataOld = pData;
426 pData = (wxStringData*)
427 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
428 if ( !pData ) {
429 // out of memory
430 free(pDataOld);
431
432 // FIXME we're going to crash...
433 return;
434 }
435
436 pData->nAllocLength = nLen;
437 m_pchData = pData->data();
438 }
439
440 // now we have enough space, just update the string length
471aebdd
VZ
441 pData->nDataLength = nLen;
442 }
c801d85f 443
f1da2f03 444 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
c801d85f
KB
445}
446
dd1eaa89 447// allocate enough memory for nLen characters
c86f1403 448void wxString::Alloc(size_t nLen)
dd1eaa89
VZ
449{
450 wxStringData *pData = GetStringData();
451 if ( pData->nAllocLength <= nLen ) {
9fbd8b8d
VZ
452 if ( pData->IsEmpty() ) {
453 nLen += EXTRA_ALLOC;
454
455 wxStringData* pData = (wxStringData*)
2bb67b80 456 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
9fbd8b8d
VZ
457 pData->nRefs = 1;
458 pData->nDataLength = 0;
459 pData->nAllocLength = nLen;
460 m_pchData = pData->data(); // data starts after wxStringData
223d09f6 461 m_pchData[0u] = wxT('\0');
9fbd8b8d 462 }
3168a13f
VZ
463 else if ( pData->IsShared() ) {
464 pData->Unlock(); // memory not freed because shared
c86f1403 465 size_t nOldLen = pData->nDataLength;
3168a13f 466 AllocBuffer(nLen);
2bb67b80 467 memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar));
3168a13f 468 }
dd1eaa89 469 else {
3168a13f
VZ
470 nLen += EXTRA_ALLOC;
471
fbf0c83d 472 wxStringData *pDataOld = pData;
dd1eaa89 473 wxStringData *p = (wxStringData *)
2bb67b80 474 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
3168a13f
VZ
475
476 if ( p == NULL ) {
fbf0c83d
VZ
477 // don't leak memory
478 free(pDataOld);
479
480 // FIXME what to do on memory error?
3168a13f 481 return;
dd1eaa89 482 }
3168a13f
VZ
483
484 // it's not important if the pointer changed or not (the check for this
485 // is not faster than assigning to m_pchData in all cases)
486 p->nAllocLength = nLen;
487 m_pchData = p->data();
dd1eaa89
VZ
488 }
489 }
490 //else: we've already got enough
491}
492
493// shrink to minimal size (releasing extra memory)
494void wxString::Shrink()
495{
496 wxStringData *pData = GetStringData();
3bbb630a 497
337a0010
VZ
498 size_t nLen = pData->nDataLength;
499 void *p = realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
500
501 wxASSERT_MSG( p != NULL, _T("can't free memory?") );
502
503 if ( p != pData )
504 {
505 // contrary to what one might believe, some realloc() implementation do
506 // move the memory block even when its size is reduced
507 pData = (wxStringData *)p;
3bbb630a 508
337a0010
VZ
509 m_pchData = pData->data();
510 }
fbf0c83d 511
337a0010 512 pData->nAllocLength = nLen;
dd1eaa89
VZ
513}
514
c801d85f 515// get the pointer to writable buffer of (at least) nLen bytes
2bb67b80 516wxChar *wxString::GetWriteBuf(size_t nLen)
c801d85f
KB
517{
518 AllocBeforeWrite(nLen);
097c080b
VZ
519
520 wxASSERT( GetStringData()->nRefs == 1 );
521 GetStringData()->Validate(FALSE);
522
c801d85f
KB
523 return m_pchData;
524}
525
097c080b
VZ
526// put string back in a reasonable state after GetWriteBuf
527void wxString::UngetWriteBuf()
528{
2bb67b80 529 GetStringData()->nDataLength = wxStrlen(m_pchData);
097c080b
VZ
530 GetStringData()->Validate(TRUE);
531}
532
8f06a017
RD
533void wxString::UngetWriteBuf(size_t nLen)
534{
535 GetStringData()->nDataLength = nLen;
536 GetStringData()->Validate(TRUE);
537}
538
c801d85f
KB
539// ---------------------------------------------------------------------------
540// data access
541// ---------------------------------------------------------------------------
542
543// all functions are inline in string.h
544
545// ---------------------------------------------------------------------------
546// assignment operators
547// ---------------------------------------------------------------------------
548
dd1eaa89 549// helper function: does real copy
2bb67b80 550void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
c801d85f
KB
551{
552 if ( nSrcLen == 0 ) {
553 Reinit();
554 }
555 else {
556 AllocBeforeWrite(nSrcLen);
2bb67b80 557 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
c801d85f 558 GetStringData()->nDataLength = nSrcLen;
223d09f6 559 m_pchData[nSrcLen] = wxT('\0');
c801d85f
KB
560 }
561}
562
563// assigns one string to another
564wxString& wxString::operator=(const wxString& stringSrc)
565{
097c080b
VZ
566 wxASSERT( stringSrc.GetStringData()->IsValid() );
567
c801d85f
KB
568 // don't copy string over itself
569 if ( m_pchData != stringSrc.m_pchData ) {
570 if ( stringSrc.GetStringData()->IsEmpty() ) {
571 Reinit();
572 }
573 else {
574 // adjust references
575 GetStringData()->Unlock();
576 m_pchData = stringSrc.m_pchData;
577 GetStringData()->Lock();
578 }
579 }
580
581 return *this;
582}
583
584// assigns a single character
2bb67b80 585wxString& wxString::operator=(wxChar ch)
c801d85f
KB
586{
587 AssignCopy(1, &ch);
588 return *this;
589}
590
a3291804 591
c801d85f 592// assigns C string
2bb67b80 593wxString& wxString::operator=(const wxChar *psz)
c801d85f 594{
2bb67b80 595 AssignCopy(wxStrlen(psz), psz);
c801d85f
KB
596 return *this;
597}
598
2bb67b80
OK
599#if !wxUSE_UNICODE
600
c801d85f
KB
601// same as 'signed char' variant
602wxString& wxString::operator=(const unsigned char* psz)
603{
604 *this = (const char *)psz;
605 return *this;
606}
607
0f3e3e0c 608#if wxUSE_WCHAR_T
c801d85f
KB
609wxString& wxString::operator=(const wchar_t *pwz)
610{
611 wxString str(pwz);
612 *this = str;
613 return *this;
614}
0f3e3e0c 615#endif
c801d85f 616
2bb67b80
OK
617#endif
618
c801d85f
KB
619// ---------------------------------------------------------------------------
620// string concatenation
621// ---------------------------------------------------------------------------
622
c801d85f 623// add something to this string
2bb67b80 624void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData)
c801d85f 625{
3168a13f 626 STATISTICS_ADD(SummandLength, nSrcLen);
c801d85f 627
05488905
VZ
628 // concatenating an empty string is a NOP
629 if ( nSrcLen > 0 ) {
630 wxStringData *pData = GetStringData();
631 size_t nLen = pData->nDataLength;
632 size_t nNewLen = nLen + nSrcLen;
c801d85f 633
05488905
VZ
634 // alloc new buffer if current is too small
635 if ( pData->IsShared() ) {
636 STATISTICS_ADD(ConcatHit, 0);
3168a13f 637
05488905
VZ
638 // we have to allocate another buffer
639 wxStringData* pOldData = GetStringData();
640 AllocBuffer(nNewLen);
2bb67b80 641 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
05488905
VZ
642 pOldData->Unlock();
643 }
644 else if ( nNewLen > pData->nAllocLength ) {
645 STATISTICS_ADD(ConcatHit, 0);
3168a13f 646
05488905
VZ
647 // we have to grow the buffer
648 Alloc(nNewLen);
649 }
650 else {
651 STATISTICS_ADD(ConcatHit, 1);
3168a13f 652
05488905
VZ
653 // the buffer is already big enough
654 }
3168a13f 655
05488905
VZ
656 // should be enough space
657 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
3168a13f 658
05488905 659 // fast concatenation - all is done in our buffer
2bb67b80 660 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
3168a13f 661
223d09f6 662 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
05488905
VZ
663 GetStringData()->nDataLength = nNewLen; // and fix the length
664 }
665 //else: the string to append was empty
c801d85f
KB
666}
667
668/*
c801d85f
KB
669 * concatenation functions come in 5 flavours:
670 * string + string
671 * char + string and string + char
672 * C str + string and string + C str
673 */
674
675wxString operator+(const wxString& string1, const wxString& string2)
676{
097c080b
VZ
677 wxASSERT( string1.GetStringData()->IsValid() );
678 wxASSERT( string2.GetStringData()->IsValid() );
679
3168a13f
VZ
680 wxString s = string1;
681 s += string2;
682
c801d85f
KB
683 return s;
684}
685
2bb67b80 686wxString operator+(const wxString& string, wxChar ch)
c801d85f 687{
3168a13f
VZ
688 wxASSERT( string.GetStringData()->IsValid() );
689
690 wxString s = string;
691 s += ch;
097c080b 692
c801d85f
KB
693 return s;
694}
695
2bb67b80 696wxString operator+(wxChar ch, const wxString& string)
c801d85f 697{
097c080b
VZ
698 wxASSERT( string.GetStringData()->IsValid() );
699
3168a13f
VZ
700 wxString s = ch;
701 s += string;
702
c801d85f
KB
703 return s;
704}
705
2bb67b80 706wxString operator+(const wxString& string, const wxChar *psz)
c801d85f 707{
097c080b
VZ
708 wxASSERT( string.GetStringData()->IsValid() );
709
c801d85f 710 wxString s;
2bb67b80 711 s.Alloc(wxStrlen(psz) + string.Len());
3168a13f
VZ
712 s = string;
713 s += psz;
714
c801d85f
KB
715 return s;
716}
717
2bb67b80 718wxString operator+(const wxChar *psz, const wxString& string)
c801d85f 719{
097c080b
VZ
720 wxASSERT( string.GetStringData()->IsValid() );
721
c801d85f 722 wxString s;
2bb67b80 723 s.Alloc(wxStrlen(psz) + string.Len());
3168a13f
VZ
724 s = psz;
725 s += string;
726
c801d85f
KB
727 return s;
728}
729
730// ===========================================================================
731// other common string functions
732// ===========================================================================
733
734// ---------------------------------------------------------------------------
735// simple sub-string extraction
736// ---------------------------------------------------------------------------
737
738// helper function: clone the data attached to this string
739void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
740{
3168a13f 741 if ( nCopyLen == 0 ) {
c801d85f
KB
742 dest.Init();
743 }
3168a13f 744 else {
c801d85f 745 dest.AllocBuffer(nCopyLen);
2bb67b80 746 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
c801d85f
KB
747 }
748}
749
750// extract string of length nCount starting at nFirst
c801d85f
KB
751wxString wxString::Mid(size_t nFirst, size_t nCount) const
752{
30d9011f
VZ
753 wxStringData *pData = GetStringData();
754 size_t nLen = pData->nDataLength;
755
566b84d2
VZ
756 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
757 if ( nCount == wxSTRING_MAXLEN )
30d9011f
VZ
758 {
759 nCount = nLen - nFirst;
760 }
761
c801d85f 762 // out-of-bounds requests return sensible things
30d9011f
VZ
763 if ( nFirst + nCount > nLen )
764 {
765 nCount = nLen - nFirst;
766 }
c801d85f 767
30d9011f
VZ
768 if ( nFirst > nLen )
769 {
770 // AllocCopy() will return empty string
c801d85f 771 nCount = 0;
30d9011f 772 }
c801d85f
KB
773
774 wxString dest;
775 AllocCopy(dest, nCount, nFirst);
30d9011f 776
c801d85f
KB
777 return dest;
778}
779
f6bcfd97
BP
780// check that the tring starts with prefix and return the rest of the string
781// in the provided pointer if it is not NULL, otherwise return FALSE
782bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const
783{
784 wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") );
785
786 // first check if the beginning of the string matches the prefix: note
787 // that we don't have to check that we don't run out of this string as
788 // when we reach the terminating NUL, either prefix string ends too (and
789 // then it's ok) or we break out of the loop because there is no match
790 const wxChar *p = c_str();
791 while ( *prefix )
792 {
793 if ( *prefix++ != *p++ )
794 {
795 // no match
796 return FALSE;
797 }
798 }
799
800 if ( rest )
801 {
802 // put the rest of the string into provided pointer
803 *rest = p;
804 }
805
806 return TRUE;
807}
808
c801d85f
KB
809// extract nCount last (rightmost) characters
810wxString wxString::Right(size_t nCount) const
811{
812 if ( nCount > (size_t)GetStringData()->nDataLength )
813 nCount = GetStringData()->nDataLength;
814
815 wxString dest;
816 AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount);
817 return dest;
818}
819
820// get all characters after the last occurence of ch
821// (returns the whole string if ch not found)
2bb67b80 822wxString wxString::AfterLast(wxChar ch) const
c801d85f
KB
823{
824 wxString str;
825 int iPos = Find(ch, TRUE);
3c67202d 826 if ( iPos == wxNOT_FOUND )
c801d85f
KB
827 str = *this;
828 else
c8cfb486 829 str = c_str() + iPos + 1;
c801d85f
KB
830
831 return str;
832}
833
834// extract nCount first (leftmost) characters
835wxString wxString::Left(size_t nCount) const
836{
837 if ( nCount > (size_t)GetStringData()->nDataLength )
838 nCount = GetStringData()->nDataLength;
839
840 wxString dest;
841 AllocCopy(dest, nCount, 0);
842 return dest;
843}
844
845// get all characters before the first occurence of ch
846// (returns the whole string if ch not found)
2bb67b80 847wxString wxString::BeforeFirst(wxChar ch) const
c801d85f
KB
848{
849 wxString str;
223d09f6 850 for ( const wxChar *pc = m_pchData; *pc != wxT('\0') && *pc != ch; pc++ )
c801d85f
KB
851 str += *pc;
852
853 return str;
854}
855
856/// get all characters before the last occurence of ch
857/// (returns empty string if ch not found)
2bb67b80 858wxString wxString::BeforeLast(wxChar ch) const
c801d85f
KB
859{
860 wxString str;
861 int iPos = Find(ch, TRUE);
3c67202d 862 if ( iPos != wxNOT_FOUND && iPos != 0 )
d1c9bbf6 863 str = wxString(c_str(), iPos);
c801d85f
KB
864
865 return str;
866}
867
868/// get all characters after the first occurence of ch
869/// (returns empty string if ch not found)
2bb67b80 870wxString wxString::AfterFirst(wxChar ch) const
c801d85f
KB
871{
872 wxString str;
873 int iPos = Find(ch);
3c67202d 874 if ( iPos != wxNOT_FOUND )
c801d85f
KB
875 str = c_str() + iPos + 1;
876
877 return str;
878}
879
880// replace first (or all) occurences of some substring with another one
2bb67b80 881size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
c801d85f 882{
c86f1403 883 size_t uiCount = 0; // count of replacements made
c801d85f 884
2bb67b80 885 size_t uiOldLen = wxStrlen(szOld);
c801d85f
KB
886
887 wxString strTemp;
2bb67b80
OK
888 const wxChar *pCurrent = m_pchData;
889 const wxChar *pSubstr;
223d09f6 890 while ( *pCurrent != wxT('\0') ) {
2bb67b80 891 pSubstr = wxStrstr(pCurrent, szOld);
c801d85f
KB
892 if ( pSubstr == NULL ) {
893 // strTemp is unused if no replacements were made, so avoid the copy
894 if ( uiCount == 0 )
895 return 0;
896
897 strTemp += pCurrent; // copy the rest
898 break; // exit the loop
899 }
900 else {
901 // take chars before match
902 strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent);
903 strTemp += szNew;
904 pCurrent = pSubstr + uiOldLen; // restart after match
905
906 uiCount++;
907
908 // stop now?
909 if ( !bReplaceAll ) {
910 strTemp += pCurrent; // copy the rest
911 break; // exit the loop
912 }
913 }
914 }
915
916 // only done if there were replacements, otherwise would have returned above
917 *this = strTemp;
918
919 return uiCount;
920}
921
922bool wxString::IsAscii() const
923{
2bb67b80 924 const wxChar *s = (const wxChar*) *this;
c801d85f
KB
925 while(*s){
926 if(!isascii(*s)) return(FALSE);
927 s++;
928 }
929 return(TRUE);
930}
dd1eaa89 931
c801d85f
KB
932bool wxString::IsWord() const
933{
2bb67b80 934 const wxChar *s = (const wxChar*) *this;
c801d85f 935 while(*s){
2bb67b80 936 if(!wxIsalpha(*s)) return(FALSE);
c801d85f
KB
937 s++;
938 }
939 return(TRUE);
940}
dd1eaa89 941
c801d85f
KB
942bool wxString::IsNumber() const
943{
2bb67b80 944 const wxChar *s = (const wxChar*) *this;
2f74ed28
GT
945 if (wxStrlen(s))
946 if ((s[0] == '-') || (s[0] == '+')) s++;
c801d85f 947 while(*s){
2bb67b80 948 if(!wxIsdigit(*s)) return(FALSE);
c801d85f
KB
949 s++;
950 }
951 return(TRUE);
952}
953
c801d85f
KB
954wxString wxString::Strip(stripType w) const
955{
956 wxString s = *this;
957 if ( w & leading ) s.Trim(FALSE);
958 if ( w & trailing ) s.Trim(TRUE);
959 return s;
960}
961
c801d85f
KB
962// ---------------------------------------------------------------------------
963// case conversion
964// ---------------------------------------------------------------------------
965
966wxString& wxString::MakeUpper()
967{
968 CopyBeforeWrite();
969
2bb67b80
OK
970 for ( wxChar *p = m_pchData; *p; p++ )
971 *p = (wxChar)wxToupper(*p);
c801d85f
KB
972
973 return *this;
974}
975
976wxString& wxString::MakeLower()
977{
978 CopyBeforeWrite();
dd1eaa89 979
2bb67b80
OK
980 for ( wxChar *p = m_pchData; *p; p++ )
981 *p = (wxChar)wxTolower(*p);
c801d85f
KB
982
983 return *this;
984}
985
986// ---------------------------------------------------------------------------
987// trimming and padding
988// ---------------------------------------------------------------------------
989
576c608d
VZ
990// some compilers (VC++ 6.0 not to name them) return TRUE for a call to
991