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