]> git.saurik.com Git - wxWidgets.git/blame - src/common/string.cpp
fix for handling from Francesco
[wxWidgets.git] / src / common / string.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/string.cpp
c801d85f 3// Purpose: wxString class
59059feb 4// Author: Vadim Zeitlin, Ryan Norton
c801d85f
KB
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
59059feb 9// (c) 2004 Ryan Norton <wxprojects@comcast.net>
65571936 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
c801d85f
KB
13/*
14 * About ref counting:
15 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
16 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
17 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
18 */
19
20// ===========================================================================
21// headers, declarations, constants
22// ===========================================================================
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
8898456d 28 #pragma hdrstop
c801d85f
KB
29#endif
30
31#ifndef WX_PRECOMP
8898456d
WS
32 #include "wx/string.h"
33 #include "wx/intl.h"
34 #include "wx/thread.h"
6b769f3d 35#endif
c801d85f
KB
36
37#include <ctype.h>
38#include <string.h>
39#include <stdlib.h>
9a08c20e 40
ce3ed50d 41#ifdef __SALFORDC__
8898456d 42 #include <clib.h>
ce3ed50d
JS
43#endif
44
3168a13f
VZ
45// allocating extra space for each string consumes more memory but speeds up
46// the concatenation operations (nLen is the current string's length)
77ca46e7
VZ
47// NB: EXTRA_ALLOC must be >= 0!
48#define EXTRA_ALLOC (19 - nLen % 16)
3168a13f 49
c801d85f
KB
50// ---------------------------------------------------------------------------
51// static class variables definition
52// ---------------------------------------------------------------------------
53
e87b7833 54#if !wxUSE_STL
2cbfa061
RN
55 //According to STL _must_ be a -1 size_t
56 const size_t wxStringBase::npos = (size_t) -1;
e87b7833 57#endif
c801d85f 58
3168a13f
VZ
59// ----------------------------------------------------------------------------
60// static data
61// ----------------------------------------------------------------------------
c801d85f 62
e87b7833
MB
63#if wxUSE_STL
64
65extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = _T("");
66
67#else
68
3c024cc2
VZ
69// for an empty string, GetStringData() will return this address: this
70// structure has the same layout as wxStringData and it's data() method will
71// return the empty string (dummy pointer)
72static const struct
73{
74 wxStringData data;
2bb67b80 75 wxChar dummy;
223d09f6 76} g_strEmpty = { {-1, 0, 0}, wxT('\0') };
3c024cc2 77
c801d85f 78// empty C style string: points to 'string data' byte of g_strEmpty
fd242375 79extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy;
c801d85f 80
e87b7833
MB
81#endif
82
3168a13f 83// ----------------------------------------------------------------------------
c801d85f 84// global functions
3168a13f 85// ----------------------------------------------------------------------------
c801d85f 86
e87b7833 87#if wxUSE_STD_IOSTREAM
c801d85f 88
6eaebb29
JS
89#include <iostream>
90
dd107c50 91wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
825ba8f0 92{
ad5bb7d6
WS
93#ifdef __BORLANDC__
94 os << str.mb_str();
95#else
96 os << str.c_str();
97#endif
98 return os;
825ba8f0
SB
99}
100
e87b7833 101#endif // wxUSE_STD_IOSTREAM
c801d85f 102
3168a13f
VZ
103// ----------------------------------------------------------------------------
104// private classes
105// ----------------------------------------------------------------------------
106
107// this small class is used to gather statistics for performance tuning
108//#define WXSTRING_STATISTICS
109#ifdef WXSTRING_STATISTICS
110 class Averager
111 {
112 public:
f6f5941b 113 Averager(const wxChar *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
2c3b684c 114 ~Averager()
f6f5941b 115 { wxPrintf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
3168a13f 116
c86f1403 117 void Add(size_t n) { m_nTotal += n; m_nCount++; }
3168a13f
VZ
118
119 private:
c86f1403 120 size_t m_nCount, m_nTotal;
f6f5941b 121 const wxChar *m_sz;
3168a13f
VZ
122 } g_averageLength("allocation size"),
123 g_averageSummandLength("summand length"),
124 g_averageConcatHit("hit probability in concat"),
125 g_averageInitialLength("initial string length");
126
127 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
128#else
129 #define STATISTICS_ADD(av, val)
130#endif // WXSTRING_STATISTICS
131
4cc6a9db
VZ
132#if !wxUSE_STL
133
ca5e07c7
GD
134// ===========================================================================
135// wxStringData class deallocation
136// ===========================================================================
137
8ecf21b7
GD
138#if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
139# pragma message (__FILE__ ": building with Multithreaded non DLL runtime has a performance impact on wxString!")
ca5e07c7
GD
140void wxStringData::Free()
141{
142 free(this);
143}
8ecf21b7 144#endif
ca5e07c7 145
c801d85f 146// ===========================================================================
e87b7833 147// wxStringBase
c801d85f
KB
148// ===========================================================================
149
c801d85f 150// takes nLength elements of psz starting at nPos
e87b7833 151void wxStringBase::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
c801d85f
KB
152{
153 Init();
154
f6bcfd97 155 // if the length is not given, assume the string to be NUL terminated
e87b7833 156 if ( nLength == npos ) {
f6bcfd97 157 wxASSERT_MSG( nPos <= wxStrlen(psz), _T("index out of bounds") );
c801d85f 158
f6bcfd97
BP
159 nLength = wxStrlen(psz + nPos);
160 }
6c68273f 161
3168a13f
VZ
162 STATISTICS_ADD(InitialLength, nLength);
163
c801d85f
KB
164 if ( nLength > 0 ) {
165 // trailing '\0' is written in AllocBuffer()
b1801e0e 166 if ( !AllocBuffer(nLength) ) {
e87b7833 167 wxFAIL_MSG( _T("out of memory in wxStringBase::InitWith") );
b1801e0e
GD
168 return;
169 }
2c09fb3b 170 wxTmemcpy(m_pchData, psz + nPos, nLength);
c801d85f
KB
171 }
172}
dd1eaa89 173
c801d85f 174// poor man's iterators are "void *" pointers
e87b7833 175wxStringBase::wxStringBase(const void *pStart, const void *pEnd)
c801d85f 176{
2bb67b80
OK
177 InitWith((const wxChar *)pStart, 0,
178 (const wxChar *)pEnd - (const wxChar *)pStart);
c801d85f
KB
179}
180
e87b7833 181wxStringBase::wxStringBase(size_type n, wxChar ch)
c801d85f 182{
e87b7833
MB
183 Init();
184 append(n, ch);
c801d85f 185}
2bb67b80 186
c801d85f
KB
187// ---------------------------------------------------------------------------
188// memory allocation
189// ---------------------------------------------------------------------------
190
191// allocates memory needed to store a C string of length nLen
e87b7833 192bool wxStringBase::AllocBuffer(size_t nLen)
c801d85f 193{
13111b2a
VZ
194 // allocating 0 sized buffer doesn't make sense, all empty strings should
195 // reuse g_strEmpty
196 wxASSERT( nLen > 0 );
197
198 // make sure that we don't overflow
199 wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) -
200 (sizeof(wxStringData) + EXTRA_ALLOC + 1) );
c801d85f 201
3168a13f
VZ
202 STATISTICS_ADD(Length, nLen);
203
c801d85f
KB
204 // allocate memory:
205 // 1) one extra character for '\0' termination
206 // 2) sizeof(wxStringData) for housekeeping info
3168a13f 207 wxStringData* pData = (wxStringData*)
2bb67b80 208 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
e015c2a3 209
b1801e0e
GD
210 if ( pData == NULL ) {
211 // allocation failures are handled by the caller
d775fa82 212 return false;
b1801e0e 213 }
e015c2a3 214
c801d85f 215 pData->nRefs = 1;
c801d85f 216 pData->nDataLength = nLen;
3168a13f 217 pData->nAllocLength = nLen + EXTRA_ALLOC;
c801d85f 218 m_pchData = pData->data(); // data starts after wxStringData
223d09f6 219 m_pchData[nLen] = wxT('\0');
d775fa82 220 return true;
c801d85f
KB
221}
222
c801d85f 223// must be called before changing this string
e87b7833 224bool wxStringBase::CopyBeforeWrite()
c801d85f
KB
225{
226 wxStringData* pData = GetStringData();
227
228 if ( pData->IsShared() ) {
229 pData->Unlock(); // memory not freed because shared
c86f1403 230 size_t nLen = pData->nDataLength;
b1801e0e
GD
231 if ( !AllocBuffer(nLen) ) {
232 // allocation failures are handled by the caller
d775fa82 233 return false;
b1801e0e 234 }
2c09fb3b 235 wxTmemcpy(m_pchData, pData->data(), nLen);
c801d85f
KB
236 }
237
3bbb630a 238 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
b1801e0e 239
d775fa82 240 return true;
c801d85f
KB
241}
242
243// must be called before replacing contents of this string
e87b7833 244bool wxStringBase::AllocBeforeWrite(size_t nLen)
c801d85f
KB
245{
246 wxASSERT( nLen != 0 ); // doesn't make any sense
247
248 // must not share string and must have enough space
3168a13f 249 wxStringData* pData = GetStringData();
fbf0c83d 250 if ( pData->IsShared() || pData->IsEmpty() ) {
c801d85f
KB
251 // can't work with old buffer, get new one
252 pData->Unlock();
b1801e0e
GD
253 if ( !AllocBuffer(nLen) ) {
254 // allocation failures are handled by the caller
d775fa82 255 return false;
b1801e0e 256 }
c801d85f 257 }
471aebdd 258 else {
fbf0c83d
VZ
259 if ( nLen > pData->nAllocLength ) {
260 // realloc the buffer instead of calling malloc() again, this is more
261 // efficient
262 STATISTICS_ADD(Length, nLen);
263
264 nLen += EXTRA_ALLOC;
265
fbf0c83d
VZ
266 pData = (wxStringData*)
267 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
e015c2a3 268
b1801e0e
GD
269 if ( pData == NULL ) {
270 // allocation failures are handled by the caller
271 // keep previous data since reallocation failed
d775fa82 272 return false;
fbf0c83d
VZ
273 }
274
275 pData->nAllocLength = nLen;
276 m_pchData = pData->data();
277 }
471aebdd 278 }
c801d85f 279
f1da2f03 280 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
b1801e0e 281
a6ed2b09
VZ
282 // it doesn't really matter what the string length is as it's going to be
283 // overwritten later but, for extra safety, set it to 0 for now as we may
284 // have some junk in m_pchData
94ac840b 285 GetStringData()->nDataLength = 0;
a6ed2b09 286
d775fa82 287 return true;
c801d85f
KB
288}
289
e87b7833
MB
290wxStringBase& wxStringBase::append(size_t n, wxChar ch)
291{
292 size_type len = length();
293
c08d805c 294 if ( !Alloc(len + n) || !CopyBeforeWrite() ) {
e87b7833
MB
295 wxFAIL_MSG( _T("out of memory in wxStringBase::append") );
296 }
297 GetStringData()->nDataLength = len + n;
298 m_pchData[len + n] = '\0';
299 for ( size_t i = 0; i < n; ++i )
300 m_pchData[len + i] = ch;
301 return *this;
302}
303
304void wxStringBase::resize(size_t nSize, wxChar ch)
305{
306 size_t len = length();
307
308 if ( nSize < len )
309 {
310 erase(begin() + nSize, end());
311 }
312 else if ( nSize > len )
313 {
314 append(nSize - len, ch);
315 }
316 //else: we have exactly the specified length, nothing to do
317}
318
dd1eaa89 319// allocate enough memory for nLen characters
e87b7833 320bool wxStringBase::Alloc(size_t nLen)
dd1eaa89
VZ
321{
322 wxStringData *pData = GetStringData();
323 if ( pData->nAllocLength <= nLen ) {
9fbd8b8d
VZ
324 if ( pData->IsEmpty() ) {
325 nLen += EXTRA_ALLOC;
326
17a1ebd1
VZ
327 pData = (wxStringData *)
328 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
b1801e0e
GD
329
330 if ( pData == NULL ) {
331 // allocation failure handled by caller
d775fa82 332 return false;
b1801e0e 333 }
e015c2a3 334
9fbd8b8d
VZ
335 pData->nRefs = 1;
336 pData->nDataLength = 0;
337 pData->nAllocLength = nLen;
338 m_pchData = pData->data(); // data starts after wxStringData
223d09f6 339 m_pchData[0u] = wxT('\0');
9fbd8b8d 340 }
3168a13f
VZ
341 else if ( pData->IsShared() ) {
342 pData->Unlock(); // memory not freed because shared
c86f1403 343 size_t nOldLen = pData->nDataLength;
b1801e0e
GD
344 if ( !AllocBuffer(nLen) ) {
345 // allocation failure handled by caller
d775fa82 346 return false;
b1801e0e 347 }
83efadb7
RN
348 // +1 to copy the terminator, too
349 memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxChar));
350 GetStringData()->nDataLength = nOldLen;
3168a13f 351 }
dd1eaa89 352 else {
3168a13f
VZ
353 nLen += EXTRA_ALLOC;
354
b1801e0e 355 pData = (wxStringData *)
2bb67b80 356 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
3168a13f 357
b1801e0e
GD
358 if ( pData == NULL ) {
359 // allocation failure handled by caller
360 // keep previous data since reallocation failed
d775fa82 361 return false;
dd1eaa89 362 }
3168a13f
VZ
363
364 // it's not important if the pointer changed or not (the check for this
365 // is not faster than assigning to m_pchData in all cases)
b1801e0e
GD
366 pData->nAllocLength = nLen;
367 m_pchData = pData->data();
dd1eaa89
VZ
368 }
369 }
370 //else: we've already got enough
d775fa82 371 return true;
dd1eaa89 372}
d775fa82 373
ac3c86ee
VS
374wxStringBase::iterator wxStringBase::begin()
375{
376 if (length() > 0)
377 CopyBeforeWrite();
378 return m_pchData;
379}
380
381wxStringBase::iterator wxStringBase::end()
382{
383 if (length() > 0)
384 CopyBeforeWrite();
385 return m_pchData + length();
386}
dd1eaa89 387
e87b7833 388wxStringBase::iterator wxStringBase::erase(iterator it)
dd1eaa89 389{
e87b7833
MB
390 size_type idx = it - begin();
391 erase(idx, 1);
392 return begin() + idx;
393}
3bbb630a 394
e87b7833
MB
395wxStringBase& wxStringBase::erase(size_t nStart, size_t nLen)
396{
7448de8d
WS
397 wxASSERT(nStart <= length());
398 size_t strLen = length() - nStart;
399 // delete nLen or up to the end of the string characters
400 nLen = strLen < nLen ? strLen : nLen;
401 wxString strTmp(c_str(), nStart);
402 strTmp.append(c_str() + nStart + nLen, length() - nStart - nLen);
403
404 swap(strTmp);
405 return *this;
e87b7833 406}
337a0010 407
e87b7833
MB
408wxStringBase& wxStringBase::insert(size_t nPos, const wxChar *sz, size_t n)
409{
7448de8d 410 wxASSERT( nPos <= length() );
3bbb630a 411
7448de8d
WS
412 if ( n == npos ) n = wxStrlen(sz);
413 if ( n == 0 ) return *this;
e87b7833 414
7448de8d
WS
415 if ( !Alloc(length() + n) || !CopyBeforeWrite() ) {
416 wxFAIL_MSG( _T("out of memory in wxStringBase::insert") );
417 }
fbf0c83d 418
7448de8d
WS
419 memmove(m_pchData + nPos + n, m_pchData + nPos,
420 (length() - nPos) * sizeof(wxChar));
421 memcpy(m_pchData + nPos, sz, n * sizeof(wxChar));
422 GetStringData()->nDataLength = length() + n;
423 m_pchData[length()] = '\0';
b1801e0e 424
7448de8d 425 return *this;
dd1eaa89
VZ
426}
427
e87b7833 428void wxStringBase::swap(wxStringBase& str)
c801d85f 429{
e87b7833
MB
430 wxChar* tmp = str.m_pchData;
431 str.m_pchData = m_pchData;
432 m_pchData = tmp;
c801d85f
KB
433}
434
e87b7833 435size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const
097c080b 436{
e66eb2df
VZ
437 // deal with the special case of empty string first
438 const size_t nLen = length();
439 const size_t nLenOther = str.length();
440
441 if ( !nLenOther )
442 {
443 // empty string is a substring of anything
444 return 0;
445 }
446
447 if ( !nLen )
448 {
449 // the other string is non empty so can't be our substring
450 return npos;
451 }
452
7448de8d 453 wxASSERT( str.GetStringData()->IsValid() );
e66eb2df 454 wxASSERT( nStart <= nLen );
e87b7833 455
e66eb2df
VZ
456 const wxChar * const other = str.c_str();
457
458 // anchor
7448de8d 459 const wxChar* p = (const wxChar*)wxTmemchr(c_str() + nStart,
e66eb2df
VZ
460 *other,
461 nLen - nStart);
7663d0d4 462
e66eb2df 463 if ( !p )
7448de8d 464 return npos;
dcb68102 465
e66eb2df 466 while ( p - c_str() + nLenOther <= nLen && wxTmemcmp(p, other, nLenOther) )
7448de8d 467 {
e66eb2df
VZ
468 p++;
469
470 // anchor again
471 p = (const wxChar*)wxTmemchr(p, *other, nLen - (p - c_str()));
472
473 if ( !p )
7448de8d
WS
474 return npos;
475 }
7663d0d4 476
e66eb2df 477 return p - c_str() + nLenOther <= nLen ? p - c_str() : npos;
097c080b
VZ
478}
479
e87b7833 480size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const
8f06a017 481{
7448de8d 482 return find(wxStringBase(sz, n), nStart);
8f06a017
RD
483}
484
e87b7833
MB
485size_t wxStringBase::find(wxChar ch, size_t nStart) const
486{
7448de8d 487 wxASSERT( nStart <= length() );
c801d85f 488
7448de8d 489 const wxChar *p = (const wxChar*)wxTmemchr(c_str() + nStart, ch, length() - nStart);
c801d85f 490
7448de8d 491 return p == NULL ? npos : p - c_str();
c801d85f
KB
492}
493
e87b7833 494size_t wxStringBase::rfind(const wxStringBase& str, size_t nStart) const
c801d85f 495{
e2101186
MB
496 wxASSERT( str.GetStringData()->IsValid() );
497 wxASSERT( nStart == npos || nStart <= length() );
498
499 if ( length() >= str.length() )
500 {
501 // avoids a corner case later
502 if ( length() == 0 && str.length() == 0 )
503 return 0;
504
505 // "top" is the point where search starts from
506 size_t top = length() - str.length();
c801d85f 507
e2101186
MB
508 if ( nStart == npos )
509 nStart = length() - 1;
510 if ( nStart < top )
511 top = nStart;
512
513 const wxChar *cursor = c_str() + top;
514 do
515 {
2c09fb3b 516 if ( wxTmemcmp(cursor, str.c_str(),
dcb68102 517 str.length()) == 0 )
e2101186
MB
518 {
519 return cursor - c_str();
520 }
78e6050b 521 } while ( cursor-- > c_str() );
e2101186 522 }
d775fa82 523
e2101186 524 return npos;
c801d85f
KB
525}
526
e87b7833 527size_t wxStringBase::rfind(const wxChar* sz, size_t nStart, size_t n) const
c801d85f 528{
e87b7833 529 return rfind(wxStringBase(sz, n), nStart);
c801d85f
KB
530}
531
e87b7833 532size_t wxStringBase::rfind(wxChar ch, size_t nStart) const
c801d85f 533{
e87b7833
MB
534 if ( nStart == npos )
535 {
536 nStart = length();
537 }
538 else
539 {
540 wxASSERT( nStart <= length() );
541 }
c801d85f 542
93c83507
MB
543 const wxChar *actual;
544 for ( actual = c_str() + ( nStart == npos ? length() : nStart + 1 );
545 actual > c_str(); --actual )
546 {
547 if ( *(actual - 1) == ch )
548 return (actual - 1) - c_str();
549 }
e87b7833 550
93c83507 551 return npos;
c801d85f
KB
552}
553
e87b7833 554size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart) const
c801d85f 555{
dcb68102
RN
556 wxASSERT(nStart <= length());
557
558 size_t len = wxStrlen(sz);
559
560 size_t i;
561 for(i = nStart; i < this->length(); ++i)
562 {
2c09fb3b 563 if (wxTmemchr(sz, *(c_str() + i), len))
dcb68102
RN
564 break;
565 }
566
567 if(i == this->length())
e87b7833 568 return npos;
dcb68102
RN
569 else
570 return i;
c801d85f 571}
e87b7833 572
e2101186
MB
573size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart,
574 size_t n) const
575{
576 return find_first_of(wxStringBase(sz, n), nStart);
577}
578
e87b7833
MB
579size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart) const
580{
581 if ( nStart == npos )
582 {
e2101186 583 nStart = length() - 1;
e87b7833
MB
584 }
585 else
586 {
65c75abe
VZ
587 wxASSERT_MSG( nStart <= length(),
588 _T("invalid index in find_last_of()") );
e87b7833
MB
589 }
590
dcb68102 591 size_t len = wxStrlen(sz);
7663d0d4 592
e2101186 593 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
e87b7833 594 {
2c09fb3b 595 if ( wxTmemchr(sz, *p, len) )
e87b7833
MB
596 return p - c_str();
597 }
598
599 return npos;
600}
601
e2101186
MB
602size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart,
603 size_t n) const
604{
605 return find_last_of(wxStringBase(sz, n), nStart);
606}
607
e87b7833
MB
608size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart) const
609{
610 if ( nStart == npos )
611 {
612 nStart = length();
613 }
614 else
615 {
616 wxASSERT( nStart <= length() );
617 }
618
dcb68102
RN
619 size_t len = wxStrlen(sz);
620
621 size_t i;
622 for(i = nStart; i < this->length(); ++i)
623 {
2c09fb3b 624 if (!wxTmemchr(sz, *(c_str() + i), len))
dcb68102
RN
625 break;
626 }
627
628 if(i == this->length())
629 return npos;
630 else
631 return i;
e2101186
MB
632}
633
634size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart,
635 size_t n) const
636{
637 return find_first_not_of(wxStringBase(sz, n), nStart);
e87b7833
MB
638}
639
640size_t wxStringBase::find_first_not_of(wxChar ch, size_t nStart) const
641{
642 wxASSERT( nStart <= length() );
643
644 for ( const wxChar *p = c_str() + nStart; *p; p++ )
645 {
646 if ( *p != ch )
647 return p - c_str();
648 }
649
650 return npos;
651}
652
653size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart) const
654{
655 if ( nStart == npos )
656 {
e2101186 657 nStart = length() - 1;
e87b7833
MB
658 }
659 else
660 {
661 wxASSERT( nStart <= length() );
662 }
663
dcb68102
RN
664 size_t len = wxStrlen(sz);
665
e2101186 666 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
e87b7833 667 {
2c09fb3b 668 if ( !wxTmemchr(sz, *p,len) )
dcb68102 669 return p - c_str();
e87b7833
MB
670 }
671
672 return npos;
673}
674
e2101186
MB
675size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart,
676 size_t n) const
677{
678 return find_last_not_of(wxStringBase(sz, n), nStart);
679}
680
e87b7833
MB
681size_t wxStringBase::find_last_not_of(wxChar ch, size_t nStart) const
682{
683 if ( nStart == npos )
684 {
e2101186 685 nStart = length() - 1;
e87b7833
MB
686 }
687 else
688 {
689 wxASSERT( nStart <= length() );
690 }
691
e2101186 692 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
e87b7833
MB
693 {
694 if ( *p != ch )
695 return p - c_str();
696 }
697
698 return npos;
699}
700
701wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
702 const wxChar *sz)
703{
704 wxASSERT_MSG( nStart <= length(),
705 _T("index out of bounds in wxStringBase::replace") );
706 size_t strLen = length() - nStart;
707 nLen = strLen < nLen ? strLen : nLen;
708
709 wxStringBase strTmp;
710 strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs
711
510bb748
RN
712 //This is kind of inefficient, but its pretty good considering...
713 //we don't want to use character access operators here because on STL
714 //it will freeze the reference count of strTmp, which means a deep copy
715 //at the end when swap is called
716 //
717 //Also, we can't use append with the full character pointer and must
718 //do it manually because this string can contain null characters
92413909
RN
719 for(size_t i1 = 0; i1 < nStart; ++i1)
720 strTmp.append(1, this->c_str()[i1]);
ad5bb7d6 721
510bb748
RN
722 //its safe to do the full version here because
723 //sz must be a normal c string
e87b7833 724 strTmp.append(sz);
510bb748
RN
725
726 for(size_t i2 = nStart + nLen; i2 < length(); ++i2)
727 strTmp.append(1, this->c_str()[i2]);
e87b7833
MB
728
729 swap(strTmp);
730 return *this;
731}
732
733wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
734 size_t nCount, wxChar ch)
735{
7663d0d4 736 return replace(nStart, nLen, wxStringBase(nCount, ch).c_str());
e87b7833
MB
737}
738
739wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
740 const wxStringBase& str,
741 size_t nStart2, size_t nLen2)
742{
743 return replace(nStart, nLen, str.substr(nStart2, nLen2));
744}
745
746wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
747 const wxChar* sz, size_t nCount)
748{
749 return replace(nStart, nLen, wxStringBase(sz, nCount).c_str());
750}
751
752wxStringBase wxStringBase::substr(size_t nStart, size_t nLen) const
753{
754 if ( nLen == npos )
755 nLen = length() - nStart;
756 return wxStringBase(*this, nStart, nLen);
757}
758
759// assigns one string to another
760wxStringBase& wxStringBase::operator=(const wxStringBase& stringSrc)
761{
762 wxASSERT( stringSrc.GetStringData()->IsValid() );
763
764 // don't copy string over itself
765 if ( m_pchData != stringSrc.m_pchData ) {
766 if ( stringSrc.GetStringData()->IsEmpty() ) {
767 Reinit();
768 }
769 else {
770 // adjust references
771 GetStringData()->Unlock();
772 m_pchData = stringSrc.m_pchData;
773 GetStringData()->Lock();
774 }
775 }
776
777 return *this;
778}
779
780// assigns a single character
781wxStringBase& wxStringBase::operator=(wxChar ch)
782{
783 if ( !AssignCopy(1, &ch) ) {
784 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(wxChar)") );
785 }
786 return *this;
787}
788
789// assigns C string
790wxStringBase& wxStringBase::operator=(const wxChar *psz)
791{
792 if ( !AssignCopy(wxStrlen(psz), psz) ) {
793 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(const wxChar *)") );
794 }
795 return *this;
796}
797
798// helper function: does real copy
799bool wxStringBase::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
800{
801 if ( nSrcLen == 0 ) {
802 Reinit();
803 }
804 else {
805 if ( !AllocBeforeWrite(nSrcLen) ) {
806 // allocation failure handled by caller
d775fa82 807 return false;
e87b7833
MB
808 }
809 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
810 GetStringData()->nDataLength = nSrcLen;
811 m_pchData[nSrcLen] = wxT('\0');
812 }
d775fa82 813 return true;
e87b7833
MB
814}
815
816// ---------------------------------------------------------------------------
817// string concatenation
818// ---------------------------------------------------------------------------
c801d85f 819
c801d85f 820// add something to this string
e87b7833
MB
821bool wxStringBase::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData,
822 size_t nMaxLen)
c801d85f 823{
3168a13f 824 STATISTICS_ADD(SummandLength, nSrcLen);
c801d85f 825
e87b7833
MB
826 nSrcLen = nSrcLen < nMaxLen ? nSrcLen : nMaxLen;
827
05488905
VZ
828 // concatenating an empty string is a NOP
829 if ( nSrcLen > 0 ) {
830 wxStringData *pData = GetStringData();
831 size_t nLen = pData->nDataLength;
832 size_t nNewLen = nLen + nSrcLen;
c801d85f 833
05488905
VZ
834 // alloc new buffer if current is too small
835 if ( pData->IsShared() ) {
836 STATISTICS_ADD(ConcatHit, 0);
3168a13f 837
05488905
VZ
838 // we have to allocate another buffer
839 wxStringData* pOldData = GetStringData();
b1801e0e
GD
840 if ( !AllocBuffer(nNewLen) ) {
841 // allocation failure handled by caller
d775fa82 842 return false;
b1801e0e 843 }
2bb67b80 844 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
05488905
VZ
845 pOldData->Unlock();
846 }
847 else if ( nNewLen > pData->nAllocLength ) {
848 STATISTICS_ADD(ConcatHit, 0);
3168a13f 849
e87b7833 850 reserve(nNewLen);
05488905 851 // we have to grow the buffer
e87b7833 852 if ( capacity() < nNewLen ) {
b1801e0e 853 // allocation failure handled by caller
d775fa82 854 return false;
b1801e0e 855 }
05488905
VZ
856 }
857 else {
858 STATISTICS_ADD(ConcatHit, 1);
3168a13f 859
05488905
VZ
860 // the buffer is already big enough
861 }
3168a13f 862
05488905
VZ
863 // should be enough space
864 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
3168a13f 865
05488905 866 // fast concatenation - all is done in our buffer
2bb67b80 867 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
3168a13f 868
e87b7833
MB
869 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
870 GetStringData()->nDataLength = nNewLen; // and fix the length
871 }
872 //else: the string to append was empty
d775fa82 873 return true;
e87b7833
MB
874}
875
876// ---------------------------------------------------------------------------
877// simple sub-string extraction
878// ---------------------------------------------------------------------------
879
880// helper function: clone the data attached to this string
881bool wxStringBase::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
882{
883 if ( nCopyLen == 0 ) {
884 dest.Init();
885 }
886 else {
887 if ( !dest.AllocBuffer(nCopyLen) ) {
888 // allocation failure handled by caller
d775fa82 889 return false;
e87b7833
MB
890 }
891 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
892 }
d775fa82 893 return true;
e87b7833
MB
894}
895
896#endif // !wxUSE_STL
897
898#if !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
899
900#if !wxUSE_STL
901 #define STRINGCLASS wxStringBase
902#else
903 #define STRINGCLASS wxString
904#endif
905
906static inline int wxDoCmp(const wxChar* s1, size_t l1,
907 const wxChar* s2, size_t l2)
908{
909 if( l1 == l2 )
2c09fb3b 910 return wxTmemcmp(s1, s2, l1);
e87b7833
MB
911 else if( l1 < l2 )
912 {
2c09fb3b 913 int ret = wxTmemcmp(s1, s2, l1);
e87b7833
MB
914 return ret == 0 ? -1 : ret;
915 }
2c09fb3b 916 else
e87b7833 917 {
2c09fb3b 918 int ret = wxTmemcmp(s1, s2, l2);
e87b7833
MB
919 return ret == 0 ? +1 : ret;
920 }
e87b7833
MB
921}
922
e87b7833
MB
923int STRINGCLASS::compare(const wxStringBase& str) const
924{
925 return ::wxDoCmp(data(), length(), str.data(), str.length());
926}
927
e87b7833
MB
928int STRINGCLASS::compare(size_t nStart, size_t nLen,
929 const wxStringBase& str) const
930{
931 wxASSERT(nStart <= length());
932 size_type strLen = length() - nStart;
933 nLen = strLen < nLen ? strLen : nLen;
934 return ::wxDoCmp(data() + nStart, nLen, str.data(), str.length());
935}
936
937int STRINGCLASS::compare(size_t nStart, size_t nLen,
938 const wxStringBase& str,
939 size_t nStart2, size_t nLen2) const
940{
941 wxASSERT(nStart <= length());
942 wxASSERT(nStart2 <= str.length());
943 size_type strLen = length() - nStart,
944 strLen2 = str.length() - nStart2;
945 nLen = strLen < nLen ? strLen : nLen;
946 nLen2 = strLen2 < nLen2 ? strLen2 : nLen2;
947 return ::wxDoCmp(data() + nStart, nLen, str.data() + nStart2, nLen2);
948}
949
e87b7833
MB
950int STRINGCLASS::compare(const wxChar* sz) const
951{
952 size_t nLen = wxStrlen(sz);
953 return ::wxDoCmp(data(), length(), sz, nLen);
954}
955
e87b7833
MB
956int STRINGCLASS::compare(size_t nStart, size_t nLen,
957 const wxChar* sz, size_t nCount) const
958{
959 wxASSERT(nStart <= length());
960 size_type strLen = length() - nStart;
961 nLen = strLen < nLen ? strLen : nLen;
962 if( nCount == npos )
963 nCount = wxStrlen(sz);
964
965 return ::wxDoCmp(data() + nStart, nLen, sz, nCount);
966}
967
968#undef STRINGCLASS
969
970#endif // !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
971
972// ===========================================================================
973// wxString class core
974// ===========================================================================
975
06386448
RN
976// ---------------------------------------------------------------------------
977// construction and conversion
e87b7833
MB
978// ---------------------------------------------------------------------------
979
980#if wxUSE_UNICODE
981
982// from multibyte string
830f8f11 983wxString::wxString(const char *psz, const wxMBConv& conv, size_t nLength)
e87b7833 984{
e87b7833 985 // anything to do?
eec47cc6 986 if ( psz && nLength != 0 )
e87b7833 987 {
eec47cc6
VZ
988 if ( nLength == npos )
989 {
467e0479 990 nLength = wxNO_LEN;
eec47cc6
VZ
991 }
992
993 size_t nLenWide;
994 wxWCharBuffer wbuf = conv.cMB2WC(psz, nLength, &nLenWide);
e4e3bbb4 995
eec47cc6
VZ
996 if ( nLenWide )
997 assign(wbuf, nLenWide);
e87b7833 998 }
7663d0d4 999}
265d5cce 1000
06386448 1001//Convert wxString in Unicode mode to a multi-byte string
830f8f11 1002const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const
265d5cce 1003{
eec47cc6 1004 return conv.cWC2MB(c_str(), length() + 1 /* size, not length */, NULL);
e87b7833
MB
1005}
1006
1007#else // ANSI
1008
1009#if wxUSE_WCHAR_T
eec47cc6 1010
e87b7833 1011// from wide string
830f8f11 1012wxString::wxString(const wchar_t *pwz, const wxMBConv& conv, size_t nLength)
e87b7833 1013{
e87b7833 1014 // anything to do?
eec47cc6 1015 if ( pwz && nLength != 0 )
e87b7833 1016 {
eec47cc6
VZ
1017 if ( nLength == npos )
1018 {
467e0479 1019 nLength = wxNO_LEN;
eec47cc6
VZ
1020 }
1021
1022 size_t nLenMB;
1023 wxCharBuffer buf = conv.cWC2MB(pwz, nLength, &nLenMB);
e4e3bbb4 1024
eec47cc6
VZ
1025 if ( nLenMB )
1026 assign(buf, nLenMB);
e87b7833 1027 }
e87b7833 1028}
265d5cce 1029
7663d0d4 1030//Converts this string to a wide character string if unicode
06386448 1031//mode is not enabled and wxUSE_WCHAR_T is enabled
830f8f11 1032const wxWCharBuffer wxString::wc_str(const wxMBConv& conv) const
265d5cce 1033{
eec47cc6 1034 return conv.cMB2WC(c_str(), length() + 1 /* size, not length */, NULL);
265d5cce 1035}
7663d0d4 1036
e87b7833
MB
1037#endif // wxUSE_WCHAR_T
1038
1039#endif // Unicode/ANSI
1040
1041// shrink to minimal size (releasing extra memory)
1042bool wxString::Shrink()
1043{
1044 wxString tmp(begin(), end());
1045 swap(tmp);
1046 return tmp.length() == length();
1047}
1048
1049#if !wxUSE_STL
1050// get the pointer to writable buffer of (at least) nLen bytes
1051wxChar *wxString::GetWriteBuf(size_t nLen)
1052{
1053 if ( !AllocBeforeWrite(nLen) ) {
1054 // allocation failure handled by caller
1055 return NULL;
1056 }
1057
1058 wxASSERT( GetStringData()->nRefs == 1 );
d775fa82 1059 GetStringData()->Validate(false);
e87b7833
MB
1060
1061 return m_pchData;
1062}
1063
1064// put string back in a reasonable state after GetWriteBuf
1065void wxString::UngetWriteBuf()
1066{
1067 GetStringData()->nDataLength = wxStrlen(m_pchData);
d775fa82 1068 GetStringData()->Validate(true);
e87b7833
MB
1069}
1070
1071void wxString::UngetWriteBuf(size_t nLen)
1072{
1073 GetStringData()->nDataLength = nLen;
d775fa82 1074 GetStringData()->Validate(true);
e87b7833
MB
1075}
1076#endif
1077
1078// ---------------------------------------------------------------------------
1079// data access
1080// ---------------------------------------------------------------------------
1081
1082// all functions are inline in string.h
1083
1084// ---------------------------------------------------------------------------
1085// assignment operators
1086// ---------------------------------------------------------------------------
1087
1088#if !wxUSE_UNICODE
1089
1090// same as 'signed char' variant
1091wxString& wxString::operator=(const unsigned char* psz)
1092{
1093 *this = (const char *)psz;
1094 return *this;
1095}
1096
1097#if wxUSE_WCHAR_T
1098wxString& wxString::operator=(const wchar_t *pwz)
1099{
1100 wxString str(pwz);
1101 swap(str);
1102 return *this;
c801d85f 1103}
e87b7833
MB
1104#endif
1105
1106#endif
c801d85f
KB
1107
1108/*
c801d85f
KB
1109 * concatenation functions come in 5 flavours:
1110 * string + string
1111 * char + string and string + char
1112 * C str + string and string + C str
1113 */
1114
b1801e0e 1115wxString operator+(const wxString& str1, const wxString& str2)
c801d85f 1116{
e87b7833 1117#if !wxUSE_STL
3458e408
WS
1118 wxASSERT( str1.GetStringData()->IsValid() );
1119 wxASSERT( str2.GetStringData()->IsValid() );
e87b7833 1120#endif
097c080b 1121
3458e408
WS
1122 wxString s = str1;
1123 s += str2;
3168a13f 1124
3458e408 1125 return s;
c801d85f
KB
1126}
1127
b1801e0e 1128wxString operator+(const wxString& str, wxChar ch)
c801d85f 1129{
e87b7833 1130#if !wxUSE_STL
3458e408 1131 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1132#endif
3168a13f 1133
3458e408
WS
1134 wxString s = str;
1135 s += ch;
097c080b 1136
3458e408 1137 return s;
c801d85f
KB
1138}
1139
b1801e0e 1140wxString operator+(wxChar ch, const wxString& str)
c801d85f 1141{
e87b7833 1142#if !wxUSE_STL
3458e408 1143 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1144#endif
097c080b 1145
3458e408
WS
1146 wxString s = ch;
1147 s += str;
3168a13f 1148
3458e408 1149 return s;
c801d85f
KB
1150}
1151
b1801e0e 1152wxString operator+(const wxString& str, const wxChar *psz)
c801d85f 1153{
e87b7833 1154#if !wxUSE_STL
3458e408 1155 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1156#endif
097c080b 1157
3458e408
WS
1158 wxString s;
1159 if ( !s.Alloc(wxStrlen(psz) + str.length()) ) {
1160 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1161 }
1162 s += str;
1163 s += psz;
3168a13f 1164
3458e408 1165 return s;
c801d85f
KB
1166}
1167
b1801e0e 1168wxString operator+(const wxChar *psz, const wxString& str)
c801d85f 1169{
e87b7833 1170#if !wxUSE_STL
3458e408 1171 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1172#endif
097c080b 1173
3458e408
WS
1174 wxString s;
1175 if ( !s.Alloc(wxStrlen(psz) + str.length()) ) {
1176 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1177 }
1178 s = psz;
1179 s += str;
3168a13f 1180
3458e408 1181 return s;
c801d85f
KB
1182}
1183
1184// ===========================================================================
1185// other common string functions
1186// ===========================================================================
1187
dcb68102
RN
1188int wxString::Cmp(const wxString& s) const
1189{
1190 return compare(s);
1191}
1192
1193int wxString::Cmp(const wxChar* psz) const
1194{
1195 return compare(psz);
1196}
1197
1198static inline int wxDoCmpNoCase(const wxChar* s1, size_t l1,
1199 const wxChar* s2, size_t l2)
1200{
1201 size_t i;
1202
1203 if( l1 == l2 )
1204 {
1205 for(i = 0; i < l1; ++i)
1206 {
1207 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1208 break;
1209 }
59059feb 1210 return i == l1 ? 0 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
dcb68102
RN
1211 }
1212 else if( l1 < l2 )
1213 {
1214 for(i = 0; i < l1; ++i)
1215 {
1216 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1217 break;
1218 }
59059feb 1219 return i == l1 ? -1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
dcb68102 1220 }
2c09fb3b 1221 else
dcb68102
RN
1222 {
1223 for(i = 0; i < l2; ++i)
1224 {
1225 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1226 break;
1227 }
35b4f9ca 1228 return i == l2 ? 1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
dcb68102 1229 }
dcb68102
RN
1230}
1231
1232int wxString::CmpNoCase(const wxString& s) const
1233{
1234 return wxDoCmpNoCase(data(), length(), s.data(), s.length());
1235}
1236
1237int wxString::CmpNoCase(const wxChar* psz) const
1238{
1239 int nLen = wxStrlen(psz);
1240
1241 return wxDoCmpNoCase(data(), length(), psz, nLen);
1242}
1243
1244
b1ac3b56 1245#if wxUSE_UNICODE
e015c2a3 1246
cf6bedce
SC
1247#ifdef __MWERKS__
1248#ifndef __SCHAR_MAX__
1249#define __SCHAR_MAX__ 127
1250#endif
1251#endif
1252
e015c2a3 1253wxString wxString::FromAscii(const char *ascii)
b1ac3b56
RR
1254{
1255 if (!ascii)
1256 return wxEmptyString;
e015c2a3 1257
b1ac3b56
RR
1258 size_t len = strlen( ascii );
1259 wxString res;
e015c2a3
VZ
1260
1261 if ( len )
1262 {
1263 wxStringBuffer buf(res, len);
1264
1265 wchar_t *dest = buf;
1266
1267 for ( ;; )
1268 {
1269 if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' )
1270 break;
1271 }
1272 }
1273
b1ac3b56
RR
1274 return res;
1275}
1276
2b5f62a0
VZ
1277wxString wxString::FromAscii(const char ascii)
1278{
1279 // What do we do with '\0' ?
1280
1281 wxString res;
1282 res += (wchar_t)(unsigned char) ascii;
8760bc65 1283
2b5f62a0
VZ
1284 return res;
1285}
1286
b1ac3b56
RR
1287const wxCharBuffer wxString::ToAscii() const
1288{
e015c2a3
VZ
1289 // this will allocate enough space for the terminating NUL too
1290 wxCharBuffer buffer(length());
b1ac3b56 1291
be7eecf8 1292
6e394fc6 1293 char *dest = buffer.data();
e015c2a3
VZ
1294
1295 const wchar_t *pwc = c_str();
1296 for ( ;; )
b1ac3b56 1297 {
6e394fc6 1298 *dest++ = (char)(*pwc > SCHAR_MAX ? wxT('_') : *pwc);
e015c2a3
VZ
1299
1300 // the output string can't have embedded NULs anyhow, so we can safely
1301 // stop at first of them even if we do have any
1302 if ( !*pwc++ )
1303 break;
b1ac3b56 1304 }
e015c2a3 1305
b1ac3b56
RR
1306 return buffer;
1307}
e015c2a3
VZ
1308
1309#endif // Unicode
b1ac3b56 1310
c801d85f 1311// extract string of length nCount starting at nFirst
c801d85f
KB
1312wxString wxString::Mid(size_t nFirst, size_t nCount) const
1313{
73f507f5 1314 size_t nLen = length();
30d9011f 1315
73f507f5
WS
1316 // default value of nCount is npos and means "till the end"
1317 if ( nCount == npos )
1318 {
1319 nCount = nLen - nFirst;
1320 }
30d9011f 1321
73f507f5
WS
1322 // out-of-bounds requests return sensible things
1323 if ( nFirst + nCount > nLen )
1324 {
1325 nCount = nLen - nFirst;
1326 }
c801d85f 1327
73f507f5
WS
1328 if ( nFirst > nLen )
1329 {
1330 // AllocCopy() will return empty string
1331 return wxEmptyString;
1332 }
c801d85f 1333
73f507f5
WS
1334 wxString dest(*this, nFirst, nCount);
1335 if ( dest.length() != nCount )
1336 {
1337 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
1338 }
30d9011f 1339
73f507f5 1340 return dest;
c801d85f
KB
1341}
1342
e87b7833 1343// check that the string starts with prefix and return the rest of the string
d775fa82 1344// in the provided pointer if it is not NULL, otherwise return false
f6bcfd97
BP
1345bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const
1346{
1347 wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") );
1348
1349 // first check if the beginning of the string matches the prefix: note
1350 // that we don't have to check that we don't run out of this string as
1351 // when we reach the terminating NUL, either prefix string ends too (and
1352 // then it's ok) or we break out of the loop because there is no match
1353 const wxChar *p = c_str();
1354 while ( *prefix )
1355 {
1356 if ( *prefix++ != *p++ )
1357 {
1358 // no match
d775fa82 1359 return false;
f6bcfd97
BP
1360 }
1361 }
1362
1363 if ( rest )
1364 {
1365 // put the rest of the string into provided pointer
1366 *rest = p;
1367 }
1368
d775fa82 1369 return true;
f6bcfd97
BP
1370}
1371
3affcd07
VZ
1372
1373// check that the string ends with suffix and return the rest of it in the
1374// provided pointer if it is not NULL, otherwise return false
1375bool wxString::EndsWith(const wxChar *suffix, wxString *rest) const
1376{
1377 wxASSERT_MSG( suffix, _T("invalid parameter in wxString::EndssWith") );
1378
1379 int start = length() - wxStrlen(suffix);
1380 if ( start < 0 || wxStrcmp(c_str() + start, suffix) != 0 )
1381 return false;
1382
1383 if ( rest )
1384 {
1385 // put the rest of the string into provided pointer
1386 rest->assign(*this, 0, start);
1387 }
1388
1389 return true;
1390}
1391
1392
c801d85f
KB
1393// extract nCount last (rightmost) characters
1394wxString wxString::Right(size_t nCount) const
1395{
e87b7833
MB
1396 if ( nCount > length() )
1397 nCount = length();
c801d85f 1398
e87b7833
MB
1399 wxString dest(*this, length() - nCount, nCount);
1400 if ( dest.length() != nCount ) {
b1801e0e
GD
1401 wxFAIL_MSG( _T("out of memory in wxString::Right") );
1402 }
c801d85f
KB
1403 return dest;
1404}
1405
1406// get all characters after the last occurence of ch
1407// (returns the whole string if ch not found)
2bb67b80 1408wxString wxString::AfterLast(wxChar ch) const
c801d85f
KB
1409{
1410 wxString str;
d775fa82 1411 int iPos = Find(ch, true);
3c67202d 1412 if ( iPos == wxNOT_FOUND )
c801d85f
KB
1413 str = *this;
1414 else
c8cfb486 1415 str = c_str() + iPos + 1;
c801d85f
KB
1416
1417 return str;
1418}
1419
1420// extract nCount first (leftmost) characters
1421wxString wxString::Left(size_t nCount) const
1422{
e87b7833
MB
1423 if ( nCount > length() )
1424 nCount = length();
c801d85f 1425
e87b7833
MB
1426 wxString dest(*this, 0, nCount);
1427 if ( dest.length() != nCount ) {
b1801e0e
GD
1428 wxFAIL_MSG( _T("out of memory in wxString::Left") );
1429 }
c801d85f
KB
1430 return dest;
1431}
1432
1433// get all characters before the first occurence of ch
1434// (returns the whole string if ch not found)
2bb67b80 1435wxString wxString::BeforeFirst(wxChar ch) const
c801d85f 1436{
e87b7833
MB
1437 int iPos = Find(ch);
1438 if ( iPos == wxNOT_FOUND ) iPos = length();
1439 return wxString(*this, 0, iPos);
c801d85f
KB
1440}
1441
1442/// get all characters before the last occurence of ch
1443/// (returns empty string if ch not found)
2bb67b80 1444wxString wxString::BeforeLast(wxChar ch) const
c801d85f
KB
1445{
1446 wxString str;
d775fa82 1447 int iPos = Find(ch, true);
3c67202d 1448 if ( iPos != wxNOT_FOUND && iPos != 0 )
d1c9bbf6 1449 str = wxString(c_str(), iPos);
c801d85f
KB
1450
1451 return str;
1452}
1453
1454/// get all characters after the first occurence of ch
1455/// (returns empty string if ch not found)
2bb67b80 1456wxString wxString::AfterFirst(wxChar ch) const
c801d85f
KB
1457{
1458 wxString str;
1459 int iPos = Find(ch);
3c67202d 1460 if ( iPos != wxNOT_FOUND )
c801d85f
KB
1461 str = c_str() + iPos + 1;
1462
1463 return str;
1464}
1465
1466// replace first (or all) occurences of some substring with another one
ad5bb7d6 1467size_t wxString::Replace(const wxChar *szOld,
510bb748 1468 const wxChar *szNew, bool bReplaceAll)
c801d85f 1469{
a8f1f1b2
VZ
1470 // if we tried to replace an empty string we'd enter an infinite loop below
1471 wxCHECK_MSG( szOld && *szOld && szNew, 0,
1472 _T("wxString::Replace(): invalid parameter") );
1473
510bb748 1474 size_t uiCount = 0; // count of replacements made
c801d85f 1475
510bb748
RN
1476 size_t uiOldLen = wxStrlen(szOld);
1477 size_t uiNewLen = wxStrlen(szNew);
c801d85f 1478
510bb748 1479 size_t dwPos = 0;
c801d85f 1480
ad5bb7d6 1481 while ( this->c_str()[dwPos] != wxT('\0') )
510bb748
RN
1482 {
1483 //DO NOT USE STRSTR HERE
1484 //this string can contain embedded null characters,
1485 //so strstr will function incorrectly
1486 dwPos = find(szOld, dwPos);
ad5bb7d6 1487 if ( dwPos == npos )
510bb748 1488 break; // exit the loop
ad5bb7d6 1489 else
510bb748
RN
1490 {
1491 //replace this occurance of the old string with the new one
1492 replace(dwPos, uiOldLen, szNew, uiNewLen);
1493
2df0258e
RN
1494 //move up pos past the string that was replaced
1495 dwPos += uiNewLen;
510bb748
RN
1496
1497 //increase replace count
1498 ++uiCount;
ad5bb7d6 1499
510bb748 1500 // stop now?
ad5bb7d6 1501 if ( !bReplaceAll )
510bb748
RN
1502 break; // exit the loop
1503 }
c801d85f 1504 }
c801d85f 1505
510bb748 1506 return uiCount;
c801d85f
KB
1507}
1508
1509bool wxString::IsAscii() const
1510{
2bb67b80 1511 const wxChar *s = (const wxChar*) *this;
c801d85f 1512 while(*s){
d775fa82 1513 if(!isascii(*s)) return(false);
c801d85f
KB
1514 s++;
1515 }
d775fa82 1516 return(true);
c801d85f 1517}
dd1eaa89 1518
c801d85f
KB
1519bool wxString::IsWord() const
1520{
2bb67b80 1521 const wxChar *s = (const wxChar*) *this;
c801d85f 1522 while(*s){
d775fa82 1523 if(!wxIsalpha(*s)) return(false);
c801d85f
KB
1524 s++;
1525 }
d775fa82 1526 return(true);
c801d85f 1527}
dd1eaa89 1528
c801d85f
KB
1529bool wxString::IsNumber() const
1530{
2bb67b80 1531 const wxChar *s = (const wxChar*) *this;
2f74ed28 1532 if (wxStrlen(s))
930357bd 1533 if ((s[0] == wxT('-')) || (s[0] == wxT('+'))) s++;
c801d85f 1534 while(*s){
d775fa82 1535 if(!wxIsdigit(*s)) return(false);
c801d85f
KB
1536 s++;
1537 }
d775fa82 1538 return(true);
c801d85f
KB
1539}
1540
c801d85f
KB
1541wxString wxString::Strip(stripType w) const
1542{
1543 wxString s = *this;
d775fa82
WS
1544 if ( w & leading ) s.Trim(false);
1545 if ( w & trailing ) s.Trim(true);
c801d85f
KB
1546 return s;
1547}
1548
c801d85f
KB
1549// ---------------------------------------------------------------------------
1550// case conversion
1551// ---------------------------------------------------------------------------
1552
1553wxString& wxString::MakeUpper()
1554{
e87b7833
MB
1555 for ( iterator it = begin(), en = end(); it != en; ++it )
1556 *it = (wxChar)wxToupper(*it);
c801d85f
KB
1557
1558 return *this;
1559}
1560
1561wxString& wxString::MakeLower()
1562{
e87b7833
MB
1563 for ( iterator it = begin(), en = end(); it != en; ++it )
1564 *it = (wxChar)wxTolower(*it);
c801d85f
KB
1565
1566 return *this;
1567}
1568
1569// ---------------------------------------------------------------------------
1570// trimming and padding
1571// ---------------------------------------------------------------------------
1572
d775fa82 1573// some compilers (VC++ 6.0 not to name them) return true for a call to
576c608d
VZ
1574