]> git.saurik.com Git - wxWidgets.git/blob - src/common/string.cpp
compilation fix for other (than GTK/MSW) ports
[wxWidgets.git] / src / common / string.cpp
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 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
74 // must define this static for VA or else you get multiply defined symbols
75 // everywhere
76 const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100;
77 #endif // Visual Age
78
79 #ifdef wxSTD_STRING_COMPATIBILITY
80 const size_t wxString::npos = wxSTRING_MAXLEN;
81 #endif // wxSTD_STRING_COMPATIBILITY
82
83 // ----------------------------------------------------------------------------
84 // static data
85 // ----------------------------------------------------------------------------
86
87 // for an empty string, GetStringData() will return this address: this
88 // structure has the same layout as wxStringData and it's data() method will
89 // return the empty string (dummy pointer)
90 static const struct
91 {
92 wxStringData data;
93 wxChar dummy;
94 } g_strEmpty = { {-1, 0, 0}, wxT('\0') };
95
96 // empty C style string: points to 'string data' byte of g_strEmpty
97 extern 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
158 wxSTD 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
189 wxSTD 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
197 extern 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
224 extern 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>
275 wxString::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
292 void 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
315 wxString::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
326 wxString::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
349 wxString::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
372 void 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
397 void 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
412 void 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
454 void 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)
500 void 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
519 wxChar *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
530 void wxString::UngetWriteBuf()
531 {
532 GetStringData()->nDataLength = wxStrlen(m_pchData);
533 GetStringData()->Validate(TRUE);
534 }
535
536 void 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
553 void 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
567 wxString& 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
588 wxString& wxString::operator=(wxChar ch)
589 {
590 AssignCopy(1, &ch);
591 return *this;
592 }
593
594
595 // assigns C string
596 wxString& 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
605 wxString& wxString::operator=(const unsigned char* psz)
606 {
607 *this = (const char *)psz;
608 return *this;
609 }
610
611 #if wxUSE_WCHAR_T
612 wxString& 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
627 void 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
678 wxString 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
689 wxString 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
699 wxString 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
709 wxString 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
721 wxString 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
742 void 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
754 wxString 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
785 bool 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
813 wxString 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)
825 wxString 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
838 wxString 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)
850 wxString 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)
861 wxString 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)
873 wxString 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
884 size_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
925 bool 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
935 bool 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
945 bool 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
957 wxString 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
969 wxString& 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
979 wxString& 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 // isspace('ê') in the C locale which seems to be broken to me, but we have to
995 // live with this by checking that the character is a 7 bit one - even if this
996 // may fail to detect some spaces (I don't know if Unicode doesn't have
997 // space-like symbols somewhere except in the first 128 chars), it is arguably
998 // still better than trimming away accented letters
999 inline int wxSafeIsspace(wxChar ch) { return (ch < 127) && wxIsspace(ch); }
1000
1001 // trims spaces (in the sense of isspace) from left or right side
1002 wxString& wxString::Trim(bool bFromRight)
1003 {
1004 // first check if we're going to modify the string at all
1005 if ( !IsEmpty() &&
1006 (
1007 (bFromRight && wxSafeIsspace(GetChar(Len() - 1))) ||
1008 (!bFromRight && wxSafeIsspace(GetChar(0u)))
1009 )
1010 )
1011 {
1012 // ok, there is at least one space to trim
1013 CopyBeforeWrite();
1014
1015 if ( bFromRight )
1016 {
1017 // find last non-space character
1018 wxChar *psz = m_pchData + GetStringData()->nDataLength - 1;
1019 while ( wxSafeIsspace(*psz) && (psz >= m_pchData) )
1020 psz--;
1021
1022 // truncate at trailing space start
1023 *++psz = wxT('\0');
1024 GetStringData()->nDataLength = psz - m_pchData;
1025 }
1026 else
1027 {
1028 // find first non-space character
1029 const wxChar *psz = m_pchData;
1030 while ( wxSafeIsspace(*psz) )
1031 psz++;
1032
1033 // fix up data and length
1034 int nDataLength = GetStringData()->nDataLength - (psz - (const wxChar*) m_pchData);
1035 memmove(m_pchData, psz, (nDataLength + 1)*sizeof(wxChar));
1036 GetStringData()->nDataLength = nDataLength;
1037 }
1038 }
1039
1040 return *this;
1041 }
1042
1043 // adds nCount characters chPad to the string from either side
1044 wxString& wxString::Pad(size_t nCount, wxChar chPad, bool bFromRight)
1045 {
1046 wxString s(chPad, nCount);
1047
1048 if ( bFromRight )
1049 *this += s;
1050 else
1051 {
1052 s += *this;
1053 *this = s;
1054 }
1055
1056 return *this;
1057 }
1058
1059 // truncate the string
1060 wxString& wxString::Truncate(size_t uiLen)
1061 {
1062 if ( uiLen < Len() ) {
1063 CopyBeforeWrite();
1064
1065 *(m_pchData + uiLen) = wxT('\0');
1066 GetStringData()->nDataLength = uiLen;
1067 }
1068 //else: nothing to do, string is already short enough
1069
1070 return *this;
1071 }
1072
1073 // ---------------------------------------------------------------------------
1074 // finding (return wxNOT_FOUND if not found and index otherwise)
1075 // ---------------------------------------------------------------------------
1076
1077 // find a character
1078 int wxString::Find(wxChar ch, bool bFromEnd) const
1079 {
1080 const wxChar *psz = bFromEnd ? wxStrrchr(m_pchData, ch) : wxStrchr(m_pchData, ch);
1081
1082 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
1083 }
1084
1085 // find a sub-string (like strstr)
1086 int wxString::Find(const wxChar *pszSub) const
1087 {
1088 const wxChar *psz = wxStrstr(m_pchData, pszSub);
1089
1090 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
1091 }
1092
1093 // ----------------------------------------------------------------------------
1094 // conversion to numbers
1095 // ----------------------------------------------------------------------------
1096
1097 bool wxString::ToLong(long *val) const
1098 {
1099 wxCHECK_MSG( val, FALSE, _T("NULL pointer in wxString::ToLong") );
1100
1101 const wxChar *start = c_str();
1102 wxChar *end;
1103 *val = wxStrtol(start, &end, 10);
1104
1105 // return TRUE only if scan was stopped by the terminating NUL and if the
1106 // string was not empty to start with
1107 return !*end && (end != start);
1108 }
1109
1110 bool wxString::ToULong(unsigned long *val) const
1111 {
1112 wxCHECK_MSG( val, FALSE, _T("NULL pointer in wxString::ToULong") );
1113
1114 const wxChar *start = c_str();
1115 wxChar *end;
1116 *val = wxStrtoul(start, &end, 10);
1117
1118 // return TRUE only if scan was stopped by the terminating NUL and if the
1119 // string was not empty to start with
1120 return !*end && (end != start);
1121 }
1122
1123 bool wxString::ToDouble(double *val) const
1124 {
1125 wxCHECK_MSG( val, FALSE, _T("NULL pointer in wxString::ToDouble") );
1126
1127 const wxChar *start = c_str();
1128 wxChar *end;
1129 *val = wxStrtod(start, &end);
1130
1131 // return TRUE only if scan was stopped by the terminating NUL and if the
1132 // string was not empty to start with
1133 return !*end && (end != start);
1134 }
1135
1136 // ---------------------------------------------------------------------------
1137 // formatted output
1138 // ---------------------------------------------------------------------------
1139
1140 /* static */
1141 wxString wxString::Format(const wxChar *pszFormat, ...)
1142 {
1143 va_list argptr;
1144 va_start(argptr, pszFormat);
1145
1146 wxString s;
1147 s.PrintfV(pszFormat, argptr);
1148
1149 va_end(argptr);
1150
1151 return s;
1152 }
1153
1154 /* static */
1155 wxString wxString::FormatV(const wxChar *pszFormat, va_list argptr)
1156 {
1157 wxString s;
1158 s.PrintfV(pszFormat, argptr);
1159 return s;
1160 }
1161
1162 int wxString::Printf(const wxChar *pszFormat, ...)
1163 {
1164 va_list argptr;
1165 va_start(argptr, pszFormat);
1166
1167 int iLen = PrintfV(pszFormat, argptr);
1168
1169 va_end(argptr);
1170
1171 return iLen;
1172 }
1173
1174 int wxString::PrintfV(const wxChar* pszFormat, va_list argptr)
1175 {
1176 #if wxUSE_EXPERIMENTAL_PRINTF
1177 // the new implementation
1178
1179 // buffer to avoid dynamic memory allocation each time for small strings
1180 char szScratch[1024];
1181
1182 Reinit();
1183 for (size_t n = 0; pszFormat[n]; n++)
1184 if (pszFormat[n] == wxT('%')) {
1185 static char s_szFlags[256] = "%";
1186 size_t flagofs = 1;
1187 bool adj_left = FALSE, in_prec = FALSE,
1188 prec_dot = FALSE, done = FALSE;
1189 int ilen = 0;
1190 size_t min_width = 0, max_width = wxSTRING_MAXLEN;
1191 do {
1192 #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1193 switch (pszFormat[++n]) {
1194 case wxT('\0'):
1195 done = TRUE;
1196 break;
1197 case wxT('%'):
1198 *this += wxT('%');
1199 done = TRUE;
1200 break;
1201 case wxT('#'):
1202 case wxT('0'):
1203 case wxT(' '):
1204 case wxT('+'):
1205 case wxT('\''):
1206 CHECK_PREC
1207 s_szFlags[flagofs++] = pszFormat[n];
1208 break;
1209 case wxT('-'):
1210 CHECK_PREC
1211 adj_left = TRUE;
1212 s_szFlags[flagofs++] = pszFormat[n];
1213 break;
1214 case wxT('.'):
1215 CHECK_PREC
1216 in_prec = TRUE;
1217 prec_dot = FALSE;
1218 max_width = 0;
1219 // dot will be auto-added to s_szFlags if non-negative number follows
1220 break;
1221 case wxT('h'):
1222 ilen = -1;
1223 CHECK_PREC
1224 s_szFlags[flagofs++] = pszFormat[n];
1225 break;
1226 case wxT('l'):
1227 ilen = 1;
1228 CHECK_PREC
1229 s_szFlags[flagofs++] = pszFormat[n];
1230 break;
1231 case wxT('q'):
1232 case wxT('L'):
1233 ilen = 2;
1234 CHECK_PREC
1235 s_szFlags[flagofs++] = pszFormat[n];
1236 break;
1237 case wxT('Z'):
1238 ilen = 3;
1239 CHECK_PREC
1240 s_szFlags[flagofs++] = pszFormat[n];
1241 break;
1242 case wxT('*'):
1243 {
1244 int len = va_arg(argptr, int);
1245 if (in_prec) {
1246 if (len<0) break;
1247 CHECK_PREC
1248 max_width = len;
1249 } else {
1250 if (len<0) {
1251 adj_left = !adj_left;
1252 s_szFlags[flagofs++] = '-';
1253 len = -len;
1254 }
1255 min_width = len;
1256 }
1257 flagofs += ::sprintf(s_szFlags+flagofs,"%d",len);
1258 }
1259 break;
1260 case wxT('1'): case wxT('2'): case wxT('3'):
1261 case wxT('4'): case wxT('5'): case wxT('6'):
1262 case wxT('7'): case wxT('8'): case wxT('9'):
1263 {
1264 int len = 0;
1265 CHECK_PREC
1266 while ((pszFormat[n]>=wxT('0')) && (pszFormat[n]<=wxT('9'))) {
1267 s_szFlags[flagofs++] = pszFormat[n];
1268 len = len*10 + (pszFormat[n] - wxT('0'));
1269 n++;
1270 }
1271 if (in_prec) max_width = len;
1272 else min_width = len;
1273 n--; // the main loop pre-increments n again
1274 }
1275 break;
1276 case wxT('d'):
1277 case wxT('i'):
1278 case wxT('o'):
1279 case wxT('u'):
1280 case wxT('x'):
1281 case wxT('X'):
1282 CHECK_PREC
1283 s_szFlags[flagofs++] = pszFormat[n];
1284 s_szFlags[flagofs] = '\0';
1285 if (ilen == 0 ) {
1286 int val = va_arg(argptr, int);
1287 ::sprintf(szScratch, s_szFlags, val);
1288 }
1289 else if (ilen == -1) {
1290 short int val = va_arg(argptr, short int);
1291 ::sprintf(szScratch, s_szFlags, val);
1292 }
1293 else if (ilen == 1) {
1294 long int val = va_arg(argptr, long int);
1295 ::sprintf(szScratch, s_szFlags, val);
1296 }
1297 else if (ilen == 2) {
1298 #if SIZEOF_LONG_LONG
1299 long long int val = va_arg(argptr, long long int);
1300 ::sprintf(szScratch, s_szFlags, val);
1301 #else
1302 long int val = va_arg(argptr, long int);
1303 ::sprintf(szScratch, s_szFlags, val);
1304 #endif
1305 }
1306 else if (ilen == 3) {
1307 size_t val = va_arg(argptr, size_t);
1308 ::sprintf(szScratch, s_szFlags, val);
1309 }
1310 *this += wxString(szScratch);
1311 done = TRUE;
1312 break;
1313 case wxT('e'):
1314 case wxT('E'):
1315 case wxT('f'):
1316 case wxT('g'):
1317 case wxT('G'):
1318 CHECK_PREC
1319 s_szFlags[flagofs++] = pszFormat[n];
1320 s_szFlags[flagofs] = '\0';
1321 if (ilen == 2) {
1322 long double val = va_arg(argptr, long double);
1323 ::sprintf(szScratch, s_szFlags, val);
1324 } else {
1325 double val = va_arg(argptr, double);
1326 ::sprintf(szScratch, s_szFlags, val);
1327 }
1328 *this += wxString(szScratch);
1329 done = TRUE;
1330 break;
1331 case wxT('p'):
1332 {
1333 void *val = va_arg(argptr, void *);
1334 CHECK_PREC
1335 s_szFlags[flagofs++] = pszFormat[n];
1336 s_szFlags[flagofs] = '\0';
1337 ::sprintf(szScratch, s_szFlags, val);
1338 *this += wxString(szScratch);
1339 done = TRUE;
1340 }
1341 break;
1342 case wxT('c'):
1343 {
1344 wxChar val = va_arg(argptr, int);
1345 // we don't need to honor padding here, do we?
1346 *this += val;
1347 done = TRUE;
1348 }
1349 break;
1350 case wxT('s'):
1351 if (ilen == -1) {
1352 // wx extension: we'll let %hs mean non-Unicode strings
1353 char *val = va_arg(argptr, char *);
1354 #if wxUSE_UNICODE
1355 // ASCII->Unicode constructor handles max_width right
1356 wxString s(val, wxConvLibc, max_width);
1357 #else
1358 size_t len = wxSTRING_MAXLEN;
1359 if (val) {
1360 for (len = 0; val[len] && (len<max_width); len++);
1361 } else val = wxT("(null)");
1362 wxString s(val, len);
1363 #endif
1364 if (s.Len() < min_width)
1365 s.Pad(min_width - s.Len(), wxT(' '), adj_left);
1366 *this += s;
1367 } else {
1368 wxChar *val = va_arg(argptr, wxChar *);
1369 size_t len = wxSTRING_MAXLEN;
1370 if (val) {
1371 for (len = 0; val[len] && (len<max_width); len++);
1372 } else val = wxT("(null)");
1373 wxString s(val, len);
1374 if (s.Len() < min_width)
1375 s.Pad(min_width - s.Len(), wxT(' '), adj_left);
1376 *this += s;
1377 }
1378 done = TRUE;
1379 break;
1380 case wxT('n'):
1381 if (ilen == 0) {
1382 int *val = va_arg(argptr, int *);
1383 *val = Len();
1384 }
1385 else if (ilen == -1) {
1386 short int *val = va_arg(argptr, short int *);
1387 *val = Len();
1388 }
1389 else if (ilen >= 1) {
1390 long int *val = va_arg(argptr, long int *);
1391 *val = Len();
1392 }
1393 done = TRUE;
1394 break;
1395 default:
1396 if (wxIsalpha(pszFormat[n]))
1397 // probably some flag not taken care of here yet
1398 s_szFlags[flagofs++] = pszFormat[n];
1399 else {
1400 // bad format
1401 *this += wxT('%'); // just to pass the glibc tst-printf.c
1402 n--;
1403 done = TRUE;
1404 }
1405 break;
1406 }
1407 #undef CHECK_PREC
1408 } while (!done);
1409 } else *this += pszFormat[n];
1410
1411 #else
1412 // buffer to avoid dynamic memory allocation each time for small strings
1413 char szScratch[1024];
1414
1415 // NB: wxVsnprintf() may return either less than the buffer size or -1 if
1416 // there is not enough place depending on implementation
1417 int iLen = wxVsnprintfA(szScratch, WXSIZEOF(szScratch), (char *)pszFormat, argptr);
1418 if ( iLen != -1 ) {
1419 // the whole string is in szScratch
1420 *this = szScratch;
1421 }
1422 else {
1423 bool outOfMemory = FALSE;
1424 int size = 2*WXSIZEOF(szScratch);
1425 while ( !outOfMemory ) {
1426 char *buf = GetWriteBuf(size);
1427 if ( buf )
1428 iLen = wxVsnprintfA(buf, size, pszFormat, argptr);
1429 else
1430 outOfMemory = TRUE;
1431
1432 UngetWriteBuf();
1433
1434 if ( iLen != -1 ) {
1435 // ok, there was enough space
1436 break;
1437 }
1438
1439 // still not enough, double it again
1440 size *= 2;
1441 }
1442
1443 if ( outOfMemory ) {
1444 // out of memory
1445 return -1;
1446 }
1447 }
1448 #endif // wxUSE_EXPERIMENTAL_PRINTF/!wxUSE_EXPERIMENTAL_PRINTF
1449
1450 return Len();
1451 }
1452
1453 // ----------------------------------------------------------------------------
1454 // misc other operations
1455 // ----------------------------------------------------------------------------
1456
1457 // returns TRUE if the string matches the pattern which may contain '*' and
1458 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1459 // of them)
1460 bool wxString::Matches(const wxChar *pszMask) const
1461 {
1462 #if wxUSE_REGEX
1463 // first translate the shell-like mask into a regex
1464 wxString pattern;
1465 pattern.reserve(wxStrlen(pszMask));
1466
1467 pattern += _T('^');
1468 while ( *pszMask )
1469 {
1470 switch ( *pszMask )
1471 {
1472 case _T('?'):
1473 pattern += _T('.');
1474 break;
1475
1476 case _T('*'):
1477 pattern += _T(".*");
1478 break;
1479
1480 case _T('^'):
1481 case _T('.'):
1482 case _T('$'):
1483 case _T('('):
1484 case _T(')'):
1485 case _T('|'):
1486 case _T('+'):
1487 case _T('\\'):
1488 // these characters are special in a RE, quote them
1489 // (however note that we don't quote '[' and ']' to allow
1490 // using them for Unix shell like matching)
1491 pattern += _T('\\');
1492 // fall through
1493
1494 default:
1495 pattern += *pszMask;
1496 }
1497
1498 pszMask++;
1499 }
1500 pattern += _T('$');
1501
1502 // and now use it
1503 return wxRegEx(pattern, wxRE_NOSUB | wxRE_EXTENDED).Matches(c_str());
1504 #else // !wxUSE_REGEX
1505 // TODO: this is, of course, awfully inefficient...
1506
1507 // the char currently being checked
1508 const wxChar *pszTxt = c_str();
1509
1510 // the last location where '*' matched
1511 const wxChar *pszLastStarInText = NULL;
1512 const wxChar *pszLastStarInMask = NULL;
1513
1514 match:
1515 for ( ; *pszMask != wxT('\0'); pszMask++, pszTxt++ ) {
1516 switch ( *pszMask ) {
1517 case wxT('?'):
1518 if ( *pszTxt == wxT('\0') )
1519 return FALSE;
1520
1521 // pszTxt and pszMask will be incremented in the loop statement
1522
1523 break;
1524
1525 case wxT('*'):
1526 {
1527 // remember where we started to be able to backtrack later
1528 pszLastStarInText = pszTxt;
1529 pszLastStarInMask = pszMask;
1530
1531 // ignore special chars immediately following this one
1532 // (should this be an error?)
1533 while ( *pszMask == wxT('*') || *pszMask == wxT('?') )
1534 pszMask++;
1535
1536 // if there is nothing more, match
1537 if ( *pszMask == wxT('\0') )
1538 return TRUE;
1539
1540 // are there any other metacharacters in the mask?
1541 size_t uiLenMask;
1542 const wxChar *pEndMask = wxStrpbrk(pszMask, wxT("*?"));
1543
1544 if ( pEndMask != NULL ) {
1545 // we have to match the string between two metachars
1546 uiLenMask = pEndMask - pszMask;
1547 }
1548 else {
1549 // we have to match the remainder of the string
1550 uiLenMask = wxStrlen(pszMask);
1551 }
1552
1553 wxString strToMatch(pszMask, uiLenMask);
1554 const wxChar* pMatch = wxStrstr(pszTxt, strToMatch);
1555 if ( pMatch == NULL )
1556 return FALSE;
1557
1558 // -1 to compensate "++" in the loop
1559 pszTxt = pMatch + uiLenMask - 1;
1560 pszMask += uiLenMask - 1;
1561 }
1562 break;
1563
1564 default:
1565 if ( *pszMask != *pszTxt )
1566 return FALSE;
1567 break;
1568 }
1569 }
1570
1571 // match only if nothing left
1572 if ( *pszTxt == wxT('\0') )
1573 return TRUE;
1574
1575 // if we failed to match, backtrack if we can
1576 if ( pszLastStarInText ) {
1577 pszTxt = pszLastStarInText + 1;
1578 pszMask = pszLastStarInMask;
1579
1580 pszLastStarInText = NULL;
1581
1582 // don't bother resetting pszLastStarInMask, it's unnecessary
1583
1584 goto match;
1585 }
1586
1587 return FALSE;
1588 #endif // wxUSE_REGEX/!wxUSE_REGEX
1589 }
1590
1591 // Count the number of chars
1592 int wxString::Freq(wxChar ch) const
1593 {
1594 int count = 0;
1595 int len = Len();
1596 for (int i = 0; i < len; i++)
1597 {
1598 if (GetChar(i) == ch)
1599 count ++;
1600 }
1601 return count;
1602 }
1603
1604 // convert to upper case, return the copy of the string
1605 wxString wxString::Upper() const
1606 { wxString s(*this); return s.MakeUpper(); }
1607
1608 // convert to lower case, return the copy of the string
1609 wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); }
1610
1611 int wxString::sprintf(const wxChar *pszFormat, ...)
1612 {
1613 va_list argptr;
1614 va_start(argptr, pszFormat);
1615 int iLen = PrintfV(pszFormat, argptr);
1616 va_end(argptr);
1617 return iLen;
1618 }
1619
1620 // ---------------------------------------------------------------------------
1621 // standard C++ library string functions
1622 // ---------------------------------------------------------------------------
1623
1624 #ifdef wxSTD_STRING_COMPATIBILITY
1625
1626 void wxString::resize(size_t nSize, wxChar ch)
1627 {
1628 size_t len = length();
1629
1630 if ( nSize < len )
1631 {
1632 Truncate(nSize);
1633 }
1634 else if ( nSize > len )
1635 {
1636 *this += wxString(ch, len - nSize);
1637 }
1638 //else: we have exactly the specified length, nothing to do
1639 }
1640
1641 void wxString::swap(wxString& str)
1642 {
1643 // this is slightly less efficient than fiddling with m_pchData directly,
1644 // but it is still quite efficient as we don't copy the string here because
1645 // ref count always stays positive
1646 wxString tmp = str;
1647 str = *this;
1648 *this = str;
1649 }
1650
1651 wxString& wxString::insert(size_t nPos, const wxString& str)
1652 {
1653 wxASSERT( str.GetStringData()->IsValid() );
1654 wxASSERT( nPos <= Len() );
1655
1656 if ( !str.IsEmpty() ) {
1657 wxString strTmp;
1658 wxChar *pc = strTmp.GetWriteBuf(Len() + str.Len());
1659 wxStrncpy(pc, c_str(), nPos);
1660 wxStrcpy(pc + nPos, str);
1661 wxStrcpy(pc + nPos + str.Len(), c_str() + nPos);
1662 strTmp.UngetWriteBuf();
1663 *this = strTmp;
1664 }
1665
1666 return *this;
1667 }
1668
1669 size_t wxString::find(const wxString& str, size_t nStart) const
1670 {
1671 wxASSERT( str.GetStringData()->IsValid() );
1672 wxASSERT( nStart <= Len() );
1673
1674 const wxChar *p = wxStrstr(c_str() + nStart, str);
1675
1676 return p == NULL ? npos : p - c_str();
1677 }
1678
1679 // VC++ 1.5 can't cope with the default argument in the header.
1680 #if !defined(__VISUALC__) || defined(__WIN32__)
1681 size_t wxString::find(const wxChar* sz, size_t nStart, size_t n) const
1682 {
1683 return find(wxString(sz, n), nStart);
1684 }
1685 #endif // VC++ 1.5
1686
1687 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1688 #if !defined(__BORLANDC__)
1689 size_t wxString::find(wxChar ch, size_t nStart) const
1690 {
1691 wxASSERT( nStart <= Len() );
1692
1693 const wxChar *p = wxStrchr(c_str() + nStart, ch);
1694
1695 return p == NULL ? npos : p - c_str();
1696 }
1697 #endif
1698
1699 size_t wxString::rfind(const wxString& str, size_t nStart) const
1700 {
1701 wxASSERT( str.GetStringData()->IsValid() );
1702 wxASSERT( nStart <= Len() );
1703
1704 // TODO could be made much quicker than that
1705 const wxChar *p = c_str() + (nStart == npos ? Len() : nStart);
1706 while ( p >= c_str() + str.Len() ) {
1707 if ( wxStrncmp(p - str.Len(), str, str.Len()) == 0 )
1708 return p - str.Len() - c_str();
1709 p--;
1710 }
1711
1712 return npos;
1713 }
1714
1715 // VC++ 1.5 can't cope with the default argument in the header.
1716 #if !defined(__VISUALC__) || defined(__WIN32__)
1717 size_t wxString::rfind(const wxChar* sz, size_t nStart, size_t n) const
1718 {
1719 return rfind(wxString(sz, n == npos ? 0 : n), nStart);
1720 }
1721
1722 size_t wxString::rfind(wxChar ch, size_t nStart) const
1723 {
1724 if ( nStart == npos )
1725 {
1726 nStart = Len();
1727 }
1728 else
1729 {
1730 wxASSERT( nStart <= Len() );
1731 }
1732
1733 const wxChar *p = wxStrrchr(c_str(), ch);
1734
1735 if ( p == NULL )
1736 return npos;
1737
1738 size_t result = p - c_str();
1739 return ( result > nStart ) ? npos : result;
1740 }
1741 #endif // VC++ 1.5
1742
1743 size_t wxString::find_first_of(const wxChar* sz, size_t nStart) const
1744 {
1745 const wxChar *start = c_str() + nStart;
1746 const wxChar *firstOf = wxStrpbrk(start, sz);
1747 if ( firstOf )
1748 return firstOf - c_str();
1749 else
1750 return npos;
1751 }
1752
1753 size_t wxString::find_last_of(const wxChar* sz, size_t nStart) const
1754 {
1755 if ( nStart == npos )
1756 {
1757 nStart = Len();
1758 }
1759 else
1760 {
1761 wxASSERT( nStart <= Len() );
1762 }
1763
1764 for ( const wxChar *p = c_str() + length() - 1; p >= c_str(); p-- )
1765 {
1766 if ( wxStrchr(sz, *p) )
1767 return p - c_str();
1768 }
1769
1770 return npos;
1771 }
1772
1773 size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart) const
1774 {
1775 if ( nStart == npos )
1776 {
1777 nStart = Len();
1778 }
1779 else
1780 {
1781 wxASSERT( nStart <= Len() );
1782 }
1783
1784 size_t nAccept = wxStrspn(c_str() + nStart, sz);
1785 if ( nAccept >= length() - nStart )
1786 return npos;
1787 else
1788 return nAccept;
1789 }
1790
1791 size_t wxString::find_first_not_of(wxChar ch, size_t nStart) const
1792 {
1793 wxASSERT( nStart <= Len() );
1794
1795 for ( const wxChar *p = c_str() + nStart; *p; p++ )
1796 {
1797 if ( *p != ch )
1798 return p - c_str();
1799 }
1800
1801 return npos;
1802 }
1803
1804 size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart) const
1805 {
1806 if ( nStart == npos )
1807 {
1808 nStart = Len();
1809 }
1810 else
1811 {
1812 wxASSERT( nStart <= Len() );
1813 }
1814
1815 for ( const wxChar *p = c_str() + nStart - 1; p >= c_str(); p-- )
1816 {
1817 if ( !wxStrchr(sz, *p) )
1818 return p - c_str();
1819 }
1820
1821 return npos;
1822 }
1823
1824 size_t wxString::find_last_not_of(wxChar ch, size_t nStart) const
1825 {
1826 if ( nStart == npos )
1827 {
1828 nStart = Len();
1829 }
1830 else
1831 {
1832 wxASSERT( nStart <= Len() );
1833 }
1834
1835 for ( const wxChar *p = c_str() + nStart - 1; p >= c_str(); p-- )
1836 {
1837 if ( *p != ch )
1838 return p - c_str();
1839 }
1840
1841 return npos;
1842 }
1843
1844 wxString& wxString::erase(size_t nStart, size_t nLen)
1845 {
1846 wxString strTmp(c_str(), nStart);
1847 if ( nLen != npos ) {
1848 wxASSERT( nStart + nLen <= Len() );
1849
1850 strTmp.append(c_str() + nStart + nLen);
1851 }
1852
1853 *this = strTmp;
1854 return *this;
1855 }
1856
1857 wxString& wxString::replace(size_t nStart, size_t nLen, const wxChar *sz)
1858 {
1859 wxASSERT_MSG( nStart + nLen <= Len(),
1860 _T("index out of bounds in wxString::replace") );
1861
1862 wxString strTmp;
1863 strTmp.Alloc(Len()); // micro optimisation to avoid multiple mem allocs
1864
1865 if ( nStart != 0 )
1866 strTmp.append(c_str(), nStart);
1867 strTmp << sz << c_str() + nStart + nLen;
1868
1869 *this = strTmp;
1870 return *this;
1871 }
1872
1873 wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch)
1874 {
1875 return replace(nStart, nLen, wxString(ch, nCount));
1876 }
1877
1878 wxString& wxString::replace(size_t nStart, size_t nLen,
1879 const wxString& str, size_t nStart2, size_t nLen2)
1880 {
1881 return replace(nStart, nLen, str.substr(nStart2, nLen2));
1882 }
1883
1884 wxString& wxString::replace(size_t nStart, size_t nLen,
1885 const wxChar* sz, size_t nCount)
1886 {
1887 return replace(nStart, nLen, wxString(sz, nCount));
1888 }
1889
1890 #endif //std::string compatibility
1891
1892 // ============================================================================
1893 // ArrayString
1894 // ============================================================================
1895
1896 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1897 #define ARRAY_MAXSIZE_INCREMENT 4096
1898 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1899 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1900 #endif
1901
1902 #define STRING(p) ((wxString *)(&(p)))
1903
1904 // ctor
1905 wxArrayString::wxArrayString(bool autoSort)
1906 {
1907 m_nSize =
1908 m_nCount = 0;
1909 m_pItems = (wxChar **) NULL;
1910 m_autoSort = autoSort;
1911 }
1912
1913 // copy ctor
1914 wxArrayString::wxArrayString(const wxArrayString& src)
1915 {
1916 m_nSize =
1917 m_nCount = 0;
1918 m_pItems = (wxChar **) NULL;
1919 m_autoSort = src.m_autoSort;
1920
1921 *this = src;
1922 }
1923
1924 // assignment operator
1925 wxArrayString& wxArrayString::operator=(const wxArrayString& src)
1926 {
1927 if ( m_nSize > 0 )
1928 Clear();
1929
1930 Copy(src);
1931
1932 m_autoSort = src.m_autoSort;
1933
1934 return *this;
1935 }
1936
1937 void wxArrayString::Copy(const wxArrayString& src)
1938 {
1939 if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
1940 Alloc(src.m_nCount);
1941
1942 for ( size_t n = 0; n < src.m_nCount; n++ )
1943 Add(src[n]);
1944 }
1945
1946 // grow the array
1947 void wxArrayString::Grow()
1948 {
1949 // only do it if no more place
1950 if ( m_nCount == m_nSize ) {
1951 // if ARRAY_DEFAULT_INITIAL_SIZE were set to 0, the initially empty would
1952 // be never resized!
1953 #if ARRAY_DEFAULT_INITIAL_SIZE == 0
1954 #error "ARRAY_DEFAULT_INITIAL_SIZE must be > 0!"
1955 #endif
1956
1957 if ( m_nSize == 0 ) {
1958 // was empty, alloc some memory
1959 m_nSize = ARRAY_DEFAULT_INITIAL_SIZE;
1960 m_pItems = new wxChar *[m_nSize];
1961 }
1962 else {
1963 // otherwise when it's called for the first time, nIncrement would be 0
1964 // and the array would never be expanded
1965 // add 50% but not too much
1966 size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
1967 ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
1968 if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
1969 nIncrement = ARRAY_MAXSIZE_INCREMENT;
1970 m_nSize += nIncrement;
1971 wxChar **pNew = new wxChar *[m_nSize];
1972
1973 // copy data to new location
1974 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
1975
1976 // delete old memory (but do not release the strings!)
1977 wxDELETEA(m_pItems);
1978
1979 m_pItems = pNew;
1980 }
1981 }
1982 }
1983
1984 void wxArrayString::Free()
1985 {
1986 for ( size_t n = 0; n < m_nCount; n++ ) {
1987 STRING(m_pItems[n])->GetStringData()->Unlock();
1988 }
1989 }
1990
1991 // deletes all the strings from the list
1992 void wxArrayString::Empty()
1993 {
1994 Free();
1995
1996 m_nCount = 0;
1997 }
1998
1999 // as Empty, but also frees memory
2000 void wxArrayString::Clear()
2001 {
2002 Free();
2003
2004 m_nSize =
2005 m_nCount = 0;
2006
2007 wxDELETEA(m_pItems);
2008 }
2009
2010 // dtor
2011 wxArrayString::~wxArrayString()
2012 {
2013 Free();
2014
2015 wxDELETEA(m_pItems);
2016 }
2017
2018 // pre-allocates memory (frees the previous data!)
2019 void wxArrayString::Alloc(size_t nSize)
2020 {
2021 wxASSERT( nSize > 0 );
2022
2023 // only if old buffer was not big enough
2024 if ( nSize > m_nSize ) {
2025 Free();
2026 wxDELETEA(m_pItems);
2027 m_pItems = new wxChar *[nSize];
2028 m_nSize = nSize;
2029 }
2030
2031 m_nCount = 0;
2032 }
2033
2034 // minimizes the memory usage by freeing unused memory
2035 void wxArrayString::Shrink()
2036 {
2037 // only do it if we have some memory to free
2038 if( m_nCount < m_nSize ) {
2039 // allocates exactly as much memory as we need
2040 wxChar **pNew = new wxChar *[m_nCount];
2041
2042 // copy data to new location
2043 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
2044 delete [] m_pItems;
2045 m_pItems = pNew;
2046 }
2047 }
2048
2049 // searches the array for an item (forward or backwards)
2050 int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const
2051 {
2052 if ( m_autoSort ) {
2053 // use binary search in the sorted array
2054 wxASSERT_MSG( bCase && !bFromEnd,
2055 wxT("search parameters ignored for auto sorted array") );
2056
2057 size_t i,
2058 lo = 0,
2059 hi = m_nCount;
2060 int res;
2061 while ( lo < hi ) {
2062 i = (lo + hi)/2;
2063
2064 res = wxStrcmp(sz, m_pItems[i]);
2065 if ( res < 0 )
2066 hi = i;
2067 else if ( res > 0 )
2068 lo = i + 1;
2069 else
2070 return i;
2071 }
2072
2073 return wxNOT_FOUND;
2074 }
2075 else {
2076 // use linear search in unsorted array
2077 if ( bFromEnd ) {
2078 if ( m_nCount > 0 ) {
2079 size_t ui = m_nCount;
2080 do {
2081 if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) )
2082 return ui;
2083 }
2084 while ( ui != 0 );
2085 }
2086 }
2087 else {
2088 for( size_t ui = 0; ui < m_nCount; ui++ ) {
2089 if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) )
2090 return ui;
2091 }
2092 }
2093 }
2094
2095 return wxNOT_FOUND;
2096 }
2097
2098 // add item at the end
2099 size_t wxArrayString::Add(const wxString& str)
2100 {
2101 if ( m_autoSort ) {
2102 // insert the string at the correct position to keep the array sorted
2103 size_t i,
2104 lo = 0,
2105 hi = m_nCount;
2106 int res;
2107 while ( lo < hi ) {
2108 i = (lo + hi)/2;
2109
2110 res = wxStrcmp(str, m_pItems[i]);
2111 if ( res < 0 )
2112 hi = i;
2113 else if ( res > 0 )
2114 lo = i + 1;
2115 else {
2116 lo = hi = i;
2117 break;
2118 }
2119 }
2120
2121 wxASSERT_MSG( lo == hi, wxT("binary search broken") );
2122
2123 Insert(str, lo);
2124
2125 return (size_t)lo;
2126 }
2127 else {
2128 wxASSERT( str.GetStringData()->IsValid() );
2129
2130 Grow();
2131
2132 // the string data must not be deleted!
2133 str.GetStringData()->Lock();
2134
2135 // just append
2136 m_pItems[m_nCount] = (wxChar *)str.c_str(); // const_cast
2137
2138 return m_nCount++;
2139 }
2140 }
2141
2142 // add item at the given position
2143 void wxArrayString::Insert(const wxString& str, size_t nIndex)
2144 {
2145 wxASSERT( str.GetStringData()->IsValid() );
2146
2147 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArrayString::Insert") );
2148
2149 Grow();
2150
2151 memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
2152 (m_nCount - nIndex)*sizeof(wxChar *));
2153
2154 str.GetStringData()->Lock();
2155 m_pItems[nIndex] = (wxChar *)str.c_str();
2156
2157 m_nCount++;
2158 }
2159
2160 // removes item from array (by index)
2161 void wxArrayString::Remove(size_t nIndex)
2162 {
2163 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArrayString::Remove") );
2164
2165 // release our lock
2166 Item(nIndex).GetStringData()->Unlock();
2167
2168 memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
2169 (m_nCount - nIndex - 1)*sizeof(wxChar *));
2170 m_nCount--;
2171 }
2172
2173 // removes item from array (by value)
2174 void wxArrayString::Remove(const wxChar *sz)
2175 {
2176 int iIndex = Index(sz);
2177
2178 wxCHECK_RET( iIndex != wxNOT_FOUND,
2179 wxT("removing inexistent element in wxArrayString::Remove") );
2180
2181 Remove(iIndex);
2182 }
2183
2184 // ----------------------------------------------------------------------------
2185 // sorting
2186 // ----------------------------------------------------------------------------
2187
2188 // we can only sort one array at a time with the quick-sort based
2189 // implementation
2190 #if wxUSE_THREADS
2191 // need a critical section to protect access to gs_compareFunction and
2192 // gs_sortAscending variables
2193 static wxCriticalSection *gs_critsectStringSort = NULL;
2194
2195 // call this before the value of the global sort vars is changed/after
2196 // you're finished with them
2197 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
2198 gs_critsectStringSort = new wxCriticalSection; \
2199 gs_critsectStringSort->Enter()
2200 #define END_SORT() gs_critsectStringSort->Leave(); \
2201 delete gs_critsectStringSort; \
2202 gs_critsectStringSort = NULL
2203 #else // !threads
2204 #define START_SORT()
2205 #define END_SORT()
2206 #endif // wxUSE_THREADS
2207
2208 // function to use for string comparaison
2209 static wxArrayString::CompareFunction gs_compareFunction = NULL;
2210
2211 // if we don't use the compare function, this flag tells us if we sort the
2212 // array in ascending or descending order
2213 static bool gs_sortAscending = TRUE;
2214
2215 // function which is called by quick sort
2216 static int LINKAGEMODE wxStringCompareFunction(const void *first, const void *second)
2217 {
2218 wxString *strFirst = (wxString *)first;
2219 wxString *strSecond = (wxString *)second;
2220
2221 if ( gs_compareFunction ) {
2222 return gs_compareFunction(*strFirst, *strSecond);
2223 }
2224 else {
2225 // maybe we should use wxStrcoll
2226 int result = wxStrcmp(strFirst->c_str(), strSecond->c_str());
2227
2228 return gs_sortAscending ? result : -result;
2229 }
2230 }
2231
2232 // sort array elements using passed comparaison function
2233 void wxArrayString::Sort(CompareFunction compareFunction)
2234 {
2235 START_SORT();
2236
2237 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
2238 gs_compareFunction = compareFunction;
2239
2240 DoSort();
2241
2242 // reset it to NULL so that Sort(bool) will work the next time
2243 gs_compareFunction = NULL;
2244
2245 END_SORT();
2246 }
2247
2248 void wxArrayString::Sort(bool reverseOrder)
2249 {
2250 START_SORT();
2251
2252 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
2253 gs_sortAscending = !reverseOrder;
2254
2255 DoSort();
2256
2257 END_SORT();
2258 }
2259
2260 void wxArrayString::DoSort()
2261 {
2262 wxCHECK_RET( !m_autoSort, wxT("can't use this method with sorted arrays") );
2263
2264 // just sort the pointers using qsort() - of course it only works because
2265 // wxString() *is* a pointer to its data
2266 qsort(m_pItems, m_nCount, sizeof(wxChar *), wxStringCompareFunction);
2267 }
2268
2269 bool wxArrayString::operator==(const wxArrayString& a) const
2270 {
2271 if ( m_nCount != a.m_nCount )
2272 return FALSE;
2273
2274 for ( size_t n = 0; n < m_nCount; n++ )
2275 {
2276 if ( Item(n) != a[n] )
2277 return FALSE;
2278 }
2279
2280 return TRUE;
2281 }
2282