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