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