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