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