]> git.saurik.com Git - wxWidgets.git/blob - src/common/string.cpp
static wxFile::Access() added
[wxWidgets.git] / src / common / string.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: string.cpp
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "string.h"
14 #endif
15
16 /*
17 * About ref counting:
18 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
19 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
20 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
21 */
22
23 // ===========================================================================
24 // headers, declarations, constants
25 // ===========================================================================
26
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
29
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif
33
34 #ifndef WX_PRECOMP
35 #include "wx/defs.h"
36 #include "wx/string.h"
37 #include <wx/intl.h>
38 #endif
39
40 #include <ctype.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #ifdef WXSTRING_IS_WXOBJECT
45 IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject)
46 #endif //WXSTRING_IS_WXOBJECT
47
48 // allocating extra space for each string consumes more memory but speeds up
49 // the concatenation operations (nLen is the current string's length)
50 // NB: EXTRA_ALLOC must be >= 0!
51 #define EXTRA_ALLOC (19 - nLen % 16)
52
53 // ---------------------------------------------------------------------------
54 // static class variables definition
55 // ---------------------------------------------------------------------------
56
57 #ifdef STD_STRING_COMPATIBILITY
58 const size_t wxString::npos = STRING_MAXLEN;
59 #endif
60
61 // ----------------------------------------------------------------------------
62 // static data
63 // ----------------------------------------------------------------------------
64
65 // for an empty string, GetStringData() will return this address
66 static int g_strEmpty[] = { -1, // ref count (locked)
67 0, // current length
68 0, // allocated memory
69 0 }; // string data
70 // empty C style string: points to 'string data' byte of g_strEmpty
71 extern const char *g_szNul = (const char *)(&g_strEmpty[3]);
72
73 // ----------------------------------------------------------------------------
74 // global functions
75 // ----------------------------------------------------------------------------
76
77 #ifdef STD_STRING_COMPATIBILITY
78
79 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
80 // iostream ones.
81 //
82 // ATTN: you can _not_ use both of these in the same program!
83 #if 0 // def _MSC_VER
84 #include <iostream>
85 #define NAMESPACE std::
86 #else
87 #include <iostream.h>
88 #define NAMESPACE
89 #endif //Visual C++
90
91 NAMESPACE istream& operator>>(NAMESPACE istream& is, wxString& WXUNUSED(str))
92 {
93 #if 0
94 int w = is.width(0);
95 if ( is.ipfx(0) ) {
96 NAMESPACE streambuf *sb = is.rdbuf();
97 str.erase();
98 while ( true ) {
99 int ch = sb->sbumpc ();
100 if ( ch == EOF ) {
101 is.setstate(NAMESPACE ios::eofbit);
102 break;
103 }
104 else if ( isspace(ch) ) {
105 sb->sungetc();
106 break;
107 }
108
109 str += ch;
110 if ( --w == 1 )
111 break;
112 }
113 }
114
115 is.isfx();
116 if ( str.length() == 0 )
117 is.setstate(NAMESPACE ios::failbit);
118 #endif
119 return is;
120 }
121
122 #endif //std::string compatibility
123
124 // ----------------------------------------------------------------------------
125 // private classes
126 // ----------------------------------------------------------------------------
127
128 // this small class is used to gather statistics for performance tuning
129 //#define WXSTRING_STATISTICS
130 #ifdef WXSTRING_STATISTICS
131 class Averager
132 {
133 public:
134 Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
135 ~Averager()
136 { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
137
138 void Add(uint n) { m_nTotal += n; m_nCount++; }
139
140 private:
141 uint m_nCount, m_nTotal;
142 const char *m_sz;
143 } g_averageLength("allocation size"),
144 g_averageSummandLength("summand length"),
145 g_averageConcatHit("hit probability in concat"),
146 g_averageInitialLength("initial string length");
147
148 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
149 #else
150 #define STATISTICS_ADD(av, val)
151 #endif // WXSTRING_STATISTICS
152
153 // ===========================================================================
154 // wxString class core
155 // ===========================================================================
156
157 // ---------------------------------------------------------------------------
158 // construction
159 // ---------------------------------------------------------------------------
160
161 // constructs string of <nLength> copies of character <ch>
162 wxString::wxString(char ch, size_t nLength)
163 {
164 Init();
165
166 if ( nLength > 0 ) {
167 AllocBuffer(nLength);
168
169 wxASSERT( sizeof(char) == 1 ); // can't use memset if not
170
171 memset(m_pchData, ch, nLength);
172 }
173 }
174
175 // takes nLength elements of psz starting at nPos
176 void wxString::InitWith(const char *psz, size_t nPos, size_t nLength)
177 {
178 Init();
179
180 wxASSERT( nPos <= Strlen(psz) );
181
182 if ( nLength == STRING_MAXLEN )
183 nLength = Strlen(psz + nPos);
184
185 STATISTICS_ADD(InitialLength, nLength);
186
187 if ( nLength > 0 ) {
188 // trailing '\0' is written in AllocBuffer()
189 AllocBuffer(nLength);
190 memcpy(m_pchData, psz + nPos, nLength*sizeof(char));
191 }
192 }
193
194 // the same as previous constructor, but for compilers using unsigned char
195 wxString::wxString(const unsigned char* psz, size_t nLength)
196 {
197 InitWith((const char *)psz, 0, nLength);
198 }
199
200 #ifdef STD_STRING_COMPATIBILITY
201
202 // poor man's iterators are "void *" pointers
203 wxString::wxString(const void *pStart, const void *pEnd)
204 {
205 InitWith((const char *)pStart, 0,
206 (const char *)pEnd - (const char *)pStart);
207 }
208
209 #endif //std::string compatibility
210
211 // from wide string
212 wxString::wxString(const wchar_t *pwz)
213 {
214 // first get necessary size
215 size_t nLen = wcstombs(NULL, pwz, 0);
216
217 // empty?
218 if ( nLen != 0 ) {
219 AllocBuffer(nLen);
220 wcstombs(m_pchData, pwz, nLen);
221 }
222 else {
223 Init();
224 }
225 }
226
227 // ---------------------------------------------------------------------------
228 // memory allocation
229 // ---------------------------------------------------------------------------
230
231 // allocates memory needed to store a C string of length nLen
232 void wxString::AllocBuffer(size_t nLen)
233 {
234 wxASSERT( nLen > 0 ); //
235 wxASSERT( nLen <= INT_MAX-1 ); // max size (enough room for 1 extra)
236
237 STATISTICS_ADD(Length, nLen);
238
239 // allocate memory:
240 // 1) one extra character for '\0' termination
241 // 2) sizeof(wxStringData) for housekeeping info
242 wxStringData* pData = (wxStringData*)
243 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(char));
244 pData->nRefs = 1;
245 pData->nDataLength = nLen;
246 pData->nAllocLength = nLen + EXTRA_ALLOC;
247 m_pchData = pData->data(); // data starts after wxStringData
248 m_pchData[nLen] = '\0';
249 }
250
251 // must be called before changing this string
252 void wxString::CopyBeforeWrite()
253 {
254 wxStringData* pData = GetStringData();
255
256 if ( pData->IsShared() ) {
257 pData->Unlock(); // memory not freed because shared
258 uint nLen = pData->nDataLength;
259 AllocBuffer(nLen);
260 memcpy(m_pchData, pData->data(), nLen*sizeof(char));
261 }
262
263 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
264 }
265
266 // must be called before replacing contents of this string
267 void wxString::AllocBeforeWrite(size_t nLen)
268 {
269 wxASSERT( nLen != 0 ); // doesn't make any sense
270
271 // must not share string and must have enough space
272 wxStringData* pData = GetStringData();
273 if ( pData->IsShared() || (nLen > pData->nAllocLength) ) {
274 // can't work with old buffer, get new one
275 pData->Unlock();
276 AllocBuffer(nLen);
277 }
278
279 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
280 }
281
282 // allocate enough memory for nLen characters
283 void wxString::Alloc(uint nLen)
284 {
285 wxStringData *pData = GetStringData();
286 if ( pData->nAllocLength <= nLen ) {
287 if ( pData->IsEmpty() ) {
288 nLen += EXTRA_ALLOC;
289
290 wxStringData* pData = (wxStringData*)
291 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(char));
292 pData->nRefs = 1;
293 pData->nDataLength = 0;
294 pData->nAllocLength = nLen;
295 m_pchData = pData->data(); // data starts after wxStringData
296 m_pchData[0u] = '\0';
297 }
298 else if ( pData->IsShared() ) {
299 pData->Unlock(); // memory not freed because shared
300 uint nOldLen = pData->nDataLength;
301 AllocBuffer(nLen);
302 memcpy(m_pchData, pData->data(), nOldLen*sizeof(char));
303 }
304 else {
305 nLen += EXTRA_ALLOC;
306
307 wxStringData *p = (wxStringData *)
308 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(char));
309
310 if ( p == NULL ) {
311 // @@@ what to do on memory error?
312 return;
313 }
314
315 // it's not important if the pointer changed or not (the check for this
316 // is not faster than assigning to m_pchData in all cases)
317 p->nAllocLength = nLen;
318 m_pchData = p->data();
319 }
320 }
321 //else: we've already got enough
322 }
323
324 // shrink to minimal size (releasing extra memory)
325 void wxString::Shrink()
326 {
327 wxStringData *pData = GetStringData();
328
329 // this variable is unused in release build, so avoid the compiler warning by
330 // just not declaring it
331 #ifdef __WXDEBUG__
332 void *p =
333 #endif
334 realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(char));
335
336 wxASSERT( p != NULL ); // can't free memory?
337 wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move!
338 }
339
340 // get the pointer to writable buffer of (at least) nLen bytes
341 char *wxString::GetWriteBuf(uint nLen)
342 {
343 AllocBeforeWrite(nLen);
344
345 wxASSERT( GetStringData()->nRefs == 1 );
346 GetStringData()->Validate(FALSE);
347
348 return m_pchData;
349 }
350
351 // put string back in a reasonable state after GetWriteBuf
352 void wxString::UngetWriteBuf()
353 {
354 GetStringData()->nDataLength = strlen(m_pchData);
355 GetStringData()->Validate(TRUE);
356 }
357
358 // ---------------------------------------------------------------------------
359 // data access
360 // ---------------------------------------------------------------------------
361
362 // all functions are inline in string.h
363
364 // ---------------------------------------------------------------------------
365 // assignment operators
366 // ---------------------------------------------------------------------------
367
368 // helper function: does real copy
369 void wxString::AssignCopy(size_t nSrcLen, const char *pszSrcData)
370 {
371 if ( nSrcLen == 0 ) {
372 Reinit();
373 }
374 else {
375 AllocBeforeWrite(nSrcLen);
376 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(char));
377 GetStringData()->nDataLength = nSrcLen;
378 m_pchData[nSrcLen] = '\0';
379 }
380 }
381
382 // assigns one string to another
383 wxString& wxString::operator=(const wxString& stringSrc)
384 {
385 wxASSERT( stringSrc.GetStringData()->IsValid() );
386
387 // don't copy string over itself
388 if ( m_pchData != stringSrc.m_pchData ) {
389 if ( stringSrc.GetStringData()->IsEmpty() ) {
390 Reinit();
391 }
392 else {
393 // adjust references
394 GetStringData()->Unlock();
395 m_pchData = stringSrc.m_pchData;
396 GetStringData()->Lock();
397 }
398 }
399
400 return *this;
401 }
402
403 // assigns a single character
404 wxString& wxString::operator=(char ch)
405 {
406 AssignCopy(1, &ch);
407 return *this;
408 }
409
410 // assigns C string
411 wxString& wxString::operator=(const char *psz)
412 {
413 AssignCopy(Strlen(psz), psz);
414 return *this;
415 }
416
417 // same as 'signed char' variant
418 wxString& wxString::operator=(const unsigned char* psz)
419 {
420 *this = (const char *)psz;
421 return *this;
422 }
423
424 wxString& wxString::operator=(const wchar_t *pwz)
425 {
426 wxString str(pwz);
427 *this = str;
428 return *this;
429 }
430
431 // ---------------------------------------------------------------------------
432 // string concatenation
433 // ---------------------------------------------------------------------------
434
435 // add something to this string
436 void wxString::ConcatSelf(int nSrcLen, const char *pszSrcData)
437 {
438 STATISTICS_ADD(SummandLength, nSrcLen);
439
440 // concatenating an empty string is a NOP, but it happens quite rarely,
441 // so we don't waste our time checking for it
442 // if ( nSrcLen > 0 )
443 wxStringData *pData = GetStringData();
444 uint nLen = pData->nDataLength;
445 uint nNewLen = nLen + nSrcLen;
446
447 // alloc new buffer if current is too small
448 if ( pData->IsShared() ) {
449 STATISTICS_ADD(ConcatHit, 0);
450
451 // we have to allocate another buffer
452 wxStringData* pOldData = GetStringData();
453 AllocBuffer(nNewLen);
454 memcpy(m_pchData, pOldData->data(), nLen*sizeof(char));
455 pOldData->Unlock();
456 }
457 else if ( nNewLen > pData->nAllocLength ) {
458 STATISTICS_ADD(ConcatHit, 0);
459
460 // we have to grow the buffer
461 Alloc(nNewLen);
462 }
463 else {
464 STATISTICS_ADD(ConcatHit, 1);
465
466 // the buffer is already big enough
467 }
468
469 // should be enough space
470 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
471
472 // fast concatenation - all is done in our buffer
473 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(char));
474
475 m_pchData[nNewLen] = '\0'; // put terminating '\0'
476 GetStringData()->nDataLength = nNewLen; // and fix the length
477 }
478
479 /*
480 * concatenation functions come in 5 flavours:
481 * string + string
482 * char + string and string + char
483 * C str + string and string + C str
484 */
485
486 wxString operator+(const wxString& string1, const wxString& string2)
487 {
488 wxASSERT( string1.GetStringData()->IsValid() );
489 wxASSERT( string2.GetStringData()->IsValid() );
490
491 wxString s = string1;
492 s += string2;
493
494 return s;
495 }
496
497 wxString operator+(const wxString& string, char ch)
498 {
499 wxASSERT( string.GetStringData()->IsValid() );
500
501 wxString s = string;
502 s += ch;
503
504 return s;
505 }
506
507 wxString operator+(char ch, const wxString& string)
508 {
509 wxASSERT( string.GetStringData()->IsValid() );
510
511 wxString s = ch;
512 s += string;
513
514 return s;
515 }
516
517 wxString operator+(const wxString& string, const char *psz)
518 {
519 wxASSERT( string.GetStringData()->IsValid() );
520
521 wxString s;
522 s.Alloc(Strlen(psz) + string.Len());
523 s = string;
524 s += psz;
525
526 return s;
527 }
528
529 wxString operator+(const char *psz, const wxString& string)
530 {
531 wxASSERT( string.GetStringData()->IsValid() );
532
533 wxString s;
534 s.Alloc(Strlen(psz) + string.Len());
535 s = psz;
536 s += string;
537
538 return s;
539 }
540
541 // ===========================================================================
542 // other common string functions
543 // ===========================================================================
544
545 // ---------------------------------------------------------------------------
546 // simple sub-string extraction
547 // ---------------------------------------------------------------------------
548
549 // helper function: clone the data attached to this string
550 void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
551 {
552 if ( nCopyLen == 0 ) {
553 dest.Init();
554 }
555 else {
556 dest.AllocBuffer(nCopyLen);
557 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(char));
558 }
559 }
560
561 // extract string of length nCount starting at nFirst
562 // default value of nCount is 0 and means "till the end"
563 wxString wxString::Mid(size_t nFirst, size_t nCount) const
564 {
565 // out-of-bounds requests return sensible things
566 if ( nCount == 0 )
567 nCount = GetStringData()->nDataLength - nFirst;
568
569 if ( nFirst + nCount > (size_t)GetStringData()->nDataLength )
570 nCount = GetStringData()->nDataLength - nFirst;
571 if ( nFirst > (size_t)GetStringData()->nDataLength )
572 nCount = 0;
573
574 wxString dest;
575 AllocCopy(dest, nCount, nFirst);
576 return dest;
577 }
578
579 // extract nCount last (rightmost) characters
580 wxString wxString::Right(size_t nCount) const
581 {
582 if ( nCount > (size_t)GetStringData()->nDataLength )
583 nCount = GetStringData()->nDataLength;
584
585 wxString dest;
586 AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount);
587 return dest;
588 }
589
590 // get all characters after the last occurence of ch
591 // (returns the whole string if ch not found)
592 wxString wxString::Right(char ch) const
593 {
594 wxString str;
595 int iPos = Find(ch, TRUE);
596 if ( iPos == NOT_FOUND )
597 str = *this;
598 else
599 str = c_str() + iPos + 1;
600
601 return str;
602 }
603
604 // extract nCount first (leftmost) characters
605 wxString wxString::Left(size_t nCount) const
606 {
607 if ( nCount > (size_t)GetStringData()->nDataLength )
608 nCount = GetStringData()->nDataLength;
609
610 wxString dest;
611 AllocCopy(dest, nCount, 0);
612 return dest;
613 }
614
615 // get all characters before the first occurence of ch
616 // (returns the whole string if ch not found)
617 wxString wxString::Left(char ch) const
618 {
619 wxString str;
620 for ( const char *pc = m_pchData; *pc != '\0' && *pc != ch; pc++ )
621 str += *pc;
622
623 return str;
624 }
625
626 /// get all characters before the last occurence of ch
627 /// (returns empty string if ch not found)
628 wxString wxString::Before(char ch) const
629 {
630 wxString str;
631 int iPos = Find(ch, TRUE);
632 if ( iPos != NOT_FOUND && iPos != 0 )
633 str = wxString(c_str(), iPos);
634
635 return str;
636 }
637
638 /// get all characters after the first occurence of ch
639 /// (returns empty string if ch not found)
640 wxString wxString::After(char ch) const
641 {
642 wxString str;
643 int iPos = Find(ch);
644 if ( iPos != NOT_FOUND )
645 str = c_str() + iPos + 1;
646
647 return str;
648 }
649
650 // replace first (or all) occurences of some substring with another one
651 uint wxString::Replace(const char *szOld, const char *szNew, bool bReplaceAll)
652 {
653 uint uiCount = 0; // count of replacements made
654
655 uint uiOldLen = Strlen(szOld);
656
657 wxString strTemp;
658 const char *pCurrent = m_pchData;
659 const char *pSubstr;
660 while ( *pCurrent != '\0' ) {
661 pSubstr = strstr(pCurrent, szOld);
662 if ( pSubstr == NULL ) {
663 // strTemp is unused if no replacements were made, so avoid the copy
664 if ( uiCount == 0 )
665 return 0;
666
667 strTemp += pCurrent; // copy the rest
668 break; // exit the loop
669 }
670 else {
671 // take chars before match
672 strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent);
673 strTemp += szNew;
674 pCurrent = pSubstr + uiOldLen; // restart after match
675
676 uiCount++;
677
678 // stop now?
679 if ( !bReplaceAll ) {
680 strTemp += pCurrent; // copy the rest
681 break; // exit the loop
682 }
683 }
684 }
685
686 // only done if there were replacements, otherwise would have returned above
687 *this = strTemp;
688
689 return uiCount;
690 }
691
692 bool wxString::IsAscii() const
693 {
694 const char *s = (const char*) *this;
695 while(*s){
696 if(!isascii(*s)) return(FALSE);
697 s++;
698 }
699 return(TRUE);
700 }
701
702 bool wxString::IsWord() const
703 {
704 const char *s = (const char*) *this;
705 while(*s){
706 if(!isalpha(*s)) return(FALSE);
707 s++;
708 }
709 return(TRUE);
710 }
711
712 bool wxString::IsNumber() const
713 {
714 const char *s = (const char*) *this;
715 while(*s){
716 if(!isdigit(*s)) return(FALSE);
717 s++;
718 }
719 return(TRUE);
720 }
721
722 wxString wxString::Strip(stripType w) const
723 {
724 wxString s = *this;
725 if ( w & leading ) s.Trim(FALSE);
726 if ( w & trailing ) s.Trim(TRUE);
727 return s;
728 }
729
730 // ---------------------------------------------------------------------------
731 // case conversion
732 // ---------------------------------------------------------------------------
733
734 wxString& wxString::MakeUpper()
735 {
736 CopyBeforeWrite();
737
738 for ( char *p = m_pchData; *p; p++ )
739 *p = (char)toupper(*p);
740
741 return *this;
742 }
743
744 wxString& wxString::MakeLower()
745 {
746 CopyBeforeWrite();
747
748 for ( char *p = m_pchData; *p; p++ )
749 *p = (char)tolower(*p);
750
751 return *this;
752 }
753
754 // ---------------------------------------------------------------------------
755 // trimming and padding
756 // ---------------------------------------------------------------------------
757
758 // trims spaces (in the sense of isspace) from left or right side
759 wxString& wxString::Trim(bool bFromRight)
760 {
761 CopyBeforeWrite();
762
763 if ( bFromRight )
764 {
765 // find last non-space character
766 char *psz = m_pchData + GetStringData()->nDataLength - 1;
767 while ( isspace(*psz) && (psz >= m_pchData) )
768 psz--;
769
770 // truncate at trailing space start
771 *++psz = '\0';
772 GetStringData()->nDataLength = psz - m_pchData;
773 }
774 else
775 {
776 // find first non-space character
777 const char *psz = m_pchData;
778 while ( isspace(*psz) )
779 psz++;
780
781 // fix up data and length
782 int nDataLength = GetStringData()->nDataLength - (psz - m_pchData);
783 memmove(m_pchData, psz, (nDataLength + 1)*sizeof(char));
784 GetStringData()->nDataLength = nDataLength;
785 }
786
787 return *this;
788 }
789
790 // adds nCount characters chPad to the string from either side
791 wxString& wxString::Pad(size_t nCount, char chPad, bool bFromRight)
792 {
793 wxString s(chPad, nCount);
794
795 if ( bFromRight )
796 *this += s;
797 else
798 {
799 s += *this;
800 *this = s;
801 }
802
803 return *this;
804 }
805
806 // truncate the string
807 wxString& wxString::Truncate(size_t uiLen)
808 {
809 *(m_pchData + uiLen) = '\0';
810 GetStringData()->nDataLength = uiLen;
811
812 return *this;
813 }
814
815 // ---------------------------------------------------------------------------
816 // finding (return NOT_FOUND if not found and index otherwise)
817 // ---------------------------------------------------------------------------
818
819 // find a character
820 int wxString::Find(char ch, bool bFromEnd) const
821 {
822 const char *psz = bFromEnd ? strrchr(m_pchData, ch) : strchr(m_pchData, ch);
823
824 return (psz == NULL) ? NOT_FOUND : psz - m_pchData;
825 }
826
827 // find a sub-string (like strstr)
828 int wxString::Find(const char *pszSub) const
829 {
830 const char *psz = strstr(m_pchData, pszSub);
831
832 return (psz == NULL) ? NOT_FOUND : psz - m_pchData;
833 }
834
835 // ---------------------------------------------------------------------------
836 // formatted output
837 // ---------------------------------------------------------------------------
838 int wxString::Printf(const char *pszFormat, ...)
839 {
840 va_list argptr;
841 va_start(argptr, pszFormat);
842
843 int iLen = PrintfV(pszFormat, argptr);
844
845 va_end(argptr);
846
847 return iLen;
848 }
849
850 int wxString::PrintfV(const char* pszFormat, va_list argptr)
851 {
852 static char s_szScratch[1024];
853
854 int iLen = vsprintf(s_szScratch, pszFormat, argptr);
855 AllocBeforeWrite(iLen);
856 strcpy(m_pchData, s_szScratch);
857
858 return iLen;
859 }
860
861 // ----------------------------------------------------------------------------
862 // misc other operations
863 // ----------------------------------------------------------------------------
864 bool wxString::Matches(const char *pszMask) const
865 {
866 // check char by char
867 const char *pszTxt;
868 for ( pszTxt = c_str(); *pszMask != '\0'; pszMask++, pszTxt++ ) {
869 switch ( *pszMask ) {
870 case '?':
871 if ( *pszTxt == '\0' )
872 return FALSE;
873
874 pszTxt++;
875 pszMask++;
876 break;
877
878 case '*':
879 {
880 // ignore special chars immediately following this one
881 while ( *pszMask == '*' || *pszMask == '?' )
882 pszMask++;
883
884 // if there is nothing more, match
885 if ( *pszMask == '\0' )
886 return TRUE;
887
888 // are there any other metacharacters in the mask?
889 uint uiLenMask;
890 const char *pEndMask = strpbrk(pszMask, "*?");
891
892 if ( pEndMask != NULL ) {
893 // we have to match the string between two metachars
894 uiLenMask = pEndMask - pszMask;
895 }
896 else {
897 // we have to match the remainder of the string
898 uiLenMask = strlen(pszMask);
899 }
900
901 wxString strToMatch(pszMask, uiLenMask);
902 const char* pMatch = strstr(pszTxt, strToMatch);
903 if ( pMatch == NULL )
904 return FALSE;
905
906 // -1 to compensate "++" in the loop
907 pszTxt = pMatch + uiLenMask - 1;
908 pszMask += uiLenMask - 1;
909 }
910 break;
911
912 default:
913 if ( *pszMask != *pszTxt )
914 return FALSE;
915 break;
916 }
917 }
918
919 // match only if nothing left
920 return *pszTxt == '\0';
921 }
922
923 // ---------------------------------------------------------------------------
924 // standard C++ library string functions
925 // ---------------------------------------------------------------------------
926 #ifdef STD_STRING_COMPATIBILITY
927
928 wxString& wxString::insert(size_t nPos, const wxString& str)
929 {
930 wxASSERT( str.GetStringData()->IsValid() );
931 wxASSERT( nPos <= Len() );
932
933 wxString strTmp;
934 char *pc = strTmp.GetWriteBuf(Len() + str.Len());
935 strncpy(pc, c_str(), nPos);
936 strcpy(pc + nPos, str);
937 strcpy(pc + nPos + str.Len(), c_str() + nPos);
938 strTmp.UngetWriteBuf();
939 *this = strTmp;
940
941 return *this;
942 }
943
944 size_t wxString::find(const wxString& str, size_t nStart) const
945 {
946 wxASSERT( str.GetStringData()->IsValid() );
947 wxASSERT( nStart <= Len() );
948
949 const char *p = strstr(c_str() + nStart, str);
950
951 return p == NULL ? npos : p - c_str();
952 }
953
954 // VC++ 1.5 can't cope with the default argument in the header.
955 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
956 size_t wxString::find(const char* sz, size_t nStart, size_t n) const
957 {
958 return find(wxString(sz, n == npos ? 0 : n), nStart);
959 }
960 #endif
961
962 size_t wxString::find(char ch, size_t nStart) const
963 {
964 wxASSERT( nStart <= Len() );
965
966 const char *p = strchr(c_str() + nStart, ch);
967
968 return p == NULL ? npos : p - c_str();
969 }
970
971 size_t wxString::rfind(const wxString& str, size_t nStart) const
972 {
973 wxASSERT( str.GetStringData()->IsValid() );
974 wxASSERT( nStart <= Len() );
975
976 // # could be quicker than that
977 const char *p = c_str() + (nStart == npos ? Len() : nStart);
978 while ( p >= c_str() + str.Len() ) {
979 if ( strncmp(p - str.Len(), str, str.Len()) == 0 )
980 return p - str.Len() - c_str();
981 p--;
982 }
983
984 return npos;
985 }
986
987 // VC++ 1.5 can't cope with the default argument in the header.
988 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
989 size_t wxString::rfind(const char* sz, size_t nStart, size_t n) const
990 {
991 return rfind(wxString(sz, n == npos ? 0 : n), nStart);
992 }
993
994 size_t wxString::rfind(char ch, size_t nStart) const
995 {
996 wxASSERT( nStart <= Len() );
997
998 const char *p = strrchr(c_str() + nStart, ch);
999
1000 return p == NULL ? npos : p - c_str();
1001 }
1002 #endif
1003
1004 wxString wxString::substr(size_t nStart, size_t nLen) const
1005 {
1006 // npos means 'take all'
1007 if ( nLen == npos )
1008 nLen = 0;
1009
1010 wxASSERT( nStart + nLen <= Len() );
1011
1012 return wxString(c_str() + nStart, nLen == npos ? 0 : nLen);
1013 }
1014
1015 wxString& wxString::erase(size_t nStart, size_t nLen)
1016 {
1017 wxString strTmp(c_str(), nStart);
1018 if ( nLen != npos ) {
1019 wxASSERT( nStart + nLen <= Len() );
1020
1021 strTmp.append(c_str() + nStart + nLen);
1022 }
1023
1024 *this = strTmp;
1025 return *this;
1026 }
1027
1028 wxString& wxString::replace(size_t nStart, size_t nLen, const char *sz)
1029 {
1030 wxASSERT( nStart + nLen <= Strlen(sz) );
1031
1032 wxString strTmp;
1033 if ( nStart != 0 )
1034 strTmp.append(c_str(), nStart);
1035 strTmp += sz;
1036 strTmp.append(c_str() + nStart + nLen);
1037
1038 *this = strTmp;
1039 return *this;
1040 }
1041
1042 wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, char ch)
1043 {
1044 return replace(nStart, nLen, wxString(ch, nCount));
1045 }
1046
1047 wxString& wxString::replace(size_t nStart, size_t nLen,
1048 const wxString& str, size_t nStart2, size_t nLen2)
1049 {
1050 return replace(nStart, nLen, str.substr(nStart2, nLen2));
1051 }
1052
1053 wxString& wxString::replace(size_t nStart, size_t nLen,
1054 const char* sz, size_t nCount)
1055 {
1056 return replace(nStart, nLen, wxString(sz, nCount));
1057 }
1058
1059 #endif //std::string compatibility
1060
1061 // ============================================================================
1062 // ArrayString
1063 // ============================================================================
1064
1065 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1066 #define ARRAY_MAXSIZE_INCREMENT 4096
1067 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1068 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1069 #endif
1070
1071 #define STRING(p) ((wxString *)(&(p)))
1072
1073 // ctor
1074 wxArrayString::wxArrayString()
1075 {
1076 m_nSize =
1077 m_nCount = 0;
1078 m_pItems = NULL;
1079 }
1080
1081 // copy ctor
1082 wxArrayString::wxArrayString(const wxArrayString& src)
1083 {
1084 m_nSize =
1085 m_nCount = 0;
1086 m_pItems = NULL;
1087
1088 *this = src;
1089 }
1090
1091 // assignment operator
1092 wxArrayString& wxArrayString::operator=(const wxArrayString& src)
1093 {
1094 if ( m_nSize > 0 )
1095 Clear();
1096
1097 if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
1098 Alloc(src.m_nCount);
1099
1100 // we can't just copy the pointers here because otherwise we would share
1101 // the strings with another array
1102 for ( uint n = 0; n < src.m_nCount; n++ )
1103 Add(src[n]);
1104
1105 if ( m_nCount != 0 )
1106 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(char *));
1107
1108 return *this;
1109 }
1110
1111 // grow the array
1112 void wxArrayString::Grow()
1113 {
1114 // only do it if no more place
1115 if( m_nCount == m_nSize ) {
1116 if( m_nSize == 0 ) {
1117 // was empty, alloc some memory
1118 m_nSize = ARRAY_DEFAULT_INITIAL_SIZE;
1119 m_pItems = new char *[m_nSize];
1120 }
1121 else {
1122 // otherwise when it's called for the first time, nIncrement would be 0
1123 // and the array would never be expanded
1124 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE != 0 );
1125
1126 // add 50% but not too much
1127 size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
1128 ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
1129 if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
1130 nIncrement = ARRAY_MAXSIZE_INCREMENT;
1131 m_nSize += nIncrement;
1132 char **pNew = new char *[m_nSize];
1133
1134 // copy data to new location
1135 memcpy(pNew, m_pItems, m_nCount*sizeof(char *));
1136
1137 // delete old memory (but do not release the strings!)
1138 wxDELETEA(m_pItems);
1139
1140 m_pItems = pNew;
1141 }
1142 }
1143 }
1144
1145 void wxArrayString::Free()
1146 {
1147 for ( size_t n = 0; n < m_nCount; n++ ) {
1148 STRING(m_pItems[n])->GetStringData()->Unlock();
1149 }
1150 }
1151
1152 // deletes all the strings from the list
1153 void wxArrayString::Empty()
1154 {
1155 Free();
1156
1157 m_nCount = 0;
1158 }
1159
1160 // as Empty, but also frees memory
1161 void wxArrayString::Clear()
1162 {
1163 Free();
1164
1165 m_nSize =
1166 m_nCount = 0;
1167
1168 wxDELETEA(m_pItems);
1169 }
1170
1171 // dtor
1172 wxArrayString::~wxArrayString()
1173 {
1174 Free();
1175
1176 wxDELETEA(m_pItems);
1177 }
1178
1179 // pre-allocates memory (frees the previous data!)
1180 void wxArrayString::Alloc(size_t nSize)
1181 {
1182 wxASSERT( nSize > 0 );
1183
1184 // only if old buffer was not big enough
1185 if ( nSize > m_nSize ) {
1186 Free();
1187 wxDELETEA(m_pItems);
1188 m_pItems = new char *[nSize];
1189 m_nSize = nSize;
1190 }
1191
1192 m_nCount = 0;
1193 }
1194
1195 // searches the array for an item (forward or backwards)
1196 int wxArrayString::Index(const char *sz, bool bCase, bool bFromEnd) const
1197 {
1198 if ( bFromEnd ) {
1199 if ( m_nCount > 0 ) {
1200 uint ui = m_nCount;
1201 do {
1202 if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) )
1203 return ui;
1204 }
1205 while ( ui != 0 );
1206 }
1207 }
1208 else {
1209 for( uint ui = 0; ui < m_nCount; ui++ ) {
1210 if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) )
1211 return ui;
1212 }
1213 }
1214
1215 return NOT_FOUND;
1216 }
1217
1218 // add item at the end
1219 void wxArrayString::Add(const wxString& str)
1220 {
1221 wxASSERT( str.GetStringData()->IsValid() );
1222
1223 Grow();
1224
1225 // the string data must not be deleted!
1226 str.GetStringData()->Lock();
1227 m_pItems[m_nCount++] = (char *)str.c_str();
1228 }
1229
1230 // add item at the given position
1231 void wxArrayString::Insert(const wxString& str, size_t nIndex)
1232 {
1233 wxASSERT( str.GetStringData()->IsValid() );
1234
1235 wxCHECK_RET( nIndex <= m_nCount, ("bad index in wxArrayString::Insert") );
1236
1237 Grow();
1238
1239 memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
1240 (m_nCount - nIndex)*sizeof(char *));
1241
1242 str.GetStringData()->Lock();
1243 m_pItems[nIndex] = (char *)str.c_str();
1244
1245 m_nCount++;
1246 }
1247
1248 // removes item from array (by index)
1249 void wxArrayString::Remove(size_t nIndex)
1250 {
1251 wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Remove") );
1252
1253 // release our lock
1254 Item(nIndex).GetStringData()->Unlock();
1255
1256 memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
1257 (m_nCount - nIndex - 1)*sizeof(char *));
1258 m_nCount--;
1259 }
1260
1261 // removes item from array (by value)
1262 void wxArrayString::Remove(const char *sz)
1263 {
1264 int iIndex = Index(sz);
1265
1266 wxCHECK_RET( iIndex != NOT_FOUND,
1267 _("removing inexistent element in wxArrayString::Remove") );
1268
1269 Remove((size_t)iIndex);
1270 }
1271
1272 // sort array elements using passed comparaison function
1273
1274 void wxArrayString::Sort(bool WXUNUSED(bCase), bool WXUNUSED(bReverse) )
1275 {
1276 //@@@@ TO DO
1277 //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);
1278 }