]> git.saurik.com Git - wxWidgets.git/blame - src/common/string.cpp
compilation fix for !wxUSE_ON_FATAL_EXCEPTION
[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 483 {
f68ca36e
RN
484 //Previosly passed as the first argument to wxTmemchr,
485 //but C/C++ standard does not specify evaluation order
486 //of arguments to functions -
487 //http://embedded.com/showArticle.jhtml?articleID=9900607
488 ++p;
489
dcb68102 490 //anchor again
f68ca36e 491 p = (const wxChar*)wxTmemchr(p,
7663d0d4 492 str.c_str()[0],
7c9b5f42 493 length() - (p - c_str()));
e87b7833 494
dcb68102
RN
495 if(!p)
496 return npos;
497 }
7663d0d4 498
dcb68102 499 return (p - c_str() + str.length() <= length()) ? p - c_str() : npos;
097c080b
VZ
500}
501
e87b7833 502size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const
8f06a017 503{
e87b7833 504 return find(wxStringBase(sz, n), nStart);
8f06a017
RD
505}
506
e87b7833
MB
507size_t wxStringBase::find(wxChar ch, size_t nStart) const
508{
509 wxASSERT( nStart <= length() );
c801d85f 510
2c09fb3b 511 const wxChar *p = (const wxChar*)wxTmemchr(c_str() + nStart, ch, length() - nStart);
c801d85f 512
e87b7833 513 return p == NULL ? npos : p - c_str();
c801d85f
KB
514}
515
e87b7833 516size_t wxStringBase::rfind(const wxStringBase& str, size_t nStart) const
c801d85f 517{
e2101186
MB
518 wxASSERT( str.GetStringData()->IsValid() );
519 wxASSERT( nStart == npos || nStart <= length() );
520
521 if ( length() >= str.length() )
522 {
523 // avoids a corner case later
524 if ( length() == 0 && str.length() == 0 )
525 return 0;
526
527 // "top" is the point where search starts from
528 size_t top = length() - str.length();
c801d85f 529
e2101186
MB
530 if ( nStart == npos )
531 nStart = length() - 1;
532 if ( nStart < top )
533 top = nStart;
534
535 const wxChar *cursor = c_str() + top;
536 do
537 {
2c09fb3b 538 if ( wxTmemcmp(cursor, str.c_str(),
dcb68102 539 str.length()) == 0 )
e2101186
MB
540 {
541 return cursor - c_str();
542 }
78e6050b 543 } while ( cursor-- > c_str() );
e2101186 544 }
d775fa82 545
e2101186 546 return npos;
c801d85f
KB
547}
548
e87b7833 549size_t wxStringBase::rfind(const wxChar* sz, size_t nStart, size_t n) const
c801d85f 550{
e87b7833 551 return rfind(wxStringBase(sz, n), nStart);
c801d85f
KB
552}
553
e87b7833 554size_t wxStringBase::rfind(wxChar ch, size_t nStart) const
c801d85f 555{
e87b7833
MB
556 if ( nStart == npos )
557 {
558 nStart = length();
559 }
560 else
561 {
562 wxASSERT( nStart <= length() );
563 }
c801d85f 564
93c83507
MB
565 const wxChar *actual;
566 for ( actual = c_str() + ( nStart == npos ? length() : nStart + 1 );
567 actual > c_str(); --actual )
568 {
569 if ( *(actual - 1) == ch )
570 return (actual - 1) - c_str();
571 }
e87b7833 572
93c83507 573 return npos;
c801d85f
KB
574}
575
e87b7833 576size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart) const
c801d85f 577{
dcb68102
RN
578 wxASSERT(nStart <= length());
579
580 size_t len = wxStrlen(sz);
581
582 size_t i;
583 for(i = nStart; i < this->length(); ++i)
584 {
2c09fb3b 585 if (wxTmemchr(sz, *(c_str() + i), len))
dcb68102
RN
586 break;
587 }
588
589 if(i == this->length())
e87b7833 590 return npos;
dcb68102
RN
591 else
592 return i;
c801d85f 593}
e87b7833 594
e2101186
MB
595size_t wxStringBase::find_first_of(const wxChar* sz, size_t nStart,
596 size_t n) const
597{
598 return find_first_of(wxStringBase(sz, n), nStart);
599}
600
e87b7833
MB
601size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart) const
602{
603 if ( nStart == npos )
604 {
e2101186 605 nStart = length() - 1;
e87b7833
MB
606 }
607 else
608 {
65c75abe
VZ
609 wxASSERT_MSG( nStart <= length(),
610 _T("invalid index in find_last_of()") );
e87b7833
MB
611 }
612
dcb68102 613 size_t len = wxStrlen(sz);
7663d0d4 614
e2101186 615 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
e87b7833 616 {
2c09fb3b 617 if ( wxTmemchr(sz, *p, len) )
e87b7833
MB
618 return p - c_str();
619 }
620
621 return npos;
622}
623
e2101186
MB
624size_t wxStringBase::find_last_of(const wxChar* sz, size_t nStart,
625 size_t n) const
626{
627 return find_last_of(wxStringBase(sz, n), nStart);
628}
629
e87b7833
MB
630size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart) const
631{
632 if ( nStart == npos )
633 {
634 nStart = length();
635 }
636 else
637 {
638 wxASSERT( nStart <= length() );
639 }
640
dcb68102
RN
641 size_t len = wxStrlen(sz);
642
643 size_t i;
644 for(i = nStart; i < this->length(); ++i)
645 {
2c09fb3b 646 if (!wxTmemchr(sz, *(c_str() + i), len))
dcb68102
RN
647 break;
648 }
649
650 if(i == this->length())
651 return npos;
652 else
653 return i;
e2101186
MB
654}
655
656size_t wxStringBase::find_first_not_of(const wxChar* sz, size_t nStart,
657 size_t n) const
658{
659 return find_first_not_of(wxStringBase(sz, n), nStart);
e87b7833
MB
660}
661
662size_t wxStringBase::find_first_not_of(wxChar ch, size_t nStart) const
663{
664 wxASSERT( nStart <= length() );
665
666 for ( const wxChar *p = c_str() + nStart; *p; p++ )
667 {
668 if ( *p != ch )
669 return p - c_str();
670 }
671
672 return npos;
673}
674
675size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart) const
676{
677 if ( nStart == npos )
678 {
e2101186 679 nStart = length() - 1;
e87b7833
MB
680 }
681 else
682 {
683 wxASSERT( nStart <= length() );
684 }
685
dcb68102
RN
686 size_t len = wxStrlen(sz);
687
e2101186 688 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
e87b7833 689 {
2c09fb3b 690 if ( !wxTmemchr(sz, *p,len) )
dcb68102 691 return p - c_str();
e87b7833
MB
692 }
693
694 return npos;
695}
696
e2101186
MB
697size_t wxStringBase::find_last_not_of(const wxChar* sz, size_t nStart,
698 size_t n) const
699{
700 return find_last_not_of(wxStringBase(sz, n), nStart);
701}
702
e87b7833
MB
703size_t wxStringBase::find_last_not_of(wxChar ch, size_t nStart) const
704{
705 if ( nStart == npos )
706 {
e2101186 707 nStart = length() - 1;
e87b7833
MB
708 }
709 else
710 {
711 wxASSERT( nStart <= length() );
712 }
713
e2101186 714 for ( const wxChar *p = c_str() + nStart; p >= c_str(); --p )
e87b7833
MB
715 {
716 if ( *p != ch )
717 return p - c_str();
718 }
719
720 return npos;
721}
722
723wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
724 const wxChar *sz)
725{
726 wxASSERT_MSG( nStart <= length(),
727 _T("index out of bounds in wxStringBase::replace") );
728 size_t strLen = length() - nStart;
729 nLen = strLen < nLen ? strLen : nLen;
730
731 wxStringBase strTmp;
732 strTmp.reserve(length()); // micro optimisation to avoid multiple mem allocs
733
734 if ( nStart != 0 )
735 strTmp.append(c_str(), nStart);
736 strTmp.append(sz);
737 strTmp.append(c_str() + nStart + nLen);
738
739 swap(strTmp);
740 return *this;
741}
742
743wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
744 size_t nCount, wxChar ch)
745{
7663d0d4 746 return replace(nStart, nLen, wxStringBase(nCount, ch).c_str());
e87b7833
MB
747}
748
749wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
750 const wxStringBase& str,
751 size_t nStart2, size_t nLen2)
752{
753 return replace(nStart, nLen, str.substr(nStart2, nLen2));
754}
755
756wxStringBase& wxStringBase::replace(size_t nStart, size_t nLen,
757 const wxChar* sz, size_t nCount)
758{
759 return replace(nStart, nLen, wxStringBase(sz, nCount).c_str());
760}
761
762wxStringBase wxStringBase::substr(size_t nStart, size_t nLen) const
763{
764 if ( nLen == npos )
765 nLen = length() - nStart;
766 return wxStringBase(*this, nStart, nLen);
767}
768
769// assigns one string to another
770wxStringBase& wxStringBase::operator=(const wxStringBase& stringSrc)
771{
772 wxASSERT( stringSrc.GetStringData()->IsValid() );
773
774 // don't copy string over itself
775 if ( m_pchData != stringSrc.m_pchData ) {
776 if ( stringSrc.GetStringData()->IsEmpty() ) {
777 Reinit();
778 }
779 else {
780 // adjust references
781 GetStringData()->Unlock();
782 m_pchData = stringSrc.m_pchData;
783 GetStringData()->Lock();
784 }
785 }
786
787 return *this;
788}
789
790// assigns a single character
791wxStringBase& wxStringBase::operator=(wxChar ch)
792{
793 if ( !AssignCopy(1, &ch) ) {
794 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(wxChar)") );
795 }
796 return *this;
797}
798
799// assigns C string
800wxStringBase& wxStringBase::operator=(const wxChar *psz)
801{
802 if ( !AssignCopy(wxStrlen(psz), psz) ) {
803 wxFAIL_MSG( _T("out of memory in wxStringBase::operator=(const wxChar *)") );
804 }
805 return *this;
806}
807
808// helper function: does real copy
809bool wxStringBase::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
810{
811 if ( nSrcLen == 0 ) {
812 Reinit();
813 }
814 else {
815 if ( !AllocBeforeWrite(nSrcLen) ) {
816 // allocation failure handled by caller
d775fa82 817 return false;
e87b7833
MB
818 }
819 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
820 GetStringData()->nDataLength = nSrcLen;
821 m_pchData[nSrcLen] = wxT('\0');
822 }
d775fa82 823 return true;
e87b7833
MB
824}
825
826// ---------------------------------------------------------------------------
827// string concatenation
828// ---------------------------------------------------------------------------
c801d85f 829
c801d85f 830// add something to this string
e87b7833
MB
831bool wxStringBase::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData,
832 size_t nMaxLen)
c801d85f 833{
3168a13f 834 STATISTICS_ADD(SummandLength, nSrcLen);
c801d85f 835
e87b7833
MB
836 nSrcLen = nSrcLen < nMaxLen ? nSrcLen : nMaxLen;
837
05488905
VZ
838 // concatenating an empty string is a NOP
839 if ( nSrcLen > 0 ) {
840 wxStringData *pData = GetStringData();
841 size_t nLen = pData->nDataLength;
842 size_t nNewLen = nLen + nSrcLen;
c801d85f 843
05488905
VZ
844 // alloc new buffer if current is too small
845 if ( pData->IsShared() ) {
846 STATISTICS_ADD(ConcatHit, 0);
3168a13f 847
05488905
VZ
848 // we have to allocate another buffer
849 wxStringData* pOldData = GetStringData();
b1801e0e
GD
850 if ( !AllocBuffer(nNewLen) ) {
851 // allocation failure handled by caller
d775fa82 852 return false;
b1801e0e 853 }
2bb67b80 854 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
05488905
VZ
855 pOldData->Unlock();
856 }
857 else if ( nNewLen > pData->nAllocLength ) {
858 STATISTICS_ADD(ConcatHit, 0);
3168a13f 859
e87b7833 860 reserve(nNewLen);
05488905 861 // we have to grow the buffer
e87b7833 862 if ( capacity() < nNewLen ) {
b1801e0e 863 // allocation failure handled by caller
d775fa82 864 return false;
b1801e0e 865 }
05488905
VZ
866 }
867 else {
868 STATISTICS_ADD(ConcatHit, 1);
3168a13f 869
05488905
VZ
870 // the buffer is already big enough
871 }
3168a13f 872
05488905
VZ
873 // should be enough space
874 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
3168a13f 875
05488905 876 // fast concatenation - all is done in our buffer
2bb67b80 877 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
3168a13f 878
e87b7833
MB
879 m_pchData[nNewLen] = wxT('\0'); // put terminating '\0'
880 GetStringData()->nDataLength = nNewLen; // and fix the length
881 }
882 //else: the string to append was empty
d775fa82 883 return true;
e87b7833
MB
884}
885
886// ---------------------------------------------------------------------------
887// simple sub-string extraction
888// ---------------------------------------------------------------------------
889
890// helper function: clone the data attached to this string
891bool wxStringBase::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
892{
893 if ( nCopyLen == 0 ) {
894 dest.Init();
895 }
896 else {
897 if ( !dest.AllocBuffer(nCopyLen) ) {
898 // allocation failure handled by caller
d775fa82 899 return false;
e87b7833
MB
900 }
901 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
902 }
d775fa82 903 return true;
e87b7833
MB
904}
905
906#endif // !wxUSE_STL
907
908#if !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
909
910#if !wxUSE_STL
911 #define STRINGCLASS wxStringBase
912#else
913 #define STRINGCLASS wxString
914#endif
915
916static inline int wxDoCmp(const wxChar* s1, size_t l1,
917 const wxChar* s2, size_t l2)
918{
919 if( l1 == l2 )
2c09fb3b 920 return wxTmemcmp(s1, s2, l1);
e87b7833
MB
921 else if( l1 < l2 )
922 {
2c09fb3b 923 int ret = wxTmemcmp(s1, s2, l1);
e87b7833
MB
924 return ret == 0 ? -1 : ret;
925 }
2c09fb3b 926 else
e87b7833 927 {
2c09fb3b 928 int ret = wxTmemcmp(s1, s2, l2);
e87b7833
MB
929 return ret == 0 ? +1 : ret;
930 }
e87b7833
MB
931}
932
e87b7833
MB
933int STRINGCLASS::compare(const wxStringBase& str) const
934{
935 return ::wxDoCmp(data(), length(), str.data(), str.length());
936}
937
e87b7833
MB
938int STRINGCLASS::compare(size_t nStart, size_t nLen,
939 const wxStringBase& str) const
940{
941 wxASSERT(nStart <= length());
942 size_type strLen = length() - nStart;
943 nLen = strLen < nLen ? strLen : nLen;
944 return ::wxDoCmp(data() + nStart, nLen, str.data(), str.length());
945}
946
947int STRINGCLASS::compare(size_t nStart, size_t nLen,
948 const wxStringBase& str,
949 size_t nStart2, size_t nLen2) const
950{
951 wxASSERT(nStart <= length());
952 wxASSERT(nStart2 <= str.length());
953 size_type strLen = length() - nStart,
954 strLen2 = str.length() - nStart2;
955 nLen = strLen < nLen ? strLen : nLen;
956 nLen2 = strLen2 < nLen2 ? strLen2 : nLen2;
957 return ::wxDoCmp(data() + nStart, nLen, str.data() + nStart2, nLen2);
958}
959
e87b7833
MB
960int STRINGCLASS::compare(const wxChar* sz) const
961{
962 size_t nLen = wxStrlen(sz);
963 return ::wxDoCmp(data(), length(), sz, nLen);
964}
965
e87b7833
MB
966int STRINGCLASS::compare(size_t nStart, size_t nLen,
967 const wxChar* sz, size_t nCount) const
968{
969 wxASSERT(nStart <= length());
970 size_type strLen = length() - nStart;
971 nLen = strLen < nLen ? strLen : nLen;
972 if( nCount == npos )
973 nCount = wxStrlen(sz);
974
975 return ::wxDoCmp(data() + nStart, nLen, sz, nCount);
976}
977
978#undef STRINGCLASS
979
980#endif // !wxUSE_STL || !defined(HAVE_STD_STRING_COMPARE)
981
982// ===========================================================================
983// wxString class core
984// ===========================================================================
985
06386448
RN
986// ---------------------------------------------------------------------------
987// construction and conversion
e87b7833
MB
988// ---------------------------------------------------------------------------
989
990#if wxUSE_UNICODE
991
992// from multibyte string
993wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
994{
31444b6a
VS
995 // if nLength != npos, then we have to make a NULL-terminated copy
996 // of first nLength bytes of psz first because the input buffer to MB2WC
997 // must always be NULL-terminated:
998 wxCharBuffer inBuf((const char *)NULL);
999 if (nLength != npos)
1000 {
b65ee6fd 1001 wxASSERT( psz != NULL );
31444b6a
VS
1002 wxCharBuffer tmp(nLength);
1003 memcpy(tmp.data(), psz, nLength);
1004 tmp.data()[nLength] = '\0';
1005 inBuf = tmp;
1006 psz = inBuf.data();
1007 }
d775fa82 1008
e87b7833
MB
1009 // first get the size of the buffer we need
1010 size_t nLen;
1011 if ( psz )
1012 {
1013 // calculate the needed size ourselves or use the provided one
06386448
RN
1014 if (nLength == npos)
1015 nLen = strlen(psz);
1016 else
1017 nLen = nLength;
e87b7833
MB
1018 }
1019 else
1020 {
1021 // nothing to convert
1022 nLen = 0;
1023 }
1024
e4e3bbb4 1025
e87b7833
MB
1026 // anything to do?
1027 if ( (nLen != 0) && (nLen != (size_t)-1) )
1028 {
f5fb6871
RN
1029 //Convert string
1030 size_t nRealSize;
1031 wxWCharBuffer theBuffer = conv.cMB2WC(psz, nLen, &nRealSize);
e4e3bbb4 1032
eecb33b0 1033 //Copy
f5fb6871
RN
1034 if (nRealSize)
1035 assign( theBuffer.data() , nRealSize - 1 );
e87b7833 1036 }
7663d0d4 1037}
265d5cce 1038
06386448 1039//Convert wxString in Unicode mode to a multi-byte string
265d5cce
RN
1040const wxCharBuffer wxString::mb_str(wxMBConv& conv) const
1041{
f5fb6871
RN
1042 size_t dwOutSize;
1043 return conv.cWC2MB(c_str(), length(), &dwOutSize);
e87b7833
MB
1044}
1045
1046#else // ANSI
1047
1048#if wxUSE_WCHAR_T
1049// from wide string
1050wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength)
1051{
31444b6a
VS
1052 // if nLength != npos, then we have to make a NULL-terminated copy
1053 // of first nLength chars of psz first because the input buffer to WC2MB
1054 // must always be NULL-terminated:
1055 wxWCharBuffer inBuf((const wchar_t *)NULL);
1056 if (nLength != npos)
1057 {
b65ee6fd 1058 wxASSERT( pwz != NULL );
31444b6a
VS
1059 wxWCharBuffer tmp(nLength);
1060 memcpy(tmp.data(), pwz, nLength * sizeof(wchar_t));
1061 tmp.data()[nLength] = '\0';
1062 inBuf = tmp;
1063 pwz = inBuf.data();
1064 }
d775fa82 1065
e87b7833
MB
1066 // first get the size of the buffer we need
1067 size_t nLen;
1068 if ( pwz )
1069 {
1070 // calculate the needed size ourselves or use the provided one
06386448
RN
1071 if (nLength == npos)
1072 nLen = wxWcslen(pwz);
1073 else
1074 nLen = nLength;
e87b7833
MB
1075 }
1076 else
1077 {
1078 // nothing to convert
1079 nLen = 0;
1080 }
1081
1082 // anything to do?
1083 if ( (nLen != 0) && (nLen != (size_t)-1) )
1084 {
f5fb6871
RN
1085 //Convert string
1086 size_t nRealSize;
1087 wxCharBuffer theBuffer = conv.cWC2MB(pwz, nLen, &nRealSize);
e4e3bbb4 1088
eecb33b0 1089 //Copy
f5fb6871
RN
1090 if (nRealSize)
1091 assign( theBuffer.data() , nRealSize - 1 );
e87b7833 1092 }
e87b7833 1093}
265d5cce 1094
7663d0d4 1095//Converts this string to a wide character string if unicode
06386448 1096//mode is not enabled and wxUSE_WCHAR_T is enabled
265d5cce
RN
1097const wxWCharBuffer wxString::wc_str(wxMBConv& conv) const
1098{
f5fb6871
RN
1099 size_t dwOutSize;
1100 return conv.cMB2WC(c_str(), length(), &dwOutSize);
265d5cce 1101}
7663d0d4 1102
e87b7833
MB
1103#endif // wxUSE_WCHAR_T
1104
1105#endif // Unicode/ANSI
1106
1107// shrink to minimal size (releasing extra memory)
1108bool wxString::Shrink()
1109{
1110 wxString tmp(begin(), end());
1111 swap(tmp);
1112 return tmp.length() == length();
1113}
1114
1115#if !wxUSE_STL
1116// get the pointer to writable buffer of (at least) nLen bytes
1117wxChar *wxString::GetWriteBuf(size_t nLen)
1118{
1119 if ( !AllocBeforeWrite(nLen) ) {
1120 // allocation failure handled by caller
1121 return NULL;
1122 }
1123
1124 wxASSERT( GetStringData()->nRefs == 1 );
d775fa82 1125 GetStringData()->Validate(false);
e87b7833
MB
1126
1127 return m_pchData;
1128}
1129
1130// put string back in a reasonable state after GetWriteBuf
1131void wxString::UngetWriteBuf()
1132{
1133 GetStringData()->nDataLength = wxStrlen(m_pchData);
d775fa82 1134 GetStringData()->Validate(true);
e87b7833
MB
1135}
1136
1137void wxString::UngetWriteBuf(size_t nLen)
1138{
1139 GetStringData()->nDataLength = nLen;
d775fa82 1140 GetStringData()->Validate(true);
e87b7833
MB
1141}
1142#endif
1143
1144// ---------------------------------------------------------------------------
1145// data access
1146// ---------------------------------------------------------------------------
1147
1148// all functions are inline in string.h
1149
1150// ---------------------------------------------------------------------------
1151// assignment operators
1152// ---------------------------------------------------------------------------
1153
1154#if !wxUSE_UNICODE
1155
1156// same as 'signed char' variant
1157wxString& wxString::operator=(const unsigned char* psz)
1158{
1159 *this = (const char *)psz;
1160 return *this;
1161}
1162
1163#if wxUSE_WCHAR_T
1164wxString& wxString::operator=(const wchar_t *pwz)
1165{
1166 wxString str(pwz);
1167 swap(str);
1168 return *this;
c801d85f 1169}
e87b7833
MB
1170#endif
1171
1172#endif
c801d85f
KB
1173
1174/*
c801d85f
KB
1175 * concatenation functions come in 5 flavours:
1176 * string + string
1177 * char + string and string + char
1178 * C str + string and string + C str
1179 */
1180
b1801e0e 1181wxString operator+(const wxString& str1, const wxString& str2)
c801d85f 1182{
e87b7833 1183#if !wxUSE_STL
b1801e0e
GD
1184 wxASSERT( str1.GetStringData()->IsValid() );
1185 wxASSERT( str2.GetStringData()->IsValid() );
e87b7833 1186#endif
097c080b 1187
b1801e0e
GD
1188 wxString s = str1;
1189 s += str2;
3168a13f 1190
c801d85f
KB
1191 return s;
1192}
1193
b1801e0e 1194wxString operator+(const wxString& str, wxChar ch)
c801d85f 1195{
e87b7833 1196#if !wxUSE_STL
b1801e0e 1197 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1198#endif
3168a13f 1199
b1801e0e 1200 wxString s = str;
3168a13f 1201 s += ch;
097c080b 1202
c801d85f
KB
1203 return s;
1204}
1205
b1801e0e 1206wxString operator+(wxChar ch, const wxString& str)
c801d85f 1207{
e87b7833 1208#if !wxUSE_STL
b1801e0e 1209 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1210#endif
097c080b 1211
3168a13f 1212 wxString s = ch;
b1801e0e 1213 s += str;
3168a13f 1214
c801d85f
KB
1215 return s;
1216}
1217
b1801e0e 1218wxString operator+(const wxString& str, const wxChar *psz)
c801d85f 1219{
e87b7833 1220#if !wxUSE_STL
b1801e0e 1221 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1222#endif
097c080b 1223
c801d85f 1224 wxString s;
b1801e0e
GD
1225 if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) {
1226 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1227 }
1228 s = str;
3168a13f
VZ
1229 s += psz;
1230
c801d85f
KB
1231 return s;
1232}
1233
b1801e0e 1234wxString operator+(const wxChar *psz, const wxString& str)
c801d85f 1235{
e87b7833 1236#if !wxUSE_STL
b1801e0e 1237 wxASSERT( str.GetStringData()->IsValid() );
e87b7833 1238#endif
097c080b 1239
c801d85f 1240 wxString s;
b1801e0e
GD
1241 if ( !s.Alloc(wxStrlen(psz) + str.Len()) ) {
1242 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
1243 }
3168a13f 1244 s = psz;
b1801e0e 1245 s += str;
3168a13f 1246
c801d85f
KB
1247 return s;
1248}
1249
1250// ===========================================================================
1251// other common string functions
1252// ===========================================================================
1253
dcb68102
RN
1254int wxString::Cmp(const wxString& s) const
1255{
1256 return compare(s);
1257}
1258
1259int wxString::Cmp(const wxChar* psz) const
1260{
1261 return compare(psz);
1262}
1263
1264static inline int wxDoCmpNoCase(const wxChar* s1, size_t l1,
1265 const wxChar* s2, size_t l2)
1266{
1267 size_t i;
1268
1269 if( l1 == l2 )
1270 {
1271 for(i = 0; i < l1; ++i)
1272 {
1273 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1274 break;
1275 }
59059feb 1276 return i == l1 ? 0 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
dcb68102
RN
1277 }
1278 else if( l1 < l2 )
1279 {
1280 for(i = 0; i < l1; ++i)
1281 {
1282 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1283 break;
1284 }
59059feb 1285 return i == l1 ? -1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
dcb68102 1286 }
2c09fb3b 1287 else
dcb68102
RN
1288 {
1289 for(i = 0; i < l2; ++i)
1290 {
1291 if(wxTolower(s1[i]) != wxTolower(s2[i]))
1292 break;
1293 }
35b4f9ca 1294 return i == l2 ? 1 : wxTolower(s1[i]) < wxTolower(s2[i]) ? -1 : 1;
dcb68102 1295 }
dcb68102
RN
1296}
1297
1298int wxString::CmpNoCase(const wxString& s) const
1299{
1300 return wxDoCmpNoCase(data(), length(), s.data(), s.length());
1301}
1302
1303int wxString::CmpNoCase(const wxChar* psz) const
1304{
1305 int nLen = wxStrlen(psz);
1306
1307 return wxDoCmpNoCase(data(), length(), psz, nLen);
1308}
1309
1310
b1ac3b56 1311#if wxUSE_UNICODE
e015c2a3 1312
cf6bedce
SC
1313#ifdef __MWERKS__
1314#ifndef __SCHAR_MAX__
1315#define __SCHAR_MAX__ 127
1316#endif
1317#endif
1318
e015c2a3 1319wxString wxString::FromAscii(const char *ascii)
b1ac3b56
RR
1320{
1321 if (!ascii)
1322 return wxEmptyString;
e015c2a3 1323
b1ac3b56
RR
1324 size_t len = strlen( ascii );
1325 wxString res;
e015c2a3
VZ
1326
1327 if ( len )
1328 {
1329 wxStringBuffer buf(res, len);
1330
1331 wchar_t *dest = buf;
1332
1333 for ( ;; )
1334 {
1335 if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' )
1336 break;
1337 }
1338 }
1339
b1ac3b56
RR
1340 return res;
1341}
1342
2b5f62a0
VZ
1343wxString wxString::FromAscii(const char ascii)
1344{
1345 // What do we do with '\0' ?
1346
1347 wxString res;
1348 res += (wchar_t)(unsigned char) ascii;
8760bc65 1349
2b5f62a0
VZ
1350 return res;
1351}
1352
b1ac3b56
RR
1353const wxCharBuffer wxString::ToAscii() const
1354{
e015c2a3
VZ
1355 // this will allocate enough space for the terminating NUL too
1356 wxCharBuffer buffer(length());
b1ac3b56 1357
be7eecf8 1358
6e394fc6 1359 char *dest = buffer.data();
e015c2a3
VZ
1360
1361 const wchar_t *pwc = c_str();
1362 for ( ;; )
b1ac3b56 1363 {
6e394fc6 1364 *dest++ = (char)(*pwc > SCHAR_MAX ? wxT('_') : *pwc);
e015c2a3
VZ
1365
1366 // the output string can't have embedded NULs anyhow, so we can safely
1367 // stop at first of them even if we do have any
1368 if ( !*pwc++ )
1369 break;
b1ac3b56 1370 }
e015c2a3 1371
b1ac3b56
RR
1372 return buffer;
1373}
e015c2a3
VZ
1374
1375#endif // Unicode
b1ac3b56 1376
c801d85f 1377// extract string of length nCount starting at nFirst
c801d85f
KB
1378wxString wxString::Mid(size_t nFirst, size_t nCount) const
1379{
e87b7833 1380 size_t nLen = length();
30d9011f 1381
e87b7833
MB
1382 // default value of nCount is npos and means "till the end"
1383 if ( nCount == npos )
30d9011f
VZ
1384 {
1385 nCount = nLen - nFirst;
1386 }
1387
c801d85f 1388 // out-of-bounds requests return sensible things
30d9011f
VZ
1389 if ( nFirst + nCount > nLen )
1390 {
1391 nCount = nLen - nFirst;
1392 }
c801d85f 1393
30d9011f
VZ
1394 if ( nFirst > nLen )
1395 {
1396 // AllocCopy() will return empty string
c801d85f 1397 nCount = 0;
30d9011f 1398 }
c801d85f 1399
e87b7833
MB
1400 wxString dest(*this, nFirst, nCount);
1401 if ( dest.length() != nCount ) {
b1801e0e
GD
1402 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
1403 }
30d9011f 1404
c801d85f
KB
1405 return dest;
1406}
1407
e87b7833 1408// check that the string starts with prefix and return the rest of the string
d775fa82 1409// in the provided pointer if it is not NULL, otherwise return false
f6bcfd97
BP
1410bool wxString::StartsWith(const wxChar *prefix, wxString *rest) const
1411{
1412 wxASSERT_MSG( prefix, _T("invalid parameter in wxString::StartsWith") );
1413
1414 // first check if the beginning of the string matches the prefix: note
1415 // that we don't have to check that we don't run out of this string as
1416 // when we reach the terminating NUL, either prefix string ends too (and
1417 // then it's ok) or we break out of the loop because there is no match
1418 const wxChar *p = c_str();
1419 while ( *prefix )
1420 {
1421 if ( *prefix++ != *p++ )
1422 {
1423 // no match
d775fa82 1424 return false;
f6bcfd97
BP
1425 }
1426 }
1427
1428 if ( rest )
1429 {
1430 // put the rest of the string into provided pointer
1431 *rest = p;
1432 }
1433
d775fa82 1434 return true;
f6bcfd97
BP
1435}
1436
c801d85f
KB
1437// extract nCount last (rightmost) characters
1438wxString wxString::Right(size_t nCount) const
1439{
e87b7833
MB
1440 if ( nCount > length() )
1441 nCount = length();
c801d85f 1442
e87b7833
MB
1443 wxString dest(*this, length() - nCount, nCount);
1444 if ( dest.length() != nCount ) {
b1801e0e
GD
1445 wxFAIL_MSG( _T("out of memory in wxString::Right") );
1446 }
c801d85f
KB
1447 return dest;
1448}
1449
1450// get all characters after the last occurence of ch
1451// (returns the whole string if ch not found)
2bb67b80 1452wxString wxString::AfterLast(wxChar ch) const
c801d85f
KB
1453{
1454 wxString str;
d775fa82 1455 int iPos = Find(ch, true);
3c67202d 1456 if ( iPos == wxNOT_FOUND )
c801d85f
KB
1457 str = *this;
1458 else
c8cfb486 1459 str = c_str() + iPos + 1;
c801d85f
KB
1460
1461 return str;
1462}
1463
1464// extract nCount first (leftmost) characters
1465wxString wxString::Left(size_t nCount) const
1466{
e87b7833
MB
1467 if ( nCount > length() )
1468 nCount = length();
c801d85f 1469
e87b7833
MB
1470 wxString dest(*this, 0, nCount);
1471 if ( dest.length() != nCount ) {
b1801e0e
GD
1472 wxFAIL_MSG( _T("out of memory in wxString::Left") );
1473 }
c801d85f
KB
1474 return dest;
1475}
1476
1477// get all characters before the first occurence of ch
1478// (returns the whole string if ch not found)
2bb67b80 1479wxString wxString::BeforeFirst(wxChar ch) const
c801d85f 1480{
e87b7833
MB
1481 int iPos = Find(ch);
1482 if ( iPos == wxNOT_FOUND ) iPos = length();
1483 return wxString(*this, 0, iPos);
c801d85f
KB
1484}
1485
1486/// get all characters before the last occurence of ch
1487/// (returns empty string if ch not found)
2bb67b80 1488wxString wxString::BeforeLast(wxChar ch) const
c801d85f
KB
1489{
1490 wxString str;
d775fa82 1491 int iPos = Find(ch, true);
3c67202d 1492 if ( iPos != wxNOT_FOUND && iPos != 0 )
d1c9bbf6 1493 str = wxString(c_str(), iPos);
c801d85f
KB
1494
1495 return str;
1496}
1497
1498/// get all characters after the first occurence of ch
1499/// (returns empty string if ch not found)
2bb67b80 1500wxString wxString::AfterFirst(wxChar ch) const
c801d85f
KB
1501{
1502 wxString str;
1503 int iPos = Find(ch);
3c67202d 1504 if ( iPos != wxNOT_FOUND )
c801d85f
KB
1505 str = c_str() + iPos + 1;
1506
1507 return str;
1508}
1509
1510// replace first (or all) occurences of some substring with another one
a8f1f1b2
VZ
1511size_t
1512wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
c801d85f 1513{
a8f1f1b2
VZ
1514 // if we tried to replace an empty string we'd enter an infinite loop below
1515 wxCHECK_MSG( szOld && *szOld && szNew, 0,
1516 _T("wxString::Replace(): invalid parameter") );
1517
c86f1403 1518 size_t uiCount = 0; // count of replacements made
c801d85f 1519
2bb67b80 1520 size_t uiOldLen = wxStrlen(szOld);
c801d85f
KB
1521
1522 wxString strTemp;
e87b7833 1523 const wxChar *pCurrent = c_str();
2bb67b80 1524 const wxChar *pSubstr;
223d09f6 1525 while ( *pCurrent != wxT('\0') ) {
2bb67b80 1526 pSubstr = wxStrstr(pCurrent, szOld);
c801d85f
KB
1527 if ( pSubstr == NULL ) {
1528 // strTemp is unused if no replacements were made, so avoid the copy
1529 if ( uiCount == 0 )
1530 return 0;
1531
1532 strTemp += pCurrent; // copy the rest
1533 break; // exit the loop
1534 }
1535 else {
1536 // take chars before match
e87b7833
MB
1537 size_type len = strTemp.length();
1538 strTemp.append(pCurrent, pSubstr - pCurrent);
1539 if ( strTemp.length() != (size_t)(len + pSubstr - pCurrent) ) {
b1801e0e
GD
1540 wxFAIL_MSG( _T("out of memory in wxString::Replace") );
1541 return 0;
1542 }
c801d85f
KB
1543 strTemp += szNew;
1544 pCurrent = pSubstr + uiOldLen; // restart after match
1545
1546 uiCount++;
1547
1548 // stop now?
1549 if ( !bReplaceAll ) {
1550 strTemp += pCurrent; // copy the rest
1551 break; // exit the loop
1552 }
1553 }
1554 }
1555
1556 // only done if there were replacements, otherwise would have returned above
e87b7833 1557 swap(strTemp);
c801d85f
KB
1558
1559 return uiCount;
1560}
1561
1562bool wxString::IsAscii() const
1563{
2bb67b80 1564 const wxChar *s = (const wxChar*) *this;
c801d85f 1565 while(*s){
d775fa82 1566 if(!isascii(*s)) return(false);
c801d85f
KB
1567 s++;
1568 }
d775fa82 1569 return(true);
c801d85f 1570}
dd1eaa89 1571
c801d85f
KB
1572bool wxString::IsWord() const
1573{
2bb67b80 1574 const wxChar *s = (const wxChar*) *this;
c801d85f 1575 while(*s){
d775fa82 1576 if(!wxIsalpha(*s)) return(false);
c801d85f
KB
1577 s++;
1578 }
d775fa82 1579 return(true);
c801d85f 1580}
dd1eaa89 1581
c801d85f
KB
1582bool wxString::IsNumber() const
1583{
2bb67b80 1584 const wxChar *s = (const wxChar*) *this;
2f74ed28 1585 if (wxStrlen(s))
930357bd 1586 if ((s[0] == wxT('-')) || (s[0] == wxT('+'))) s++;
c801d85f 1587 while(*s){
d775fa82 1588 if(!wxIsdigit(*s)) return(false);
c801d85f
KB
1589 s++;
1590 }
d775fa82 1591 return(true);
c801d85f
KB
1592}
1593
c801d85f
KB
1594wxString wxString::Strip(stripType w) const
1595{
1596 wxString s = *this;
d775fa82
WS
1597 if ( w & leading ) s.Trim(false);
1598 if ( w & trailing ) s.Trim(true);
c801d85f
KB
1599 return s;
1600}
1601
c801d85f
KB
1602// ---------------------------------------------------------------------------
1603// case conversion
1604// ---------------------------------------------------------------------------
1605
1606wxString& wxString::MakeUpper()
1607{
e87b7833
MB
1608 for ( iterator it = begin(), en = end(); it != en; ++it )
1609 *it = (wxChar)wxToupper(*it);
c801d85f
KB
1610
1611 return *this;
1612}
1613
1614wxString& wxString::MakeLower()
1615{
e87b7833
MB
1616 for ( iterator it = begin(), en = end(); it != en; ++it )
1617 *it = (wxChar)wxTolower(*it);
c801d85f
KB
1618
1619 return *this;
1620}
1621
1622// ---------------------------------------------------------------------------
1623// trimming and padding
1624// ---------------------------------------------------------------------------
1625
d775fa82 1626// some compilers (VC++ 6.0 not to name them) return true for a call to
576c608d
VZ
1627