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