]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/string.cpp
toplevel code transferred to wxTopLevelWindow
[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 <ctype.h>
42#include <string.h>
43#include <stdlib.h>
44
45#ifdef __SALFORDC__
46 #include <clib.h>
47#endif
48
49#ifdef WXSTRING_IS_WXOBJECT
50 IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject)
51#endif //WXSTRING_IS_WXOBJECT
52
53#if wxUSE_UNICODE
54#undef wxUSE_EXPERIMENTAL_PRINTF
55#define wxUSE_EXPERIMENTAL_PRINTF 1
56#endif
57
58// allocating extra space for each string consumes more memory but speeds up
59// the concatenation operations (nLen is the current string's length)
60// NB: EXTRA_ALLOC must be >= 0!
61#define EXTRA_ALLOC (19 - nLen % 16)
62
63// ---------------------------------------------------------------------------
64// static class variables definition
65// ---------------------------------------------------------------------------
66
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
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// empty C style string: points to 'string data' byte of g_strEmpty
91extern const wxChar WXDLLEXPORT *wxEmptyString = &g_strEmpty.dummy;
92
93// ----------------------------------------------------------------------------
94// conditional compilation
95// ----------------------------------------------------------------------------
96
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
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.
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
111#ifdef __WXMSW__
112 #if defined(__VISUALC__) || (defined(__MINGW32__) && wxUSE_NORLANDER_HEADERS)
113 #define wxVsnprintfA _vsnprintf
114 #endif
115#elif defined(__WXMAC__)
116 #define wxVsnprintfA vsnprintf
117#else // !Windows
118 #ifdef HAVE_VSNPRINTF
119 #define wxVsnprintfA vsnprintf
120 #endif
121#endif // Windows/!Windows
122
123#ifndef wxVsnprintfA
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
127 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
128
129 #if defined(__VISUALC__)
130 #pragma message("Using sprintf() because no snprintf()-like function defined")
131 #elif defined(__GNUG__)
132 #warning "Using sprintf() because no snprintf()-like function defined"
133 #endif //compiler
134#endif // no vsnprintf
135
136#if defined(_AIX)
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
141// ----------------------------------------------------------------------------
142// global functions
143// ----------------------------------------------------------------------------
144
145#if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
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!
151
152wxSTD istream& operator>>(wxSTD istream& is, wxString& WXUNUSED(str))
153{
154#if 0
155 int w = is.width(0);
156 if ( is.ipfx(0) ) {
157 streambuf *sb = is.rdbuf();
158 str.erase();
159 while ( true ) {
160 int ch = sb->sbumpc ();
161 if ( ch == EOF ) {
162 is.setstate(ios::eofbit);
163 break;
164 }
165 else if ( isspace(ch) ) {
166 sb->sungetc();
167 break;
168 }
169
170 str += ch;
171 if ( --w == 1 )
172 break;
173 }
174 }
175
176 is.isfx();
177 if ( str.length() == 0 )
178 is.setstate(ios::failbit);
179#endif
180 return is;
181}
182
183wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
184{
185 os << str.c_str();
186 return os;
187}
188
189#endif //std::string compatibility
190
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 {
200 wxStrncpy(buf, s.c_str(), len);
201 buf[len-1] = wxT('\0');
202 }
203
204 return iLen;
205#else // ANSI
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);
209 if ( rc == -1 )
210 {
211 buf[len] = 0;
212 }
213
214 return rc;
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
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; }
242 ~Averager()
243 { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
244
245 void Add(size_t n) { m_nTotal += n; m_nCount++; }
246
247 private:
248 size_t m_nCount, m_nTotal;
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
260// ===========================================================================
261// wxString class core
262// ===========================================================================
263
264// ---------------------------------------------------------------------------
265// construction
266// ---------------------------------------------------------------------------
267
268// constructs string of <nLength> copies of character <ch>
269wxString::wxString(wxChar ch, size_t nLength)
270{
271 Init();
272
273 if ( nLength > 0 ) {
274 AllocBuffer(nLength);
275
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
280 memset(m_pchData, ch, nLength);
281#endif
282 }
283}
284
285// takes nLength elements of psz starting at nPos
286void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
287{
288 Init();
289
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") );
293
294 nLength = wxStrlen(psz + nPos);
295 }
296
297 STATISTICS_ADD(InitialLength, nLength);
298
299 if ( nLength > 0 ) {
300 // trailing '\0' is written in AllocBuffer()
301 AllocBuffer(nLength);
302 memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar));
303 }
304}
305
306#ifdef wxSTD_STRING_COMPATIBILITY
307
308// poor man's iterators are "void *" pointers
309wxString::wxString(const void *pStart, const void *pEnd)
310{
311 InitWith((const wxChar *)pStart, 0,
312 (const wxChar *)pEnd - (const wxChar *)pStart);
313}
314
315#endif //std::string compatibility
316
317#if wxUSE_UNICODE
318
319// from multibyte string
320wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
321{
322 // first get necessary size
323 size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
324
325 // nLength is number of *Unicode* characters here!
326 if ((nLen != (size_t)-1) && (nLen > nLength))
327 nLen = nLength;
328
329 // empty?
330 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
331 AllocBuffer(nLen);
332 conv.MB2WC(m_pchData, psz, nLen);
333 }
334 else {
335 Init();
336 }
337}
338
339#else // ANSI
340
341#if wxUSE_WCHAR_T
342// from wide string
343wxString::wxString(const wchar_t *pwz, wxMBConv& conv)
344{
345 // first get necessary size
346 size_t nLen = pwz ? conv.WC2MB((char *) NULL, pwz, 0) : 0;
347
348 // empty?
349 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
350 AllocBuffer(nLen);
351 conv.WC2MB(m_pchData, pwz, nLen);
352 }
353 else {
354 Init();
355 }
356}
357#endif // wxUSE_WCHAR_T
358
359#endif // Unicode/ANSI
360
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{
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) );
375
376 STATISTICS_ADD(Length, nLen);
377
378 // allocate memory:
379 // 1) one extra character for '\0' termination
380 // 2) sizeof(wxStringData) for housekeeping info
381 wxStringData* pData = (wxStringData*)
382 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
383 pData->nRefs = 1;
384 pData->nDataLength = nLen;
385 pData->nAllocLength = nLen + EXTRA_ALLOC;
386 m_pchData = pData->data(); // data starts after wxStringData
387 m_pchData[nLen] = wxT('\0');
388}
389
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
397 size_t nLen = pData->nDataLength;
398 AllocBuffer(nLen);
399 memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar));
400 }
401
402 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
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
411 wxStringData* pData = GetStringData();
412 if ( pData->IsShared() || pData->IsEmpty() ) {
413 // can't work with old buffer, get new one
414 pData->Unlock();
415 AllocBuffer(nLen);
416 }
417 else {
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
441 pData->nDataLength = nLen;
442 }
443
444 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
445}
446
447// allocate enough memory for nLen characters
448void wxString::Alloc(size_t nLen)
449{
450 wxStringData *pData = GetStringData();
451 if ( pData->nAllocLength <= nLen ) {
452 if ( pData->IsEmpty() ) {
453 nLen += EXTRA_ALLOC;
454
455 wxStringData* pData = (wxStringData*)
456 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
457 pData->nRefs = 1;
458 pData->nDataLength = 0;
459 pData->nAllocLength = nLen;
460 m_pchData = pData->data(); // data starts after wxStringData
461 m_pchData[0u] = wxT('\0');
462 }
463 else if ( pData->IsShared() ) {
464 pData->Unlock(); // memory not freed because shared
465 size_t nOldLen = pData->nDataLength;
466 AllocBuffer(nLen);
467 memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar));
468 }
469 else {
470 nLen += EXTRA_ALLOC;
471
472 wxStringData *pDataOld = pData;
473 wxStringData *p = (wxStringData *)
474 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
475
476 if ( p == NULL ) {
477 // don't leak memory
478 free(pDataOld);
479
480 // FIXME what to do on memory error?
481 return;
482 }
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();
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();
497
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;
508
509 m_pchData = pData->data();
510 }
511
512 pData->nAllocLength = nLen;
513}
514
515// get the pointer to writable buffer of (at least) nLen bytes
516wxChar *wxString::GetWriteBuf(size_t nLen)
517{
518 AllocBeforeWrite(nLen);
519
520 wxASSERT( GetStringData()->nRefs == 1 );
521 GetStringData()->Validate(FALSE);
522
523 return m_pchData;
524}
525
526// put string back in a reasonable state after GetWriteBuf
527void wxString::UngetWriteBuf()
528{
529 GetStringData()->nDataLength = wxStrlen(m_pchData);
530 GetStringData()->Validate(TRUE);
531}
532
533void wxString::UngetWriteBuf(size_t nLen)
534{
535 GetStringData()->nDataLength = nLen;
536 GetStringData()->Validate(TRUE);
537}
538
539// ---------------------------------------------------------------------------
540// data access
541// ---------------------------------------------------------------------------
542
543// all functions are inline in string.h
544
545// ---------------------------------------------------------------------------
546// assignment operators
547// ---------------------------------------------------------------------------
548
549// helper function: does real copy
550void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
551{
552 if ( nSrcLen == 0 ) {
553 Reinit();
554 }
555 else {
556 AllocBeforeWrite(nSrcLen);
557 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
558 GetStringData()->nDataLength = nSrcLen;
559 m_pchData[nSrcLen] = wxT('\0');
560 }
561}
562
563// assigns one string to another
564wxString& wxString::operator=(const wxString& stringSrc)
565{
566 wxASSERT( stringSrc.GetStringData()->IsValid() );
567
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
585wxString& wxString::operator=(wxChar ch)
586{
587 AssignCopy(1, &ch);
588 return *this;
589}
590
591
592// assigns C string
593wxString& wxString::operator=(const wxChar *psz)
594{
595 AssignCopy(wxStrlen(psz), psz);
596 return *this;
597}
598
599#if !wxUSE_UNICODE
600
601// same as 'signed char' variant
602wxString& wxString::operator=(const unsigned char* psz)
603{
604 *this = (const char *)psz;
605 return *this;
606}
607
608#if wxUSE_WCHAR_T
609wxString& wxString::operator=(const wchar_t *pwz)
610{
611 wxString str(pwz);
612 *this = str;
613 return *this;
614}
615#endif
616
617#endif
618
619// ---------------------------------------------------------------------------
620// string concatenation
621// ---------------------------------------------------------------------------
622
623// add something to this string
624void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData)
625{
626 STATISTICS_ADD(SummandLength, nSrcLen);
627
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;
633
634 // alloc new buffer if current is too small
635 if ( pData->IsShared() ) {
636 STATISTICS_ADD(ConcatHit, 0);
637
638 // we have to allocate another buffer
639 wxStringData* pOldData = GetStringData();
640 AllocBuffer(nNewLen);
641 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
642 pOldData->Unlock();
643 }
644 else if ( nNewLen > pData->nAllocLength ) {
645 STATISTICS_ADD(ConcatHit, 0);
646
647 // we have to grow the buffer
648 Alloc(nNewLen);
649 }
650 else {
651 STATISTICS_ADD(ConcatHit, 1);
652
653 // the buffer is already big enough
654 }
655
656 // should be enough space
657 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
658
659 // fast concatenation - all is done in our buffer
660 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
661
662 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
663 GetStringData()->nDataLength = nNewLen; // and fix the length
664 }
665 //else: the string to append was empty
666}
667
668/*
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{
677 wxASSERT( string1.GetStringData()->IsValid() );
678 wxASSERT( string2.GetStringData()->IsValid() );
679
680 wxString s = string1;
681 s += string2;
682
683 return s;
684}
685
686wxString operator+(const wxString& string, wxChar ch)
687{
688 wxASSERT( string.GetStringData()->IsValid() );
689
690 wxString s = string;
691 s += ch;
692
693 return s;
694}
695
696wxString operator+(wxChar ch, const wxString& string)
697{
698 wxASSERT( string.GetStringData()->IsValid() );
699
700 wxString s = ch;
701 s += string;
702
703 return s;
704}
705
706wxString operator+(const wxString& string, const wxChar *psz)
707{
708 wxASSERT( string.GetStringData()->IsValid() );
709
710 wxString s;
711 s.Alloc(wxStrlen(psz) + string.Len());
712 s = string;
713 s += psz;
714
715 return s;
716}
717
718wxString operator+(const wxChar *psz, const wxString& string)
719{
720 wxASSERT( string.GetStringData()->IsValid() );
721
722 wxString s;
723 s.Alloc(wxStrlen(psz) + string.Len());
724 s = psz;
725 s += string;
726
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{
741 if ( nCopyLen == 0 ) {
742 dest.Init();
743 }
744 else {
745 dest.AllocBuffer(nCopyLen);
746 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
747 }
748}
749
750// extract string of length nCount starting at nFirst
751wxString wxString::Mid(size_t nFirst, size_t nCount) const
752{
753 wxStringData *pData = GetStringData();
754 size_t nLen = pData->nDataLength;
755
756 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
757 if ( nCount == wxSTRING_MAXLEN )
758 {
759 nCount = nLen - nFirst;
760 }
761
762 // out-of-bounds requests return sensible things
763 if ( nFirst + nCount > nLen )
764 {
765 nCount = nLen - nFirst;
766 }
767
768 if ( nFirst > nLen )
769 {
770 // AllocCopy() will return empty string
771 nCount = 0;
772 }
773
774 wxString dest;
775 AllocCopy(dest, nCount, nFirst);
776
777 return dest;
778}
779
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
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)
822wxString wxString::AfterLast(wxChar ch) const
823{
824 wxString str;
825 int iPos = Find(ch, TRUE);
826 if ( iPos == wxNOT_FOUND )
827 str = *this;
828 else
829 str = c_str() + iPos + 1;
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)
847wxString wxString::BeforeFirst(wxChar ch) const
848{
849 wxString str;
850 for ( const wxChar *pc = m_pchData; *pc != wxT('\0') && *pc != ch; pc++ )
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)
858wxString wxString::BeforeLast(wxChar ch) const
859{
860 wxString str;
861 int iPos = Find(ch, TRUE);
862 if ( iPos != wxNOT_FOUND && iPos != 0 )
863 str = wxString(c_str(), iPos);
864
865 return str;
866}
867
868/// get all characters after the first occurence of ch
869/// (returns empty string if ch not found)
870wxString wxString::AfterFirst(wxChar ch) const
871{
872 wxString str;
873 int iPos = Find(ch);
874 if ( iPos != wxNOT_FOUND )
875 str = c_str() + iPos + 1;
876
877 return str;
878}
879
880// replace first (or all) occurences of some substring with another one
881size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
882{
883 size_t uiCount = 0; // count of replacements made
884
885 size_t uiOldLen = wxStrlen(szOld);
886
887 wxString strTemp;
888 const wxChar *pCurrent = m_pchData;
889 const wxChar *pSubstr;
890 while ( *pCurrent != wxT('\0') ) {
891 pSubstr = wxStrstr(pCurrent, szOld);
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{
924 const wxChar *s = (const wxChar*) *this;
925 while(*s){
926 if(!isascii(*s)) return(FALSE);
927 s++;
928 }
929 return(TRUE);
930}
931
932bool wxString::IsWord() const
933{
934 const wxChar *s = (const wxChar*) *this;
935 while(*s){
936 if(!wxIsalpha(*s)) return(FALSE);
937 s++;
938 }
939 return(TRUE);
940}
941
942bool wxString::IsNumber() const
943{
944 const wxChar *s = (const wxChar*) *this;
945 if (wxStrlen(s))
946 if ((s[0] == '-') || (s[0] == '+')) s++;
947 while(*s){
948 if(!wxIsdigit(*s)) return(FALSE);
949 s++;
950 }
951 return(TRUE);
952}
953
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
962// ---------------------------------------------------------------------------
963// case conversion
964// ---------------------------------------------------------------------------
965
966wxString& wxString::MakeUpper()
967{
968 CopyBeforeWrite();
969
970 for ( wxChar *p = m_pchData; *p; p++ )
971 *p = (wxChar)wxToupper(*p);
972
973 return *this;
974}
975
976wxString& wxString::MakeLower()
977{
978 CopyBeforeWrite();
979
980 for ( wxChar *p = m_pchData; *p; p++ )
981 *p = (wxChar)wxTolower(*p);
982
983 return *this;
984}
985
986// ---------------------------------------------------------------------------
987// trimming and padding
988// ---------------------------------------------------------------------------
989
990// some compilers (VC++ 6.0 not to name them) return TRUE for a call to
991