]> git.saurik.com Git - wxWidgets.git/blob - src/common/string.cpp
Avoid double-define on WinCE
[wxWidgets.git] / src / common / string.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: string.cpp
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin, Ryan Norton
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // (c) 2004 Ryan Norton <wxprojects@comcast.net>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "string.h"
15 #endif
16
17 /*
18 * About ref counting:
19 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
20 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
21 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
22 */
23
24 // ===========================================================================
25 // headers, declarations, constants
26 // ===========================================================================
27
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
30
31 #ifdef __BORLANDC__
32 #pragma hdrstop
33 #endif
34
35 #ifndef WX_PRECOMP
36 #include "wx/defs.h"
37 #include "wx/string.h"
38 #include "wx/intl.h"
39 #include "wx/thread.h"
40 #endif
41
42 #include <ctype.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 #ifdef __SALFORDC__
47 #include <clib.h>
48 #endif
49
50 // allocating extra space for each string consumes more memory but speeds up
51 // the concatenation operations (nLen is the current string's length)
52 // NB: EXTRA_ALLOC must be >= 0!
53 #define EXTRA_ALLOC (19 - nLen % 16)
54
55 // ---------------------------------------------------------------------------
56 // static class variables definition
57 // ---------------------------------------------------------------------------
58
59 #if !wxUSE_STL
60 //According to STL _must_ be a -1 size_t
61 const size_t wxStringBase::npos = (size_t) -1;
62 #endif
63
64 // ----------------------------------------------------------------------------
65 // static data
66 // ----------------------------------------------------------------------------
67
68 #if wxUSE_STL
69
70 extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T("");
71
72 #else
73
74 // for an empty string, GetStringData() will return this address: this
75 // structure has the same layout as wxStringData and it's data() method will
76 // return the empty string (dummy pointer)
77 static const struct
78 {
79 wxStringData data;
80 wxChar dummy;
81 } g_strEmpty = { {-1, 0, 0}, wxT('\0') };
82
83 // empty C style string: points to 'string data' byte of g_strEmpty
84 extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy;
85
86 #endif
87
88 // ----------------------------------------------------------------------------
89 // global functions
90 // ----------------------------------------------------------------------------
91
92 #if wxUSE_STD_IOSTREAM
93
94 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
95 // iostream ones.
96 //
97 // ATTN: you can _not_ use both of these in the same program!
98
99 #include <iostream>
100
101 wxSTD istream& operator>>(wxSTD istream& is, wxString& WXUNUSED(str))
102 {
103 #if 0
104 int w = is.width(0);
105 if ( is.ipfx(0) ) {
106 streambuf *sb = is.rdbuf();
107 str.erase();
108 while ( true ) {
109 int ch = sb->sbumpc ();
110 if ( ch == EOF ) {
111 is.setstate(ios::eofbit);
112 break;
113 }
114 else if ( isspace(ch) ) {
115 sb->sungetc();
116 break;
117 }
118
119 str += ch;
120 if ( --w == 1 )
121 break;
122 }
123 }
124
125 is.isfx();
126 if ( str.length() == 0 )
127 is.setstate(ios::failbit);
128 #endif
129 return is;
130 }
131
132 wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
133 {
134 os << str.c_str();
135 return os;
136 }
137
138 #endif // wxUSE_STD_IOSTREAM
139
140 // ----------------------------------------------------------------------------
141 // private classes
142 // ----------------------------------------------------------------------------
143
144 // this small class is used to gather statistics for performance tuning
145 //#define WXSTRING_STATISTICS
146 #ifdef WXSTRING_STATISTICS
147 class Averager
148 {
149 public:
150 Averager(const wxChar *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
151 ~Averager()
152 { wxPrintf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
153
154 void Add(size_t n) { m_nTotal += n; m_nCount++; }
155
156 private:
157 size_t m_nCount, m_nTotal;
158 const wxChar *m_sz;
159 } g_averageLength("allocation size"),
160 g_averageSummandLength("summand length"),
161 g_averageConcatHit("hit probability in concat"),
162 g_averageInitialLength("initial string length");
163
164 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
165 #else
166 #define STATISTICS_ADD(av, val)
167 #endif // WXSTRING_STATISTICS
168
169 #if !wxUSE_STL
170
171 // ===========================================================================
172 // wxStringData class deallocation
173 // ===========================================================================
174
175 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
176 # pragma message (__FILE__ ": building with Multithreaded non DLL runtime has a performance impact on wxString!")
177 void wxStringData::Free()
178 {
179 free(this);
180 }
181 #endif
182
183 // ===========================================================================
184 // wxStringBase
185 // ===========================================================================
186
187 // takes nLength elements of psz starting at nPos
188 void wxStringBase::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
189 {
190 Init();
191
192 // if the length is not given, assume the string to be NUL terminated
193 if ( nLength == npos ) {
194 wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") );
195
196 nLength = wxStrlen(psz + nPos);
197 }
198
199 STATISTICS_ADD(InitialLength, nLength);
200
201 if ( nLength > 0 ) {
202 // trailing '\0' is written in AllocBuffer()
203 if ( !AllocBuffer(nLength) ) {
204 wxFAIL_MSG( _T("out of memory in wxStringBase::InitWith") );
205 return;
206 }
207 wxTmemcpy(m_pchData, psz + nPos, nLength);
208 }
209 }
210
211 // poor man's iterators are "void *" pointers
212 wxStringBase::wxStringBase(const void *pStart, const void *pEnd)
213 {
214 InitWith((const wxChar *)pStart, 0,
215 (const wxChar *)pEnd - (const wxChar *)pStart);
216 }
217
218 wxStringBase::wxStringBase(size_type n, wxChar ch)
219 {
220 Init();
221 append(n, ch);
222 }
223
224 // ---------------------------------------------------------------------------
225 // memory allocation
226 // ---------------------------------------------------------------------------
227
228 // allocates memory needed to store a C string of length nLen
229 bool wxStringBase::AllocBuffer(size_t nLen)
230 {
231 // allocating 0 sized buffer doesn't make sense, all empty strings should
232 // reuse g_strEmpty
233 wxASSERT( nLen > 0 );
234
235 // make sure that we don't overflow
236 wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) -
237 (sizeof(wxStringData) + EXTRA_ALLOC + 1) );
238
239 STATISTICS_ADD(Length, nLen);
240
241 // allocate memory:
242 // 1) one extra character for '\0' termination
243 // 2) sizeof(wxStringData) for housekeeping info
244 wxStringData* pData = (wxStringData*)
245 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
246
247 if ( pData == NULL ) {
248 // allocation failures are handled by the caller
249 return false;
250 }
251
252 pData->nRefs = 1;
253 pData->nDataLength = nLen;
254 pData->nAllocLength = nLen + EXTRA_ALLOC;
255 m_pchData = pData->data(); // data starts after wxStringData
256 m_pchData[nLen] = wxT('\0');
257 return true;
258 }
259
260 // must be called before changing this string
261 bool wxStringBase::CopyBeforeWrite()
262 {
263 wxStringData* pData = GetStringData();
264
265 if ( pData->IsShared() ) {
266 pData->Unlock(); // memory not freed because shared
267 size_t nLen = pData->nDataLength;
268 if ( !AllocBuffer(nLen) ) {
269 // allocation failures are handled by the caller
270 return false;
271 }
272 wxTmemcpy(m_pchData, pData->data(), nLen);
273 }
274
275 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
276
277 return true;
278 }
279
280 // must be called before replacing contents of this string
281 bool wxStringBase::AllocBeforeWrite(size_t nLen)
282 {
283 wxASSERT( nLen != 0 ); // doesn't make any sense
284
285 // must not share string and must have enough space
286 wxStringData* pData = GetStringData();
287 if ( pData->IsShared() || pData->IsEmpty() ) {
288 // can't work with old buffer, get new one
289 pData->Unlock();
290 if ( !AllocBuffer(nLen) ) {
291 // allocation failures are handled by the caller
292 return false;
293 }
294 }
295 else {
296 if ( nLen > pData->nAllocLength ) {
297 // realloc the buffer instead of calling malloc() again, this is more
298 // efficient
299 STATISTICS_ADD(Length, nLen);
300
301 nLen += EXTRA_ALLOC;
302
303 pData = (wxStringData*)
304 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
305
306 if ( pData == NULL ) {
307 // allocation failures are handled by the caller
308 // keep previous data since reallocation failed
309 return false;
310 }
311
312 pData->nAllocLength = nLen;
313 m_pchData = pData->data();
314 }
315
316 // now we have enough space, just update the string length
317 pData->nDataLength = nLen;
318 }
319
320 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
321
322 return true;
323 }
324
325 wxStringBase& wxStringBase::append(size_t n, wxChar ch)
326 {
327 size_type len = length();
328
329 if ( !CopyBeforeWrite() || !Alloc(len + n) ) {
330 wxFAIL_MSG( _T("out of memory in wxStringBase::append") );
331 }
332 GetStringData()->nDataLength = len + n;
333 m_pchData[len + n] = '\0';
334 for ( size_t i = 0; i < n; ++i )
335 m_pchData[len + i] = ch;
336 return *this;
337 }
338
339 void wxStringBase::resize(size_t nSize, wxChar ch)
340 {
341 size_t len = length();
342
343 if ( nSize < len )
344 {
345 erase(begin() + nSize, end());
346 }
347 else if ( nSize > len )
348 {
349 append(nSize - len, ch);
350 }
351 //else: we have exactly the specified length, nothing to do
352 }
353
354 // allocate enough memory for nLen characters
355 bool wxStringBase::Alloc(size_t nLen)
356 {
357 wxStringData *pData = GetStringData();
358 if ( pData->nAllocLength <= nLen ) {
359 if ( pData->IsEmpty() ) {
360 nLen += EXTRA_ALLOC;
361
362 wxStringData* pData = (wxStringData*)
363 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
364
365 if ( pData == NULL ) {
366 // allocation failure handled by caller
367 return false;
368 }
369
370 pData->nRefs = 1;
371 pData->nDataLength = 0;
372 pData->nAllocLength = nLen;
373 m_pchData = pData->data(); // data starts after wxStringData
374 m_pchData[0u] = wxT('\0');
375 }
376 else if ( pData->IsShared() ) {
377 pData->Unlock(); // memory not freed because shared
378 size_t nOldLen = pData->nDataLength;
379 if ( !AllocBuffer(nLen) ) {
380 // allocation failure handled by caller
381 return false;
382 }
383 memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar));
384 }
385 else {
386 nLen += EXTRA_ALLOC;
387
388 pData = (wxStringData *)
389 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
390
391 if ( pData == NULL ) {
392 // allocation failure handled by caller
393 // keep previous data since reallocation failed
394 return false;
395 }
396
397 // it's not important if the pointer changed or not (the check for this
398 // is not faster than assigning to m_pchData in all cases)
399 pData->nAllocLength = nLen;
400 m_pchData = pData->data();
401 }
402 }
403 //else: we've already got enough
404 return true;
405 }
406
407 wxStringBase::iterator wxStringBase::begin()
408 {
409 if (length() > 0)
410 CopyBeforeWrite();
411 return m_pchData;
412 }
413
414 wxStringBase::iterator wxStringBase::end()
415 {
416 if (length() > 0)
417 CopyBeforeWrite();
418 return m_pchData + length();
419 }
420
421 wxStringBase::iterator wxStringBase::erase(iterator it)
422 {
423 size_type idx = it - begin();
424 erase(idx, 1);
425 return begin() + idx;
426 }
427
428 wxStringBase& wxStringBase::erase(size_t nStart, size_t nLen)
429 {
430 wxASSERT(nStart <= length());
431 size_t strLen = length() - nStart;
432 // delete nLen or up to the end of the string characters
433 nLen = strLen < nLen ? strLen : nLen;
434 wxString strTmp(c_str(), nStart);
435 strTmp.append(c_str() + nStart + nLen, length() - nStart - nLen);
436
437 swap(strTmp);
438 return *this;
439 }
440
441 wxStringBase& wxStringBase::insert(size_t nPos, const wxChar *sz, size_t n)
442 {
443 wxASSERT( nPos <= length() );
444
445 if ( n == npos ) n = wxStrlen(sz);
446 if ( n == 0 ) return *this;
447
448 if ( !CopyBeforeWrite() || !Alloc(length() + n) ) {
449 wxFAIL_MSG( _T("out of memory in wxStringBase::insert") );
450 }
451
452 memmove(m_pchData + nPos + n, m_pchData + nPos,
453 (length() - nPos) * sizeof(wxChar));
454 memcpy(m_pchData + nPos, sz, n * sizeof(wxChar));
455 GetStringData()->nDataLength = length() + n;
456 m_pchData[length()] = '\0';
457
458 return *this;
459 }
460
461 void wxStringBase::swap(wxStringBase& str)
462 {
463 wxChar* tmp = str.m_pchData;
464 str.m_pchData = m_pchData;
465 m_pchData = tmp;
466 }
467
468 size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const
469 {
470 wxASSERT( str.GetStringData()->IsValid() );
471 wxASSERT( nStart <= length() );
472
473 //anchor
474 const wxChar* p = (const wxChar*)wxTmemchr(c_str() + nStart,
475 str.c_str()[0],
476 length() - nStart);
477
478 if(!p)
479 return npos;
480
481 while(p - c_str() + str.length() <= length() &&
482 wxTmemcmp(p, str.c_str(), str.length()) )
483 {
484 //Previosly passed as the first argument to wxTmemchr,
485 //but C/C++ standard does not specify evaluation order
486 //of arguments to functions -
487 //http://embedded.com/showArticle.jhtml?articleID=9900607
488 ++p;
489
490 //anchor again
491 p = (const wxChar*)wxTmemchr(p,
492 str.c_str()[0],
493 length() - (p - c_str()));
494
495 if(!p)
496 return npos;
497 }
498
499 return (p - c_str() + str.length() <= length()) ? p - c_str() : npos;
500 }
501
502 size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const
503 {
504 return find(wxStringBase(sz, n), nStart);
505 }
506
507 size_t wxStringBase::find(wxChar ch, size_t nStart) const
508 {
509 wxASSERT( nStart <= length() );
510
511 const wxChar *p = (const wxChar*)wxTmemchr(c_str() + nStart, ch, length() - nStart);
512
513 return p == NULL ? npos : p - c_str();
514 }
515
516 size_t wxStringBase::rfind(const wxStringBase& str, size_t nStart) const
517 {
518 wxASSERT( str.GetStringData()->IsValid() );
519 wxASSERT( nStart == npos || nStart <= length() );
520
521 if ( length() >= str.length() )
522 {
523 // avoids a corner case later
524 if ( length() == 0 && str.length() == 0 )
525 return 0;
526
527 // "top" is the point where search starts from
528 size_t top = length() - str.length();
529
530 if ( nStart == npos )
531 nStart = length() - 1;
532 if ( nStart < top )
533 top = nStart;
534
535 const wxChar *cursor = c_str() + top;
536 do
537 {
538 if ( wxTmemcmp(cursor, str.c_str(),
539 str.length()) == 0 )
540 {
541 return cursor - c_str();
542 }
543 } while ( cursor-- > c_str() );
544 }
545
546 return npos;
547 }
548
549 size_t wxStringBase::rfind(const wxChar* sz, size_t nStart, size_t n) const
550 {
551 return rfind(wxStringBase(sz, n), nStart);
552 }
553
554 size_t wxStringBase::rfind(wxChar ch, size_t nStart) const
555 {
556 if ( nStart == npos )
557 {
558 nStart = length();
559 }
560 else
561 {
562 wxASSERT( nStart <= length() );
563 }
564
565 const wxChar *actual;
566 for ( actual = c_str() + ( nStart == npos ? length() : nStart + 1 );
567 actual > c_str(); --actual )
568 {
569 if ( *(actual - 1) == ch )
570 return (actual - 1) - c_str();
571 }
572
573 return npos;
574 }
575
576 size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart) const
577 {
578 wxASSERT(nStart <= length());
579
580 size_t len = wxStrlen(sz);
581
582 size_t i;
583 for(i = nStart; i < this->length(); ++i)
584 {
585 if (wxTmemchr(sz, *(c_str() + i), len))
586 break;
587 }
588
589 if(i == this->length())
590 return npos;
591 else
592 return i;
593 }
594
595 size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart,
596 size_t n) const
597 {
598 return find_first_of(wxStringBase(sz, n), nStart);
599 }
600
601 size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart) const
602 {
603 if ( nStart == npos )
604 {
605 nStart = length() - 1;
606 }
607 else
608 {
609 wxASSERT_MSG( nStart <= length(),
610 _T("invalid index in find_last_of()") );
611 }
612
613 size_t len = wxStrlen(sz);
614
615 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
616 {
617 if ( wxTmemchr(sz, *p, len) )
618 return p - c_str();
619 }
620
621 return npos;
622 }
623
624 size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart,
625 size_t n) const
626 {
627 return find_last_of(wxStringBase(sz, n), nStart);
628 }
629
630 size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart) const
631 {
632 if ( nStart == npos )
633 {
634 nStart = length();
635 }
636 else
637 {
638 wxASSERT( nStart <= length() );
639 }
640
641 size_t len = wxStrlen(sz);
642
643 size_t i;
644 for(i = nStart; i < this->length(); ++i)
645 {
646 if (!wxTmemchr(sz, *(c_str() + i), len))
647 break;
648 }
649
650 if(i == this->length())
651 return npos;
652 else
653 return i;
654 }
655
656 size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart,
657 size_t n) const
658 {
659 return find_first_not_of(wxStringBase(sz, n), nStart);
660 }
661
662 size_t wxStringBase::find_first_not_of(wxChar ch, size_t nStart) const
663 {
664 wxASSERT( nStart <= length() );
665
666 for ( const wxChar *p = c_str() + nStart; *p; p++ )
667 {
668 if ( *p != ch )
669 return p - c_str();
670 }
671
672 return npos;
673 }
674
675 size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart) const
676 {
677 if ( nStart == npos )
678 {
679 nStart = length() - 1;
680 }
681 else
682 {
683 wxASSERT( nStart <= length() );
684 }
685
686 size_t len = wxStrlen(sz);
687
688 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
689 {
690 if ( !wxTmemchr(sz, *p,len) )
691 return p - c_str();
692 }
693
694 return npos;
695 }
696
697 size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart,
698 size_t n) const
699 {
700 return find_last_not_of(wxStringBase(sz, n), nStart);
701 }
702
703 size_t wxStringBase::find_last_not_of(wxChar ch, size_t nStart) const
704 {
705 if ( nStart == npos )
706 {
707 nStart = length() - 1;
708 }
709 else
710 {
711 wxASSERT( nStart <= length() );
712 }
713
714 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
715 {
716 if ( *p != ch )
717 return p - c_str();
718 }
719
720 return npos;
721 }
722
723 wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
724 const wxChar *sz)
725 {
726 wxASSERT_MSG( nStart <= length(),
727 _T("index out of bounds in wxStringBase::replace") );
728 size_t strLen = length() - nStart;
729 nLen = strLen < nLen ? strLen : nLen;
730
731 wxStringBase strTmp;
732 strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs
733
734 if ( nStart != 0 )
735 strTmp.append(c_str(), nStart);
736 strTmp.append(sz);
737 strTmp.append(c_str() + nStart + nLen);
738
739 swap(strTmp);
740 return *this;
741 }
742
743 wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
744 size_t nCount, wxChar ch)
745 {
746 return replace(nStart, nLen, wxStringBase(nCount, ch).c_str());
747 }
748
749 wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
750 const wxStringBase& str,
751 size_t nStart2, size_t nLen2)
752 {
753 return replace(nStart, nLen, str.substr(nStart2, nLen2));
754 }
755
756 wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
757 const wxChar* sz, size_t nCount)
758 {
759 return replace(nStart, nLen, wxStringBase(sz, nCount).c_str());
760 }
761
762 wxStringBase wxStringBase::substr(size_t nStart, size_t nLen) const
763 {
764 if ( nLen == npos )
765 nLen = length() - nStart;
766 return wxStringBase(*this, nStart, nLen);
767 }
768
769 // assigns one string to another
770 wxStringBase& wxStringBase::operator=(const wxStringBase& stringSrc)
771 {
772 wxASSERT( stringSrc.GetStringData()->IsValid() );
773
774 // don't copy string over itself
775 if ( m_pchData != stringSrc.m_pchData ) {
776 if ( stringSrc.GetStringData()->IsEmpty() ) {
777 Reinit();
778 }
779 else {
780 // adjust references
781 GetStringData()->Unlock();
782 m_pchData = stringSrc.m_pchData;
783 GetStringData()->Lock();
784 }
785 }
786
787 return *this;
788 }
789
790 // assigns a single character
791 wxStringBase& wxStringBase::operator=(wxChar ch)
792 {
793 if ( !AssignCopy(1, &ch) ) {
794 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(wxChar)") );
795 }
796 return *this;
797 }
798
799 // assigns C string
800 wxStringBase& wxStringBase::operator=(const wxChar *psz)
801 {
802 if ( !AssignCopy(wxStrlen(psz), psz) ) {
803 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(const wxChar *)") );
804 }
805 return *this;
806 }
807
808 // helper function: does real copy
809 bool wxStringBase::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
810 {
811 if ( nSrcLen == 0 ) {
812 Reinit();
813 }
814 else {
815 if ( !AllocBeforeWrite(nSrcLen) ) {
816 // allocation failure handled by caller
817 return false;
818 }
819 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
820 GetStringData()->nDataLength = nSrcLen;
821 m_pchData[nSrcLen] = wxT('\0');
822 }
823 return true;
824 }
825
826 // ---------------------------------------------------------------------------
827 // string concatenation
828 // ---------------------------------------------------------------------------
829
830 // add something to this string
831 bool wxStringBase::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData,
832 size_t nMaxLen)
833 {
834 STATISTICS_ADD(SummandLength, nSrcLen);
835
836 nSrcLen = nSrcLen < nMaxLen ? nSrcLen : nMaxLen;
837
838 // concatenating an empty string is a NOP
839 if ( nSrcLen > 0 ) {
840 wxStringData *pData = GetStringData();
841 size_t nLen = pData->nDataLength;
842 size_t nNewLen = nLen + nSrcLen;
843
844 // alloc new buffer if current is too small
845 if ( pData->IsShared() ) {
846 STATISTICS_ADD(ConcatHit, 0);
847
848 // we have to allocate another buffer
849 wxStringData* pOldData = GetStringData();
850 if ( !AllocBuffer(nNewLen) ) {
851 // allocation failure handled by caller
852 return false;
853 }
854 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
855 pOldData->Unlock();
856 }
857 else if ( nNewLen > pData->nAllocLength ) {
858 STATISTICS_ADD(ConcatHit, 0);
859
860 reserve(nNewLen);
861 // we have to grow the buffer
862 if ( capacity() < nNewLen ) {
863 // allocation failure handled by caller
864 return false;
865 }
866 }
867 else {
868 STATISTICS_ADD(ConcatHit, 1);
869
870 // the buffer is already big enough
871 }
872
873 // should be enough space
874 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
875
876 // fast concatenation - all is done in our buffer
877 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
878
879 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
880 GetStringData()->nDataLength = nNewLen; // and fix the length
881 }
882 //else: the string to append was empty
883 return true;
884 }
885
886 // ---------------------------------------------------------------------------
887 // simple sub-string extraction
888 // ---------------------------------------------------------------------------
889
890 // helper function: clone the data attached to this string
891 bool wxStringBase::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
892 {
893 if ( nCopyLen == 0 ) {
894 dest.Init();
895 }
896 else {
897 if ( !dest.AllocBuffer(nCopyLen) ) {
898 // allocation failure handled by caller
899 return false;
900 }
901 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
902 }
903 return true;
904 }
905
906 #endif // !wxUSE_STL
907
908 #if !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
909
910 #if !wxUSE_STL
911 #define STRINGCLASS wxStringBase
912 #else
913 #define STRINGCLASS wxString
914 #endif
915
916 static inline int wxDoCmp(const wxChar* s1, size_t l1,
917 const wxChar* s2, size_t l2)
918 {
919 if( l1 == l2 )
920 return wxTmemcmp(s1, s2, l1);
921 else if( l1 < l2 )
922 {
923 int ret = wxTmemcmp(s1, s2, l1);
924 return ret == 0 ? -1 : ret;
925 }
926 else
927 {
928 int ret = wxTmemcmp(s1, s2, l2);
929 return ret == 0 ? +1 : ret;
930 }
931 }
932
933 int STRINGCLASS::compare(const wxStringBase& str) const
934 {
935 return ::wxDoCmp(data(), length(), str.data(), str.length());
936 }
937
938 int STRINGCLASS::compare(size_t nStart, size_t nLen,
939 const wxStringBase& str) const
940 {
941 wxASSERT(nStart <= length());
942 size_type strLen = length() - nStart;
943 nLen = strLen < nLen ? strLen : nLen;
944 return ::wxDoCmp(data() + nStart, nLen, str.data(), str.length());
945 }
946
947 int STRINGCLASS::compare(size_t nStart, size_t nLen,
948 const wxStringBase& str,
949 size_t nStart2, size_t nLen2) const
950 {
951 wxASSERT(nStart <= length());
952 wxASSERT(nStart2 <= str.length());
953 size_type strLen = length() - nStart,
954 strLen2 = str.length() - nStart2;
955 nLen = strLen < nLen ? strLen : nLen;
956 nLen2 = strLen2 < nLen2 ? strLen2 : nLen2;
957 return ::wxDoCmp(data() + nStart, nLen, str.data() + nStart2, nLen2);
958 }
959
960 int STRINGCLASS::compare(const wxChar* sz) const
961 {
962 size_t nLen = wxStrlen(sz);
963 return ::wxDoCmp(data(), length(), sz, nLen);
964 }
965
966 int STRINGCLASS::compare(size_t nStart, size_t nLen,
967 const wxChar* sz, size_t nCount) const
968 {
969 wxASSERT(nStart <= length());
970 size_type strLen = length() - nStart;
971 nLen = strLen < nLen ? strLen : nLen;
972 if( nCount == npos )
973 nCount = wxStrlen(sz);
974
975 return ::wxDoCmp(data() + nStart, nLen, sz, nCount);
976 }
977
978 #undef STRINGCLASS
979
980 #endif // !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
981
982 // ===========================================================================
983 // wxString class core
984 // ===========================================================================
985
986 // ---------------------------------------------------------------------------
987 // construction and conversion
988 // ---------------------------------------------------------------------------
989
990 #if wxUSE_UNICODE
991
992 // from multibyte string
993 wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
994 {
995 // if nLength != npos, then we have to make a NULL-terminated copy
996 // of first nLength bytes of psz first because the input buffer to MB2WC
997 // must always be NULL-terminated:
998 wxCharBuffer inBuf((const char *)NULL);
999 if (nLength != npos)
1000 {
1001 wxASSERT( psz != NULL );
1002 wxCharBuffer tmp(nLength);
1003 memcpy(tmp.data(), psz, nLength);
1004 tmp.data()[nLength] = '\0';
1005 inBuf = tmp;
1006 psz = inBuf.data();
1007 }
1008
1009 // first get the size of the buffer we need
1010 size_t nLen;
1011 if ( psz )
1012 {
1013 // calculate the needed size ourselves or use the provided one
1014 if (nLength == npos)
1015 nLen = strlen(psz);
1016 else
1017 nLen = nLength;
1018 }
1019 else
1020 {
1021 // nothing to convert
1022 nLen = 0;
1023 }
1024
1025
1026 // anything to do?
1027 if ( (nLen != 0) && (nLen != (size_t)-1) )
1028 {
1029 //Convert string
1030 size_t nRealSize;
1031 wxWCharBuffer theBuffer = conv.cMB2WC(psz, nLen, &nRealSize);
1032
1033 //Copy
1034 if (nRealSize)
1035 assign( theBuffer.data() , nRealSize - 1 );
1036 }
1037 }
1038
1039 //Convert wxString in Unicode mode to a multi-byte string
1040 const wxCharBuffer wxString::mb_str(wxMBConv& conv) const
1041 {
1042 size_t dwOutSize;
1043 return conv.cWC2MB(c_str(), length(), &dwOutSize);
1044 }
1045
1046 #else // ANSI
1047
1048 #if wxUSE_WCHAR_T
1049 // from wide string
1050 wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength)
1051 {
1052 // if nLength != npos, then we have to make a NULL-terminated copy
1053 // of first nLength chars of psz first because the input buffer to WC2MB
1054 // must always be NULL-terminated:
1055 wxWCharBuffer inBuf((const wchar_t *)NULL);
1056 if (nLength != npos)
1057 {
1058 wxASSERT( pwz != NULL );
1059 wxWCharBuffer tmp(nLength);
1060 memcpy(tmp.data(), pwz, nLength * sizeof(wchar_t));
1061 tmp.data()[nLength] = '\0';
1062 inBuf = tmp;
1063 pwz = inBuf.data();
1064 }
1065
1066 // first get the size of the buffer we need
1067 size_t nLen;
1068 if ( pwz )
1069 {
1070 // calculate the needed size ourselves or use the provided one
1071 if (nLength == npos)
1072 nLen = wxWcslen(pwz);
1073 else
1074 nLen = nLength;
1075 }
1076 else
1077 {
1078 // nothing to convert
1079 nLen = 0;
1080 }
1081
1082 // anything to do?
1083 if ( (nLen != 0) && (nLen != (size_t)-1) )
1084 {
1085 //Convert string
1086 size_t nRealSize;
1087 wxCharBuffer theBuffer = conv.cWC2MB(pwz, nLen, &nRealSize);
1088
1089 //Copy
1090 if (nRealSize)
1091 assign( theBuffer.data() , nRealSize - 1 );
1092 }
1093 }
1094
1095 //Converts this string to a wide character string if unicode
1096 //mode is not enabled and wxUSE_WCHAR_T is enabled
1097 const wxWCharBuffer wxString::wc_str(wxMBConv& conv) const
1098 {
1099 size_t dwOutSize;
1100 return conv.cMB2WC(c_str(), length(), &dwOutSize);
1101 }
1102
1103 #endif // wxUSE_WCHAR_T
1104
1105 #endif // Unicode/ANSI
1106
1107 // shrink to minimal size (releasing extra memory)
1108 bool wxString::Shrink()
1109 {
1110 wxString tmp(begin(), end());
1111 swap(tmp);
1112 return tmp.length() == length();
1113 }
1114
1115 #if !wxUSE_STL
1116 // get the pointer to writable buffer of (at least) nLen bytes
1117 wxChar *wxString::GetWriteBuf(size_t nLen)
1118 {
1119 if ( !AllocBeforeWrite(nLen) ) {
1120 // allocation failure handled by caller
1121 return NULL;
1122 }
1123
1124 wxASSERT( GetStringData()->nRefs == 1 );
1125 GetStringData()->Validate(false);
1126
1127 return m_pchData;
1128 }
1129
1130 // put string back in a reasonable state after GetWriteBuf
1131 void wxString::UngetWriteBuf()
1132 {
1133 GetStringData()->nDataLength = wxStrlen(m_pchData);
1134 GetStringData()->Validate(true);
1135 }
1136
1137 void wxString::UngetWriteBuf(size_t nLen)
1138 {
1139 GetStringData()->nDataLength = nLen;
1140 GetStringData()->Validate(true);
1141 }
1142 #endif
1143
1144 // ---------------------------------------------------------------------------
1145 // data access
1146 // ---------------------------------------------------------------------------
1147
1148 // all functions are inline in string.h
1149
1150 // ---------------------------------------------------------------------------
1151 // assignment operators
1152 // ---------------------------------------------------------------------------
1153
1154 #if !wxUSE_UNICODE
1155
1156 // same as 'signed char' variant
1157 wxString& wxString::operator=(const unsigned char* psz)
1158 {
1159 *this = (const char *)psz;
1160 return *this;
1161 }
1162
1163 #if wxUSE_WCHAR_T
1164 wxString& wxString::operator=(const wchar_t *pwz)
1165 {
1166 wxString str(pwz);
1167 swap(str);
1168 return *this;
1169 }
1170 #endif
1171
1172 #endif
1173
1174 /*
1175 * concatenation functions come in 5 flavours:
1176 * string + string
1177 * char + string and string + char
1178 * C str + string and string + C str
1179 */
1180
1181 wxString operator+(const wxString& str1, const wxString& str2)
1182 {
1183 #if !wxUSE_STL
1184 wxASSERT( str1.GetStringData()->IsValid() );
1185 wxASSERT( str2.GetStringData()->IsValid() );
1186 #endif
1187
1188 wxString s = str1;
1189 s += str2;
1190
1191 return s;
1192 }
1193
1194 wxString operator+(const wxString& str, wxChar ch)
1195 {
1196 #if !wxUSE_STL
1197 wxASSERT( str.GetStringData()->IsValid() );
1198 #endif
1199
1200 wxString s = str;
1201 s += ch;
1202
1203 return s;
1204 }
1205
1206 wxString operator+(wxChar ch, const wxString& str)
1207 {
1208 #if !wxUSE_STL
1209 wxASSERT( str.GetStringData()->IsValid() );
1210 #endif
1211
1212 wxString s = ch;
1213 s += str;
1214
1215 return s;
1216 }
1217
1218 wxString operator+(const wxString& str, const wxChar *psz)
1219 {
1220 #if !wxUSE_STL
1221 wxASSERT( str.GetStringData()->IsValid() );
1222 #endif
1223
1224 wxString s;
1225 if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) {
1226 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1227 }
1228 s = str;
1229 s += psz;
1230
1231 return s;
1232 }
1233
1234 wxString operator+(const wxChar *psz, const wxString& str)
1235 {
1236 #if !wxUSE_STL
1237 wxASSERT( str.GetStringData()->IsValid() );
1238 #endif
1239
1240 wxString s;
1241 if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) {
1242 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1243 }
1244 s = psz;
1245 s += str;
1246
1247 return s;
1248 }
1249
1250 // ===========================================================================
1251 // other common string functions
1252 // ===========================================================================
1253
1254 int wxString::Cmp(const wxString& s) const
1255 {
1256 return compare(s);
1257 }
1258
1259 int wxString::Cmp(const wxChar* psz) const
1260 {
1261 return compare(psz);
1262 }
1263
1264 static inline int wxDoCmpNoCase(const wxChar* s1, size_t l1,
1265 const wxChar* s2, size_t l2)
1266 {
1267 size_t i;
1268
1269 if( l1 == l2 )
1270 {
1271 for(i = 0; i < l1; ++i)
1272 {
1273 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1274 break;
1275 }
1276 return i == l1 ? 0 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
1277 }
1278 else if( l1 < l2 )
1279 {
1280 for(i = 0; i < l1; ++i)
1281 {
1282 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1283 break;
1284 }
1285 return i == l1 ? -1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
1286 }
1287 else
1288 {
1289 for(i = 0; i < l2; ++i)
1290 {
1291 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1292 break;
1293 }
1294 return i == l2 ? 1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
1295 }
1296 }
1297
1298 int wxString::CmpNoCase(const wxString& s) const
1299 {
1300 return wxDoCmpNoCase(data(), length(), s.data(), s.length());
1301 }
1302
1303 int wxString::CmpNoCase(const wxChar* psz) const
1304 {
1305 int nLen = wxStrlen(psz);
1306
1307 return wxDoCmpNoCase(data(), length(), psz, nLen);
1308 }
1309
1310
1311 #if wxUSE_UNICODE
1312
1313 #ifdef __MWERKS__
1314 #ifndef __SCHAR_MAX__
1315 #define __SCHAR_MAX__ 127
1316 #endif
1317 #endif
1318
1319 wxString wxString::FromAscii(const char *ascii)
1320 {
1321 if (!ascii)
1322 return wxEmptyString;
1323
1324 size_t len = strlen( ascii );
1325 wxString res;
1326
1327 if ( len )
1328 {
1329 wxStringBuffer buf(res, len);
1330
1331 wchar_t *dest = buf;
1332
1333 for ( ;; )
1334 {
1335 if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' )
1336 break;
1337 }
1338 }
1339
1340 return res;
1341 }
1342
1343 wxString wxString::FromAscii(const char ascii)
1344 {
1345 // What do we do with '\0' ?
1346
1347 wxString res;
1348 res += (wchar_t)(unsigned char) ascii;
1349
1350 return res;
1351 }
1352
1353 const wxCharBuffer wxString::ToAscii() const
1354 {
1355 // this will allocate enough space for the terminating NUL too
1356 wxCharBuffer buffer(length());
1357
1358
1359 char *dest = buffer.data();
1360
1361 const wchar_t *pwc = c_str();
1362 for ( ;; )
1363 {
1364 *dest++ = (char)(*pwc > SCHAR_MAX ? wxT('_') : *pwc);
1365
1366 // the output string can't have embedded NULs anyhow, so we can safely
1367 // stop at first of them even if we do have any
1368 if ( !*pwc++ )
1369 break;
1370 }
1371
1372 return buffer;
1373 }
1374
1375 #endif // Unicode
1376
1377 // extract string of length nCount starting at nFirst
1378 wxString wxString::Mid(size_t nFirst, size_t nCount) const
1379 {
1380 size_t nLen = length();
1381
1382 // default value of nCount is npos and means "till the end"
1383 if ( nCount == npos )
1384 {
1385 nCount = nLen - nFirst;
1386 }
1387
1388 // out-of-bounds requests return sensible things
1389 if ( nFirst + nCount > nLen )
1390 {
1391 nCount = nLen - nFirst;
1392 }
1393
1394 if ( nFirst > nLen )
1395 {
1396 // AllocCopy() will return empty string
1397 nCount = 0;
1398 }
1399
1400 wxString dest(*this, nFirst, nCount);
1401 if ( dest.length() != nCount ) {
1402 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
1403 }
1404
1405 return dest;
1406 }
1407
1408 // check that the string starts with prefix and return the rest of the string
1409 // in the provided pointer if it is not NULL, otherwise return false
1410 bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const
1411 {
1412 wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") );
1413
1414 // first check if the beginning of the string matches the prefix: note
1415 // that we don't have to check that we don't run out of this string as
1416 // when we reach the terminating NUL, either prefix string ends too (and
1417 // then it's ok) or we break out of the loop because there is no match
1418 const wxChar *p = c_str();
1419 while ( *prefix )
1420 {
1421 if ( *prefix++ != *p++ )
1422 {
1423 // no match
1424 return false;
1425 }
1426 }
1427
1428 if ( rest )
1429 {
1430 // put the rest of the string into provided pointer
1431 *rest = p;
1432 }
1433
1434 return true;
1435 }
1436
1437 // extract nCount last (rightmost) characters
1438 wxString wxString::Right(size_t nCount) const
1439 {
1440 if ( nCount > length() )
1441 nCount = length();
1442
1443 wxString dest(*this, length() - nCount, nCount);
1444 if ( dest.length() != nCount ) {
1445 wxFAIL_MSG( _T("out of memory in wxString::Right") );
1446 }
1447 return dest;
1448 }
1449
1450 // get all characters after the last occurence of ch
1451 // (returns the whole string if ch not found)
1452 wxString wxString::AfterLast(wxChar ch) const
1453 {
1454 wxString str;
1455 int iPos = Find(ch, true);
1456 if ( iPos == wxNOT_FOUND )
1457 str = *this;
1458 else
1459 str = c_str() + iPos + 1;
1460
1461 return str;
1462 }
1463
1464 // extract nCount first (leftmost) characters
1465 wxString wxString::Left(size_t nCount) const
1466 {
1467 if ( nCount > length() )
1468 nCount = length();
1469
1470 wxString dest(*this, 0, nCount);
1471 if ( dest.length() != nCount ) {
1472 wxFAIL_MSG( _T("out of memory in wxString::Left") );
1473 }
1474 return dest;
1475 }
1476
1477 // get all characters before the first occurence of ch
1478 // (returns the whole string if ch not found)
1479 wxString wxString::BeforeFirst(wxChar ch) const
1480 {
1481 int iPos = Find(ch);
1482 if ( iPos == wxNOT_FOUND ) iPos = length();
1483 return wxString(*this, 0, iPos);
1484 }
1485
1486 /// get all characters before the last occurence of ch
1487 /// (returns empty string if ch not found)
1488 wxString wxString::BeforeLast(wxChar ch) const
1489 {
1490 wxString str;
1491 int iPos = Find(ch, true);
1492 if ( iPos != wxNOT_FOUND && iPos != 0 )
1493 str = wxString(c_str(), iPos);
1494
1495 return str;
1496 }
1497
1498 /// get all characters after the first occurence of ch
1499 /// (returns empty string if ch not found)
1500 wxString wxString::AfterFirst(wxChar ch) const
1501 {
1502 wxString str;
1503 int iPos = Find(ch);
1504 if ( iPos != wxNOT_FOUND )
1505 str = c_str() + iPos + 1;
1506
1507 return str;
1508 }
1509
1510 // replace first (or all) occurences of some substring with another one
1511 size_t
1512 wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
1513 {
1514 // if we tried to replace an empty string we'd enter an infinite loop below
1515 wxCHECK_MSG( szOld && *szOld && szNew, 0,
1516 _T("wxString::Replace(): invalid parameter") );
1517
1518 size_t uiCount = 0; // count of replacements made
1519
1520 size_t uiOldLen = wxStrlen(szOld);
1521
1522 wxString strTemp;
1523 const wxChar *pCurrent = c_str();
1524 const wxChar *pSubstr;
1525 while ( *pCurrent != wxT('\0') ) {
1526 pSubstr = wxStrstr(pCurrent, szOld);
1527 if ( pSubstr == NULL ) {
1528 // strTemp is unused if no replacements were made, so avoid the copy
1529 if ( uiCount == 0 )
1530 return 0;
1531
1532 strTemp += pCurrent; // copy the rest
1533 break; // exit the loop
1534 }
1535 else {
1536 // take chars before match
1537 size_type len = strTemp.length();
1538 strTemp.append(pCurrent, pSubstr - pCurrent);
1539 if ( strTemp.length() != (size_t)(len + pSubstr - pCurrent) ) {
1540 wxFAIL_MSG( _T("out of memory in wxString::Replace") );
1541 return 0;
1542 }
1543 strTemp += szNew;
1544 pCurrent = pSubstr + uiOldLen; // restart after match
1545
1546 uiCount++;
1547
1548 // stop now?
1549 if ( !bReplaceAll ) {
1550 strTemp += pCurrent; // copy the rest
1551 break; // exit the loop
1552 }
1553 }
1554 }
1555
1556 // only done if there were replacements, otherwise would have returned above
1557 swap(strTemp);
1558
1559 return uiCount;
1560 }
1561
1562 bool wxString::IsAscii() const
1563 {
1564 const wxChar *s = (const wxChar*) *this;
1565 while(*s){
1566 if(!isascii(*s)) return(false);
1567 s++;
1568 }
1569 return(true);
1570 }
1571
1572 bool wxString::IsWord() const
1573 {
1574 const wxChar *s = (const wxChar*) *this;
1575 while(*s){
1576 if(!wxIsalpha(*s)) return(false);
1577 s++;
1578 }
1579 return(true);
1580 }
1581
1582 bool wxString::IsNumber() const
1583 {
1584 const wxChar *s = (const wxChar*) *this;
1585 if (wxStrlen(s))
1586 if ((s[0] == wxT('-')) || (s[0] == wxT('+'))) s++;
1587 while(*s){
1588 if(!wxIsdigit(*s)) return(false);
1589 s++;
1590 }
1591 return(true);
1592 }
1593
1594 wxString wxString::Strip(stripType w) const
1595 {
1596 wxString s = *this;
1597 if ( w & leading ) s.Trim(false);
1598 if ( w & trailing ) s.Trim(true);
1599 return s;
1600 }
1601
1602 // ---------------------------------------------------------------------------
1603 // case conversion
1604 // ---------------------------------------------------------------------------
1605
1606 wxString& wxString::MakeUpper()
1607 {
1608 for ( iterator it = begin(), en = end(); it != en; ++it )
1609 *it = (wxChar)wxToupper(*it);
1610
1611 return *this;
1612 }
1613
1614 wxString& wxString::MakeLower()
1615 {
1616 for ( iterator it = begin(), en = end(); it != en; ++it )
1617 *it = (wxChar)wxTolower(*it);
1618
1619 return *this;
1620 }
1621
1622 // ---------------------------------------------------------------------------
1623 // trimming and padding
1624 // ---------------------------------------------------------------------------
1625
1626 // some compilers (VC++ 6.0 not to name them) return true for a call to
1627 // isspace('ê') in the C locale which seems to be broken to me, but we have to
1628 // live with this by checking that the character is a 7 bit one - even if this
1629 // may fail to detect some spaces (I don't know if Unicode doesn't have
1630 // space-like symbols somewhere except in the first 128 chars), it is arguably
1631 // still better than trimming away accented letters
1632 inline int wxSafeIsspace(wxChar ch) { return (ch < 127) && wxIsspace(ch); }
1633
1634 // trims spaces (in the sense of isspace) from left or right side
1635 wxString& wxString::Trim(bool bFromRight)
1636 {
1637 // first check if we're going to modify the string at all
1638 if ( !empty() &&
1639 (
1640 (bFromRight && wxSafeIsspace(GetChar(Len() - 1))) ||
1641 (!bFromRight && wxSafeIsspace(GetChar(0u)))
1642 )
1643 )
1644 {
1645 if ( bFromRight )
1646 {
1647 // find last non-space character
1648 iterator psz = begin() + length() - 1;
1649 while ( wxSafeIsspace(*psz) && (psz >= begin()) )
1650 psz--;
1651
1652 // truncate at trailing space start
1653 *++psz = wxT('\0');
1654 erase(psz, end());
1655 }
1656 else
1657 {
1658 // find first non-space character
1659 iterator psz = begin();
1660 while ( wxSafeIsspace(*psz) )
1661 psz++;
1662
1663 // fix up data and length
1664 erase(begin(), psz);
1665 }
1666 }
1667
1668 return *this;
1669 }
1670
1671 // adds nCount characters chPad to the string from either side
1672 wxString& wxString::Pad(size_t nCount, wxChar chPad, bool bFromRight)
1673 {
1674 wxString s(chPad, nCount);
1675
1676 if ( bFromRight )
1677 *this += s;
1678 else
1679 {
1680 s += *this;
1681 swap(s);
1682 }
1683
1684 return *this;
1685 }
1686
1687 // truncate the string
1688 wxString& wxString::Truncate(size_t uiLen)
1689 {
1690 if ( uiLen < Len() ) {
1691 erase(begin() + uiLen, end());
1692 }
1693 //else: nothing to do, string is already short enough
1694
1695 return *this;
1696 }
1697
1698 // ---------------------------------------------------------------------------
1699 // finding (return wxNOT_FOUND if not found and index otherwise)
1700 // ---------------------------------------------------------------------------
1701
1702 // find a character
1703 int wxString::Find(wxChar ch, bool bFromEnd) const
1704 {
1705 size_type idx = bFromEnd ? find_last_of(ch) : find_first_of(ch);
1706
1707 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1708 }
1709
1710 // find a sub-string (like strstr)
1711 int wxString::Find(const wxChar *pszSub) const
1712 {
1713 size_type idx = find(pszSub);
1714
1715 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1716 }
1717
1718 // ----------------------------------------------------------------------------
1719 // conversion to numbers
1720 // ----------------------------------------------------------------------------
1721
1722 bool wxString::ToLong(long *val, int base) const
1723 {
1724 wxCHECK_MSG( val, false, _T("NULL pointer in wxString::ToLong") );
1725 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
1726
1727 const wxChar *start = c_str();
1728 wxChar *end;
1729 *val = wxStrtol(start, &end, base);
1730
1731 // return true only if scan was stopped by the terminating NUL and if the
1732 // string was not empty to start with
1733 return !*end && (end != start);
1734 }
1735
1736 bool wxString::ToULong(unsigned long *val, int base) const
1737 {
1738 wxCHECK_MSG( val, false, _T("NULL pointer in wxString::ToULong") );
1739 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
1740
1741 const wxChar *start = c_str();
1742 wxChar *end;
1743 *val = wxStrtoul(start, &end, base);
1744
1745 // return true only if scan was stopped by the terminating NUL and if the
1746 // string was not empty to start with
1747 return !*end && (end != start);
1748 }
1749
1750 bool wxString::ToDouble(double *val) const
1751 {
1752 wxCHECK_MSG( val, false, _T("NULL pointer in wxString::ToDouble") );
1753
1754 const wxChar *start = c_str();
1755 wxChar *end;
1756 *val = wxStrtod(start, &end);
1757
1758 // return true only if scan was stopped by the terminating NUL and if the
1759 // string was not empty to start with
1760 return !*end && (end != start);
1761 }
1762
1763 // ---------------------------------------------------------------------------
1764 // formatted output
1765 // ---------------------------------------------------------------------------
1766
1767 /* static */
1768 wxString wxString::Format(const wxChar *pszFormat, ...)
1769 {
1770 va_list argptr;
1771 va_start(argptr, pszFormat);
1772
1773 wxString s;
1774 s.PrintfV(pszFormat, argptr);
1775
1776 va_end(argptr);
1777
1778 return s;
1779 }
1780
1781 /* static */
1782 wxString wxString::FormatV(const wxChar *pszFormat, va_list argptr)
1783 {
1784 wxString s;
1785 s.PrintfV(pszFormat, argptr);
1786 return s;
1787 }
1788
1789 int wxString::Printf(const wxChar *pszFormat, ...)
1790 {
1791 va_list argptr;
1792 va_start(argptr, pszFormat);
1793
1794 int iLen = PrintfV(pszFormat, argptr);
1795
1796 va_end(argptr);
1797
1798 return iLen;
1799 }
1800
1801 int wxString::PrintfV(const wxChar* pszFormat, va_list argptr)
1802 {
1803 int size = 1024;
1804 int len;
1805
1806 for ( ;; )
1807 {
1808 {
1809 wxStringBuffer tmp(*this, size + 1);
1810 wxChar* buf = tmp;
1811
1812 if ( !buf )
1813 {
1814 // out of memory
1815 return -1;
1816 }
1817
1818 // wxVsnprintf() may modify the original arg pointer, so pass it
1819 // only a copy
1820 va_list argptrcopy;
1821 wxVaCopy(argptrcopy, argptr);
1822 len = wxVsnprintf(buf, size, pszFormat, argptrcopy);
1823 va_end(argptrcopy);
1824
1825 // some implementations of vsnprintf() don't NUL terminate
1826 // the string if there is not enough space for it so
1827 // always do it manually
1828 buf[size] = _T('\0');
1829 }
1830
1831 // vsnprintf() may return either -1 (traditional Unix behaviour) or the
1832 // total number of characters which would have been written if the
1833 // buffer were large enough
1834 if ( len >= 0 && len <= size )
1835 {
1836 // ok, there was enough space
1837 break;
1838 }
1839
1840 // still not enough, double it again
1841 size *= 2;
1842 }
1843
1844 // we could have overshot
1845 Shrink();
1846
1847 return Len();
1848 }
1849
1850 // ----------------------------------------------------------------------------
1851 // misc other operations
1852 // ----------------------------------------------------------------------------
1853
1854 // returns true if the string matches the pattern which may contain '*' and
1855 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1856 // of them)
1857 bool wxString::Matches(const wxChar *pszMask) const
1858 {
1859 // I disable this code as it doesn't seem to be faster (in fact, it seems
1860 // to be much slower) than the old, hand-written code below and using it
1861 // here requires always linking with libregex even if the user code doesn't
1862 // use it
1863 #if 0 // wxUSE_REGEX
1864 // first translate the shell-like mask into a regex
1865 wxString pattern;
1866 pattern.reserve(wxStrlen(pszMask));
1867
1868 pattern += _T('^');
1869 while ( *pszMask )
1870 {
1871 switch ( *pszMask )
1872 {
1873 case _T('?'):
1874 pattern += _T('.');
1875 break;
1876
1877 case _T('*'):
1878 pattern += _T(".*");
1879 break;
1880
1881 case _T('^'):
1882 case _T('.'):
1883 case _T('$'):
1884 case _T('('):
1885 case _T(')'):
1886 case _T('|'):
1887 case _T('+'):
1888 case _T('\\'):
1889 // these characters are special in a RE, quote them
1890 // (however note that we don't quote '[' and ']' to allow
1891 // using them for Unix shell like matching)
1892 pattern += _T('\\');
1893 // fall through
1894
1895 default:
1896 pattern += *pszMask;
1897 }
1898
1899 pszMask++;
1900 }
1901 pattern += _T('$');
1902
1903 // and now use it
1904 return wxRegEx(pattern, wxRE_NOSUB | wxRE_EXTENDED).Matches(c_str());
1905 #else // !wxUSE_REGEX
1906 // TODO: this is, of course, awfully inefficient...
1907
1908 // the char currently being checked
1909 const wxChar *pszTxt = c_str();
1910
1911 // the last location where '*' matched
1912 const wxChar *pszLastStarInText = NULL;
1913 const wxChar *pszLastStarInMask = NULL;
1914
1915 match:
1916 for ( ; *pszMask != wxT('\0'); pszMask++, pszTxt++ ) {
1917 switch ( *pszMask ) {
1918 case wxT('?'):
1919 if ( *pszTxt == wxT('\0') )
1920 return false;
1921
1922 // pszTxt and pszMask will be incremented in the loop statement
1923
1924 break;
1925
1926 case wxT('*'):
1927 {
1928 // remember where we started to be able to backtrack later
1929 pszLastStarInText = pszTxt;
1930 pszLastStarInMask = pszMask;
1931
1932 // ignore special chars immediately following this one
1933 // (should this be an error?)
1934 while ( *pszMask == wxT('*') || *pszMask == wxT('?') )
1935 pszMask++;
1936
1937 // if there is nothing more, match
1938 if ( *pszMask == wxT('\0') )
1939 return true;
1940
1941 // are there any other metacharacters in the mask?
1942 size_t uiLenMask;
1943 const wxChar *pEndMask = wxStrpbrk(pszMask, wxT("*?"));
1944
1945 if ( pEndMask != NULL ) {
1946 // we have to match the string between two metachars
1947 uiLenMask = pEndMask - pszMask;
1948 }
1949 else {
1950 // we have to match the remainder of the string
1951 uiLenMask = wxStrlen(pszMask);
1952 }
1953
1954 wxString strToMatch(pszMask, uiLenMask);
1955 const wxChar* pMatch = wxStrstr(pszTxt, strToMatch);
1956 if ( pMatch == NULL )
1957 return false;
1958
1959 // -1 to compensate "++" in the loop
1960 pszTxt = pMatch + uiLenMask - 1;
1961 pszMask += uiLenMask - 1;
1962 }
1963 break;
1964
1965 default:
1966 if ( *pszMask != *pszTxt )
1967 return false;
1968 break;
1969 }
1970 }
1971
1972 // match only if nothing left
1973 if ( *pszTxt == wxT('\0') )
1974 return true;
1975
1976 // if we failed to match, backtrack if we can
1977 if ( pszLastStarInText ) {
1978 pszTxt = pszLastStarInText + 1;
1979 pszMask = pszLastStarInMask;
1980
1981 pszLastStarInText = NULL;
1982
1983 // don't bother resetting pszLastStarInMask, it's unnecessary
1984
1985 goto match;
1986 }
1987
1988 return false;
1989 #endif // wxUSE_REGEX/!wxUSE_REGEX
1990 }
1991
1992 // Count the number of chars
1993 int wxString::Freq(wxChar ch) const
1994 {
1995 int count = 0;
1996 int len = Len();
1997 for (int i = 0; i < len; i++)
1998 {
1999 if (GetChar(i) == ch)
2000 count ++;
2001 }
2002 return count;
2003 }
2004
2005 // convert to upper case, return the copy of the string
2006 wxString wxString::Upper() const
2007 { wxString s(*this); return s.MakeUpper(); }
2008
2009 // convert to lower case, return the copy of the string
2010 wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); }
2011
2012 int wxString::sprintf(const wxChar *pszFormat, ...)
2013 {
2014 va_list argptr;
2015 va_start(argptr, pszFormat);
2016 int iLen = PrintfV(pszFormat, argptr);
2017 va_end(argptr);
2018 return iLen;
2019 }
2020
2021 // ============================================================================
2022 // ArrayString
2023 // ============================================================================
2024
2025 #include "wx/arrstr.h"
2026
2027 #if !wxUSE_STL
2028
2029 // size increment = min(50% of current size, ARRAY_MAXSIZE_INCREMENT)
2030 #define ARRAY_MAXSIZE_INCREMENT 4096
2031
2032 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
2033 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
2034 #endif
2035
2036 #define STRING(p) ((wxString *)(&(p)))
2037
2038 // ctor
2039 void wxArrayString::Init(bool autoSort)
2040 {
2041 m_nSize =
2042 m_nCount = 0;
2043 m_pItems = (wxChar **) NULL;
2044 m_autoSort = autoSort;
2045 }
2046
2047 // copy ctor
2048 wxArrayString::wxArrayString(const wxArrayString& src)
2049 {
2050 Init(src.m_autoSort);
2051
2052 *this = src;
2053 }
2054
2055 // assignment operator
2056 wxArrayString& wxArrayString::operator=(const wxArrayString& src)
2057 {
2058 if ( m_nSize > 0 )
2059 Clear();
2060
2061 Copy(src);
2062
2063 m_autoSort = src.m_autoSort;
2064
2065 return *this;
2066 }
2067
2068 void wxArrayString::Copy(const wxArrayString& src)
2069 {
2070 if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
2071 Alloc(src.m_nCount);
2072
2073 for ( size_t n = 0; n < src.m_nCount; n++ )
2074 Add(src[n]);
2075 }
2076
2077 // grow the array
2078 void wxArrayString::Grow(size_t nIncrement)
2079 {
2080 // only do it if no more place
2081 if ( (m_nSize - m_nCount) < nIncrement ) {
2082 // if ARRAY_DEFAULT_INITIAL_SIZE were set to 0, the initially empty would
2083 // be never resized!
2084 #if ARRAY_DEFAULT_INITIAL_SIZE == 0
2085 #error "ARRAY_DEFAULT_INITIAL_SIZE must be > 0!"
2086 #endif
2087
2088 if ( m_nSize == 0 ) {
2089 // was empty, alloc some memory
2090 m_nSize = ARRAY_DEFAULT_INITIAL_SIZE;
2091 if (m_nSize < nIncrement)
2092 m_nSize = nIncrement;
2093 m_pItems = new wxChar *[m_nSize];
2094 }
2095 else {
2096 // otherwise when it's called for the first time, nIncrement would be 0
2097 // and the array would never be expanded
2098 // add 50% but not too much
2099 size_t ndefIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
2100 ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
2101 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT )
2102 ndefIncrement = ARRAY_MAXSIZE_INCREMENT;
2103 if ( nIncrement < ndefIncrement )
2104 nIncrement = ndefIncrement;
2105 m_nSize += nIncrement;
2106 wxChar **pNew = new wxChar *[m_nSize];
2107
2108 // copy data to new location
2109 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
2110
2111 // delete old memory (but do not release the strings!)
2112 wxDELETEA(m_pItems);
2113
2114 m_pItems = pNew;
2115 }
2116 }
2117 }
2118
2119 void wxArrayString::Free()
2120 {
2121 for ( size_t n = 0; n < m_nCount; n++ ) {
2122 STRING(m_pItems[n])->GetStringData()->Unlock();
2123 }
2124 }
2125
2126 // deletes all the strings from the list
2127 void wxArrayString::Empty()
2128 {
2129 Free();
2130
2131 m_nCount = 0;
2132 }
2133
2134 // as Empty, but also frees memory
2135 void wxArrayString::Clear()
2136 {
2137 Free();
2138
2139 m_nSize =
2140 m_nCount = 0;
2141
2142 wxDELETEA(m_pItems);
2143 }
2144
2145 // dtor
2146 wxArrayString::~wxArrayString()
2147 {
2148 Free();
2149
2150 wxDELETEA(m_pItems);
2151 }
2152
2153 void wxArrayString::reserve(size_t nSize)
2154 {
2155 Alloc(nSize);
2156 }
2157
2158 // pre-allocates memory (frees the previous data!)
2159 void wxArrayString::Alloc(size_t nSize)
2160 {
2161 // only if old buffer was not big enough
2162 if ( nSize > m_nSize ) {
2163 Free();
2164 wxDELETEA(m_pItems);
2165 m_pItems = new wxChar *[nSize];
2166 m_nSize = nSize;
2167 }
2168
2169 m_nCount = 0;
2170 }
2171
2172 // minimizes the memory usage by freeing unused memory
2173 void wxArrayString::Shrink()
2174 {
2175 // only do it if we have some memory to free
2176 if( m_nCount < m_nSize ) {
2177 // allocates exactly as much memory as we need
2178 wxChar **pNew = new wxChar *[m_nCount];
2179
2180 // copy data to new location
2181 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
2182 delete [] m_pItems;
2183 m_pItems = pNew;
2184 }
2185 }
2186
2187 #if WXWIN_COMPATIBILITY_2_4
2188
2189 // return a wxString[] as required for some control ctors.
2190 wxString* wxArrayString::GetStringArray() const
2191 {
2192 wxString *array = 0;
2193
2194 if( m_nCount > 0 )
2195 {
2196 array = new wxString[m_nCount];
2197 for( size_t i = 0; i < m_nCount; i++ )
2198 array[i] = m_pItems[i];
2199 }
2200
2201 return array;
2202 }
2203
2204 void wxArrayString::Remove(size_t nIndex, size_t nRemove)
2205 {
2206 RemoveAt(nIndex, nRemove);
2207 }
2208
2209 #endif // WXWIN_COMPATIBILITY_2_4
2210
2211 // searches the array for an item (forward or backwards)
2212 int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const
2213 {
2214 if ( m_autoSort ) {
2215 // use binary search in the sorted array
2216 wxASSERT_MSG( bCase && !bFromEnd,
2217 wxT("search parameters ignored for auto sorted array") );
2218
2219 size_t i,
2220 lo = 0,
2221 hi = m_nCount;
2222 int res;
2223 while ( lo < hi ) {
2224 i = (lo + hi)/2;
2225
2226 res = wxStrcmp(sz, m_pItems[i]);
2227 if ( res < 0 )
2228 hi = i;
2229 else if ( res > 0 )
2230 lo = i + 1;
2231 else
2232 return i;
2233 }
2234
2235 return wxNOT_FOUND;
2236 }
2237 else {
2238 // use linear search in unsorted array
2239 if ( bFromEnd ) {
2240 if ( m_nCount > 0 ) {
2241 size_t ui = m_nCount;
2242 do {
2243 if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) )
2244 return ui;
2245 }
2246 while ( ui != 0 );
2247 }
2248 }
2249 else {
2250 for( size_t ui = 0; ui < m_nCount; ui++ ) {
2251 if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) )
2252 return ui;
2253 }
2254 }
2255 }
2256
2257 return wxNOT_FOUND;
2258 }
2259
2260 // add item at the end
2261 size_t wxArrayString::Add(const wxString& str, size_t nInsert)
2262 {
2263 if ( m_autoSort ) {
2264 // insert the string at the correct position to keep the array sorted
2265 size_t i,
2266 lo = 0,
2267 hi = m_nCount;
2268 int res;
2269 while ( lo < hi ) {
2270 i = (lo + hi)/2;
2271
2272 res = str.Cmp(m_pItems[i]);
2273 if ( res < 0 )
2274 hi = i;
2275 else if ( res > 0 )
2276 lo = i + 1;
2277 else {
2278 lo = hi = i;
2279 break;
2280 }
2281 }
2282
2283 wxASSERT_MSG( lo == hi, wxT("binary search broken") );
2284
2285 Insert(str, lo, nInsert);
2286
2287 return (size_t)lo;
2288 }
2289 else {
2290 wxASSERT( str.GetStringData()->IsValid() );
2291
2292 Grow(nInsert);
2293
2294 for (size_t i = 0; i < nInsert; i++)
2295 {
2296 // the string data must not be deleted!
2297 str.GetStringData()->Lock();
2298
2299 // just append
2300 m_pItems[m_nCount + i] = (wxChar *)str.c_str(); // const_cast
2301 }
2302 size_t ret = m_nCount;
2303 m_nCount += nInsert;
2304 return ret;
2305 }
2306 }
2307
2308 // add item at the given position
2309 void wxArrayString::Insert(const wxString& str, size_t nIndex, size_t nInsert)
2310 {
2311 wxASSERT( str.GetStringData()->IsValid() );
2312
2313 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArrayString::Insert") );
2314 wxCHECK_RET( m_nCount <= m_nCount + nInsert,
2315 wxT("array size overflow in wxArrayString::Insert") );
2316
2317 Grow(nInsert);
2318
2319 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex],
2320 (m_nCount - nIndex)*sizeof(wxChar *));
2321
2322 for (size_t i = 0; i < nInsert; i++)
2323 {
2324 str.GetStringData()->Lock();
2325 m_pItems[nIndex + i] = (wxChar *)str.c_str();
2326 }
2327 m_nCount += nInsert;
2328 }
2329
2330 // range insert (STL 23.2.4.3)
2331 void
2332 wxArrayString::insert(iterator it, const_iterator first, const_iterator last)
2333 {
2334 const int idx = it - begin();
2335
2336 // grow it once
2337 Grow(last - first);
2338
2339 // reset "it" since it can change inside Grow()
2340 it = begin() + idx;
2341
2342 while ( first != last )
2343 {
2344 it = insert(it, *first);
2345
2346 // insert returns an iterator to the last element inserted but we need
2347 // insert the next after this one, that is before the next one
2348 ++it;
2349
2350 ++first;
2351 }
2352 }
2353
2354 // expand the array
2355 void wxArrayString::SetCount(size_t count)
2356 {
2357 Alloc(count);
2358
2359 wxString s;
2360 while ( m_nCount < count )
2361 m_pItems[m_nCount++] = (wxChar *)s.c_str();
2362 }
2363
2364 // removes item from array (by index)
2365 void wxArrayString::RemoveAt(size_t nIndex, size_t nRemove)
2366 {
2367 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArrayString::Remove") );
2368 wxCHECK_RET( nIndex + nRemove <= m_nCount,
2369 wxT("removing too many elements in wxArrayString::Remove") );
2370
2371 // release our lock
2372 for (size_t i = 0; i < nRemove; i++)
2373 Item(nIndex + i).GetStringData()->Unlock();
2374
2375 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove],
2376 (m_nCount - nIndex - nRemove)*sizeof(wxChar *));
2377 m_nCount -= nRemove;
2378 }
2379
2380 // removes item from array (by value)
2381 void wxArrayString::Remove(const wxChar *sz)
2382 {
2383 int iIndex = Index(sz);
2384
2385 wxCHECK_RET( iIndex != wxNOT_FOUND,
2386 wxT("removing inexistent element in wxArrayString::Remove") );
2387
2388 RemoveAt(iIndex);
2389 }
2390
2391 void wxArrayString::assign(const_iterator first, const_iterator last)
2392 {
2393 reserve(last - first);
2394 for(; first != last; ++first)
2395 push_back(*first);
2396 }
2397
2398 // ----------------------------------------------------------------------------
2399 // sorting
2400 // ----------------------------------------------------------------------------
2401
2402 // we can only sort one array at a time with the quick-sort based
2403 // implementation
2404 #if wxUSE_THREADS
2405 // need a critical section to protect access to gs_compareFunction and
2406 // gs_sortAscending variables
2407 static wxCriticalSection *gs_critsectStringSort = NULL;
2408
2409 // call this before the value of the global sort vars is changed/after
2410 // you're finished with them
2411 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
2412 gs_critsectStringSort = new wxCriticalSection; \
2413 gs_critsectStringSort->Enter()
2414 #define END_SORT() gs_critsectStringSort->Leave(); \
2415 delete gs_critsectStringSort; \
2416 gs_critsectStringSort = NULL
2417 #else // !threads
2418 #define START_SORT()
2419 #define END_SORT()
2420 #endif // wxUSE_THREADS
2421
2422 // function to use for string comparaison
2423 static wxArrayString::CompareFunction gs_compareFunction = NULL;
2424
2425 // if we don't use the compare function, this flag tells us if we sort the
2426 // array in ascending or descending order
2427 static bool gs_sortAscending = true;
2428
2429 // function which is called by quick sort
2430 extern "C" int wxC_CALLING_CONV // LINKAGEMODE
2431 wxStringCompareFunction(const void *first, const void *second)
2432 {
2433 wxString *strFirst = (wxString *)first;
2434 wxString *strSecond = (wxString *)second;
2435
2436 if ( gs_compareFunction ) {
2437 return gs_compareFunction(*strFirst, *strSecond);
2438 }
2439 else {
2440 // maybe we should use wxStrcoll
2441 int result = strFirst->Cmp(*strSecond);
2442
2443 return gs_sortAscending ? result : -result;
2444 }
2445 }
2446
2447 // sort array elements using passed comparaison function
2448 void wxArrayString::Sort(CompareFunction compareFunction)
2449 {
2450 START_SORT();
2451
2452 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
2453 gs_compareFunction = compareFunction;
2454
2455 DoSort();
2456
2457 // reset it to NULL so that Sort(bool) will work the next time
2458 gs_compareFunction = NULL;
2459
2460 END_SORT();
2461 }
2462
2463 typedef int (wxC_CALLING_CONV * wxStringCompareFn)(const void *first, const void *second);
2464
2465 void wxArrayString::Sort(CompareFunction2 compareFunction)
2466 {
2467 qsort(m_pItems, m_nCount, sizeof(wxChar *), (wxStringCompareFn)compareFunction);
2468 }
2469
2470 void wxArrayString::Sort(bool reverseOrder)
2471 {
2472 Sort(reverseOrder ? wxStringSortDescending : wxStringSortAscending);
2473 }
2474
2475 void wxArrayString::DoSort()
2476 {
2477 wxCHECK_RET( !m_autoSort, wxT("can't use this method with sorted arrays") );
2478
2479 // just sort the pointers using qsort() - of course it only works because
2480 // wxString() *is* a pointer to its data
2481 qsort(m_pItems, m_nCount, sizeof(wxChar *), wxStringCompareFunction);
2482 }
2483
2484 bool wxArrayString::operator==(const wxArrayString& a) const
2485 {
2486 if ( m_nCount != a.m_nCount )
2487 return false;
2488
2489 for ( size_t n = 0; n < m_nCount; n++ )
2490 {
2491 if ( Item(n) != a[n] )
2492 return false;
2493 }
2494
2495 return true;
2496 }
2497
2498 #endif // !wxUSE_STL
2499
2500 int wxCMPFUNC_CONV wxStringSortAscending(wxString* s1, wxString* s2)
2501 {
2502 return s1->Cmp(*s2);
2503 }
2504
2505 int wxCMPFUNC_CONV wxStringSortDescending(wxString* s1, wxString* s2)
2506 {
2507 return -s1->Cmp(*s2);
2508 }