]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/string.cpp
better learn the operators...
[wxWidgets.git] / src / common / string.cpp
... / ...
CommitLineData
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>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13 #pragma implementation "string.h"
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__
31 #pragma hdrstop
32#endif
33
34#ifndef WX_PRECOMP
35 #include "wx/defs.h"
36 #include "wx/string.h"
37 #include "wx/intl.h"
38 #include "wx/thread.h"
39#endif
40
41#include "wx/regex.h" // for wxString::Matches()
42
43#include <ctype.h>
44#include <string.h>
45#include <stdlib.h>
46
47#ifdef __SALFORDC__
48 #include <clib.h>
49#endif
50
51#if wxUSE_WCSRTOMBS
52 #include <wchar.h> // for wcsrtombs(), see comments where it's used
53#endif // GNU
54
55#ifdef WXSTRING_IS_WXOBJECT
56 IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject)
57#endif //WXSTRING_IS_WXOBJECT
58
59#if wxUSE_UNICODE
60#undef wxUSE_EXPERIMENTAL_PRINTF
61#define wxUSE_EXPERIMENTAL_PRINTF 1
62#endif
63
64// allocating extra space for each string consumes more memory but speeds up
65// the concatenation operations (nLen is the current string's length)
66// NB: EXTRA_ALLOC must be >= 0!
67#define EXTRA_ALLOC (19 - nLen % 16)
68
69// ---------------------------------------------------------------------------
70// static class variables definition
71// ---------------------------------------------------------------------------
72
73#ifdef wxSTD_STRING_COMPATIBILITY
74 const size_t wxString::npos = wxSTRING_MAXLEN;
75#endif // wxSTD_STRING_COMPATIBILITY
76
77// ----------------------------------------------------------------------------
78// static data
79// ----------------------------------------------------------------------------
80
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;
87 wxChar dummy;
88} g_strEmpty = { {-1, 0, 0}, wxT('\0') };
89
90#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
91// must define this static for VA or else you get multiply defined symbols
92// everywhere
93const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100;
94#endif // Visual Age
95
96// empty C style string: points to 'string data' byte of g_strEmpty
97extern const wxChar WXDLLEXPORT *wxEmptyString = &g_strEmpty.dummy;
98
99// ----------------------------------------------------------------------------
100// conditional compilation
101// ----------------------------------------------------------------------------
102
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
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.
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
117#ifdef __WXMSW__
118 #if defined(__VISUALC__) || (defined(__MINGW32__) && wxUSE_NORLANDER_HEADERS)
119 #define wxVsnprintfA _vsnprintf
120 #endif
121#elif defined(__WXMAC__)
122 #define wxVsnprintfA vsnprintf
123#else // !Windows
124 #ifdef HAVE_VSNPRINTF
125 #define wxVsnprintfA vsnprintf
126 #endif
127#endif // Windows/!Windows
128
129#ifndef wxVsnprintfA
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
133 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
134
135 #if defined(__VISUALC__)
136 #pragma message("Using sprintf() because no snprintf()-like function defined")
137 #elif defined(__GNUG__)
138 #warning "Using sprintf() because no snprintf()-like function defined"
139 #endif //compiler
140#endif // no vsnprintf
141
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
147// ----------------------------------------------------------------------------
148// global functions
149// ----------------------------------------------------------------------------
150
151#if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
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!
157
158wxSTD istream& operator>>(wxSTD istream& is, wxString& WXUNUSED(str))
159{
160#if 0
161 int w = is.width(0);
162 if ( is.ipfx(0) ) {
163 streambuf *sb = is.rdbuf();
164 str.erase();
165 while ( true ) {
166 int ch = sb->sbumpc ();
167 if ( ch == EOF ) {
168 is.setstate(ios::eofbit);
169 break;
170 }
171 else if ( isspace(ch) ) {
172 sb->sungetc();
173 break;
174 }
175
176 str += ch;
177 if ( --w == 1 )
178 break;
179 }
180 }
181
182 is.isfx();
183 if ( str.length() == 0 )
184 is.setstate(ios::failbit);
185#endif
186 return is;
187}
188
189wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
190{
191 os << str.c_str();
192 return os;
193}
194
195#endif //std::string compatibility
196
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 {
206 wxStrncpy(buf, s.c_str(), len);
207 buf[len-1] = wxT('\0');
208 }
209
210 return iLen;
211#else // ANSI
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);
215 if ( rc == -1 )
216 {
217 buf[len] = 0;
218 }
219
220 return rc;
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
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; }
248 ~Averager()
249 { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
250
251 void Add(size_t n) { m_nTotal += n; m_nCount++; }
252
253 private:
254 size_t m_nCount, m_nTotal;
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
266// ===========================================================================
267// wxString class core
268// ===========================================================================
269
270// ---------------------------------------------------------------------------
271// construction
272// ---------------------------------------------------------------------------
273
274// constructs string of <nLength> copies of character <ch>
275wxString::wxString(wxChar ch, size_t nLength)
276{
277 Init();
278
279 if ( nLength > 0 ) {
280 AllocBuffer(nLength);
281
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
286 memset(m_pchData, ch, nLength);
287#endif
288 }
289}
290
291// takes nLength elements of psz starting at nPos
292void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
293{
294 Init();
295
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") );
299
300 nLength = wxStrlen(psz + nPos);
301 }
302
303 STATISTICS_ADD(InitialLength, nLength);
304
305 if ( nLength > 0 ) {
306 // trailing '\0' is written in AllocBuffer()
307 AllocBuffer(nLength);
308 memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar));
309 }
310}
311
312#ifdef wxSTD_STRING_COMPATIBILITY
313
314// poor man's iterators are "void *" pointers
315wxString::wxString(const void *pStart, const void *pEnd)
316{
317 InitWith((const wxChar *)pStart, 0,
318 (const wxChar *)pEnd - (const wxChar *)pStart);
319}
320
321#endif //std::string compatibility
322
323#if wxUSE_UNICODE
324
325// from multibyte string
326wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
327{
328 // first get necessary size
329 size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
330
331 // nLength is number of *Unicode* characters here!
332 if ((nLen != (size_t)-1) && (nLen > nLength))
333 nLen = nLength;
334
335 // empty?
336 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
337 AllocBuffer(nLen);
338 conv.MB2WC(m_pchData, psz, nLen);
339 }
340 else {
341 Init();
342 }
343}
344
345#else // ANSI
346
347#if wxUSE_WCHAR_T
348// from wide string
349wxString::wxString(const wchar_t *pwz, wxMBConv& conv)
350{
351 // first get necessary size
352 size_t nLen = pwz ? conv.WC2MB((char *) NULL, pwz, 0) : 0;
353
354 // empty?
355 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
356 AllocBuffer(nLen);
357 conv.WC2MB(m_pchData, pwz, nLen);
358 }
359 else {
360 Init();
361 }
362}
363#endif // wxUSE_WCHAR_T
364
365#endif // Unicode/ANSI
366
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{
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) );
381
382 STATISTICS_ADD(Length, nLen);
383
384 // allocate memory:
385 // 1) one extra character for '\0' termination
386 // 2) sizeof(wxStringData) for housekeeping info
387 wxStringData* pData = (wxStringData*)
388 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
389 pData->nRefs = 1;
390 pData->nDataLength = nLen;
391 pData->nAllocLength = nLen + EXTRA_ALLOC;
392 m_pchData = pData->data(); // data starts after wxStringData
393 m_pchData[nLen] = wxT('\0');
394}
395
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
403 size_t nLen = pData->nDataLength;
404 AllocBuffer(nLen);
405 memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar));
406 }
407
408 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
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
417 wxStringData* pData = GetStringData();
418 if ( pData->IsShared() || pData->IsEmpty() ) {
419 // can't work with old buffer, get new one
420 pData->Unlock();
421 AllocBuffer(nLen);
422 }
423 else {
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
447 pData->nDataLength = nLen;
448 }
449
450 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
451}
452
453// allocate enough memory for nLen characters
454void wxString::Alloc(size_t nLen)
455{
456 wxStringData *pData = GetStringData();
457 if ( pData->nAllocLength <= nLen ) {
458 if ( pData->IsEmpty() ) {
459 nLen += EXTRA_ALLOC;
460
461 wxStringData* pData = (wxStringData*)
462 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
463 pData->nRefs = 1;
464 pData->nDataLength = 0;
465 pData->nAllocLength = nLen;
466 m_pchData = pData->data(); // data starts after wxStringData
467 m_pchData[0u] = wxT('\0');
468 }
469 else if ( pData->IsShared() ) {
470 pData->Unlock(); // memory not freed because shared
471 size_t nOldLen = pData->nDataLength;
472 AllocBuffer(nLen);
473 memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar));
474 }
475 else {
476 nLen += EXTRA_ALLOC;
477
478 wxStringData *pDataOld = pData;
479 wxStringData *p = (wxStringData *)
480 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
481
482 if ( p == NULL ) {
483 // don't leak memory
484 free(pDataOld);
485
486 // FIXME what to do on memory error?
487 return;
488 }
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();
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();
503
504 // this variable is unused in release build, so avoid the compiler warning
505 // by just not declaring it
506#ifdef __WXDEBUG__
507 void *p =
508#endif
509 realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(wxChar));
510
511 // we rely on a reasonable realloc() implementation here - so far I haven't
512 // seen any which wouldn't behave like this
513
514 wxASSERT( p != NULL ); // can't free memory?
515 wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move!
516}
517
518// get the pointer to writable buffer of (at least) nLen bytes
519wxChar *wxString::GetWriteBuf(size_t nLen)
520{
521 AllocBeforeWrite(nLen);
522
523 wxASSERT( GetStringData()->nRefs == 1 );
524 GetStringData()->Validate(FALSE);
525
526 return m_pchData;
527}
528
529// put string back in a reasonable state after GetWriteBuf
530void wxString::UngetWriteBuf()
531{
532 GetStringData()->nDataLength = wxStrlen(m_pchData);
533 GetStringData()->Validate(TRUE);
534}
535
536void wxString::UngetWriteBuf(size_t nLen)
537{
538 GetStringData()->nDataLength = nLen;
539 GetStringData()->Validate(TRUE);
540}
541
542// ---------------------------------------------------------------------------
543// data access
544// ---------------------------------------------------------------------------
545
546// all functions are inline in string.h
547
548// ---------------------------------------------------------------------------
549// assignment operators
550// ---------------------------------------------------------------------------
551
552// helper function: does real copy
553void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
554{
555 if ( nSrcLen == 0 ) {
556 Reinit();
557 }
558 else {
559 AllocBeforeWrite(nSrcLen);
560 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
561 GetStringData()->nDataLength = nSrcLen;
562 m_pchData[nSrcLen] = wxT('\0');
563 }
564}
565
566// assigns one string to another
567wxString& wxString::operator=(const wxString& stringSrc)
568{
569 wxASSERT( stringSrc.GetStringData()->IsValid() );
570
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
588wxString& wxString::operator=(wxChar ch)
589{
590 AssignCopy(1, &ch);
591 return *this;
592}
593
594
595// assigns C string
596wxString& wxString::operator=(const wxChar *psz)
597{
598 AssignCopy(wxStrlen(psz), psz);
599 return *this;
600}
601
602#if !wxUSE_UNICODE
603
604// same as 'signed char' variant
605wxString& wxString::operator=(const unsigned char* psz)
606{
607 *this = (const char *)psz;
608 return *this;
609}
610
611#if wxUSE_WCHAR_T
612wxString& wxString::operator=(const wchar_t *pwz)
613{
614 wxString str(pwz);
615 *this = str;
616 return *this;
617}
618#endif
619
620#endif
621
622// ---------------------------------------------------------------------------
623// string concatenation
624// ---------------------------------------------------------------------------
625
626// add something to this string
627void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData)
628{
629 STATISTICS_ADD(SummandLength, nSrcLen);
630
631 // concatenating an empty string is a NOP
632 if ( nSrcLen > 0 ) {
633 wxStringData *pData = GetStringData();
634 size_t nLen = pData->nDataLength;
635 size_t nNewLen = nLen + nSrcLen;
636
637 // alloc new buffer if current is too small
638 if ( pData->IsShared() ) {
639 STATISTICS_ADD(ConcatHit, 0);
640
641 // we have to allocate another buffer
642 wxStringData* pOldData = GetStringData();
643 AllocBuffer(nNewLen);
644 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
645 pOldData->Unlock();
646 }
647 else if ( nNewLen > pData->nAllocLength ) {
648 STATISTICS_ADD(ConcatHit, 0);
649
650 // we have to grow the buffer
651 Alloc(nNewLen);
652 }
653 else {
654 STATISTICS_ADD(ConcatHit, 1);
655
656 // the buffer is already big enough
657 }
658
659 // should be enough space
660 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
661
662 // fast concatenation - all is done in our buffer
663 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
664
665 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
666 GetStringData()->nDataLength = nNewLen; // and fix the length
667 }
668 //else: the string to append was empty
669}
670
671/*
672 * concatenation functions come in 5 flavours:
673 * string + string
674 * char + string and string + char
675 * C str + string and string + C str
676 */
677
678wxString operator+(const wxString& string1, const wxString& string2)
679{
680 wxASSERT( string1.GetStringData()->IsValid() );
681 wxASSERT( string2.GetStringData()->IsValid() );
682
683 wxString s = string1;
684 s += string2;
685
686 return s;
687}
688
689wxString operator+(const wxString& string, wxChar ch)
690{
691 wxASSERT( string.GetStringData()->IsValid() );
692
693 wxString s = string;
694 s += ch;
695
696 return s;
697}
698
699wxString operator+(wxChar ch, const wxString& string)
700{
701 wxASSERT( string.GetStringData()->IsValid() );
702
703 wxString s = ch;
704 s += string;
705
706 return s;
707}
708
709wxString operator+(const wxString& string, const wxChar *psz)
710{
711 wxASSERT( string.GetStringData()->IsValid() );
712
713 wxString s;
714 s.Alloc(wxStrlen(psz) + string.Len());
715 s = string;
716 s += psz;
717
718 return s;
719}
720
721wxString operator+(const wxChar *psz, const wxString& string)
722{
723 wxASSERT( string.GetStringData()->IsValid() );
724
725 wxString s;
726 s.Alloc(wxStrlen(psz) + string.Len());
727 s = psz;
728 s += string;
729
730 return s;
731}
732
733// ===========================================================================
734// other common string functions
735// ===========================================================================
736
737// ---------------------------------------------------------------------------
738// simple sub-string extraction
739// ---------------------------------------------------------------------------
740
741// helper function: clone the data attached to this string
742void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
743{
744 if ( nCopyLen == 0 ) {
745 dest.Init();
746 }
747 else {
748 dest.AllocBuffer(nCopyLen);
749 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
750 }
751}
752
753// extract string of length nCount starting at nFirst
754wxString wxString::Mid(size_t nFirst, size_t nCount) const
755{
756 wxStringData *pData = GetStringData();
757 size_t nLen = pData->nDataLength;
758
759 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
760 if ( nCount == wxSTRING_MAXLEN )
761 {
762 nCount = nLen - nFirst;
763 }
764
765 // out-of-bounds requests return sensible things
766 if ( nFirst + nCount > nLen )
767 {
768 nCount = nLen - nFirst;
769 }
770
771 if ( nFirst > nLen )
772 {
773 // AllocCopy() will return empty string
774 nCount = 0;
775 }
776
777 wxString dest;
778 AllocCopy(dest, nCount, nFirst);
779
780 return dest;
781}
782
783// check that the tring starts with prefix and return the rest of the string
784// in the provided pointer if it is not NULL, otherwise return FALSE
785bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const
786{
787 wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") );
788
789 // first check if the beginning of the string matches the prefix: note
790 // that we don't have to check that we don't run out of this string as
791 // when we reach the terminating NUL, either prefix string ends too (and
792 // then it's ok) or we break out of the loop because there is no match
793 const wxChar *p = c_str();
794 while ( *prefix )
795 {
796 if ( *prefix++ != *p++ )
797 {
798 // no match
799 return FALSE;
800 }
801 }
802
803 if ( rest )
804 {
805 // put the rest of the string into provided pointer
806 *rest = p;
807 }
808
809 return TRUE;
810}
811
812// extract nCount last (rightmost) characters
813wxString wxString::Right(size_t nCount) const
814{
815 if ( nCount > (size_t)GetStringData()->nDataLength )
816 nCount = GetStringData()->nDataLength;
817
818 wxString dest;
819 AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount);
820 return dest;
821}
822
823// get all characters after the last occurence of ch
824// (returns the whole string if ch not found)
825wxString wxString::AfterLast(wxChar ch) const
826{
827 wxString str;
828 int iPos = Find(ch, TRUE);
829 if ( iPos == wxNOT_FOUND )
830 str = *this;
831 else
832 str = c_str() + iPos + 1;
833
834 return str;
835}
836
837// extract nCount first (leftmost) characters
838wxString wxString::Left(size_t nCount) const
839{
840 if ( nCount > (size_t)GetStringData()->nDataLength )
841 nCount = GetStringData()->nDataLength;
842
843 wxString dest;
844 AllocCopy(dest, nCount, 0);
845 return dest;
846}
847
848// get all characters before the first occurence of ch
849// (returns the whole string if ch not found)
850wxString wxString::BeforeFirst(wxChar ch) const
851{
852 wxString str;
853 for ( const wxChar *pc = m_pchData; *pc != wxT('\0') && *pc != ch; pc++ )
854 str += *pc;
855
856 return str;
857}
858
859/// get all characters before the last occurence of ch
860/// (returns empty string if ch not found)
861wxString wxString::BeforeLast(wxChar ch) const
862{
863 wxString str;
864 int iPos = Find(ch, TRUE);
865 if ( iPos != wxNOT_FOUND && iPos != 0 )
866 str = wxString(c_str(), iPos);
867
868 return str;
869}
870
871/// get all characters after the first occurence of ch
872/// (returns empty string if ch not found)
873wxString wxString::AfterFirst(wxChar ch) const
874{
875 wxString str;
876 int iPos = Find(ch);
877 if ( iPos != wxNOT_FOUND )
878 str = c_str() + iPos + 1;
879
880 return str;
881}
882
883// replace first (or all) occurences of some substring with another one
884size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
885{
886 size_t uiCount = 0; // count of replacements made
887
888 size_t uiOldLen = wxStrlen(szOld);
889
890 wxString strTemp;
891 const wxChar *pCurrent = m_pchData;
892 const wxChar *pSubstr;
893 while ( *pCurrent != wxT('\0') ) {
894 pSubstr = wxStrstr(pCurrent, szOld);
895 if ( pSubstr == NULL ) {
896 // strTemp is unused if no replacements were made, so avoid the copy
897 if ( uiCount == 0 )
898 return 0;
899
900 strTemp += pCurrent; // copy the rest
901 break; // exit the loop
902 }
903 else {
904 // take chars before match
905 strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent);
906 strTemp += szNew;
907 pCurrent = pSubstr + uiOldLen; // restart after match
908
909 uiCount++;
910
911 // stop now?
912 if ( !bReplaceAll ) {
913 strTemp += pCurrent; // copy the rest
914 break; // exit the loop
915 }
916 }
917 }
918
919 // only done if there were replacements, otherwise would have returned above
920 *this = strTemp;
921
922 return uiCount;
923}
924
925bool wxString::IsAscii() const
926{
927 const wxChar *s = (const wxChar*) *this;
928 while(*s){
929 if(!isascii(*s)) return(FALSE);
930 s++;
931 }
932 return(TRUE);
933}
934
935bool wxString::IsWord() const
936{
937 const wxChar *s = (const wxChar*) *this;
938 while(*s){
939 if(!wxIsalpha(*s)) return(FALSE);
940 s++;
941 }
942 return(TRUE);
943}
944
945bool wxString::IsNumber() const
946{
947 const wxChar *s = (const wxChar*) *this;
948 if (wxStrlen(s))
949 if ((s[0] == '-') || (s[0] == '+')) s++;
950 while(*s){
951 if(!wxIsdigit(*s)) return(FALSE);
952 s++;
953 }
954 return(TRUE);
955}
956
957wxString wxString::Strip(stripType w) const
958{
959 wxString s = *this;
960 if ( w & leading ) s.Trim(FALSE);
961 if ( w & trailing ) s.Trim(TRUE);
962 return s;
963}
964
965// ---------------------------------------------------------------------------
966// case conversion
967// ---------------------------------------------------------------------------
968
969wxString& wxString::MakeUpper()
970{
971 CopyBeforeWrite();
972
973 for ( wxChar *p = m_pchData; *p; p++ )
974 *p = (wxChar)wxToupper(*p);
975
976 return *this;
977}
978
979wxString& wxString::MakeLower()
980{
981 CopyBeforeWrite();
982
983 for ( wxChar *p = m_pchData; *p; p++ )
984 *p = (wxChar)wxTolower(*p);
985
986 return *this;
987}
988
989// ---------------------------------------------------------------------------
990// trimming and padding
991// ---------------------------------------------------------------------------
992
993// some compilers (VC++ 6.0 not to name them) return TRUE for a call to
994