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