]> git.saurik.com Git - wxWidgets.git/blame - src/common/string.cpp
Added configure patch for DEC
[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>
dd1eaa89 9// Licence: wxWindows license
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"
6b769f3d
OK
38#if wxUSE_THREADS
39 #include <wx/thread.h>
40#endif
c801d85f
KB
41#endif
42
43#include <ctype.h>
44#include <string.h>
45#include <stdlib.h>
46
ce3ed50d 47#ifdef __SALFORDC__
30b21f9a 48 #include <clib.h>
ce3ed50d
JS
49#endif
50
ede25f5b 51#if wxUSE_WCSRTOMBS
fb4e5803
VZ
52 #include <wchar.h> // for wcsrtombs(), see comments where it's used
53#endif // GNU
54
c801d85f
KB
55#ifdef WXSTRING_IS_WXOBJECT
56 IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject)
57#endif //WXSTRING_IS_WXOBJECT
58
3168a13f
VZ
59// allocating extra space for each string consumes more memory but speeds up
60// the concatenation operations (nLen is the current string's length)
77ca46e7
VZ
61// NB: EXTRA_ALLOC must be >= 0!
62#define EXTRA_ALLOC (19 - nLen % 16)
3168a13f 63
c801d85f
KB
64// ---------------------------------------------------------------------------
65// static class variables definition
66// ---------------------------------------------------------------------------
67
8de2e39c 68#ifdef wxSTD_STRING_COMPATIBILITY
566b84d2 69 const size_t wxString::npos = wxSTRING_MAXLEN;
8de2e39c 70#endif // wxSTD_STRING_COMPATIBILITY
c801d85f 71
3168a13f
VZ
72// ----------------------------------------------------------------------------
73// static data
74// ----------------------------------------------------------------------------
c801d85f 75
3c024cc2
VZ
76// for an empty string, GetStringData() will return this address: this
77// structure has the same layout as wxStringData and it's data() method will
78// return the empty string (dummy pointer)
79static const struct
80{
81 wxStringData data;
2bb67b80
OK
82 wxChar dummy;
83} g_strEmpty = { {-1, 0, 0}, _T('\0') };
3c024cc2 84
c801d85f 85// empty C style string: points to 'string data' byte of g_strEmpty
2bb67b80 86extern const wxChar WXDLLEXPORT *g_szNul = &g_strEmpty.dummy;
c801d85f 87
89b892a2
VZ
88// ----------------------------------------------------------------------------
89// conditional compilation
90// ----------------------------------------------------------------------------
91
92// we want to find out if the current platform supports vsnprintf()-like
93// function: for Unix this is done with configure, for Windows we test the
94// compiler explicitly.
95#ifdef __WXMSW__
3f4a0c5b 96 #ifdef __VISUALC__
2bb67b80 97 #define wxVsnprintf _vsnprintf
89b892a2
VZ
98 #endif
99#else // !Windows
100 #ifdef HAVE_VSNPRINTF
2bb67b80 101 #define wxVsnprintf vsnprintf
89b892a2
VZ
102 #endif
103#endif // Windows/!Windows
104
2bb67b80 105#ifndef wxVsnprintf
89b892a2
VZ
106 // in this case we'll use vsprintf() (which is ANSI and thus should be
107 // always available), but it's unsafe because it doesn't check for buffer
108 // size - so give a warning
2bb67b80 109 #define wxVsnprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
566b84d2 110
57493f9f
VZ
111 #if defined(__VISUALC__)
112 #pragma message("Using sprintf() because no snprintf()-like function defined")
113 #elif defined(__GNUG__) && !defined(__UNIX__)
114 #warning "Using sprintf() because no snprintf()-like function defined"
115 #elif defined(__MWERKS__)
116 #warning "Using sprintf() because no snprintf()-like function defined"
117 #endif //compiler
3f4a0c5b 118#endif // no vsnprintf
89b892a2 119
227b5cd7
VZ
120#ifdef _AIX
121 // AIX has vsnprintf, but there's no prototype in the system headers.
122 extern "C" int vsnprintf(char* str, size_t n, const char* format, va_list ap);
123#endif
124
3168a13f 125// ----------------------------------------------------------------------------
c801d85f 126// global functions
3168a13f 127// ----------------------------------------------------------------------------
c801d85f 128
8de2e39c 129#ifdef wxSTD_STRING_COMPATIBILITY
c801d85f
KB
130
131// MS Visual C++ version 5.0 provides the new STL headers as well as the old
132// iostream ones.
133//
134// ATTN: you can _not_ use both of these in the same program!
a38b83c3 135
3f4a0c5b 136istream& operator>>(istream& is, wxString& WXUNUSED(str))
c801d85f
KB
137{
138#if 0
139 int w = is.width(0);
140 if ( is.ipfx(0) ) {
3f4a0c5b 141 streambuf *sb = is.rdbuf();
c801d85f
KB
142 str.erase();
143 while ( true ) {
144 int ch = sb->sbumpc ();
145 if ( ch == EOF ) {
3f4a0c5b 146 is.setstate(ios::eofbit);
c801d85f
KB
147 break;
148 }
149 else if ( isspace(ch) ) {
150 sb->sungetc();
151 break;
152 }
dd1eaa89 153
c801d85f
KB
154 str += ch;
155 if ( --w == 1 )
156 break;
157 }
158 }
159
160 is.isfx();
161 if ( str.length() == 0 )
3f4a0c5b 162 is.setstate(ios::failbit);
c801d85f
KB
163#endif
164 return is;
165}
166
167#endif //std::string compatibility
168
3168a13f
VZ
169// ----------------------------------------------------------------------------
170// private classes
171// ----------------------------------------------------------------------------
172
173// this small class is used to gather statistics for performance tuning
174//#define WXSTRING_STATISTICS
175#ifdef WXSTRING_STATISTICS
176 class Averager
177 {
178 public:
179 Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
2c3b684c 180 ~Averager()
3168a13f
VZ
181 { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
182
c86f1403 183 void Add(size_t n) { m_nTotal += n; m_nCount++; }
3168a13f
VZ
184
185 private:
c86f1403 186 size_t m_nCount, m_nTotal;
3168a13f
VZ
187 const char *m_sz;
188 } g_averageLength("allocation size"),
189 g_averageSummandLength("summand length"),
190 g_averageConcatHit("hit probability in concat"),
191 g_averageInitialLength("initial string length");
192
193 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
194#else
195 #define STATISTICS_ADD(av, val)
196#endif // WXSTRING_STATISTICS
197
c801d85f
KB
198// ===========================================================================
199// wxString class core
200// ===========================================================================
201
202// ---------------------------------------------------------------------------
203// construction
204// ---------------------------------------------------------------------------
205
c801d85f 206// constructs string of <nLength> copies of character <ch>
2bb67b80 207wxString::wxString(wxChar ch, size_t nLength)
c801d85f
KB
208{
209 Init();
210
211 if ( nLength > 0 ) {
212 AllocBuffer(nLength);
f1da2f03 213
2bb67b80
OK
214#if wxUSE_UNICODE
215 // memset only works on char
216 for (size_t n=0; n<nLength; n++) m_pchData[n] = ch;
217#else
c801d85f 218 memset(m_pchData, ch, nLength);
2bb67b80 219#endif
c801d85f
KB
220 }
221}
222
223// takes nLength elements of psz starting at nPos
2bb67b80 224void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
c801d85f
KB
225{
226 Init();
227
2bb67b80 228 wxASSERT( nPos <= wxStrlen(psz) );
c801d85f 229
566b84d2 230 if ( nLength == wxSTRING_MAXLEN )
2bb67b80 231 nLength = wxStrlen(psz + nPos);
c801d85f 232
3168a13f
VZ
233 STATISTICS_ADD(InitialLength, nLength);
234
c801d85f
KB
235 if ( nLength > 0 ) {
236 // trailing '\0' is written in AllocBuffer()
237 AllocBuffer(nLength);
2bb67b80 238 memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar));
c801d85f
KB
239 }
240}
dd1eaa89 241
8de2e39c 242#ifdef wxSTD_STRING_COMPATIBILITY
c801d85f 243
c801d85f
KB
244// poor man's iterators are "void *" pointers
245wxString::wxString(const void *pStart, const void *pEnd)
246{
2bb67b80
OK
247 InitWith((const wxChar *)pStart, 0,
248 (const wxChar *)pEnd - (const wxChar *)pStart);
c801d85f
KB
249}
250
251#endif //std::string compatibility
252
2bb67b80
OK
253#if wxUSE_UNICODE
254
255// from multibyte string
cf2f341a 256wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
2bb67b80
OK
257{
258 // first get necessary size
435595e0 259 size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
2bb67b80
OK
260
261 // nLength is number of *Unicode* characters here!
eea4f86a 262 if ((nLen != (size_t)-1) && (nLen > nLength))
2bb67b80
OK
263 nLen = nLength;
264
265 // empty?
eea4f86a 266 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
2bb67b80
OK
267 AllocBuffer(nLen);
268 conv.MB2WC(m_pchData, psz, nLen);
269 }
270 else {
271 Init();
272 }
273}
274
275#else
276
c801d85f
KB
277// from wide string
278wxString::wxString(const wchar_t *pwz)
279{
280 // first get necessary size
435595e0 281 size_t nLen = pwz ? wxWC2MB((char *) NULL, pwz, 0) : 0;
c801d85f
KB
282
283 // empty?
eea4f86a 284 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
c801d85f 285 AllocBuffer(nLen);
2bb67b80 286 wxWC2MB(m_pchData, pwz, nLen);
c801d85f
KB
287 }
288 else {
289 Init();
290 }
291}
292
2bb67b80
OK
293#endif
294
c801d85f
KB
295// ---------------------------------------------------------------------------
296// memory allocation
297// ---------------------------------------------------------------------------
298
299// allocates memory needed to store a C string of length nLen
300void wxString::AllocBuffer(size_t nLen)
301{
302 wxASSERT( nLen > 0 ); //
303 wxASSERT( nLen <= INT_MAX-1 ); // max size (enough room for 1 extra)
304
3168a13f
VZ
305 STATISTICS_ADD(Length, nLen);
306
c801d85f
KB
307 // allocate memory:
308 // 1) one extra character for '\0' termination
309 // 2) sizeof(wxStringData) for housekeeping info
3168a13f 310 wxStringData* pData = (wxStringData*)
2bb67b80 311 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
c801d85f 312 pData->nRefs = 1;
c801d85f 313 pData->nDataLength = nLen;
3168a13f 314 pData->nAllocLength = nLen + EXTRA_ALLOC;
c801d85f 315 m_pchData = pData->data(); // data starts after wxStringData
2bb67b80 316 m_pchData[nLen] = _T('\0');
c801d85f
KB
317}
318
c801d85f
KB
319// must be called before changing this string
320void wxString::CopyBeforeWrite()
321{
322 wxStringData* pData = GetStringData();
323
324 if ( pData->IsShared() ) {
325 pData->Unlock(); // memory not freed because shared
c86f1403 326 size_t nLen = pData->nDataLength;
3168a13f 327 AllocBuffer(nLen);
2bb67b80 328 memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar));
c801d85f
KB
329 }
330
3bbb630a 331 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
c801d85f
KB
332}
333
334// must be called before replacing contents of this string
335void wxString::AllocBeforeWrite(size_t nLen)
336{
337 wxASSERT( nLen != 0 ); // doesn't make any sense
338
339 // must not share string and must have enough space
3168a13f 340 wxStringData* pData = GetStringData();
c801d85f
KB
341 if ( pData->IsShared() || (nLen > pData->nAllocLength) ) {
342 // can't work with old buffer, get new one
343 pData->Unlock();
344 AllocBuffer(nLen);
345 }
471aebdd
VZ
346 else {
347 // update the string length
348 pData->nDataLength = nLen;
349 }
c801d85f 350
f1da2f03 351 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
c801d85f
KB
352}
353
dd1eaa89 354// allocate enough memory for nLen characters
c86f1403 355void wxString::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*)
2bb67b80 363 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
9fbd8b8d
VZ
364 pData->nRefs = 1;
365 pData->nDataLength = 0;
366 pData->nAllocLength = nLen;
367 m_pchData = pData->data(); // data starts after wxStringData
2bb67b80 368 m_pchData[0u] = _T('\0');
9fbd8b8d 369 }
3168a13f
VZ
370 else if ( pData->IsShared() ) {
371 pData->Unlock(); // memory not freed because shared
c86f1403 372 size_t nOldLen = pData->nDataLength;
3168a13f 373 AllocBuffer(nLen);
2bb67b80 374 memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar));
3168a13f 375 }
dd1eaa89 376 else {
3168a13f
VZ
377 nLen += EXTRA_ALLOC;
378
dd1eaa89 379 wxStringData *p = (wxStringData *)
2bb67b80 380 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
3168a13f
VZ
381
382 if ( p == NULL ) {
383 // @@@ what to do on memory error?
384 return;
dd1eaa89 385 }
3168a13f
VZ
386
387 // it's not important if the pointer changed or not (the check for this
388 // is not faster than assigning to m_pchData in all cases)
389 p->nAllocLength = nLen;
390 m_pchData = p->data();
dd1eaa89
VZ
391 }
392 }
393 //else: we've already got enough
394}
395
396// shrink to minimal size (releasing extra memory)
397void wxString::Shrink()
398{
399 wxStringData *pData = GetStringData();
3bbb630a
VZ
400
401 // this variable is unused in release build, so avoid the compiler warning by
402 // just not declaring it
403#ifdef __WXDEBUG__
404 void *p =
405#endif
2bb67b80 406 realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(wxChar));
3bbb630a 407
3168a13f 408 wxASSERT( p != NULL ); // can't free memory?
dd1eaa89
VZ
409 wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move!
410}
411
c801d85f 412// get the pointer to writable buffer of (at least) nLen bytes
2bb67b80 413wxChar *wxString::GetWriteBuf(size_t nLen)
c801d85f
KB
414{
415 AllocBeforeWrite(nLen);
097c080b
VZ
416
417 wxASSERT( GetStringData()->nRefs == 1 );
418 GetStringData()->Validate(FALSE);
419
c801d85f
KB
420 return m_pchData;
421}
422
097c080b
VZ
423// put string back in a reasonable state after GetWriteBuf
424void wxString::UngetWriteBuf()
425{
2bb67b80 426 GetStringData()->nDataLength = wxStrlen(m_pchData);
097c080b
VZ
427 GetStringData()->Validate(TRUE);
428}
429
c801d85f
KB
430// ---------------------------------------------------------------------------
431// data access
432// ---------------------------------------------------------------------------
433
434// all functions are inline in string.h
435
436// ---------------------------------------------------------------------------
437// assignment operators
438// ---------------------------------------------------------------------------
439
dd1eaa89 440// helper function: does real copy
2bb67b80 441void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
c801d85f
KB
442{
443 if ( nSrcLen == 0 ) {
444 Reinit();
445 }
446 else {
447 AllocBeforeWrite(nSrcLen);
2bb67b80 448 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
c801d85f 449 GetStringData()->nDataLength = nSrcLen;
2bb67b80 450 m_pchData[nSrcLen] = _T('\0');
c801d85f
KB
451 }
452}
453
454// assigns one string to another
455wxString& wxString::operator=(const wxString& stringSrc)
456{
097c080b
VZ
457 wxASSERT( stringSrc.GetStringData()->IsValid() );
458
c801d85f
KB
459 // don't copy string over itself
460 if ( m_pchData != stringSrc.m_pchData ) {
461 if ( stringSrc.GetStringData()->IsEmpty() ) {
462 Reinit();
463 }
464 else {
465 // adjust references
466 GetStringData()->Unlock();
467 m_pchData = stringSrc.m_pchData;
468 GetStringData()->Lock();
469 }
470 }
471
472 return *this;
473}
474
475// assigns a single character
2bb67b80 476wxString& wxString::operator=(wxChar ch)
c801d85f
KB
477{
478 AssignCopy(1, &ch);
479 return *this;
480}
481
482// assigns C string
2bb67b80 483wxString& wxString::operator=(const wxChar *psz)
c801d85f 484{
2bb67b80 485 AssignCopy(wxStrlen(psz), psz);
c801d85f
KB
486 return *this;
487}
488
2bb67b80
OK
489#if !wxUSE_UNICODE
490
c801d85f
KB
491// same as 'signed char' variant
492wxString& wxString::operator=(const unsigned char* psz)
493{
494 *this = (const char *)psz;
495 return *this;
496}
497
498wxString& wxString::operator=(const wchar_t *pwz)
499{
500 wxString str(pwz);
501 *this = str;
502 return *this;
503}
504
2bb67b80
OK
505#endif
506
c801d85f
KB
507// ---------------------------------------------------------------------------
508// string concatenation
509// ---------------------------------------------------------------------------
510
c801d85f 511// add something to this string
2bb67b80 512void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData)
c801d85f 513{
3168a13f 514 STATISTICS_ADD(SummandLength, nSrcLen);
c801d85f 515
05488905
VZ
516 // concatenating an empty string is a NOP
517 if ( nSrcLen > 0 ) {
518 wxStringData *pData = GetStringData();
519 size_t nLen = pData->nDataLength;
520 size_t nNewLen = nLen + nSrcLen;
c801d85f 521
05488905
VZ
522 // alloc new buffer if current is too small
523 if ( pData->IsShared() ) {
524 STATISTICS_ADD(ConcatHit, 0);
3168a13f 525
05488905
VZ
526 // we have to allocate another buffer
527 wxStringData* pOldData = GetStringData();
528 AllocBuffer(nNewLen);
2bb67b80 529 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
05488905
VZ
530 pOldData->Unlock();
531 }
532 else if ( nNewLen > pData->nAllocLength ) {
533 STATISTICS_ADD(ConcatHit, 0);
3168a13f 534
05488905
VZ
535 // we have to grow the buffer
536 Alloc(nNewLen);
537 }
538 else {
539 STATISTICS_ADD(ConcatHit, 1);
3168a13f 540
05488905
VZ
541 // the buffer is already big enough
542 }
3168a13f 543
05488905
VZ
544 // should be enough space
545 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
3168a13f 546
05488905 547 // fast concatenation - all is done in our buffer
2bb67b80 548 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
3168a13f 549
2bb67b80 550 m_pchData[nNewLen] = _T('\0'); // put terminating '\0'
05488905
VZ
551 GetStringData()->nDataLength = nNewLen; // and fix the length
552 }
553 //else: the string to append was empty
c801d85f
KB
554}
555
556/*
c801d85f
KB
557 * concatenation functions come in 5 flavours:
558 * string + string
559 * char + string and string + char
560 * C str + string and string + C str
561 */
562
563wxString operator+(const wxString& string1, const wxString& string2)
564{
097c080b
VZ
565 wxASSERT( string1.GetStringData()->IsValid() );
566 wxASSERT( string2.GetStringData()->IsValid() );
567
3168a13f
VZ
568 wxString s = string1;
569 s += string2;
570
c801d85f
KB
571 return s;
572}
573
2bb67b80 574wxString operator+(const wxString& string, wxChar ch)
c801d85f 575{
3168a13f
VZ
576 wxASSERT( string.GetStringData()->IsValid() );
577
578 wxString s = string;
579 s += ch;
097c080b 580
c801d85f
KB
581 return s;
582}
583
2bb67b80 584wxString operator+(wxChar ch, const wxString& string)
c801d85f 585{
097c080b
VZ
586 wxASSERT( string.GetStringData()->IsValid() );
587
3168a13f
VZ
588 wxString s = ch;
589 s += string;
590
c801d85f
KB
591 return s;
592}
593
2bb67b80 594wxString operator+(const wxString& string, const wxChar *psz)
c801d85f 595{
097c080b
VZ
596 wxASSERT( string.GetStringData()->IsValid() );
597
c801d85f 598 wxString s;
2bb67b80 599 s.Alloc(wxStrlen(psz) + string.Len());
3168a13f
VZ
600 s = string;
601 s += psz;
602
c801d85f
KB
603 return s;
604}
605
2bb67b80 606wxString operator+(const wxChar *psz, const wxString& string)
c801d85f 607{
097c080b
VZ
608 wxASSERT( string.GetStringData()->IsValid() );
609
c801d85f 610 wxString s;
2bb67b80 611 s.Alloc(wxStrlen(psz) + string.Len());
3168a13f
VZ
612 s = psz;
613 s += string;
614
c801d85f
KB
615 return s;
616}
617
618// ===========================================================================
619// other common string functions
620// ===========================================================================
621
622// ---------------------------------------------------------------------------
623// simple sub-string extraction
624// ---------------------------------------------------------------------------
625
626// helper function: clone the data attached to this string
627void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
628{
3168a13f 629 if ( nCopyLen == 0 ) {
c801d85f
KB
630 dest.Init();
631 }
3168a13f 632 else {
c801d85f 633 dest.AllocBuffer(nCopyLen);
2bb67b80 634 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
c801d85f
KB
635 }
636}
637
638// extract string of length nCount starting at nFirst
c801d85f
KB
639wxString wxString::Mid(size_t nFirst, size_t nCount) const
640{
30d9011f
VZ
641 wxStringData *pData = GetStringData();
642 size_t nLen = pData->nDataLength;
643
566b84d2
VZ
644 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
645 if ( nCount == wxSTRING_MAXLEN )
30d9011f
VZ
646 {
647 nCount = nLen - nFirst;
648 }
649
c801d85f 650 // out-of-bounds requests return sensible things
30d9011f
VZ
651 if ( nFirst + nCount > nLen )
652 {
653 nCount = nLen - nFirst;
654 }
c801d85f 655
30d9011f
VZ
656 if ( nFirst > nLen )
657 {
658 // AllocCopy() will return empty string
c801d85f 659 nCount = 0;
30d9011f 660 }
c801d85f
KB
661
662 wxString dest;
663 AllocCopy(dest, nCount, nFirst);
30d9011f 664
c801d85f
KB
665 return dest;
666}
667
668// extract nCount last (rightmost) characters
669wxString wxString::Right(size_t nCount) const
670{
671 if ( nCount > (size_t)GetStringData()->nDataLength )
672 nCount = GetStringData()->nDataLength;
673
674 wxString dest;
675 AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount);
676 return dest;
677}
678
679// get all characters after the last occurence of ch
680// (returns the whole string if ch not found)
2bb67b80 681wxString wxString::AfterLast(wxChar ch) const
c801d85f
KB
682{
683 wxString str;
684 int iPos = Find(ch, TRUE);
3c67202d 685 if ( iPos == wxNOT_FOUND )
c801d85f
KB
686 str = *this;
687 else
c8cfb486 688 str = c_str() + iPos + 1;
c801d85f
KB
689
690 return str;
691}
692
693// extract nCount first (leftmost) characters
694wxString wxString::Left(size_t nCount) const
695{
696 if ( nCount > (size_t)GetStringData()->nDataLength )
697 nCount = GetStringData()->nDataLength;
698
699 wxString dest;
700 AllocCopy(dest, nCount, 0);
701 return dest;
702}
703
704// get all characters before the first occurence of ch
705// (returns the whole string if ch not found)
2bb67b80 706wxString wxString::BeforeFirst(wxChar ch) const
c801d85f
KB
707{
708 wxString str;
2bb67b80 709 for ( const wxChar *pc = m_pchData; *pc != _T('\0') && *pc != ch; pc++ )
c801d85f
KB
710 str += *pc;
711
712 return str;
713}
714
715/// get all characters before the last occurence of ch
716/// (returns empty string if ch not found)
2bb67b80 717wxString wxString::BeforeLast(wxChar ch) const
c801d85f
KB
718{
719 wxString str;
720 int iPos = Find(ch, TRUE);
3c67202d 721 if ( iPos != wxNOT_FOUND && iPos != 0 )
d1c9bbf6 722 str = wxString(c_str(), iPos);
c801d85f
KB
723
724 return str;
725}
726
727/// get all characters after the first occurence of ch
728/// (returns empty string if ch not found)
2bb67b80 729wxString wxString::AfterFirst(wxChar ch) const
c801d85f
KB
730{
731 wxString str;
732 int iPos = Find(ch);
3c67202d 733 if ( iPos != wxNOT_FOUND )
c801d85f
KB
734 str = c_str() + iPos + 1;
735
736 return str;
737}
738
739// replace first (or all) occurences of some substring with another one
2bb67b80 740size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
c801d85f 741{
c86f1403 742 size_t uiCount = 0; // count of replacements made
c801d85f 743
2bb67b80 744 size_t uiOldLen = wxStrlen(szOld);
c801d85f
KB
745
746 wxString strTemp;
2bb67b80
OK
747 const wxChar *pCurrent = m_pchData;
748 const wxChar *pSubstr;
749 while ( *pCurrent != _T('\0') ) {
750 pSubstr = wxStrstr(pCurrent, szOld);
c801d85f
KB
751 if ( pSubstr == NULL ) {
752 // strTemp is unused if no replacements were made, so avoid the copy
753 if ( uiCount == 0 )
754 return 0;
755
756 strTemp += pCurrent; // copy the rest
757 break; // exit the loop
758 }
759 else {
760 // take chars before match
761 strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent);
762 strTemp += szNew;
763 pCurrent = pSubstr + uiOldLen; // restart after match
764
765 uiCount++;
766
767 // stop now?
768 if ( !bReplaceAll ) {
769 strTemp += pCurrent; // copy the rest
770 break; // exit the loop
771 }
772 }
773 }
774
775 // only done if there were replacements, otherwise would have returned above
776 *this = strTemp;
777
778 return uiCount;
779}
780
781bool wxString::IsAscii() const
782{
2bb67b80 783 const wxChar *s = (const wxChar*) *this;
c801d85f
KB
784 while(*s){
785 if(!isascii(*s)) return(FALSE);
786 s++;
787 }
788 return(TRUE);
789}
dd1eaa89 790
c801d85f
KB
791bool wxString::IsWord() const
792{
2bb67b80 793 const wxChar *s = (const wxChar*) *this;
c801d85f 794 while(*s){
2bb67b80 795 if(!wxIsalpha(*s)) return(FALSE);
c801d85f
KB
796 s++;
797 }
798 return(TRUE);
799}
dd1eaa89 800
c801d85f
KB
801bool wxString::IsNumber() const
802{
2bb67b80 803 const wxChar *s = (const wxChar*) *this;
c801d85f 804 while(*s){
2bb67b80 805 if(!wxIsdigit(*s)) return(FALSE);
c801d85f
KB
806 s++;
807 }
808 return(TRUE);
809}
810
c801d85f
KB
811wxString wxString::Strip(stripType w) const
812{
813 wxString s = *this;
814 if ( w & leading ) s.Trim(FALSE);
815 if ( w & trailing ) s.Trim(TRUE);
816 return s;
817}
818
c801d85f
KB
819// ---------------------------------------------------------------------------
820// case conversion
821// ---------------------------------------------------------------------------
822
823wxString& wxString::MakeUpper()
824{
825 CopyBeforeWrite();
826
2bb67b80
OK
827 for ( wxChar *p = m_pchData; *p; p++ )
828 *p = (wxChar)wxToupper(*p);
c801d85f
KB
829
830 return *this;
831}
832
833wxString& wxString::MakeLower()
834{
835 CopyBeforeWrite();
dd1eaa89 836
2bb67b80
OK
837 for ( wxChar *p = m_pchData; *p; p++ )
838 *p = (wxChar)wxTolower(*p);
c801d85f
KB
839
840 return *this;
841}
842
843// ---------------------------------------------------------------------------
844// trimming and padding
845// ---------------------------------------------------------------------------
846
847// trims spaces (in the sense of isspace) from left or right side
848wxString& wxString::Trim(bool bFromRight)
849{
2c3b684c
VZ
850 // first check if we're going to modify the string at all
851 if ( !IsEmpty() &&
852 (
2bb67b80
OK
853 (bFromRight && wxIsspace(GetChar(Len() - 1))) ||
854 (!bFromRight && wxIsspace(GetChar(0u)))
2c3b684c
VZ
855 )
856 )
c801d85f 857 {
2c3b684c
VZ
858 // ok, there is at least one space to trim
859 CopyBeforeWrite();
860
861 if ( bFromRight )
862 {
863 // find last non-space character
2bb67b80
OK
864 wxChar *psz = m_pchData + GetStringData()->nDataLength - 1;
865 while ( wxIsspace(*psz) && (psz >= m_pchData) )
2c3b684c
VZ
866 psz--;
867
868 // truncate at trailing space start
2bb67b80 869 *++psz = _T('\0');
2c3b684c
VZ
870 GetStringData()->nDataLength = psz - m_pchData;
871 }
872 else
873 {
874 // find first non-space character
2bb67b80
OK
875 const wxChar *psz = m_pchData;
876 while ( wxIsspace(*psz) )
2c3b684c
VZ
877 psz++;
878
879 // fix up data and length
2bb67b80
OK
880 int nDataLength = GetStringData()->nDataLength - (psz - (const wxChar*) m_pchData);
881 memmove(m_pchData, psz, (nDataLength + 1)*sizeof(wxChar));
2c3b684c
VZ
882 GetStringData()->nDataLength = nDataLength;
883 }
c801d85f
KB
884 }
885
886 return *this;
887}
888
889// adds nCount characters chPad to the string from either side
2bb67b80 890wxString& wxString::Pad(size_t nCount, wxChar chPad, bool bFromRight)
c801d85f
KB
891{
892 wxString s(chPad, nCount);
893
894 if ( bFromRight )
895 *this += s;
896 else
897 {
898 s += *this;
899 *this = s;
900 }
901
902 return *this;
903}
904
905// truncate the string
906wxString& wxString::Truncate(size_t uiLen)
907{
79a773ba
VZ
908 if ( uiLen < Len() ) {
909 CopyBeforeWrite();
910
2bb67b80 911 *(m_pchData + uiLen) = _T('\0');
79a773ba
VZ
912 GetStringData()->nDataLength = uiLen;
913 }
914 //else: nothing to do, string is already short enough
c801d85f
KB
915
916 return *this;
917}
918
919// ---------------------------------------------------------------------------
3c67202d 920// finding (return wxNOT_FOUND if not found and index otherwise)
c801d85f
KB
921// ---------------------------------------------------------------------------
922
923// find a character
2bb67b80 924int wxString::Find(wxChar ch, bool bFromEnd) const
c801d85f 925{
2bb67b80 926 const wxChar *psz = bFromEnd ? wxStrrchr(m_pchData, ch) : wxStrchr(m_pchData, ch);
c801d85f 927
2bb67b80 928 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
c801d85f
KB
929}
930
931// find a sub-string (like strstr)
2bb67b80 932int wxString::Find(const wxChar *pszSub) const
c801d85f 933{
2bb67b80 934 const wxChar *psz = wxStrstr(m_pchData, pszSub);
c801d85f 935
2bb67b80 936 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
c801d85f
KB
937}
938
7be07660
VZ
939// ---------------------------------------------------------------------------
940// stream-like operators
941// ---------------------------------------------------------------------------
942wxString& wxString::operator<<(int i)
943{
944 wxString res;
2bb67b80 945 res.Printf(_T("%d"), i);
7be07660
VZ
946
947 return (*this) << res;
948}
949
950wxString& wxString::operator<<(float f)
951{
952 wxString res;
2bb67b80 953 res.Printf(_T("%f"), f);
7be07660
VZ
954
955 return (*this) << res;
956}
957
958wxString& wxString::operator<<(double d)
959{
960 wxString res;
2bb67b80 961 res.Printf(_T("%g"), d);
7be07660
VZ
962
963 return (*this) << res;
964}
965
c801d85f 966// ---------------------------------------------------------------------------
9efd3367 967// formatted output
c801d85f 968// ---------------------------------------------------------------------------
2bb67b80 969int wxString::Printf(const wxChar *pszFormat, ...)
c801d85f
KB
970{
971 va_list argptr;
972 va_start(argptr, pszFormat);
973
974 int iLen = PrintfV(pszFormat, argptr);
975
976 va_end(argptr);
977
978 return iLen;
979}
980
2bb67b80 981int wxString::PrintfV(const wxChar* pszFormat, va_list argptr)
c801d85f 982{
7be07660 983 // static buffer to avoid dynamic memory allocation each time
9efd3367 984 static char s_szScratch[1024];
2bb67b80
OK
985#if wxUSE_THREADS
986 // protect the static buffer
987 static wxCriticalSection critsect;
988 wxCriticalSectionLocker lock(critsect);
989#endif
c801d85f 990
2bb67b80
OK
991#if 1 // the new implementation
992
993 Reinit();
994 for (size_t n = 0; pszFormat[n]; n++)
995 if (pszFormat[n] == _T('%')) {
996 static char s_szFlags[256] = "%";
997 size_t flagofs = 1;
998 bool adj_left = FALSE, in_prec = FALSE,
999 prec_dot = FALSE, done = FALSE;
1000 int ilen = 0;
1001 size_t min_width = 0, max_width = wxSTRING_MAXLEN;
1002 do {
1003#define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1004 switch (pszFormat[++n]) {
1005 case _T('\0'):
1006 done = TRUE;
1007 break;
1008 case _T('%'):
1009 *this += _T('%');
1010 done = TRUE;
1011 break;
1012 case _T('#'):
1013 case _T('0'):
1014 case _T(' '):
1015 case _T('+'):
1016 case _T('\''):
1017 CHECK_PREC
1018 s_szFlags[flagofs++] = pszFormat[n];
1019 break;
1020 case _T('-'):
1021 CHECK_PREC
1022 adj_left = TRUE;
1023 s_szFlags[flagofs++] = pszFormat[n];
1024 break;
1025 case _T('.'):
1026 CHECK_PREC
1027 in_prec = TRUE;
1028 prec_dot = FALSE;
1029 max_width = 0;
1030 // dot will be auto-added to s_szFlags if non-negative number follows
1031 break;
1032 case _T('h'):
1033 ilen = -1;
1034 CHECK_PREC
1035 s_szFlags[flagofs++] = pszFormat[n];
1036 break;
1037 case _T('l'):
1038 ilen = 1;
1039 CHECK_PREC
1040 s_szFlags[flagofs++] = pszFormat[n];
1041 break;
1042 case _T('q'):
1043 case _T('L'):
1044 ilen = 2;
1045 CHECK_PREC
1046 s_szFlags[flagofs++] = pszFormat[n];
1047 break;
1048 case _T('Z'):
1049 ilen = 3;
1050 CHECK_PREC
1051 s_szFlags[flagofs++] = pszFormat[n];
1052 break;
1053 case _T('*'):
1054 {
1055 int len = va_arg(argptr, int);
1056 if (in_prec) {
1057 if (len<0) break;
1058 CHECK_PREC
1059 max_width = len;
1060 } else {
1061 if (len<0) {
1062 adj_left = !adj_left;
1063 s_szFlags[flagofs++] = '-';
1064 len = -len;
1065 }
1066 min_width = len;
1067 }
1068 flagofs += ::sprintf(s_szFlags+flagofs,"%d",len);
1069 }
1070 break;
1071 case _T('1'): case _T('2'): case _T('3'):
1072 case _T('4'): case _T('5'): case _T('6'):
1073 case _T('7'): case _T('8'): case _T('9'):
1074 {
1075 int len = 0;
1076 CHECK_PREC
1077 while ((pszFormat[n]>=_T('0')) && (pszFormat[n]<=_T('9'))) {
1078 s_szFlags[flagofs++] = pszFormat[n];
1079 len = len*10 + (pszFormat[n] - _T('0'));
1080 n++;
1081 }
1082 if (in_prec) max_width = len;
1083 else min_width = len;
1084 n--; // the main loop pre-increments n again
1085 }
1086 break;
1087 case _T('d'):
1088 case _T('i'):
1089 case _T('o'):
1090 case _T('u'):
1091 case _T('x'):
1092 case _T('X'):
1093 CHECK_PREC
1094 s_szFlags[flagofs++] = pszFormat[n];
1095 s_szFlags[flagofs] = '\0';
1096 if (ilen == 0 ) {
1097 int val = va_arg(argptr, int);
1098 ::sprintf(s_szScratch, s_szFlags, val);
1099 }
1100 else if (ilen == -1) {
1101 short int val = va_arg(argptr, short int);
1102 ::sprintf(s_szScratch, s_szFlags, val);
1103 }
1104 else if (ilen == 1) {
1105 long int val = va_arg(argptr, long int);
1106 ::sprintf(s_szScratch, s_szFlags, val);
1107 }
1108 else if (ilen == 2) {
1109#if SIZEOF_LONG_LONG
1110 long long int val = va_arg(argptr, long long int);
1111 ::sprintf(s_szScratch, s_szFlags, val);
1112#else
1113 long int val = va_arg(argptr, long int);
1114 ::sprintf(s_szScratch, s_szFlags, val);
1115#endif
1116 }
1117 else if (ilen == 3) {
1118 size_t val = va_arg(argptr, size_t);
1119 ::sprintf(s_szScratch, s_szFlags, val);
1120 }
1121 *this += wxString(s_szScratch);
1122 done = TRUE;
1123 break;
1124 case _T('e'):
1125 case _T('E'):
1126 case _T('f'):
1127 case _T('g'):
1128 case _T('G'):
1129 CHECK_PREC
1130 s_szFlags[flagofs++] = pszFormat[n];
1131 s_szFlags[flagofs] = '\0';
1132 if (ilen == 2) {
1133 long double val = va_arg(argptr, long double);
1134 ::sprintf(s_szScratch, s_szFlags, val);
1135 } else {
1136 double val = va_arg(argptr, double);
1137 ::sprintf(s_szScratch, s_szFlags, val);
1138 }
1139 *this += wxString(s_szScratch);
1140 done = TRUE;
1141 break;
1142 case _T('p'):
1143 {
1144 void *val = va_arg(argptr, void *);
1145 CHECK_PREC
1146 s_szFlags[flagofs++] = pszFormat[n];
1147 s_szFlags[flagofs] = '\0';
1148 ::sprintf(s_szScratch, s_szFlags, val);
1149 *this += wxString(s_szScratch);
1150 done = TRUE;
1151 }
1152 break;
1153 case _T('c'):
1154 {
1155 wxChar val = va_arg(argptr, int);
1156 // we don't need to honor padding here, do we?
1157 *this += val;
1158 done = TRUE;
1159 }
1160 break;
1161 case _T('s'):
1162 if (ilen == -1) {
1163 // wx extension: we'll let %hs mean non-Unicode strings
1164 char *val = va_arg(argptr, char *);
1165#if wxUSE_UNICODE
1166 // ASCII->Unicode constructor handles max_width right
cf2f341a 1167 wxString s(val, wxConv_libc, max_width);
2bb67b80
OK
1168#else
1169 size_t len = wxSTRING_MAXLEN;
1170 if (val) {
1171 for (len = 0; val[len] && (len<max_width); len++);
1172 } else val = _T("(null)");
1173 wxString s(val, len);
1174#endif
1175 if (s.Len() < min_width)
1176 s.Pad(min_width - s.Len(), _T(' '), adj_left);
1177 *this += s;
1178 } else {
1179 wxChar *val = va_arg(argptr, wxChar *);
1180 size_t len = wxSTRING_MAXLEN;
1181 if (val) {
1182 for (len = 0; val[len] && (len<max_width); len++);
1183 } else val = _T("(null)");
1184 wxString s(val, len);
1185 if (s.Len() < min_width)
1186 s.Pad(min_width - s.Len(), _T(' '), adj_left);
1187 *this += s;
2bb67b80 1188 }
b43ca95a 1189 done = TRUE;
2bb67b80
OK
1190 break;
1191 case _T('n'):
1192 if (ilen == 0) {
1193 int *val = va_arg(argptr, int *);
1194 *val = Len();
1195 }
1196 else if (ilen == -1) {
1197 short int *val = va_arg(argptr, short int *);
1198 *val = Len();
1199 }
1200 else if (ilen >= 1) {
1201 long int *val = va_arg(argptr, long int *);
1202 *val = Len();
1203 }
1204 done = TRUE;
1205 break;
1206 default:
1207 if (wxIsalpha(pszFormat[n]))
1208 // probably some flag not taken care of here yet
1209 s_szFlags[flagofs++] = pszFormat[n];
1210 else {
1211 // bad format
1212 *this += _T('%'); // just to pass the glibc tst-printf.c
1213 n--;
1214 done = TRUE;
1215 }
1216 break;
1217 }
1218#undef CHECK_PREC
1219 } while (!done);
1220 } else *this += pszFormat[n];
1221
1222#else
1223 // NB: wxVsnprintf() may return either less than the buffer size or -1 if there
89b892a2 1224 // is not enough place depending on implementation
2bb67b80 1225 int iLen = wxVsnprintf(s_szScratch, WXSIZEOF(s_szScratch), pszFormat, argptr);
7be07660 1226 char *buffer;
89b892a2 1227 if ( iLen < (int)WXSIZEOF(s_szScratch) ) {
7be07660
VZ
1228 buffer = s_szScratch;
1229 }
1230 else {
1231 int size = WXSIZEOF(s_szScratch) * 2;
1232 buffer = (char *)malloc(size);
1233 while ( buffer != NULL ) {
2bb67b80 1234 iLen = wxVsnprintf(buffer, WXSIZEOF(s_szScratch), pszFormat, argptr);
7be07660
VZ
1235 if ( iLen < size ) {
1236 // ok, there was enough space
1237 break;
1238 }
1239
1240 // still not enough, double it again
1241 buffer = (char *)realloc(buffer, size *= 2);
1242 }
1243
1244 if ( !buffer ) {
1245 // out of memory
1246 return -1;
1247 }
1248 }
1249
2bb67b80
OK
1250 wxString s(buffer);
1251 *this = s;
7be07660
VZ
1252
1253 if ( buffer != s_szScratch )
1254 free(buffer);
2bb67b80 1255#endif
c801d85f 1256
2bb67b80 1257 return Len();
c801d85f
KB
1258}
1259
097c080b
VZ
1260// ----------------------------------------------------------------------------
1261// misc other operations
1262// ----------------------------------------------------------------------------
2bb67b80 1263bool wxString::Matches(const wxChar *pszMask) const
097c080b
VZ
1264{
1265 // check char by char
2bb67b80
OK
1266 const wxChar *pszTxt;
1267 for ( pszTxt = c_str(); *pszMask != _T('\0'); pszMask++, pszTxt++ ) {
097c080b 1268 switch ( *pszMask ) {
2bb67b80
OK
1269 case _T('?'):
1270 if ( *pszTxt == _T('\0') )
097c080b
VZ
1271 return FALSE;
1272
1273 pszTxt++;
1274 pszMask++;
1275 break;
1276
2bb67b80 1277 case _T('*'):
097c080b
VZ
1278 {
1279 // ignore special chars immediately following this one
2bb67b80 1280 while ( *pszMask == _T('*') || *pszMask == _T('?') )
097c080b
VZ
1281 pszMask++;
1282
1283 // if there is nothing more, match
2bb67b80 1284 if ( *pszMask == _T('\0') )
097c080b
VZ
1285 return TRUE;
1286
1287 // are there any other metacharacters in the mask?
c86f1403 1288 size_t uiLenMask;
2bb67b80 1289 const wxChar *pEndMask = wxStrpbrk(pszMask, _T("*?"));
097c080b
VZ
1290
1291 if ( pEndMask != NULL ) {
1292 // we have to match the string between two metachars
1293 uiLenMask = pEndMask - pszMask;
1294 }
1295 else {
1296 // we have to match the remainder of the string
2bb67b80 1297 uiLenMask = wxStrlen(pszMask);
097c080b
VZ
1298 }
1299
1300 wxString strToMatch(pszMask, uiLenMask);
2bb67b80 1301 const wxChar* pMatch = wxStrstr(pszTxt, strToMatch);
097c080b
VZ
1302 if ( pMatch == NULL )
1303 return FALSE;
1304
1305 // -1 to compensate "++" in the loop
1306 pszTxt = pMatch + uiLenMask - 1;
1307 pszMask += uiLenMask - 1;
1308 }
1309 break;
1310
1311 default:
1312 if ( *pszMask != *pszTxt )
1313 return FALSE;
1314 break;
1315 }
1316 }
1317
1318 // match only if nothing left
2bb67b80 1319 return *pszTxt == _T('\0');
097c080b
VZ
1320}
1321
1fc5dd6f 1322// Count the number of chars
2bb67b80 1323int wxString::Freq(wxChar ch) const
1fc5dd6f
JS
1324{
1325 int count = 0;
1326 int len = Len();
1327 for (int i = 0; i < len; i++)
1328 {
1329 if (GetChar(i) == ch)
1330 count ++;
1331 }
1332 return count;
1333}
1334
03ab016d
JS
1335// convert to upper case, return the copy of the string
1336wxString wxString::Upper() const
1337{ wxString s(*this); return s.MakeUpper(); }
1338
1339// convert to lower case, return the copy of the string
1340wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); }
1341
2bb67b80 1342int wxString::sprintf(const wxChar *pszFormat, ...)
8870c26e
JS
1343 {
1344 va_list argptr;
1345 va_start(argptr, pszFormat);
1346 int iLen = PrintfV(pszFormat, argptr);
1347 va_end(argptr);
1348 return iLen;
1349 }
1350
c801d85f
KB
1351// ---------------------------------------------------------------------------
1352// standard C++ library string functions
1353// ---------------------------------------------------------------------------
8de2e39c 1354#ifdef wxSTD_STRING_COMPATIBILITY
c801d85f
KB
1355
1356wxString& wxString::insert(size_t nPos, const wxString& str)
1357{
097c080b 1358 wxASSERT( str.GetStringData()->IsValid() );
c801d85f
KB
1359 wxASSERT( nPos <= Len() );
1360
cb6780ff
VZ
1361 if ( !str.IsEmpty() ) {
1362 wxString strTmp;
2bb67b80
OK
1363 wxChar *pc = strTmp.GetWriteBuf(Len() + str.Len());
1364 wxStrncpy(pc, c_str(), nPos);
1365 wxStrcpy(pc + nPos, str);
1366 wxStrcpy(pc + nPos + str.Len(), c_str() + nPos);
cb6780ff
VZ
1367 strTmp.UngetWriteBuf();
1368 *this = strTmp;
1369 }
dd1eaa89
VZ
1370
1371 return *this;
c801d85f
KB
1372}
1373
1374size_t wxString::find(const wxString& str, size_t nStart) const
1375{
097c080b 1376 wxASSERT( str.GetStringData()->IsValid() );
c801d85f
KB
1377 wxASSERT( nStart <= Len() );
1378
2bb67b80 1379 const wxChar *p = wxStrstr(c_str() + nStart, str);
dd1eaa89 1380
c801d85f
KB
1381 return p == NULL ? npos : p - c_str();
1382}
1383
f0b3249b 1384// VC++ 1.5 can't cope with the default argument in the header.
3f4a0c5b 1385#if !defined(__VISUALC__) || defined(__WIN32__)
2bb67b80 1386size_t wxString::find(const wxChar* sz, size_t nStart, size_t n) const
c801d85f
KB
1387{
1388 return find(wxString(sz, n == npos ? 0 : n), nStart);
1389}
3f4a0c5b 1390#endif // VC++ 1.5
dd1eaa89 1391
62448488
JS
1392// Gives a duplicate symbol (presumably a case-insensitivity problem)
1393#if !defined(__BORLANDC__)
2bb67b80 1394size_t wxString::find(wxChar ch, size_t nStart) const
c801d85f
KB
1395{
1396 wxASSERT( nStart <= Len() );
1397
2bb67b80 1398 const wxChar *p = wxStrchr(c_str() + nStart, ch);
dd1eaa89 1399
c801d85f
KB
1400 return p == NULL ? npos : p - c_str();
1401}
62448488 1402#endif
c801d85f
KB
1403
1404size_t wxString::rfind(const wxString& str, size_t nStart) const
1405{
097c080b 1406 wxASSERT( str.GetStringData()->IsValid() );
c801d85f
KB
1407 wxASSERT( nStart <= Len() );
1408
1409 // # could be quicker than that
2bb67b80 1410 const wxChar *p = c_str() + (nStart == npos ? Len() : nStart);
c801d85f 1411 while ( p >= c_str() + str.Len() ) {
2bb67b80 1412 if ( wxStrncmp(p - str.Len(), str, str.Len()) == 0 )
c801d85f
KB
1413 return p - str.Len() - c_str();
1414 p--;
1415 }
dd1eaa89 1416
c801d85f
KB
1417 return npos;
1418}
dd1eaa89 1419
f0b3249b 1420// VC++ 1.5 can't cope with the default argument in the header.
3f4a0c5b 1421#if !defined(__VISUALC__) || defined(__WIN32__)
2bb67b80 1422size_t wxString::rfind(const wxChar* sz, size_t nStart, size_t n) const
c801d85f
KB
1423{
1424 return rfind(wxString(sz, n == npos ? 0 : n), nStart);
1425}
1426
2bb67b80 1427size_t wxString::rfind(wxChar ch, size_t nStart) const
c801d85f
KB
1428{
1429 wxASSERT( nStart <= Len() );
1430
2bb67b80 1431 const wxChar *p = wxStrrchr(c_str() + nStart, ch);
dd1eaa89 1432
c801d85f
KB
1433 return p == NULL ? npos : p - c_str();
1434}
3f4a0c5b 1435#endif // VC++ 1.5
c801d85f
KB
1436
1437wxString wxString::substr(size_t nStart, size_t nLen) const
1438{
1439 // npos means 'take all'
1440 if ( nLen == npos )
1441 nLen = 0;
1442
1443 wxASSERT( nStart + nLen <= Len() );
1444
1445 return wxString(c_str() + nStart, nLen == npos ? 0 : nLen);
1446}
1447
1448wxString& wxString::erase(size_t nStart, size_t nLen)
1449{
1450 wxString strTmp(c_str(), nStart);
1451 if ( nLen != npos ) {
1452 wxASSERT( nStart + nLen <= Len() );
1453
1454 strTmp.append(c_str() + nStart + nLen);
1455 }
1456
1457 *this = strTmp;
1458 return *this;
1459}
1460
2bb67b80 1461wxString& wxString::replace(size_t nStart, size_t nLen, const wxChar *sz)
c801d85f 1462{
2bb67b80 1463 wxASSERT( nStart + nLen <= wxStrlen(sz) );
c801d85f
KB
1464
1465 wxString strTmp;
1466 if ( nStart != 0 )
1467 strTmp.append(c_str(), nStart);
1468 strTmp += sz;
1469 strTmp.append(c_str() + nStart + nLen);
dd1eaa89 1470
c801d85f
KB
1471 *this = strTmp;
1472 return *this;
1473}
1474
2bb67b80 1475wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch)
c801d85f
KB
1476{
1477 return replace(nStart, nLen, wxString(ch, nCount));
1478}
1479
dd1eaa89 1480wxString& wxString::replace(size_t nStart, size_t nLen,
097c080b 1481 const wxString& str, size_t nStart2, size_t nLen2)
c801d85f
KB
1482{
1483 return replace(nStart, nLen, str.substr(nStart2, nLen2));
1484}
1485
dd1eaa89 1486wxString& wxString::replace(size_t nStart, size_t nLen,
2bb67b80 1487 const wxChar* sz, size_t nCount)
c801d85f
KB
1488{
1489 return replace(nStart, nLen, wxString(sz, nCount));
1490}
1491
1492#endif //std::string compatibility
1493
1494// ============================================================================
1495// ArrayString
1496// ============================================================================
1497
1498// size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1499#define ARRAY_MAXSIZE_INCREMENT 4096
1500#ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1501 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1502#endif
1503
1504#define STRING(p) ((wxString *)(&(p)))
1505
1506// ctor
1507wxArrayString::wxArrayString()
1508{
1509 m_nSize =
1510 m_nCount = 0;
2bb67b80 1511 m_pItems = (wxChar **) NULL;
c801d85f
KB
1512}
1513
1514// copy ctor
1515wxArrayString::wxArrayString(const wxArrayString& src)
1516{
3bbb630a
VZ
1517 m_nSize =
1518 m_nCount = 0;
2bb67b80 1519 m_pItems = (wxChar **) NULL;
c801d85f 1520
4d14b524 1521 *this = src;
c801d85f
KB
1522}
1523
4d14b524 1524// assignment operator
c801d85f
KB
1525wxArrayString& wxArrayString::operator=(const wxArrayString& src)
1526{
d93f63db
VZ
1527 if ( m_nSize > 0 )
1528 Clear();
c801d85f 1529
4d14b524
VZ
1530 if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
1531 Alloc(src.m_nCount);
c801d85f 1532
4d14b524
VZ
1533 // we can't just copy the pointers here because otherwise we would share
1534 // the strings with another array
c86f1403 1535 for ( size_t n = 0; n < src.m_nCount; n++ )
4d14b524 1536 Add(src[n]);
c801d85f 1537
3bbb630a 1538 if ( m_nCount != 0 )
2bb67b80 1539 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(wxChar *));
3bbb630a 1540
c801d85f
KB
1541 return *this;
1542}
1543
1544// grow the array
1545void wxArrayString::Grow()
1546{
1547 // only do it if no more place
1548 if( m_nCount == m_nSize ) {
1549 if( m_nSize == 0 ) {
1550 // was empty, alloc some memory
1551 m_nSize = ARRAY_DEFAULT_INITIAL_SIZE;
2bb67b80 1552 m_pItems = new wxChar *[m_nSize];
c801d85f
KB
1553 }
1554 else {
3bbb630a
VZ
1555 // otherwise when it's called for the first time, nIncrement would be 0
1556 // and the array would never be expanded
1557 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE != 0 );
1558
c801d85f 1559 // add 50% but not too much
3bbb630a 1560 size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
4d14b524 1561 ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
c801d85f
KB
1562 if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
1563 nIncrement = ARRAY_MAXSIZE_INCREMENT;
1564 m_nSize += nIncrement;
2bb67b80 1565 wxChar **pNew = new wxChar *[m_nSize];
c801d85f
KB
1566
1567 // copy data to new location
2bb67b80 1568 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
c801d85f
KB
1569
1570 // delete old memory (but do not release the strings!)
a3622daa 1571 wxDELETEA(m_pItems);
c801d85f
KB
1572
1573 m_pItems = pNew;
1574 }
1575 }
1576}
1577
1578void wxArrayString::Free()
1579{
1580 for ( size_t n = 0; n < m_nCount; n++ ) {
1581 STRING(m_pItems[n])->GetStringData()->Unlock();
1582 }
1583}
1584
1585// deletes all the strings from the list
1586void wxArrayString::Empty()
1587{
1588 Free();
1589
1590 m_nCount = 0;
1591}
1592
1593// as Empty, but also frees memory
1594void wxArrayString::Clear()
1595{
1596 Free();
1597
dd1eaa89 1598 m_nSize =
c801d85f
KB
1599 m_nCount = 0;
1600
a3622daa 1601 wxDELETEA(m_pItems);
c801d85f
KB
1602}
1603
1604// dtor
1605wxArrayString::~wxArrayString()
1606{
1607 Free();
1608
a3622daa 1609 wxDELETEA(m_pItems);
c801d85f
KB
1610}
1611
1612// pre-allocates memory (frees the previous data!)
1613void wxArrayString::Alloc(size_t nSize)
1614{
1615 wxASSERT( nSize > 0 );
1616
1617 // only if old buffer was not big enough
1618 if ( nSize > m_nSize ) {
1619 Free();
a3622daa 1620 wxDELETEA(m_pItems);
2bb67b80 1621 m_pItems = new wxChar *[nSize];
c801d85f
KB
1622 m_nSize = nSize;
1623 }
1624
1625 m_nCount = 0;
1626}
1627
d4ffe273
OK
1628// minimizes the memory usage by freeing unused memory
1629void wxArrayString::Shrink()
1630{
1631 // only do it if we have some memory to free
1632 if( m_nCount < m_nSize ) {
1633 // allocates exactly as much memory as we need
1634 wxChar **pNew = new wxChar *[m_nCount];
1635
1636 // copy data to new location
1637 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
1638 delete [] m_pItems;
1639 m_pItems = pNew;
1640 }
1641}
1642
c801d85f 1643// searches the array for an item (forward or backwards)
2bb67b80 1644int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const
c801d85f
KB
1645{
1646 if ( bFromEnd ) {
1647 if ( m_nCount > 0 ) {
c86f1403 1648 size_t ui = m_nCount;
c801d85f
KB
1649 do {
1650 if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) )
1651 return ui;
1652 }
1653 while ( ui != 0 );
1654 }
1655 }
1656 else {
c86f1403 1657 for( size_t ui = 0; ui < m_nCount; ui++ ) {
c801d85f
KB
1658 if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) )
1659 return ui;
1660 }
1661 }
1662
3c67202d 1663 return wxNOT_FOUND;
c801d85f
KB
1664}
1665
1666// add item at the end
097c080b 1667void wxArrayString::Add(const wxString& str)
c801d85f 1668{
097c080b
VZ
1669 wxASSERT( str.GetStringData()->IsValid() );
1670
c801d85f
KB
1671 Grow();
1672
1673 // the string data must not be deleted!
097c080b 1674 str.GetStringData()->Lock();
2bb67b80 1675 m_pItems[m_nCount++] = (wxChar *)str.c_str();
c801d85f
KB
1676}
1677
1678// add item at the given position
097c080b 1679void wxArrayString::Insert(const wxString& str, size_t nIndex)
c801d85f 1680{
097c080b
VZ
1681 wxASSERT( str.GetStringData()->IsValid() );
1682
cf2f341a 1683 wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Insert") );
c801d85f
KB
1684
1685 Grow();
1686
dd1eaa89 1687 memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
2bb67b80 1688 (m_nCount - nIndex)*sizeof(wxChar *));
c801d85f 1689
097c080b 1690 str.GetStringData()->Lock();
2bb67b80 1691 m_pItems[nIndex] = (wxChar *)str.c_str();
c801d85f
KB
1692
1693 m_nCount++;
1694}
1695
1696// removes item from array (by index)
1697void wxArrayString::Remove(size_t nIndex)
1698{
1a5a8367 1699 wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Remove") );
c801d85f
KB
1700
1701 // release our lock
1702 Item(nIndex).GetStringData()->Unlock();
1703
dd1eaa89 1704 memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
2bb67b80 1705 (m_nCount - nIndex - 1)*sizeof(wxChar *));
c801d85f
KB
1706 m_nCount--;
1707}
1708
1709// removes item from array (by value)
2bb67b80 1710void wxArrayString::Remove(const wxChar *sz)
c801d85f
KB
1711{
1712 int iIndex = Index(sz);
1713
3c67202d 1714 wxCHECK_RET( iIndex != wxNOT_FOUND,
1a5a8367 1715 _("removing inexistent element in wxArrayString::Remove") );
c801d85f 1716
c86f1403 1717 Remove(iIndex);
c801d85f
KB
1718}
1719
30b21f9a
VZ
1720// ----------------------------------------------------------------------------
1721// sorting
1722// ----------------------------------------------------------------------------
1723
1724// we can only sort one array at a time with the quick-sort based
1725// implementation
1726#if wxUSE_THREADS
30b21f9a
VZ
1727 // need a critical section to protect access to gs_compareFunction and
1728 // gs_sortAscending variables
26128999 1729 static wxCriticalSection *gs_critsectStringSort = NULL;
30b21f9a
VZ
1730
1731 // call this before the value of the global sort vars is changed/after
1732 // you're finished with them
26128999
VZ
1733 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
1734 gs_critsectStringSort = new wxCriticalSection; \
1735 gs_critsectStringSort->Enter()
1736 #define END_SORT() gs_critsectStringSort->Leave(); \
1737 delete gs_critsectStringSort; \
1738 gs_critsectStringSort = NULL
30b21f9a
VZ
1739#else // !threads
1740 #define START_SORT()
1741 #define END_SORT()
1742#endif // wxUSE_THREADS
1743
1744// function to use for string comparaison
1745static wxArrayString::CompareFunction gs_compareFunction = NULL;
1746
1747// if we don't use the compare function, this flag tells us if we sort the
1748// array in ascending or descending order
1749static bool gs_sortAscending = TRUE;
1750
1751// function which is called by quick sort
1752static int wxStringCompareFunction(const void *first, const void *second)
1753{
1754 wxString *strFirst = (wxString *)first;
1755 wxString *strSecond = (wxString *)second;
1756
64716cd7 1757 if ( gs_compareFunction ) {
30b21f9a 1758 return gs_compareFunction(*strFirst, *strSecond);
64716cd7 1759 }
30b21f9a 1760 else {
2bb67b80
OK
1761 // maybe we should use wxStrcoll
1762 int result = wxStrcmp(strFirst->c_str(), strSecond->c_str());
30b21f9a
VZ
1763
1764 return gs_sortAscending ? result : -result;
1765 }
1766}
1767
c801d85f 1768// sort array elements using passed comparaison function
30b21f9a
VZ
1769void wxArrayString::Sort(CompareFunction compareFunction)
1770{
1771 START_SORT();
1772
1773 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
1774 gs_compareFunction = compareFunction;
1775
1776 DoSort();
1777
1778 END_SORT();
1779}
1780
1781void wxArrayString::Sort(bool reverseOrder)
1782{
1783 START_SORT();
1784
1785 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
1786 gs_sortAscending = !reverseOrder;
1787
1788 DoSort();
1789
1790 END_SORT();
1791}
c801d85f 1792
30b21f9a 1793void wxArrayString::DoSort()
c801d85f 1794{
30b21f9a
VZ
1795 // just sort the pointers using qsort() - of course it only works because
1796 // wxString() *is* a pointer to its data
2bb67b80 1797 qsort(m_pItems, m_nCount, sizeof(wxChar *), wxStringCompareFunction);
c801d85f 1798}
2bb67b80
OK
1799
1800// ============================================================================
1801// MBConv
1802// ============================================================================
1803
eea4f86a 1804#if wxUSE_WCHAR_T
3e473156
OK
1805WXDLLEXPORT_DATA(wxMBConv *) wxConv_current = &wxConv_libc;
1806
2bb67b80
OK
1807// ----------------------------------------------------------------------------
1808// standard libc conversion
1809// ----------------------------------------------------------------------------
1810
ba555d51
OK
1811WXDLLEXPORT_DATA(wxMBConv) wxConv_libc;
1812
6b769f3d 1813size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
2bb67b80
OK
1814{
1815 return wxMB2WC(buf, psz, n);
1816}
1817
6b769f3d 1818size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
2bb67b80
OK
1819{
1820 return wxWC2MB(buf, psz, n);
1821}
1822
1823// ----------------------------------------------------------------------------
ba555d51 1824// standard file conversion
2bb67b80
OK
1825// ----------------------------------------------------------------------------
1826
5f709e67 1827WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile;
ba555d51
OK
1828
1829// just use the libc conversion for now
5f709e67 1830size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const
ba555d51
OK
1831{
1832 return wxMB2WC(buf, psz, n);
1833}
1834
5f709e67 1835size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const
2bb67b80 1836{
ba555d51
OK
1837 return wxWC2MB(buf, psz, n);
1838}
2bb67b80 1839
ba555d51
OK
1840// ----------------------------------------------------------------------------
1841// standard gdk conversion
1842// ----------------------------------------------------------------------------
1843
e5ea3f7a 1844#ifdef __WXGTK12__
5f709e67 1845WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk;
ba555d51
OK
1846
1847#include <gdk/gdk.h>
1848
5f709e67 1849size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const
ba555d51
OK
1850{
1851 if (buf) {
1852 return gdk_mbstowcs((GdkWChar *)buf, psz, n);
1853 } else {
1854 GdkWChar *nbuf = new GdkWChar[n=strlen(psz)];
1855 size_t len = gdk_mbstowcs(nbuf, psz, n);
1856 delete [] nbuf;
1857 return len;
1858 }
1859}
1860
5f709e67 1861size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const
ba555d51
OK
1862{
1863 char *mbstr = gdk_wcstombs((GdkWChar *)psz);
1864 size_t len = mbstr ? strlen(mbstr) : 0;
1865 if (buf) {
1866 if (len > n) len = n;
1867 memcpy(buf, psz, len);
1868 if (len < n) buf[len] = 0;
1869 }
1870 return len;
1871}
7fd43a44 1872#endif // GTK > 1.0
ba555d51
OK
1873
1874// ----------------------------------------------------------------------------
1875// UTF-7
1876// ----------------------------------------------------------------------------
1877
5f709e67 1878WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7;
2bb67b80 1879
eea4f86a
OK
1880#if 0
1881static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1882 "abcdefghijklmnopqrstuvwxyz"
1883 "0123456789'(),-./:?";
1884static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}";
1885static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1886 "abcdefghijklmnopqrstuvwxyz"
1887 "0123456789+/";
1888#endif
1889
2bb67b80 1890// TODO: write actual implementations of UTF-7 here
5f709e67 1891size_t wxMBConvUTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const
2bb67b80
OK
1892{
1893 return 0;
1894}
1895
5f709e67 1896size_t wxMBConvUTF7::WC2MB(char *buf, const wchar_t *psz, size_t n) const
2bb67b80
OK
1897{
1898 return 0;
1899}
1900
1901// ----------------------------------------------------------------------------
1902// UTF-8
1903// ----------------------------------------------------------------------------
1904
5f709e67 1905WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8;
2bb67b80 1906
eea4f86a
OK
1907static unsigned long utf8_max[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff};
1908
5f709e67 1909size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
2bb67b80 1910{
eea4f86a
OK
1911 size_t len = 0;
1912
1913 while (*psz && ((!buf) || (len<n))) {
1914 unsigned char cc=*psz++, fc=cc;
1915 unsigned cnt;
1916 for (cnt=0; fc&0x80; cnt++) fc<<=1;
1917 if (!cnt) {
1918 // plain ASCII char
1919 if (buf) *buf++=cc;
1920 len++;
1921 } else {
1922 cnt--;
1923 if (!cnt) {
1924 // invalid UTF-8 sequence
1925 return (size_t)-1;
1926 } else {
1927 unsigned ocnt=cnt-1;
1928 unsigned long res=cc&(0x3f>>cnt);
1929 while (cnt--) {
1930 cc = *psz++;
1931 if ((cc&0xC0)!=0x80) {
1932 // invalid UTF-8 sequence
1933 return (size_t)-1;
1934 }
1935 res=(res<<6)|(cc&0x3f);
1936 }
1937 if (res<=utf8_max[ocnt]) {
1938 // illegal UTF-8 encoding
1939 return (size_t)-1;
1940 }
1941 if (buf) *buf++=res;
1942 len++;
1943 }
1944 }
1945 }
1946 if (buf && (len<n)) *buf = 0;
1947 return len;
2bb67b80
OK
1948}
1949
5f709e67 1950size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
2bb67b80 1951{
eea4f86a
OK
1952 size_t len = 0;
1953
1954 while (*psz && ((!buf) || (len<n))) {
1955 unsigned long cc=(*psz++)&0x7fffffff;
1956 unsigned cnt;
1957 for (cnt=0; cc>utf8_max[cnt]; cnt++);
1958 if (!cnt) {
1959 // plain ASCII char
1960 if (buf) *buf++=cc;
1961 len++;
1962 } else {
1963 len+=cnt+1;
1964 if (buf) {
1965 *buf++=(-128>>cnt)|((cc>>(cnt*6))&(0x3f>>cnt));
1966 while (cnt--)
1967 *buf++=0x80|((cc>>(cnt*6))&0x3f);
1968 }
1969 }
1970 }
1971 if (buf && (len<n)) *buf = 0;
1972 return len;
2bb67b80
OK
1973}
1974
1975// ----------------------------------------------------------------------------
1976// specified character set
1977// ----------------------------------------------------------------------------
1978
3e473156
OK
1979class wxCharacterSet
1980{
1981public:
1982 wxArrayString names;
1983 wchar_t *data;
1984};
1985
1986#ifndef WX_PRECOMP
1987 #include "wx/dynarray.h"
1988 #include "wx/filefn.h"
1989 #include "wx/textfile.h"
1990 #include "wx/tokenzr.h"
1991 #include "wx/utils.h"
1992#endif
1993
1994WX_DECLARE_OBJARRAY(wxCharacterSet, wxCSArray);
1995#include "wx/arrimpl.cpp"
1996WX_DEFINE_OBJARRAY(wxCSArray);
1997
1998static wxCSArray wxCharsets;
1999
2000static void wxLoadCharacterSets(void)
2001{
2002 static bool already_loaded = FALSE;
2003
a28d23bb
OK
2004 if (already_loaded) return;
2005
eea4f86a 2006 already_loaded = TRUE;
c71c744a 2007#if defined(__UNIX__)
3e473156 2008 // search through files in /usr/share/i18n/charmaps
a28d23bb 2009 wxString fname;
a28d23bb 2010 for (fname = ::wxFindFirstFile(_T("/usr/share/i18n/charmaps/*"));
3e473156
OK
2011 !fname.IsEmpty();
2012 fname = ::wxFindNextFile()) {
2013 wxTextFile cmap(fname);
2014 if (cmap.Open()) {
2015 wxCharacterSet *cset = new wxCharacterSet;
2016 wxString comchar,escchar;
2017 bool in_charset = FALSE;
2018
c71c744a 2019 // wxFprintf(stderr,_T("Loaded: %s\n"),fname.c_str());
3e473156 2020
a28d23bb
OK
2021 wxString line;
2022 for (line = cmap.GetFirstLine();
3e473156
OK
2023 !cmap.Eof();
2024 line = cmap.GetNextLine()) {
c71c744a 2025 // wxFprintf(stderr,_T("line contents: %s\n"),line.c_str());
3e473156
OK
2026 wxStringTokenizer token(line);
2027 wxString cmd = token.GetNextToken();
2028 if (cmd == comchar) {
5f709e67
OK
2029 if (token.GetNextToken() == _T("alias"))
2030 cset->names.Add(token.GetNextToken());
3e473156
OK
2031 }
2032 else if (cmd == _T("<code_set_name>"))
2033 cset->names.Add(token.GetNextToken());
2034 else if (cmd == _T("<comment_char>"))
2035 comchar = token.GetNextToken();
2036 else if (cmd == _T("<escape_char>"))
2037 escchar = token.GetNextToken();
c71c744a 2038 else if (cmd == _T("<mb_cur_min>")) {
3e473156 2039 delete cset;
c71c744a
OK
2040 cset = (wxCharacterSet *) NULL;
2041 break; // we don't support multibyte charsets ourselves (yet)
3e473156
OK
2042 }
2043 else if (cmd == _T("CHARMAP")) {
c71c744a 2044 cset->data = (wchar_t *)calloc(256, sizeof(wchar_t));
3e473156
OK
2045 in_charset = TRUE;
2046 }
2047 else if (cmd == _T("END")) {
2048 if (token.GetNextToken() == _T("CHARMAP"))
2049 in_charset = FALSE;
2050 }
2051 else if (in_charset) {
2052 // format: <NUL> /x00 <U0000> NULL (NUL)
c71c744a 2053 // <A> /x41 <U0041> LATIN CAPITAL LETTER A
3e473156 2054 wxString hex = token.GetNextToken();
c71c744a
OK
2055 // skip whitespace (why doesn't wxStringTokenizer do this?)
2056 while (wxIsEmpty(hex) && token.HasMoreTokens()) hex = token.GetNextToken();
3e473156 2057 wxString uni = token.GetNextToken();
c71c744a
OK
2058 // skip whitespace again
2059 while (wxIsEmpty(uni) && token.HasMoreTokens()) uni = token.GetNextToken();
2060
eea4f86a 2061 if ((hex.Len() > 2) && (hex.GetChar(0) == escchar) && (hex.GetChar(1) == _T('x')) &&
c71c744a
OK
2062 (uni.Left(2) == _T("<U"))) {
2063 hex.MakeUpper(); uni.MakeUpper();
2064 int pos = ::wxHexToDec(hex.Mid(2,2));
2065 if (pos>=0) {
2066 unsigned long uni1 = ::wxHexToDec(uni.Mid(2,2));
2067 unsigned long uni2 = ::wxHexToDec(uni.Mid(4,2));
2068 cset->data[pos] = (uni1 << 16) | uni2;
2069 // wxFprintf(stderr,_T("char %02x mapped to %04x (%c)\n"),pos,cset->data[pos],cset->data[pos]);
2070 }
2071 }
3e473156
OK
2072 }
2073 }
c71c744a
OK
2074 if (cset) {
2075 cset->names.Shrink();
2076 wxCharsets.Add(cset);
2077 }
3e473156
OK
2078 }
2079 }
2080#endif
2081 wxCharsets.Shrink();
3e473156
OK
2082}
2083
a28d23bb 2084static wxCharacterSet *wxFindCharacterSet(const wxChar *charset)
3e473156 2085{
6cf7c00f 2086 if (!charset) return (wxCharacterSet *)NULL;
a28d23bb 2087 wxLoadCharacterSets();
3e473156
OK
2088 for (size_t n=0; n<wxCharsets.GetCount(); n++)
2089 if (wxCharsets[n].names.Index(charset) != wxNOT_FOUND)
2090 return &(wxCharsets[n]);
2091 return (wxCharacterSet *)NULL;
2092}
2093
5f709e67 2094WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL);
3e473156 2095
2bb67b80
OK
2096wxCSConv::wxCSConv(const wxChar *charset)
2097{
6cf7c00f 2098 m_name = (wxChar *) NULL;
a28d23bb 2099 m_cset = (wxCharacterSet *) NULL;
6cf7c00f
OK
2100 m_deferred = TRUE;
2101 SetName(charset);
2102}
2103
2104wxCSConv::~wxCSConv()
2105{
2106 if (m_name) free(m_name);
2107}
2108
2109void wxCSConv::SetName(const wxChar *charset)
2110{
a28d23bb 2111 if (charset) {
3e473156 2112#ifdef __UNIX__
a28d23bb
OK
2113 // first, convert the character set name to standard form
2114 wxString codeset;
2115 if (wxString(charset,3).CmpNoCase(_T("ISO")) == 0) {
2116 // make sure it's represented in the standard form: ISO_8859-1
2117 codeset = _T("ISO_");
2118 charset += 3;
2119 if ((*charset == _T('-')) || (*charset == _T('_'))) charset++;
2120 if (wxStrlen(charset)>4) {
2121 if (wxString(charset,4) == _T("8859")) {
2122 codeset << _T("8859-");
2123 if (*charset == _T('-')) charset++;
2124 }
3e473156
OK
2125 }
2126 }
a28d23bb
OK
2127 codeset << charset;
2128 codeset.MakeUpper();
2129 m_name = wxStrdup(codeset.c_str());
2130 m_deferred = TRUE;
3e473156 2131#endif
a28d23bb
OK
2132 }
2133}
2134
a28d23bb 2135void wxCSConv::LoadNow()
ba555d51 2136{
a28d23bb
OK
2137// wxPrintf(_T("Conversion request\n"));
2138 if (m_deferred) {
6cf7c00f
OK
2139 if (!m_name) {
2140#ifdef __UNIX__
2141 wxChar *lang = wxGetenv(_T("LANG"));
2142 wxChar *dot = lang ? wxStrchr(lang, _T('.')) : (wxChar *)NULL;
2143 if (dot) SetName(dot+1);
2144#endif
2145 }
a28d23bb
OK
2146 m_cset = wxFindCharacterSet(m_name);
2147 m_deferred = FALSE;
2148 }
ba555d51
OK
2149}
2150
6b769f3d 2151size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
2bb67b80 2152{
a28d23bb 2153 ((wxCSConv *)this)->LoadNow(); // discard constness
3e473156 2154 if (buf) {
a28d23bb 2155 if (m_cset) {
38444daf 2156 for (size_t c=0; c<n; c++)
eea4f86a 2157 buf[c] = m_cset->data[(unsigned char)(psz[c])];
3e473156
OK
2158 } else {
2159 // latin-1 (direct)
38444daf 2160 for (size_t c=0; c<n; c++)
eea4f86a 2161 buf[c] = (unsigned char)(psz[c]);
3e473156 2162 }
38444daf 2163 return n;
2bb67b80 2164 }
38444daf 2165 return strlen(psz);
2bb67b80
OK
2166}
2167
6b769f3d 2168size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
2bb67b80 2169{
a28d23bb 2170 ((wxCSConv *)this)->LoadNow(); // discard constness
3e473156 2171 if (buf) {
a28d23bb 2172 if (m_cset) {
38444daf 2173 for (size_t c=0; c<n; c++) {
3e473156 2174 size_t n;
a28d23bb 2175 for (n=0; (n<256) && (m_cset->data[n] != psz[c]); n++);
3e473156
OK
2176 buf[c] = (n>0xff) ? '?' : n;
2177 }
2178 } else {
2179 // latin-1 (direct)
38444daf 2180 for (size_t c=0; c<n; c++)
3e473156
OK
2181 buf[c] = (psz[c]>0xff) ? '?' : psz[c];
2182 }
38444daf 2183 return n;
2bb67b80 2184 }
38444daf 2185 return wcslen(psz);
2bb67b80 2186}
eea4f86a
OK
2187
2188#endif//wxUSE_WCHAR_T