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