]> git.saurik.com Git - wxWidgets.git/blame - src/common/string.cpp
Allow user code to override key events in generic wxListCtrl.
[wxWidgets.git] / src / common / string.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/string.cpp
c801d85f 3// Purpose: wxString class
59059feb 4// Author: Vadim Zeitlin, Ryan Norton
c801d85f
KB
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
59059feb 9// (c) 2004 Ryan Norton <wxprojects@comcast.net>
65571936 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
c801d85f
KB
13// ===========================================================================
14// headers, declarations, constants
15// ===========================================================================
16
17// For compilers that support precompilation, includes "wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
8898456d 21 #pragma hdrstop
c801d85f
KB
22#endif
23
24#ifndef WX_PRECOMP
8898456d 25 #include "wx/string.h"
2523e9b7 26 #include "wx/wxcrtvararg.h"
105993f7 27 #include "wx/intl.h"
ba7e7253 28 #include "wx/log.h"
6b769f3d 29#endif
c801d85f
KB
30
31#include <ctype.h>
92df97b8
WS
32
33#ifndef __WXWINCE__
34 #include <errno.h>
35#endif
36
c801d85f
KB
37#include <string.h>
38#include <stdlib.h>
9a08c20e 39
8116a0c5 40#include "wx/hashmap.h"
072682ce 41#include "wx/vector.h"
529e491c 42#include "wx/xlocale.h"
8f93a29f 43
825d69c1
VZ
44#ifdef __WXMSW__
45 #include "wx/msw/wrapwin.h"
46#endif // __WXMSW__
47
951201d8
VZ
48#if wxUSE_STD_IOSTREAM
49 #include <sstream>
50#endif
51
8f93a29f
VS
52// string handling functions used by wxString:
53#if wxUSE_UNICODE_UTF8
54 #define wxStringMemcpy memcpy
55 #define wxStringMemcmp memcmp
56 #define wxStringMemchr memchr
57 #define wxStringStrlen strlen
58#else
59 #define wxStringMemcpy wxTmemcpy
60 #define wxStringMemcmp wxTmemcmp
a7ea63e2
VS
61 #define wxStringMemchr wxTmemchr
62 #define wxStringStrlen wxStrlen
63#endif
8f93a29f 64
b96a56e6
VZ
65// define a function declared in wx/buffer.h here as we don't have buffer.cpp
66// and don't want to add it just because of this simple function
4e79262f
VZ
67namespace wxPrivate
68{
69
b96a56e6
VZ
70// wxXXXBuffer classes can be (implicitly) used during global statics
71// initialization so wrap the status UntypedBufferData variable in a function
72// to make it safe to access it even before all global statics are initialized
73UntypedBufferData *GetUntypedNullData()
74{
75 static UntypedBufferData s_untypedNullData(NULL, 0);
4e79262f 76
b96a56e6
VZ
77 return &s_untypedNullData;
78}
4e79262f
VZ
79
80} // namespace wxPrivate
e87b7833 81
a7ea63e2
VS
82// ---------------------------------------------------------------------------
83// static class variables definition
84// ---------------------------------------------------------------------------
e87b7833 85
a7ea63e2
VS
86//According to STL _must_ be a -1 size_t
87const size_t wxString::npos = (size_t) -1;
8f93a29f 88
68482dc5 89#if wxUSE_STRING_POS_CACHE
68482dc5 90
e810df36
VZ
91#ifdef wxHAS_COMPILER_TLS
92
93wxTLS_TYPE(wxString::Cache) wxString::ms_cache;
94
95#else // !wxHAS_COMPILER_TLS
96
ad8ae788
VZ
97struct wxStrCacheInitializer
98{
99 wxStrCacheInitializer()
100 {
101 // calling this function triggers s_cache initialization in it, and
102 // from now on it becomes safe to call from multiple threads
103 wxString::GetCache();
104 }
105};
106
e317bd3f
SC
107/*
108wxString::Cache& wxString::GetCache()
109{
110 static wxTLS_TYPE(Cache) s_cache;
111
112 return wxTLS_VALUE(s_cache);
113}
114*/
115
ad8ae788
VZ
116static wxStrCacheInitializer gs_stringCacheInit;
117
e810df36
VZ
118#endif // wxHAS_COMPILER_TLS/!wxHAS_COMPILER_TLS
119
68482dc5
VZ
120// gdb seems to be unable to display thread-local variables correctly, at least
121// not my 6.4.98 version under amd64, so provide this debugging helper to do it
4b6a582b 122#if wxDEBUG_LEVEL >= 2
68482dc5
VZ
123
124struct wxStrCacheDumper
125{
126 static void ShowAll()
127 {
128 puts("*** wxString cache dump:");
129 for ( unsigned n = 0; n < wxString::Cache::SIZE; n++ )
130 {
131 const wxString::Cache::Element&
8b73c531 132 c = wxString::GetCacheBegin()[n];
68482dc5
VZ
133
134 printf("\t%u%s\t%p: pos=(%lu, %lu), len=%ld\n",
135 n,
8b73c531 136 n == wxString::LastUsedCacheElement() ? " [*]" : "",
68482dc5
VZ
137 c.str,
138 (unsigned long)c.pos,
139 (unsigned long)c.impl,
140 (long)c.len);
141 }
142 }
143};
144
145void wxDumpStrCache() { wxStrCacheDumper::ShowAll(); }
146
4b6a582b 147#endif // wxDEBUG_LEVEL >= 2
68482dc5
VZ
148
149#ifdef wxPROFILE_STRING_CACHE
150
151wxString::CacheStats wxString::ms_cacheStats;
152
8c3b65d9 153struct wxStrCacheStatsDumper
68482dc5 154{
8c3b65d9 155 ~wxStrCacheStatsDumper()
68482dc5
VZ
156 {
157 const wxString::CacheStats& stats = wxString::ms_cacheStats;
158
159 if ( stats.postot )
160 {
161 puts("*** wxString cache statistics:");
162 printf("\tTotal non-trivial calls to PosToImpl(): %u\n",
163 stats.postot);
164 printf("\tHits %u (of which %u not used) or %.2f%%\n",
165 stats.poshits,
166 stats.mishits,
167 100.*float(stats.poshits - stats.mishits)/stats.postot);
168 printf("\tAverage position requested: %.2f\n",
169 float(stats.sumpos) / stats.postot);
170 printf("\tAverage offset after cached hint: %.2f\n",
171 float(stats.sumofs) / stats.postot);
172 }
173
174 if ( stats.lentot )
175 {
176 printf("\tNumber of calls to length(): %u, hits=%.2f%%\n",
177 stats.lentot, 100.*float(stats.lenhits)/stats.lentot);
178 }
179 }
8c3b65d9 180};
68482dc5 181
8c3b65d9 182static wxStrCacheStatsDumper s_showCacheStats;
68482dc5
VZ
183
184#endif // wxPROFILE_STRING_CACHE
185
186#endif // wxUSE_STRING_POS_CACHE
187
a7ea63e2
VS
188// ----------------------------------------------------------------------------
189// global functions
190// ----------------------------------------------------------------------------
e87b7833 191
a7ea63e2 192#if wxUSE_STD_IOSTREAM
8f93a29f 193
a7ea63e2 194#include <iostream>
8f93a29f 195
a7ea63e2 196wxSTD ostream& operator<<(wxSTD ostream& os, const wxCStrData& str)
8f93a29f 197{
7a906e1a 198#if wxUSE_UNICODE && !wxUSE_UNICODE_UTF8
de4983f3 199 const wxScopedCharBuffer buf(str.AsCharBuf());
ddf01bdb
VZ
200 if ( !buf )
201 os.clear(wxSTD ios_base::failbit);
202 else
203 os << buf.data();
204
205 return os;
a7ea63e2 206#else
7a906e1a 207 return os << str.AsInternal();
a7ea63e2 208#endif
8f93a29f
VS
209}
210
04abe4bc
VS
211wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
212{
213 return os << str.c_str();
214}
215
de4983f3 216wxSTD ostream& operator<<(wxSTD ostream& os, const wxScopedCharBuffer& str)
04abe4bc
VS
217{
218 return os << str.data();
219}
220
221#ifndef __BORLANDC__
de4983f3 222wxSTD ostream& operator<<(wxSTD ostream& os, const wxScopedWCharBuffer& str)
04abe4bc
VS
223{
224 return os << str.data();
225}
226#endif
227
6a6ea041 228#if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
6b61b594
VZ
229
230wxSTD wostream& operator<<(wxSTD wostream& wos, const wxString& str)
231{
232 return wos << str.wc_str();
233}
234
235wxSTD wostream& operator<<(wxSTD wostream& wos, const wxCStrData& str)
236{
237 return wos << str.AsWChar();
238}
239
de4983f3 240wxSTD wostream& operator<<(wxSTD wostream& wos, const wxScopedWCharBuffer& str)
6b61b594
VZ
241{
242 return wos << str.data();
243}
244
6a6ea041 245#endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
6b61b594 246
a7ea63e2 247#endif // wxUSE_STD_IOSTREAM
e87b7833 248
81727065
VS
249// ===========================================================================
250// wxString class core
251// ===========================================================================
252
253#if wxUSE_UNICODE_UTF8
254
81727065
VS
255void wxString::PosLenToImpl(size_t pos, size_t len,
256 size_t *implPos, size_t *implLen) const
257{
258 if ( pos == npos )
68482dc5 259 {
81727065 260 *implPos = npos;
68482dc5
VZ
261 }
262 else // have valid start position
81727065 263 {
68482dc5
VZ
264 const const_iterator b = GetIterForNthChar(pos);
265 *implPos = wxStringImpl::const_iterator(b.impl()) - m_impl.begin();
81727065 266 if ( len == npos )
68482dc5 267 {
81727065 268 *implLen = npos;
68482dc5
VZ
269 }
270 else // have valid length too
81727065 271 {
68482dc5
VZ
272 // we need to handle the case of length specifying a substring
273 // going beyond the end of the string, just as std::string does
274 const const_iterator e(end());
275 const_iterator i(b);
276 while ( len && i <= e )
277 {
278 ++i;
279 --len;
280 }
281
282 *implLen = i.impl() - b.impl();
81727065
VS
283 }
284 }
285}
286
287#endif // wxUSE_UNICODE_UTF8
288
11aac4ba
VS
289// ----------------------------------------------------------------------------
290// wxCStrData converted strings caching
291// ----------------------------------------------------------------------------
292
132276cf
VS
293// FIXME-UTF8: temporarily disabled because it doesn't work with global
294// string objects; re-enable after fixing this bug and benchmarking
295// performance to see if using a hash is a good idea at all
296#if 0
297
11aac4ba
VS
298// For backward compatibility reasons, it must be possible to assign the value
299// returned by wxString::c_str() to a char* or wchar_t* variable and work with
300// it. Returning wxCharBuffer from (const char*)c_str() wouldn't do the trick,
301// because the memory would be freed immediately, but it has to be valid as long
302// as the string is not modified, so that code like this still works:
303//
304// const wxChar *s = str.c_str();
305// while ( s ) { ... }
306
307// FIXME-UTF8: not thread safe!
308// FIXME-UTF8: we currently clear the cached conversion only when the string is
309// destroyed, but we should do it when the string is modified, to
310// keep memory usage down
311// FIXME-UTF8: we do the conversion every time As[W]Char() is called, but if we
312// invalidated the cache on every change, we could keep the previous
313// conversion
314// FIXME-UTF8: add tracing of usage of these two methods - new code is supposed
315// to use mb_str() or wc_str() instead of (const [w]char*)c_str()
316
317template<typename T>
318static inline void DeleteStringFromConversionCache(T& hash, const wxString *s)
319{
6c4ebcda 320 typename T::iterator i = hash.find(wxConstCast(s, wxString));
11aac4ba
VS
321 if ( i != hash.end() )
322 {
323 free(i->second);
324 hash.erase(i);
325 }
326}
327
328#if wxUSE_UNICODE
6c4ebcda
VS
329// NB: non-STL implementation doesn't compile with "const wxString*" key type,
330// so we have to use wxString* here and const-cast when used
11aac4ba
VS
331WX_DECLARE_HASH_MAP(wxString*, char*, wxPointerHash, wxPointerEqual,
332 wxStringCharConversionCache);
333static wxStringCharConversionCache gs_stringsCharCache;
334
335const char* wxCStrData::AsChar() const
336{
337 // remove previously cache value, if any (see FIXMEs above):
338 DeleteStringFromConversionCache(gs_stringsCharCache, m_str);
339
340 // convert the string and keep it:
6c4ebcda
VS
341 const char *s = gs_stringsCharCache[wxConstCast(m_str, wxString)] =
342 m_str->mb_str().release();
11aac4ba
VS
343
344 return s + m_offset;
345}
346#endif // wxUSE_UNICODE
347
348#if !wxUSE_UNICODE_WCHAR
349WX_DECLARE_HASH_MAP(wxString*, wchar_t*, wxPointerHash, wxPointerEqual,
350 wxStringWCharConversionCache);
351static wxStringWCharConversionCache gs_stringsWCharCache;
352
353const wchar_t* wxCStrData::AsWChar() const
354{
355 // remove previously cache value, if any (see FIXMEs above):
356 DeleteStringFromConversionCache(gs_stringsWCharCache, m_str);
357
358 // convert the string and keep it:
6c4ebcda
VS
359 const wchar_t *s = gs_stringsWCharCache[wxConstCast(m_str, wxString)] =
360 m_str->wc_str().release();
11aac4ba
VS
361
362 return s + m_offset;
363}
364#endif // !wxUSE_UNICODE_WCHAR
365
11aac4ba
VS
366wxString::~wxString()
367{
368#if wxUSE_UNICODE
369 // FIXME-UTF8: do this only if locale is not UTF8 if wxUSE_UNICODE_UTF8
370 DeleteStringFromConversionCache(gs_stringsCharCache, this);
371#endif
372#if !wxUSE_UNICODE_WCHAR
373 DeleteStringFromConversionCache(gs_stringsWCharCache, this);
374#endif
375}
132276cf
VS
376#endif
377
132276cf
VS
378// ===========================================================================
379// wxString class core
380// ===========================================================================
381
382// ---------------------------------------------------------------------------
383// construction and conversion
384// ---------------------------------------------------------------------------
11aac4ba 385
81727065 386#if wxUSE_UNICODE_WCHAR
8f93a29f
VS
387/* static */
388wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength,
04abe4bc 389 const wxMBConv& conv)
8f93a29f
VS
390{
391 // anything to do?
392 if ( !psz || nLength == 0 )
de4983f3 393 return SubstrBufFromMB(wxWCharBuffer(L""), 0);
8f93a29f
VS
394
395 if ( nLength == npos )
396 nLength = wxNO_LEN;
397
398 size_t wcLen;
de4983f3 399 wxScopedWCharBuffer wcBuf(conv.cMB2WC(psz, nLength, &wcLen));
8f93a29f 400 if ( !wcLen )
de4983f3 401 return SubstrBufFromMB(wxWCharBuffer(L""), 0);
8f93a29f
VS
402 else
403 return SubstrBufFromMB(wcBuf, wcLen);
404}
81727065
VS
405#endif // wxUSE_UNICODE_WCHAR
406
407#if wxUSE_UNICODE_UTF8
408/* static */
409wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength,
410 const wxMBConv& conv)
411{
81727065
VS
412 // anything to do?
413 if ( !psz || nLength == 0 )
de4983f3 414 return SubstrBufFromMB(wxCharBuffer(""), 0);
81727065 415
111d9948
VS
416 // if psz is already in UTF-8, we don't have to do the roundtrip to
417 // wchar_t* and back:
418 if ( conv.IsUTF8() )
419 {
420 // we need to validate the input because UTF8 iterators assume valid
421 // UTF-8 sequence and psz may be invalid:
422 if ( wxStringOperations::IsValidUtf8String(psz, nLength) )
423 {
9ef1ad0d
VZ
424 // we must pass the real string length to SubstrBufFromMB ctor
425 if ( nLength == npos )
426 nLength = psz ? strlen(psz) : 0;
38d26d60 427 return SubstrBufFromMB(wxScopedCharBuffer::CreateNonOwned(psz, nLength),
6df09f32 428 nLength);
111d9948
VS
429 }
430 // else: do the roundtrip through wchar_t*
431 }
432
81727065
VS
433 if ( nLength == npos )
434 nLength = wxNO_LEN;
435
436 // first convert to wide string:
437 size_t wcLen;
de4983f3 438 wxScopedWCharBuffer wcBuf(conv.cMB2WC(psz, nLength, &wcLen));
81727065 439 if ( !wcLen )
de4983f3 440 return SubstrBufFromMB(wxCharBuffer(""), 0);
81727065
VS
441
442 // and then to UTF-8:
4fdfe2f3 443 SubstrBufFromMB buf(ConvertStr(wcBuf, wcLen, wxMBConvStrictUTF8()));
81727065 444 // widechar -> UTF-8 conversion isn't supposed to ever fail:
9a83f860 445 wxASSERT_MSG( buf.data, wxT("conversion to UTF-8 failed") );
81727065
VS
446
447 return buf;
448}
449#endif // wxUSE_UNICODE_UTF8
450
451#if wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE
8f93a29f
VS
452/* static */
453wxString::SubstrBufFromWC wxString::ConvertStr(const wchar_t *pwz, size_t nLength,
04abe4bc 454 const wxMBConv& conv)
8f93a29f
VS
455{
456 // anything to do?
457 if ( !pwz || nLength == 0 )
de4983f3 458 return SubstrBufFromWC(wxCharBuffer(""), 0);
8f93a29f
VS
459
460 if ( nLength == npos )
461 nLength = wxNO_LEN;
462
463 size_t mbLen;
de4983f3 464 wxScopedCharBuffer mbBuf(conv.cWC2MB(pwz, nLength, &mbLen));
8f93a29f 465 if ( !mbLen )
de4983f3 466 return SubstrBufFromWC(wxCharBuffer(""), 0);
8f93a29f
VS
467 else
468 return SubstrBufFromWC(mbBuf, mbLen);
469}
81727065 470#endif // wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE
8f93a29f 471
f54cb154
VZ
472// This std::string::c_str()-like method returns a wide char pointer to string
473// contents. In wxUSE_UNICODE_WCHAR case it is trivial as it can simply return
474// a pointer to the internal representation. Otherwise a conversion is required
475// and it returns a temporary buffer.
476//
477// However for compatibility with c_str() and to avoid breaking existing code
478// doing
479//
480// for ( const wchar_t *p = s.wc_str(); *p; p++ )
481// ... use *p...
482//
483// we actually need to ensure that the returned buffer is _not_ temporary and
484// so we use wxString::m_convertedToWChar to store the returned data
485#if !wxUSE_UNICODE_WCHAR
8f93a29f 486
f54cb154 487const wchar_t *wxString::AsWChar(const wxMBConv& conv) const
265d5cce 488{
f54cb154
VZ
489 const char * const strMB = m_impl.c_str();
490 const size_t lenMB = m_impl.length();
491
492 // find out the size of the buffer needed
493 const size_t lenWC = conv.ToWChar(NULL, 0, strMB, lenMB);
494 if ( lenWC == wxCONV_FAILED )
495 return NULL;
496
497 // keep the same buffer if the string size didn't change: this is not only
498 // an optimization but also ensure that code which modifies string
499 // character by character (without changing its length) can continue to use
500 // the pointer returned by a previous wc_str() call even after changing the
501 // string
502
503 // TODO-UTF8: we could check for ">" instead of "!=" here as this would
504 // allow to save on buffer reallocations but at the cost of
505 // consuming (even) more memory, we should benchmark this to
506 // determine if it's worth doing
507 if ( !m_convertedToWChar.m_str || lenWC != m_convertedToWChar.m_len )
508 {
509 if ( !const_cast<wxString *>(this)->m_convertedToWChar.Extend(lenWC) )
510 return NULL;
511 }
e87b7833 512
f54cb154
VZ
513 // finally do convert
514 m_convertedToWChar.m_str[lenWC] = L'\0';
515 if ( conv.ToWChar(m_convertedToWChar.m_str, lenWC,
516 strMB, lenMB) == wxCONV_FAILED )
517 return NULL;
e87b7833 518
f54cb154 519 return m_convertedToWChar.m_str;
81727065
VS
520}
521
f54cb154
VZ
522#endif // !wxUSE_UNICODE_WCHAR
523
524
525// Same thing for mb_str() which returns a normal char pointer to string
526// contents: this always requires converting it to the specified encoding in
527// non-ANSI build except if we need to convert to UTF-8 and this is what we
528// already use internally.
529#if wxUSE_UNICODE
530
531const char *wxString::AsChar(const wxMBConv& conv) const
81727065 532{
f54cb154 533#if wxUSE_UNICODE_UTF8
111d9948 534 if ( conv.IsUTF8() )
f54cb154 535 return m_impl.c_str();
111d9948 536
f54cb154
VZ
537 const wchar_t * const strWC = AsWChar(wxMBConvStrictUTF8());
538 const size_t lenWC = m_convertedToWChar.m_len;
539#else // wxUSE_UNICODE_WCHAR
540 const wchar_t * const strWC = m_impl.c_str();
541 const size_t lenWC = m_impl.length();
542#endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
81727065 543
f54cb154
VZ
544 const size_t lenMB = conv.FromWChar(NULL, 0, strWC, lenWC);
545 if ( lenMB == wxCONV_FAILED )
546 return NULL;
547
548 if ( !m_convertedToChar.m_str || lenMB != m_convertedToChar.m_len )
549 {
550 if ( !const_cast<wxString *>(this)->m_convertedToChar.Extend(lenMB) )
551 return NULL;
552 }
81727065 553
f54cb154
VZ
554 m_convertedToChar.m_str[lenMB] = '\0';
555 if ( conv.FromWChar(m_convertedToChar.m_str, lenMB,
556 strWC, lenWC) == wxCONV_FAILED )
557 return NULL;
eec47cc6 558
f54cb154 559 return m_convertedToChar.m_str;
265d5cce 560}
7663d0d4 561
f54cb154 562#endif // wxUSE_UNICODE
e87b7833
MB
563
564// shrink to minimal size (releasing extra memory)
565bool wxString::Shrink()
566{
567 wxString tmp(begin(), end());
568 swap(tmp);
569 return tmp.length() == length();
570}
571
d8a4b666 572// deprecated compatibility code:
a7ea63e2 573#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
c87a0bc8 574wxStringCharType *wxString::GetWriteBuf(size_t nLen)
d8a4b666
VS
575{
576 return DoGetWriteBuf(nLen);
577}
578
579void wxString::UngetWriteBuf()
580{
581 DoUngetWriteBuf();
582}
583
584void wxString::UngetWriteBuf(size_t nLen)
585{
586 DoUngetWriteBuf(nLen);
587}
a7ea63e2 588#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
e87b7833 589
d8a4b666 590
e87b7833
MB
591// ---------------------------------------------------------------------------
592// data access
593// ---------------------------------------------------------------------------
594
595// all functions are inline in string.h
596
597// ---------------------------------------------------------------------------
e8f59039 598// concatenation operators
e87b7833
MB
599// ---------------------------------------------------------------------------
600
c801d85f 601/*
c801d85f
KB
602 * concatenation functions come in 5 flavours:
603 * string + string
604 * char + string and string + char
605 * C str + string and string + C str
606 */
607
b1801e0e 608wxString operator+(const wxString& str1, const wxString& str2)
c801d85f 609{
992527a5 610#if !wxUSE_STL_BASED_WXSTRING
8f93a29f
VS
611 wxASSERT( str1.IsValid() );
612 wxASSERT( str2.IsValid() );
e87b7833 613#endif
097c080b 614
3458e408
WS
615 wxString s = str1;
616 s += str2;
3168a13f 617
3458e408 618 return s;
c801d85f
KB
619}
620
c9f78968 621wxString operator+(const wxString& str, wxUniChar ch)
c801d85f 622{
992527a5 623#if !wxUSE_STL_BASED_WXSTRING
8f93a29f 624 wxASSERT( str.IsValid() );
e87b7833 625#endif
3168a13f 626
3458e408
WS
627 wxString s = str;
628 s += ch;
097c080b 629
3458e408 630 return s;
c801d85f
KB
631}
632
c9f78968 633wxString operator+(wxUniChar ch, const wxString& str)
c801d85f 634{
992527a5 635#if !wxUSE_STL_BASED_WXSTRING
8f93a29f 636 wxASSERT( str.IsValid() );
e87b7833 637#endif
097c080b 638
3458e408
WS
639 wxString s = ch;
640 s += str;
3168a13f 641
3458e408 642 return s;
c801d85f
KB
643}
644
8f93a29f 645wxString operator+(const wxString& str, const char *psz)
c801d85f 646{
992527a5 647#if !wxUSE_STL_BASED_WXSTRING
8f93a29f 648 wxASSERT( str.IsValid() );
e87b7833 649#endif
097c080b 650
3458e408 651 wxString s;
8f93a29f 652 if ( !s.Alloc(strlen(psz) + str.length()) ) {
9a83f860 653 wxFAIL_MSG( wxT("out of memory in wxString::operator+") );
3458e408
WS
654 }
655 s += str;
656 s += psz;
3168a13f 657
3458e408 658 return s;
c801d85f
KB
659}
660
8f93a29f 661wxString operator+(const wxString& str, const wchar_t *pwz)
c801d85f 662{
992527a5 663#if !wxUSE_STL_BASED_WXSTRING
8f93a29f
VS
664 wxASSERT( str.IsValid() );
665#endif
666
667 wxString s;
668 if ( !s.Alloc(wxWcslen(pwz) + str.length()) ) {
9a83f860 669 wxFAIL_MSG( wxT("out of memory in wxString::operator+") );
8f93a29f
VS
670 }
671 s += str;
672 s += pwz;
673
674 return s;
675}
676
677wxString operator+(const char *psz, const wxString& str)
678{
a7ea63e2
VS
679#if !wxUSE_STL_BASED_WXSTRING
680 wxASSERT( str.IsValid() );
681#endif
682
683 wxString s;
684 if ( !s.Alloc(strlen(psz) + str.length()) ) {
9a83f860 685 wxFAIL_MSG( wxT("out of memory in wxString::operator+") );
a7ea63e2
VS
686 }
687 s = psz;
688 s += str;
689
690 return s;
691}
692
693wxString operator+(const wchar_t *pwz, const wxString& str)
694{
695#if !wxUSE_STL_BASED_WXSTRING
696 wxASSERT( str.IsValid() );
697#endif
698
699 wxString s;
700 if ( !s.Alloc(wxWcslen(pwz) + str.length()) ) {
9a83f860 701 wxFAIL_MSG( wxT("out of memory in wxString::operator+") );
a7ea63e2
VS
702 }
703 s = pwz;
704 s += str;
705
706 return s;
707}
708
709// ---------------------------------------------------------------------------
710// string comparison
711// ---------------------------------------------------------------------------
712
52de37c7
VS
713bool wxString::IsSameAs(wxUniChar c, bool compareWithCase) const
714{
715 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
716 : wxToupper(GetChar(0u)) == wxToupper(c));
717}
718
a7ea63e2
VS
719#ifdef HAVE_STD_STRING_COMPARE
720
721// NB: Comparison code (both if HAVE_STD_STRING_COMPARE and if not) works with
722// UTF-8 encoded strings too, thanks to UTF-8's design which allows us to
723// sort strings in characters code point order by sorting the byte sequence
724// in byte values order (i.e. what strcmp() and memcmp() do).
725
726int wxString::compare(const wxString& str) const
727{
728 return m_impl.compare(str.m_impl);
729}
730
731int wxString::compare(size_t nStart, size_t nLen,
732 const wxString& str) const
733{
734 size_t pos, len;
735 PosLenToImpl(nStart, nLen, &pos, &len);
736 return m_impl.compare(pos, len, str.m_impl);
737}
738
739int wxString::compare(size_t nStart, size_t nLen,
740 const wxString& str,
741 size_t nStart2, size_t nLen2) const
742{
743 size_t pos, len;
744 PosLenToImpl(nStart, nLen, &pos, &len);
745
746 size_t pos2, len2;
747 str.PosLenToImpl(nStart2, nLen2, &pos2, &len2);
748
749 return m_impl.compare(pos, len, str.m_impl, pos2, len2);
750}
751
752int wxString::compare(const char* sz) const
753{
754 return m_impl.compare(ImplStr(sz));
755}
756
757int wxString::compare(const wchar_t* sz) const
758{
759 return m_impl.compare(ImplStr(sz));
760}
761
762int wxString::compare(size_t nStart, size_t nLen,
763 const char* sz, size_t nCount) const
764{
765 size_t pos, len;
766 PosLenToImpl(nStart, nLen, &pos, &len);
767
768 SubstrBufFromMB str(ImplStr(sz, nCount));
769
770 return m_impl.compare(pos, len, str.data, str.len);
771}
772
773int wxString::compare(size_t nStart, size_t nLen,
774 const wchar_t* sz, size_t nCount) const
775{
776 size_t pos, len;
777 PosLenToImpl(nStart, nLen, &pos, &len);
778
779 SubstrBufFromWC str(ImplStr(sz, nCount));
780
781 return m_impl.compare(pos, len, str.data, str.len);
782}
783
784#else // !HAVE_STD_STRING_COMPARE
785
786static inline int wxDoCmp(const wxStringCharType* s1, size_t l1,
787 const wxStringCharType* s2, size_t l2)
788{
789 if( l1 == l2 )
790 return wxStringMemcmp(s1, s2, l1);
791 else if( l1 < l2 )
792 {
793 int ret = wxStringMemcmp(s1, s2, l1);
794 return ret == 0 ? -1 : ret;
795 }
796 else
797 {
798 int ret = wxStringMemcmp(s1, s2, l2);
799 return ret == 0 ? +1 : ret;
800 }
801}
802
803int wxString::compare(const wxString& str) const
804{
805 return ::wxDoCmp(m_impl.data(), m_impl.length(),
806 str.m_impl.data(), str.m_impl.length());
807}
808
809int wxString::compare(size_t nStart, size_t nLen,
810 const wxString& str) const
811{
812 wxASSERT(nStart <= length());
813 size_type strLen = length() - nStart;
814 nLen = strLen < nLen ? strLen : nLen;
815
816 size_t pos, len;
817 PosLenToImpl(nStart, nLen, &pos, &len);
818
819 return ::wxDoCmp(m_impl.data() + pos, len,
820 str.m_impl.data(), str.m_impl.length());
821}
822
823int wxString::compare(size_t nStart, size_t nLen,
824 const wxString& str,
825 size_t nStart2, size_t nLen2) const
826{
827 wxASSERT(nStart <= length());
828 wxASSERT(nStart2 <= str.length());
829 size_type strLen = length() - nStart,
830 strLen2 = str.length() - nStart2;
831 nLen = strLen < nLen ? strLen : nLen;
832 nLen2 = strLen2 < nLen2 ? strLen2 : nLen2;
833
834 size_t pos, len;
835 PosLenToImpl(nStart, nLen, &pos, &len);
836 size_t pos2, len2;
837 str.PosLenToImpl(nStart2, nLen2, &pos2, &len2);
838
839 return ::wxDoCmp(m_impl.data() + pos, len,
840 str.m_impl.data() + pos2, len2);
841}
842
843int wxString::compare(const char* sz) const
844{
845 SubstrBufFromMB str(ImplStr(sz, npos));
846 if ( str.len == npos )
847 str.len = wxStringStrlen(str.data);
848 return ::wxDoCmp(m_impl.data(), m_impl.length(), str.data, str.len);
849}
850
851int wxString::compare(const wchar_t* sz) const
852{
853 SubstrBufFromWC str(ImplStr(sz, npos));
854 if ( str.len == npos )
855 str.len = wxStringStrlen(str.data);
856 return ::wxDoCmp(m_impl.data(), m_impl.length(), str.data, str.len);
857}
858
859int wxString::compare(size_t nStart, size_t nLen,
860 const char* sz, size_t nCount) const
861{
862 wxASSERT(nStart <= length());
863 size_type strLen = length() - nStart;
864 nLen = strLen < nLen ? strLen : nLen;
097c080b 865
a7ea63e2
VS
866 size_t pos, len;
867 PosLenToImpl(nStart, nLen, &pos, &len);
3168a13f 868
a7ea63e2
VS
869 SubstrBufFromMB str(ImplStr(sz, nCount));
870 if ( str.len == npos )
871 str.len = wxStringStrlen(str.data);
872
873 return ::wxDoCmp(m_impl.data() + pos, len, str.data, str.len);
c801d85f
KB
874}
875
a7ea63e2
VS
876int wxString::compare(size_t nStart, size_t nLen,
877 const wchar_t* sz, size_t nCount) const
8f93a29f 878{
a7ea63e2
VS
879 wxASSERT(nStart <= length());
880 size_type strLen = length() - nStart;
881 nLen = strLen < nLen ? strLen : nLen;
8f93a29f 882
a7ea63e2
VS
883 size_t pos, len;
884 PosLenToImpl(nStart, nLen, &pos, &len);
8f93a29f 885
a7ea63e2
VS
886 SubstrBufFromWC str(ImplStr(sz, nCount));
887 if ( str.len == npos )
888 str.len = wxStringStrlen(str.data);
889
890 return ::wxDoCmp(m_impl.data() + pos, len, str.data, str.len);
8f93a29f
VS
891}
892
a7ea63e2
VS
893#endif // HAVE_STD_STRING_COMPARE/!HAVE_STD_STRING_COMPARE
894
895
8f93a29f
VS
896// ---------------------------------------------------------------------------
897// find_{first,last}_[not]_of functions
898// ---------------------------------------------------------------------------
899
900#if !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
c801d85f 901
8f93a29f
VS
902// NB: All these functions are implemented with the argument being wxChar*,
903// i.e. widechar string in any Unicode build, even though native string
904// representation is char* in the UTF-8 build. This is because we couldn't
905// use memchr() to determine if a character is in a set encoded as UTF-8.
906
907size_t wxString::find_first_of(const wxChar* sz, size_t nStart) const
dcb68102 908{
8f93a29f 909 return find_first_of(sz, nStart, wxStrlen(sz));
dcb68102
RN
910}
911
8f93a29f 912size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart) const
dcb68102 913{
8f93a29f 914 return find_first_not_of(sz, nStart, wxStrlen(sz));
dcb68102
RN
915}
916
8f93a29f 917size_t wxString::find_first_of(const wxChar* sz, size_t nStart, size_t n) const
dcb68102 918{
9a83f860 919 wxASSERT_MSG( nStart <= length(), wxT("invalid index") );
dcb68102 920
8f93a29f
VS
921 size_t idx = nStart;
922 for ( const_iterator i = begin() + nStart; i != end(); ++idx, ++i )
dcb68102 923 {
8f93a29f
VS
924 if ( wxTmemchr(sz, *i, n) )
925 return idx;
dcb68102 926 }
8f93a29f
VS
927
928 return npos;
929}
930
931size_t wxString::find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const
932{
9a83f860 933 wxASSERT_MSG( nStart <= length(), wxT("invalid index") );
8f93a29f
VS
934
935 size_t idx = nStart;
936 for ( const_iterator i = begin() + nStart; i != end(); ++idx, ++i )
dcb68102 937 {
8f93a29f
VS
938 if ( !wxTmemchr(sz, *i, n) )
939 return idx;
940 }
941
942 return npos;
943}
944
945
946size_t wxString::find_last_of(const wxChar* sz, size_t nStart) const
947{
948 return find_last_of(sz, nStart, wxStrlen(sz));
949}
950
951size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart) const
952{
953 return find_last_not_of(sz, nStart, wxStrlen(sz));
954}
955
956size_t wxString::find_last_of(const wxChar* sz, size_t nStart, size_t n) const
957{
958 size_t len = length();
959
960 if ( nStart == npos )
961 {
962 nStart = len - 1;
dcb68102 963 }
2c09fb3b 964 else
dcb68102 965 {
9a83f860 966 wxASSERT_MSG( nStart <= len, wxT("invalid index") );
dcb68102 967 }
8f93a29f
VS
968
969 size_t idx = nStart;
970 for ( const_reverse_iterator i = rbegin() + (len - nStart - 1);
971 i != rend(); --idx, ++i )
972 {
973 if ( wxTmemchr(sz, *i, n) )
974 return idx;
975 }
976
977 return npos;
dcb68102
RN
978}
979
8f93a29f 980size_t wxString::find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const
dcb68102 981{
8f93a29f
VS
982 size_t len = length();
983
984 if ( nStart == npos )
985 {
986 nStart = len - 1;
987 }
988 else
989 {
9a83f860 990 wxASSERT_MSG( nStart <= len, wxT("invalid index") );
8f93a29f
VS
991 }
992
993 size_t idx = nStart;
994 for ( const_reverse_iterator i = rbegin() + (len - nStart - 1);
995 i != rend(); --idx, ++i )
996 {
997 if ( !wxTmemchr(sz, *i, n) )
998 return idx;
999 }
1000
1001 return npos;
dcb68102
RN
1002}
1003
8f93a29f 1004size_t wxString::find_first_not_of(wxUniChar ch, size_t nStart) const
dcb68102 1005{
9a83f860 1006 wxASSERT_MSG( nStart <= length(), wxT("invalid index") );
8f93a29f
VS
1007
1008 size_t idx = nStart;
1009 for ( const_iterator i = begin() + nStart; i != end(); ++idx, ++i )
1010 {
1011 if ( *i != ch )
1012 return idx;
1013 }
1014
1015 return npos;
1016}
1017
1018size_t wxString::find_last_not_of(wxUniChar ch, size_t nStart) const
1019{
1020 size_t len = length();
1021
1022 if ( nStart == npos )
1023 {
1024 nStart = len - 1;
1025 }
1026 else
1027 {
9a83f860 1028 wxASSERT_MSG( nStart <= len, wxT("invalid index") );
8f93a29f
VS
1029 }
1030
1031 size_t idx = nStart;
1032 for ( const_reverse_iterator i = rbegin() + (len - nStart - 1);
1033 i != rend(); --idx, ++i )
1034 {
1035 if ( *i != ch )
1036 return idx;
1037 }
1038
1039 return npos;
1040}
1041
1042// the functions above were implemented for wchar_t* arguments in Unicode
1043// build and char* in ANSI build; below are implementations for the other
1044// version:
1045#if wxUSE_UNICODE
1046 #define wxOtherCharType char
1047 #define STRCONV (const wxChar*)wxConvLibc.cMB2WC
1048#else
1049 #define wxOtherCharType wchar_t
1050 #define STRCONV (const wxChar*)wxConvLibc.cWC2MB
1051#endif
1052
1053size_t wxString::find_first_of(const wxOtherCharType* sz, size_t nStart) const
1054 { return find_first_of(STRCONV(sz), nStart); }
1055
1056size_t wxString::find_first_of(const wxOtherCharType* sz, size_t nStart,
1057 size_t n) const
1058 { return find_first_of(STRCONV(sz, n, NULL), nStart, n); }
1059size_t wxString::find_last_of(const wxOtherCharType* sz, size_t nStart) const
1060 { return find_last_of(STRCONV(sz), nStart); }
1061size_t wxString::find_last_of(const wxOtherCharType* sz, size_t nStart,
1062 size_t n) const
1063 { return find_last_of(STRCONV(sz, n, NULL), nStart, n); }
1064size_t wxString::find_first_not_of(const wxOtherCharType* sz, size_t nStart) const
1065 { return find_first_not_of(STRCONV(sz), nStart); }
1066size_t wxString::find_first_not_of(const wxOtherCharType* sz, size_t nStart,
1067 size_t n) const
1068 { return find_first_not_of(STRCONV(sz, n, NULL), nStart, n); }
1069size_t wxString::find_last_not_of(const wxOtherCharType* sz, size_t nStart) const
1070 { return find_last_not_of(STRCONV(sz), nStart); }
1071size_t wxString::find_last_not_of(const wxOtherCharType* sz, size_t nStart,
1072 size_t n) const
1073 { return find_last_not_of(STRCONV(sz, n, NULL), nStart, n); }
1074
1075#undef wxOtherCharType
1076#undef STRCONV
1077
1078#endif // !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
1079
1080// ===========================================================================
1081// other common string functions
1082// ===========================================================================
1083
1084int wxString::CmpNoCase(const wxString& s) const
1085{
825d69c1 1086#if defined(__WXMSW__) && !wxUSE_UNICODE_UTF8
1935acd7
VZ
1087 // Prefer to use CompareString() if available as it's more efficient than
1088 // doing it manually or even using wxStricmp() (see #10375)
1089 //
1090 // Also note that not using NORM_STRINGSORT may result in not having a
1091 // strict weak ordering (e.g. s1 < s2 and s2 < s3 but s3 < s1) and so break
1092 // algorithms such as std::sort that rely on it. It's also more consistent
1093 // with the fall back version below.
1094 switch ( ::CompareString(LOCALE_USER_DEFAULT,
1095 NORM_IGNORECASE | SORT_STRINGSORT,
825d69c1
VZ
1096 m_impl.c_str(), m_impl.length(),
1097 s.m_impl.c_str(), s.m_impl.length()) )
1098 {
1099 case CSTR_LESS_THAN:
1100 return -1;
1101
1102 case CSTR_EQUAL:
1103 return 0;
1104
1105 case CSTR_GREATER_THAN:
1106 return 1;
8f93a29f 1107
825d69c1
VZ
1108 default:
1109 wxFAIL_MSG( "unexpected CompareString() return value" );
1110 // fall through
1111
1112 case 0:
1113 wxLogLastError("CompareString");
1114 // use generic code below
1115 }
1116#endif // __WXMSW__ && !wxUSE_UNICODE_UTF8
1117
1118 // do the comparison manually: notice that we can't use wxStricmp() as it
1119 // doesn't handle embedded NULs
1120
1121 // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
8f93a29f
VS
1122 const_iterator i1 = begin();
1123 const_iterator end1 = end();
1124 const_iterator i2 = s.begin();
1125 const_iterator end2 = s.end();
1126
0d8b0f94 1127 for ( ; i1 != end1 && i2 != end2; ++i1, ++i2 )
8f93a29f
VS
1128 {
1129 wxUniChar lower1 = (wxChar)wxTolower(*i1);
1130 wxUniChar lower2 = (wxChar)wxTolower(*i2);
1131 if ( lower1 != lower2 )
1132 return lower1 < lower2 ? -1 : 1;
1133 }
1134
1135 size_t len1 = length();
1136 size_t len2 = s.length();
dcb68102 1137
8f93a29f
VS
1138 if ( len1 < len2 )
1139 return -1;
1140 else if ( len1 > len2 )
1141 return 1;
1142 return 0;
dcb68102
RN
1143}
1144
1145
b1ac3b56 1146#if wxUSE_UNICODE
e015c2a3 1147
cf6bedce
SC
1148#ifdef __MWERKS__
1149#ifndef __SCHAR_MAX__
1150#define __SCHAR_MAX__ 127
1151#endif
1152#endif
1153
e6310bbc 1154wxString wxString::FromAscii(const char *ascii, size_t len)
b1ac3b56 1155{
e6310bbc 1156 if (!ascii || len == 0)
b1ac3b56 1157 return wxEmptyString;
e015c2a3 1158
b1ac3b56 1159 wxString res;
e015c2a3 1160
e6310bbc 1161 {
6798451b 1162 wxStringInternalBuffer buf(res, len);
602a857b 1163 wxStringCharType *dest = buf;
c1eada83 1164
602a857b
VS
1165 for ( ; len > 0; --len )
1166 {
1167 unsigned char c = (unsigned char)*ascii++;
1168 wxASSERT_MSG( c < 0x80,
9a83f860 1169 wxT("Non-ASCII value passed to FromAscii().") );
c1eada83 1170
602a857b
VS
1171 *dest++ = (wchar_t)c;
1172 }
e015c2a3
VZ
1173 }
1174
b1ac3b56
RR
1175 return res;
1176}
1177
e6310bbc
VS
1178wxString wxString::FromAscii(const char *ascii)
1179{
0081dd72 1180 return FromAscii(ascii, wxStrlen(ascii));
e6310bbc
VS
1181}
1182
c5288c5c 1183wxString wxString::FromAscii(char ascii)
2b5f62a0
VZ
1184{
1185 // What do we do with '\0' ?
1186
c1eada83 1187 unsigned char c = (unsigned char)ascii;
8760bc65 1188
9a83f860 1189 wxASSERT_MSG( c < 0x80, wxT("Non-ASCII value passed to FromAscii().") );
c1eada83
VS
1190
1191 // NB: the cast to wchar_t causes interpretation of 'ascii' as Latin1 value
1192 return wxString(wxUniChar((wchar_t)c));
2b5f62a0
VZ
1193}
1194
de4983f3 1195const wxScopedCharBuffer wxString::ToAscii() const
b1ac3b56 1196{
e015c2a3
VZ
1197 // this will allocate enough space for the terminating NUL too
1198 wxCharBuffer buffer(length());
6e394fc6 1199 char *dest = buffer.data();
e015c2a3 1200
c1eada83 1201 for ( const_iterator i = begin(); i != end(); ++i )
b1ac3b56 1202 {
c1eada83
VS
1203 wxUniChar c(*i);
1204 // FIXME-UTF8: unify substituted char ('_') with wxUniChar ('?')
1205 *dest++ = c.IsAscii() ? (char)c : '_';
e015c2a3
VZ
1206
1207 // the output string can't have embedded NULs anyhow, so we can safely
1208 // stop at first of them even if we do have any
c1eada83 1209 if ( !c )
e015c2a3 1210 break;
b1ac3b56 1211 }
e015c2a3 1212
b1ac3b56
RR
1213 return buffer;
1214}
e015c2a3 1215
c1eada83 1216#endif // wxUSE_UNICODE
b1ac3b56 1217
c801d85f 1218// extract string of length nCount starting at nFirst
c801d85f
KB
1219wxString wxString::Mid(size_t nFirst, size_t nCount) const
1220{
73f507f5 1221 size_t nLen = length();
30d9011f 1222
73f507f5
WS
1223 // default value of nCount is npos and means "till the end"
1224 if ( nCount == npos )
1225 {
1226 nCount = nLen - nFirst;
1227 }
30d9011f 1228
73f507f5
WS
1229 // out-of-bounds requests return sensible things
1230 if ( nFirst + nCount > nLen )
1231 {
1232 nCount = nLen - nFirst;
1233 }
c801d85f 1234
73f507f5
WS
1235 if ( nFirst > nLen )
1236 {
1237 // AllocCopy() will return empty string
1238 return wxEmptyString;
1239 }
c801d85f 1240
73f507f5
WS
1241 wxString dest(*this, nFirst, nCount);
1242 if ( dest.length() != nCount )
1243 {
9a83f860 1244 wxFAIL_MSG( wxT("out of memory in wxString::Mid") );
73f507f5 1245 }
30d9011f 1246
73f507f5 1247 return dest;
c801d85f
KB
1248}
1249
e87b7833 1250// check that the string starts with prefix and return the rest of the string
d775fa82 1251// in the provided pointer if it is not NULL, otherwise return false
c5e7a7d7 1252bool wxString::StartsWith(const wxString& prefix, wxString *rest) const
f6bcfd97 1253{
c5e7a7d7
VS
1254 if ( compare(0, prefix.length(), prefix) != 0 )
1255 return false;
f6bcfd97
BP
1256
1257 if ( rest )
1258 {
1259 // put the rest of the string into provided pointer
c5e7a7d7 1260 rest->assign(*this, prefix.length(), npos);
f6bcfd97
BP
1261 }
1262
d775fa82 1263 return true;
f6bcfd97
BP
1264}
1265
3affcd07
VZ
1266
1267// check that the string ends with suffix and return the rest of it in the
1268// provided pointer if it is not NULL, otherwise return false
c5e7a7d7 1269bool wxString::EndsWith(const wxString& suffix, wxString *rest) const
3affcd07 1270{
c5e7a7d7 1271 int start = length() - suffix.length();
81727065
VS
1272
1273 if ( start < 0 || compare(start, npos, suffix) != 0 )
3affcd07
VZ
1274 return false;
1275
1276 if ( rest )
1277 {
1278 // put the rest of the string into provided pointer
1279 rest->assign(*this, 0, start);
1280 }
1281
1282 return true;
1283}
1284
1285
c801d85f
KB
1286// extract nCount last (rightmost) characters
1287wxString wxString::Right(size_t nCount) const
1288{
e87b7833
MB
1289 if ( nCount > length() )
1290 nCount = length();
c801d85f 1291
e87b7833
MB
1292 wxString dest(*this, length() - nCount, nCount);
1293 if ( dest.length() != nCount ) {
9a83f860 1294 wxFAIL_MSG( wxT("out of memory in wxString::Right") );
b1801e0e 1295 }
c801d85f
KB
1296 return dest;
1297}
1298
7929902d 1299// get all characters after the last occurrence of ch
c801d85f 1300// (returns the whole string if ch not found)
c9f78968 1301wxString wxString::AfterLast(wxUniChar ch) const
c801d85f
KB
1302{
1303 wxString str;
d775fa82 1304 int iPos = Find(ch, true);
3c67202d 1305 if ( iPos == wxNOT_FOUND )
c801d85f
KB
1306 str = *this;
1307 else
c565abe1 1308 str.assign(*this, iPos + 1, npos);
c801d85f
KB
1309
1310 return str;
1311}
1312
1313// extract nCount first (leftmost) characters
1314wxString wxString::Left(size_t nCount) const
1315{
e87b7833
MB
1316 if ( nCount > length() )
1317 nCount = length();
c801d85f 1318
e87b7833
MB
1319 wxString dest(*this, 0, nCount);
1320 if ( dest.length() != nCount ) {
9a83f860 1321 wxFAIL_MSG( wxT("out of memory in wxString::Left") );
b1801e0e 1322 }
c801d85f
KB
1323 return dest;
1324}
1325
7929902d 1326// get all characters before the first occurrence of ch
c801d85f 1327// (returns the whole string if ch not found)
c9f78968 1328wxString wxString::BeforeFirst(wxUniChar ch) const
c801d85f 1329{
e87b7833 1330 int iPos = Find(ch);
c565abe1
VZ
1331 if ( iPos == wxNOT_FOUND )
1332 iPos = length();
e87b7833 1333 return wxString(*this, 0, iPos);
c801d85f
KB
1334}
1335
7929902d 1336/// get all characters before the last occurrence of ch
c801d85f 1337/// (returns empty string if ch not found)
c9f78968 1338wxString wxString::BeforeLast(wxUniChar ch) const
c801d85f
KB
1339{
1340 wxString str;
d775fa82 1341 int iPos = Find(ch, true);
3c67202d 1342 if ( iPos != wxNOT_FOUND && iPos != 0 )
d1c9bbf6 1343 str = wxString(c_str(), iPos);
c801d85f
KB
1344
1345 return str;
1346}
1347
7929902d 1348/// get all characters after the first occurrence of ch
c801d85f 1349/// (returns empty string if ch not found)
c9f78968 1350wxString wxString::AfterFirst(wxUniChar ch) const
c801d85f
KB
1351{
1352 wxString str;
1353 int iPos = Find(ch);
3c67202d 1354 if ( iPos != wxNOT_FOUND )
c565abe1 1355 str.assign(*this, iPos + 1, npos);
c801d85f
KB
1356
1357 return str;
1358}
1359
7929902d 1360// replace first (or all) occurrences of some substring with another one
8a540c88
VS
1361size_t wxString::Replace(const wxString& strOld,
1362 const wxString& strNew, bool bReplaceAll)
c801d85f 1363{
a8f1f1b2 1364 // if we tried to replace an empty string we'd enter an infinite loop below
8a540c88 1365 wxCHECK_MSG( !strOld.empty(), 0,
9a83f860 1366 wxT("wxString::Replace(): invalid parameter") );
a8f1f1b2 1367
68482dc5
VZ
1368 wxSTRING_INVALIDATE_CACHE();
1369
510bb748 1370 size_t uiCount = 0; // count of replacements made
c801d85f 1371
8a627032
VZ
1372 // optimize the special common case: replacement of one character by
1373 // another one (in UTF-8 case we can only do this for ASCII characters)
1374 //
1375 // benchmarks show that this special version is around 3 times faster
1376 // (depending on the proportion of matching characters and UTF-8/wchar_t
1377 // build)
1378 if ( strOld.m_impl.length() == 1 && strNew.m_impl.length() == 1 )
1379 {
1380 const wxStringCharType chOld = strOld.m_impl[0],
1381 chNew = strNew.m_impl[0];
1382
1383 // this loop is the simplified version of the one below
1384 for ( size_t pos = 0; ; )
1385 {
1386 pos = m_impl.find(chOld, pos);
1387 if ( pos == npos )
1388 break;
c801d85f 1389
8a627032
VZ
1390 m_impl[pos++] = chNew;
1391
1392 uiCount++;
1393
1394 if ( !bReplaceAll )
1395 break;
1396 }
1397 }
072682ce
VZ
1398 else if ( !bReplaceAll)
1399 {
1400 size_t pos = m_impl.find(strOld, 0);
1401 if ( pos != npos )
1402 {
1403 m_impl.replace(pos, strOld.m_impl.length(), strNew.m_impl);
1404 uiCount = 1;
1405 }
1406 }
1407 else // replace all occurrences
510bb748 1408 {
8a627032
VZ
1409 const size_t uiOldLen = strOld.m_impl.length();
1410 const size_t uiNewLen = strNew.m_impl.length();
1411
072682ce
VZ
1412 // first scan the string to find all positions at which the replacement
1413 // should be made
1414 wxVector<size_t> replacePositions;
1415
1416 size_t pos;
1417 for ( pos = m_impl.find(strOld.m_impl, 0);
1418 pos != npos;
1419 pos = m_impl.find(strOld.m_impl, pos + uiOldLen))
8a627032 1420 {
072682ce
VZ
1421 replacePositions.push_back(pos);
1422 ++uiCount;
1423 }
510bb748 1424
072682ce
VZ
1425 if ( !uiCount )
1426 return 0;
510bb748 1427
072682ce
VZ
1428 // allocate enough memory for the whole new string
1429 wxString tmp;
1430 tmp.m_impl.reserve(m_impl.length() + uiCount*(uiNewLen - uiOldLen));
ad5bb7d6 1431
072682ce
VZ
1432 // copy this string to tmp doing replacements on the fly
1433 size_t replNum = 0;
1434 for ( pos = 0; replNum < uiCount; replNum++ )
1435 {
1436 const size_t nextReplPos = replacePositions[replNum];
394b2900 1437
072682ce
VZ
1438 if ( pos != nextReplPos )
1439 {
1440 tmp.m_impl.append(m_impl, pos, nextReplPos - pos);
1441 }
1442
1443 tmp.m_impl.append(strNew.m_impl);
1444 pos = nextReplPos + uiOldLen;
8a627032 1445 }
072682ce
VZ
1446
1447 if ( pos != m_impl.length() )
1448 {
1449 // append the rest of the string unchanged
1450 tmp.m_impl.append(m_impl, pos, m_impl.length() - pos);
1451 }
1452
1453 swap(tmp);
c801d85f 1454 }
c801d85f 1455
510bb748 1456 return uiCount;
c801d85f
KB
1457}
1458
1459bool wxString::IsAscii() const
1460{
a4a44612
VS
1461 for ( const_iterator i = begin(); i != end(); ++i )
1462 {
1463 if ( !(*i).IsAscii() )
1464 return false;
1465 }
1466
1467 return true;
c801d85f 1468}
dd1eaa89 1469
c801d85f
KB
1470bool wxString::IsWord() const
1471{
a4a44612
VS
1472 for ( const_iterator i = begin(); i != end(); ++i )
1473 {
1474 if ( !wxIsalpha(*i) )
1475 return false;
1476 }
1477
1478 return true;
c801d85f 1479}
dd1eaa89 1480
c801d85f
KB
1481bool wxString::IsNumber() const
1482{
a4a44612
VS
1483 if ( empty() )
1484 return true;
1485
1486 const_iterator i = begin();
1487
9a83f860 1488 if ( *i == wxT('-') || *i == wxT('+') )
a4a44612
VS
1489 ++i;
1490
1491 for ( ; i != end(); ++i )
1492 {
1493 if ( !wxIsdigit(*i) )
1494 return false;
1495 }
1496
1497 return true;
c801d85f
KB
1498}
1499
c801d85f
KB
1500wxString wxString::Strip(stripType w) const
1501{
1502 wxString s = *this;
d775fa82
WS
1503 if ( w & leading ) s.Trim(false);
1504 if ( w & trailing ) s.Trim(true);
c801d85f
KB
1505 return s;
1506}
1507
c801d85f
KB
1508// ---------------------------------------------------------------------------
1509// case conversion
1510// ---------------------------------------------------------------------------
1511
1512wxString& wxString::MakeUpper()
1513{
e87b7833
MB
1514 for ( iterator it = begin(), en = end(); it != en; ++it )
1515 *it = (wxChar)wxToupper(*it);
c801d85f
KB
1516
1517 return *this;
1518}
1519
1520wxString& wxString::MakeLower()
1521{
e87b7833
MB
1522 for ( iterator it = begin(), en = end(); it != en; ++it )
1523 *it = (wxChar)wxTolower(*it);
c801d85f
KB
1524
1525 return *this;
1526}
1527
0c7db140
VZ
1528wxString& wxString::MakeCapitalized()
1529{
1530 const iterator en = end();
1531 iterator it = begin();
1532 if ( it != en )
1533 {
1534 *it = (wxChar)wxToupper(*it);
1535 for ( ++it; it != en; ++it )
1536 *it = (wxChar)wxTolower(*it);
1537 }
1538
1539 return *this;
1540}
1541
c801d85f
KB
1542// ---------------------------------------------------------------------------
1543// trimming and padding
1544// ---------------------------------------------------------------------------
1545
d775fa82 1546// some compilers (VC++ 6.0 not to name them) return true for a call to
9d55bfef 1547// isspace('\xEA') in the C locale which seems to be broken to me, but we have
c95e653c 1548// to live with this by checking that the character is a 7 bit one - even if
9d55bfef 1549// this may fail to detect some spaces (I don't know if Unicode doesn't have
576c608d
VZ
1550// space-like symbols somewhere except in the first 128 chars), it is arguably
1551// still better than trimming away accented letters
1552inline int wxSafeIsspace(wxChar ch) { return (ch < 127) && wxIsspace(ch); }
1553
c801d85f
KB
1554// trims spaces (in the sense of isspace) from left or right side
1555wxString& wxString::Trim(bool bFromRight)
1556{
3458e408
WS
1557 // first check if we're going to modify the string at all
1558 if ( !empty() &&
1559 (
1560 (bFromRight && wxSafeIsspace(GetChar(length() - 1))) ||
1561 (!bFromRight && wxSafeIsspace(GetChar(0u)))
1562 )
2c3b684c 1563 )
2c3b684c 1564 {
3458e408
WS
1565 if ( bFromRight )
1566 {
1567 // find last non-space character
d4d02bd5 1568 reverse_iterator psz = rbegin();
32c62191 1569 while ( (psz != rend()) && wxSafeIsspace(*psz) )
0d8b0f94 1570 ++psz;
92df97b8 1571
3458e408 1572 // truncate at trailing space start
d4d02bd5 1573 erase(psz.base(), end());
3458e408
WS
1574 }
1575 else
1576 {
1577 // find first non-space character
1578 iterator psz = begin();
32c62191 1579 while ( (psz != end()) && wxSafeIsspace(*psz) )
0d8b0f94 1580 ++psz;
2c3b684c 1581
3458e408
WS
1582 // fix up data and length
1583 erase(begin(), psz);
1584 }
2c3b684c 1585 }
c801d85f 1586
3458e408 1587 return *this;
c801d85f
KB
1588}
1589
1590// adds nCount characters chPad to the string from either side
c9f78968 1591wxString& wxString::Pad(size_t nCount, wxUniChar chPad, bool bFromRight)
c801d85f 1592{
3458e408 1593 wxString s(chPad, nCount);
c801d85f 1594
3458e408
WS
1595 if ( bFromRight )
1596 *this += s;
1597 else
1598 {
1599 s += *this;
1600 swap(s);
1601 }
c801d85f 1602
3458e408 1603 return *this;
c801d85f
KB
1604}
1605
1606// truncate the string
1607wxString& wxString::Truncate(size_t uiLen)
1608{
3458e408
WS
1609 if ( uiLen < length() )
1610 {
1611 erase(begin() + uiLen, end());
1612 }
1613 //else: nothing to do, string is already short enough
c801d85f 1614
3458e408 1615 return *this;
c801d85f
KB
1616}
1617
1618// ---------------------------------------------------------------------------
3c67202d 1619// finding (return wxNOT_FOUND if not found and index otherwise)
c801d85f
KB
1620// ---------------------------------------------------------------------------
1621
1622// find a character
c9f78968 1623int wxString::Find(wxUniChar ch, bool bFromEnd) const
c801d85f 1624{
3458e408 1625 size_type idx = bFromEnd ? find_last_of(ch) : find_first_of(ch);
c801d85f 1626
3458e408 1627 return (idx == npos) ? wxNOT_FOUND : (int)idx;
c801d85f
KB
1628}
1629
cd0b1709
VZ
1630// ----------------------------------------------------------------------------
1631// conversion to numbers
1632// ----------------------------------------------------------------------------
1633
52de37c7
VS
1634// The implementation of all the functions below is exactly the same so factor
1635// it out. Note that number extraction works correctly on UTF-8 strings, so
1636// we can use wxStringCharType and wx_str() for maximum efficiency.
122f3c5d 1637
92df97b8 1638#ifndef __WXWINCE__
941a4e62
VS
1639 #define DO_IF_NOT_WINCE(x) x
1640#else
1641 #define DO_IF_NOT_WINCE(x)
92df97b8 1642#endif
4ea4767e 1643
529e491c 1644#define WX_STRING_TO_X_TYPE_START \
9a83f860 1645 wxCHECK_MSG( pVal, false, wxT("NULL output pointer") ); \
941a4e62 1646 DO_IF_NOT_WINCE( errno = 0; ) \
941a4e62 1647 const wxStringCharType *start = wx_str(); \
529e491c
FM
1648 wxStringCharType *end;
1649
69d31e31
VZ
1650// notice that we return false without modifying the output parameter at all if
1651// nothing could be parsed but we do modify it and return false then if we did
1652// parse something successfully but not the entire string
529e491c 1653#define WX_STRING_TO_X_TYPE_END \
69d31e31 1654 if ( end == start DO_IF_NOT_WINCE(|| errno == ERANGE) ) \
c95e653c 1655 return false; \
529e491c 1656 *pVal = val; \
69d31e31 1657 return !*end;
cd0b1709 1658
c95e653c 1659bool wxString::ToLong(long *pVal, int base) const
cd0b1709 1660{
9a83f860 1661 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
529e491c
FM
1662
1663 WX_STRING_TO_X_TYPE_START
1664 long val = wxStrtol(start, &end, base);
1665 WX_STRING_TO_X_TYPE_END
619dcb09 1666}
cd0b1709 1667
c95e653c 1668bool wxString::ToULong(unsigned long *pVal, int base) const
619dcb09 1669{
9a83f860 1670 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
529e491c
FM
1671
1672 WX_STRING_TO_X_TYPE_START
1673 unsigned long val = wxStrtoul(start, &end, base);
1674 WX_STRING_TO_X_TYPE_END
cd0b1709
VZ
1675}
1676
c95e653c 1677bool wxString::ToLongLong(wxLongLong_t *pVal, int base) const
d6718dd1 1678{
9a83f860 1679 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
529e491c
FM
1680
1681 WX_STRING_TO_X_TYPE_START
1682 wxLongLong_t val = wxStrtoll(start, &end, base);
1683 WX_STRING_TO_X_TYPE_END
d6718dd1
VZ
1684}
1685
c95e653c 1686bool wxString::ToULongLong(wxULongLong_t *pVal, int base) const
d6718dd1 1687{
9a83f860 1688 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
529e491c
FM
1689
1690 WX_STRING_TO_X_TYPE_START
1691 wxULongLong_t val = wxStrtoull(start, &end, base);
1692 WX_STRING_TO_X_TYPE_END
d6718dd1
VZ
1693}
1694
c95e653c 1695bool wxString::ToDouble(double *pVal) const
cd0b1709 1696{
529e491c
FM
1697 WX_STRING_TO_X_TYPE_START
1698 double val = wxStrtod(start, &end);
1699 WX_STRING_TO_X_TYPE_END
1700}
cd0b1709 1701
529e491c 1702#if wxUSE_XLOCALE
e71e5b37 1703
529e491c
FM
1704bool wxString::ToCLong(long *pVal, int base) const
1705{
9a83f860 1706 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
cd0b1709 1707
529e491c 1708 WX_STRING_TO_X_TYPE_START
a51fdf81 1709#if (wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE) && defined(wxHAS_XLOCALE_SUPPORT)
529e491c
FM
1710 long val = wxStrtol_lA(start, &end, base, wxCLocale);
1711#else
1712 long val = wxStrtol_l(start, &end, base, wxCLocale);
1713#endif
1714 WX_STRING_TO_X_TYPE_END
1715}
c95e653c 1716
529e491c
FM
1717bool wxString::ToCULong(unsigned long *pVal, int base) const
1718{
9a83f860 1719 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
c95e653c 1720
529e491c 1721 WX_STRING_TO_X_TYPE_START
a51fdf81 1722#if (wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE) && defined(wxHAS_XLOCALE_SUPPORT)
529e491c
FM
1723 unsigned long val = wxStrtoul_lA(start, &end, base, wxCLocale);
1724#else
1725 unsigned long val = wxStrtoul_l(start, &end, base, wxCLocale);
1726#endif
1727 WX_STRING_TO_X_TYPE_END
cd0b1709
VZ
1728}
1729
529e491c
FM
1730bool wxString::ToCDouble(double *pVal) const
1731{
1732 WX_STRING_TO_X_TYPE_START
a51fdf81 1733#if (wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE) && defined(wxHAS_XLOCALE_SUPPORT)
529e491c
FM
1734 double val = wxStrtod_lA(start, &end, wxCLocale);
1735#else
1736 double val = wxStrtod_l(start, &end, wxCLocale);
1737#endif
1738 WX_STRING_TO_X_TYPE_END
1739}
1740
105993f7
VZ
1741#else // wxUSE_XLOCALE
1742
1743// Provide implementation of these functions even when wxUSE_XLOCALE is
1744// disabled, we still need them in wxWidgets internal code.
1745
1746// For integers we just assume the current locale uses the same number
1747// representation as the C one as there is nothing else we can do.
1748bool wxString::ToCLong(long *pVal, int base) const
1749{
1750 return ToLong(pVal, base);
1751}
1752
1753bool wxString::ToCULong(unsigned long *pVal, int base) const
1754{
1755 return ToULong(pVal, base);
1756}
1757
1758// For floating point numbers we have to handle the problem of the decimal
1759// point which is different in different locales.
1760bool wxString::ToCDouble(double *pVal) const
1761{
1762 // Create a copy of this string using the decimal point instead of whatever
1763 // separator the current locale uses.
1764#if wxUSE_INTL
1765 wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
1766 wxLOCALE_CAT_NUMBER);
1767 if ( sep == "." )
1768 {
1769 // We can avoid an unnecessary string copy in this case.
1770 return ToDouble(pVal);
1771 }
1772#else // !wxUSE_INTL
1773 // We don't know what the current separator is so it might even be a point
1774 // already, try to parse the string as a double:
1775 if ( ToDouble(pVal) )
1776 {
1777 // It must have been the point, nothing else to do.
1778 return true;
1779 }
1780
1781 // Try to guess the separator, using the most common alternative value.
1782 wxString sep(",");
1783#endif // wxUSE_INTL/!wxUSE_INTL
1784 wxString cstr(*this);
1785 cstr.Replace(".", sep);
1786
1787 return cstr.ToDouble(pVal);
1788}
1789
1790#endif // wxUSE_XLOCALE/!wxUSE_XLOCALE
529e491c 1791
951201d8
VZ
1792// ----------------------------------------------------------------------------
1793// number to string conversion
1794// ----------------------------------------------------------------------------
1795
1796/* static */
1797wxString wxString::FromCDouble(double val)
1798{
1799#if wxUSE_STD_IOSTREAM && wxUSE_STD_STRING
1800 // We assume that we can use the ostream and not wstream for numbers.
1801 wxSTD ostringstream os;
1802 os << val;
1803 return os.str();
1804#else // wxUSE_STD_IOSTREAM
1805 // Can't use iostream locale support, fall back to the manual method
1806 // instead.
1807 wxString s = FromDouble(val);
1808#if wxUSE_INTL
1809 wxString sep = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
1810 wxLOCALE_CAT_NUMBER);
1811#else // !wxUSE_INTL
1812 // As above, this is the most common alternative value. Notice that here it
1813 // doesn't matter if we guess wrongly and the current separator is already
1814 // ".": we'll just waste a call to Replace() in this case.
1815 wxString sep(",");
1816#endif // wxUSE_INTL/!wxUSE_INTL
1817
1818 s.Replace(sep, ".");
1819 return s;
1820#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
1821}
1822
c801d85f 1823// ---------------------------------------------------------------------------
9efd3367 1824// formatted output
c801d85f 1825// ---------------------------------------------------------------------------
378b05f7 1826
d1f6e2cf 1827#if !wxUSE_UTF8_LOCALE_ONLY
341e7d28 1828/* static */
c9f78968 1829#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1528e0b8 1830wxString wxStringPrintfMixinBase::DoFormatWchar(const wxChar *format, ...)
c9f78968 1831#else
d1f6e2cf 1832wxString wxString::DoFormatWchar(const wxChar *format, ...)
c9f78968 1833#endif
341e7d28 1834{
77c3e48a 1835 va_list argptr;
c9f78968 1836 va_start(argptr, format);
341e7d28 1837
77c3e48a 1838 wxString s;
c9f78968 1839 s.PrintfV(format, argptr);
341e7d28 1840
77c3e48a 1841 va_end(argptr);
341e7d28 1842
77c3e48a 1843 return s;
341e7d28 1844}
d1f6e2cf
VS
1845#endif // !wxUSE_UTF8_LOCALE_ONLY
1846
1847#if wxUSE_UNICODE_UTF8
1848/* static */
1849wxString wxString::DoFormatUtf8(const char *format, ...)
1850{
1851 va_list argptr;
1852 va_start(argptr, format);
1853
1854 wxString s;
1855 s.PrintfV(format, argptr);
1856
1857 va_end(argptr);
1858
1859 return s;
1860}
1861#endif // wxUSE_UNICODE_UTF8
341e7d28
VZ
1862
1863/* static */
c9f78968 1864wxString wxString::FormatV(const wxString& format, va_list argptr)
341e7d28
VZ
1865{
1866 wxString s;
c9f78968 1867 s.PrintfV(format, argptr);
341e7d28
VZ
1868 return s;
1869}
1870
d1f6e2cf 1871#if !wxUSE_UTF8_LOCALE_ONLY
c9f78968 1872#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
d1f6e2cf 1873int wxStringPrintfMixinBase::DoPrintfWchar(const wxChar *format, ...)
c9f78968 1874#else
d1f6e2cf 1875int wxString::DoPrintfWchar(const wxChar *format, ...)
c9f78968 1876#endif
c801d85f 1877{
ba9bbf13 1878 va_list argptr;
c9f78968 1879 va_start(argptr, format);
c801d85f 1880
c9f78968
VS
1881#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1882 // get a pointer to the wxString instance; we have to use dynamic_cast<>
1883 // because it's the only cast that works safely for downcasting when
1884 // multiple inheritance is used:
1885 wxString *str = static_cast<wxString*>(this);
1886#else
1887 wxString *str = this;
1888#endif
1889
1890 int iLen = str->PrintfV(format, argptr);
c801d85f 1891
ba9bbf13 1892 va_end(argptr);
c801d85f 1893
ba9bbf13 1894 return iLen;
c801d85f 1895}
d1f6e2cf
VS
1896#endif // !wxUSE_UTF8_LOCALE_ONLY
1897
1898#if wxUSE_UNICODE_UTF8
1899int wxString::DoPrintfUtf8(const char *format, ...)
1900{
1901 va_list argptr;
1902 va_start(argptr, format);
1903
1904 int iLen = PrintfV(format, argptr);
1905
1906 va_end(argptr);
1907
1908 return iLen;
1909}
1910#endif // wxUSE_UNICODE_UTF8
c801d85f 1911
67612ff1
DE
1912/*
1913 Uses wxVsnprintf and places the result into the this string.
1914
1915 In ANSI build, wxVsnprintf is effectively vsnprintf but in Unicode build
1916 it is vswprintf. Due to a discrepancy between vsnprintf and vswprintf in
1917 the ISO C99 (and thus SUSv3) standard the return value for the case of
1918 an undersized buffer is inconsistent. For conforming vsnprintf
1919 implementations the function must return the number of characters that
1920 would have been printed had the buffer been large enough. For conforming
1921 vswprintf implementations the function must return a negative number
1922 and set errno.
1923
1924 What vswprintf sets errno to is undefined but Darwin seems to set it to
a9a854d7
DE
1925 EOVERFLOW. The only expected errno are EILSEQ and EINVAL. Both of
1926 those are defined in the standard and backed up by several conformance
1927 statements. Note that ENOMEM mentioned in the manual page does not
1928 apply to swprintf, only wprintf and fwprintf.
1929
1930 Official manual page:
1931 http://www.opengroup.org/onlinepubs/009695399/functions/swprintf.html
1932
1933 Some conformance statements (AIX, Solaris):
1934 http://www.opengroup.org/csq/view.mhtml?RID=ibm%2FSD1%2F3
1935 http://www.theopengroup.org/csq/view.mhtml?norationale=1&noreferences=1&RID=Fujitsu%2FSE2%2F10
1936
1937 Since EILSEQ and EINVAL are rather common but EOVERFLOW is not and since
1938 EILSEQ and EINVAL are specifically defined to mean the error is other than
1939 an undersized buffer and no other errno are defined we treat those two
1940 as meaning hard errors and everything else gets the old behavior which
1941 is to keep looping and increasing buffer size until the function succeeds.
c95e653c 1942
67612ff1
DE
1943 In practice it's impossible to determine before compilation which behavior
1944 may be used. The vswprintf function may have vsnprintf-like behavior or
1945 vice-versa. Behavior detected on one release can theoretically change
1946 with an updated release. Not to mention that configure testing for it
1947 would require the test to be run on the host system, not the build system
1948 which makes cross compilation difficult. Therefore, we make no assumptions
1949 about behavior and try our best to handle every known case, including the
1950 case where wxVsnprintf returns a negative number and fails to set errno.
1951
1952 There is yet one more non-standard implementation and that is our own.
1953 Fortunately, that can be detected at compile-time.
1954
1955 On top of all that, ISO C99 explicitly defines snprintf to write a null
1956 character to the last position of the specified buffer. That would be at
1957 at the given buffer size minus 1. It is supposed to do this even if it
1958 turns out that the buffer is sized too small.
1959
1960 Darwin (tested on 10.5) follows the C99 behavior exactly.
1961
1962 Glibc 2.6 almost follows the C99 behavior except vswprintf never sets
1963 errno even when it fails. However, it only seems to ever fail due
1964 to an undersized buffer.
1965*/
2523e9b7
VS
1966#if wxUSE_UNICODE_UTF8
1967template<typename BufferType>
1968#else
1969// we only need one version in non-UTF8 builds and at least two Windows
1970// compilers have problems with this function template, so use just one
1971// normal function here
1972#endif
1973static int DoStringPrintfV(wxString& str,
1974 const wxString& format, va_list argptr)
c801d85f 1975{
f6f5941b 1976 int size = 1024;
e87b7833 1977
f6f5941b
VZ
1978 for ( ;; )
1979 {
2523e9b7
VS
1980#if wxUSE_UNICODE_UTF8
1981 BufferType tmp(str, size + 1);
1982 typename BufferType::CharType *buf = tmp;
1983#else
1984 wxStringBuffer tmp(str, size + 1);
de2589be 1985 wxChar *buf = tmp;
2523e9b7 1986#endif
2bb67b80 1987
ba9bbf13
WS
1988 if ( !buf )
1989 {
1990 // out of memory
a33c7045
VS
1991
1992 // in UTF-8 build, leaving uninitialized junk in the buffer
1993 // could result in invalid non-empty UTF-8 string, so just
1994 // reset the string to empty on failure:
1995 buf[0] = '\0';
ba9bbf13 1996 return -1;
e87b7833 1997 }
f6f5941b 1998
ba9bbf13
WS
1999 // wxVsnprintf() may modify the original arg pointer, so pass it
2000 // only a copy
2001 va_list argptrcopy;
2002 wxVaCopy(argptrcopy, argptr);
67612ff1
DE
2003
2004#ifndef __WXWINCE__
2005 // Set errno to 0 to make it determinate if wxVsnprintf fails to set it.
2006 errno = 0;
2007#endif
2523e9b7 2008 int len = wxVsnprintf(buf, size, format, argptrcopy);
ba9bbf13
WS
2009 va_end(argptrcopy);
2010
2011 // some implementations of vsnprintf() don't NUL terminate
2012 // the string if there is not enough space for it so
2013 // always do it manually
67612ff1
DE
2014 // FIXME: This really seems to be the wrong and would be an off-by-one
2015 // bug except the code above allocates an extra character.
9a83f860 2016 buf[size] = wxT('\0');
ba9bbf13 2017
caff62f2
VZ
2018 // vsnprintf() may return either -1 (traditional Unix behaviour) or the
2019 // total number of characters which would have been written if the
b1727cfe 2020 // buffer were large enough (newer standards such as Unix98)
de2589be
VZ
2021 if ( len < 0 )
2022 {
52de37c7
VS
2023 // NB: wxVsnprintf() may call either wxCRT_VsnprintfW or
2024 // wxCRT_VsnprintfA in UTF-8 build; wxUSE_WXVSNPRINTF
2025 // is true if *both* of them use our own implementation,
2026 // otherwise we can't be sure
f2bbe5b6
VZ
2027#if wxUSE_WXVSNPRINTF
2028 // we know that our own implementation of wxVsnprintf() returns -1
2029 // only for a format error - thus there's something wrong with
2030 // the user's format string
a33c7045 2031 buf[0] = '\0';
f2bbe5b6 2032 return -1;
52de37c7
VS
2033#else // possibly using system version
2034 // assume it only returns error if there is not enough space, but
2035 // as we don't know how much we need, double the current size of
2036 // the buffer
67612ff1 2037#ifndef __WXWINCE__
a9a854d7
DE
2038 if( (errno == EILSEQ) || (errno == EINVAL) )
2039 // If errno was set to one of the two well-known hard errors
2040 // then fail immediately to avoid an infinite loop.
2041 return -1;
2042 else
2043#endif // __WXWINCE__
67612ff1
DE
2044 // still not enough, as we don't know how much we need, double the
2045 // current size of the buffer
2046 size *= 2;
f2bbe5b6 2047#endif // wxUSE_WXVSNPRINTF/!wxUSE_WXVSNPRINTF
de2589be 2048 }
64f8f94c 2049 else if ( len >= size )
de2589be 2050 {
f2bbe5b6 2051#if wxUSE_WXVSNPRINTF
c95e653c 2052 // we know that our own implementation of wxVsnprintf() returns
f2bbe5b6
VZ
2053 // size+1 when there's not enough space but that's not the size
2054 // of the required buffer!
2055 size *= 2; // so we just double the current size of the buffer
2056#else
64f8f94c
VZ
2057 // some vsnprintf() implementations NUL-terminate the buffer and
2058 // some don't in len == size case, to be safe always add 1
67612ff1
DE
2059 // FIXME: I don't quite understand this comment. The vsnprintf
2060 // function is specifically defined to return the number of
2061 // characters printed not including the null terminator.
2062 // So OF COURSE you need to add 1 to get the right buffer size.
2063 // The following line is definitely correct, no question.
64f8f94c 2064 size = len + 1;
f2bbe5b6 2065#endif
de2589be
VZ
2066 }
2067 else // ok, there was enough space
f6f5941b 2068 {
f6f5941b
VZ
2069 break;
2070 }
f6f5941b
VZ
2071 }
2072
2073 // we could have overshot
2523e9b7
VS
2074 str.Shrink();
2075
2076 return str.length();
2077}
c801d85f 2078
2523e9b7
VS
2079int wxString::PrintfV(const wxString& format, va_list argptr)
2080{
2523e9b7
VS
2081#if wxUSE_UNICODE_UTF8
2082 #if wxUSE_STL_BASED_WXSTRING
2083 typedef wxStringTypeBuffer<char> Utf8Buffer;
2084 #else
6798451b 2085 typedef wxStringInternalBuffer Utf8Buffer;
2523e9b7
VS
2086 #endif
2087#endif
2088
2089#if wxUSE_UTF8_LOCALE_ONLY
c6255a6e 2090 return DoStringPrintfV<Utf8Buffer>(*this, format, argptr);
2523e9b7
VS
2091#else
2092 #if wxUSE_UNICODE_UTF8
2093 if ( wxLocaleIsUtf8 )
c6255a6e 2094 return DoStringPrintfV<Utf8Buffer>(*this, format, argptr);
2523e9b7
VS
2095 else
2096 // wxChar* version
c6255a6e 2097 return DoStringPrintfV<wxStringBuffer>(*this, format, argptr);
2523e9b7 2098 #else
c6255a6e 2099 return DoStringPrintfV(*this, format, argptr);
2523e9b7
VS
2100 #endif // UTF8/WCHAR
2101#endif
c801d85f
KB
2102}
2103
097c080b
VZ
2104// ----------------------------------------------------------------------------
2105// misc other operations
2106// ----------------------------------------------------------------------------
0c5d3e1c 2107
d775fa82 2108// returns true if the string matches the pattern which may contain '*' and
0c5d3e1c
VZ
2109// '?' metacharacters (as usual, '?' matches any character and '*' any number
2110// of them)
8a540c88 2111bool wxString::Matches(const wxString& mask) const
097c080b 2112{
d6044f58
VZ
2113 // I disable this code as it doesn't seem to be faster (in fact, it seems
2114 // to be much slower) than the old, hand-written code below and using it
2115 // here requires always linking with libregex even if the user code doesn't
2116 // use it
2117#if 0 // wxUSE_REGEX
706c2ac9
VZ
2118 // first translate the shell-like mask into a regex
2119 wxString pattern;
2120 pattern.reserve(wxStrlen(pszMask));
2121
9a83f860 2122 pattern += wxT('^');
706c2ac9
VZ
2123 while ( *pszMask )
2124 {
2125 switch ( *pszMask )
2126 {
9a83f860
VZ
2127 case wxT('?'):
2128 pattern += wxT('.');
706c2ac9
VZ
2129 break;
2130
9a83f860
VZ
2131 case wxT('*'):
2132 pattern += wxT(".*");
706c2ac9
VZ
2133 break;
2134
9a83f860
VZ
2135 case wxT('^'):
2136 case wxT('.'):
2137 case wxT('$'):
2138 case wxT('('):
2139 case wxT(')'):
2140 case wxT('|'):
2141 case wxT('+'):
2142 case wxT('\\'):
706c2ac9
VZ
2143 // these characters are special in a RE, quote them
2144 // (however note that we don't quote '[' and ']' to allow
2145 // using them for Unix shell like matching)
9a83f860 2146 pattern += wxT('\\');
706c2ac9
VZ
2147 // fall through
2148
2149 default:
2150 pattern += *pszMask;
2151 }
2152
2153 pszMask++;
2154 }
9a83f860 2155 pattern += wxT('$');
706c2ac9
VZ
2156
2157 // and now use it
2158 return wxRegEx(pattern, wxRE_NOSUB | wxRE_EXTENDED).Matches(c_str());
2159#else // !wxUSE_REGEX
9a4232dc
VZ
2160 // TODO: this is, of course, awfully inefficient...
2161
8a540c88
VS
2162 // FIXME-UTF8: implement using iterators, remove #if
2163#if wxUSE_UNICODE_UTF8
de4983f3
VS
2164 const wxScopedWCharBuffer maskBuf = mask.wc_str();
2165 const wxScopedWCharBuffer txtBuf = wc_str();
8a540c88
VS
2166 const wxChar *pszMask = maskBuf.data();
2167 const wxChar *pszTxt = txtBuf.data();
2168#else
2169 const wxChar *pszMask = mask.wx_str();
9a4232dc 2170 // the char currently being checked
8a540c88
VS
2171 const wxChar *pszTxt = wx_str();
2172#endif
9a4232dc
VZ
2173
2174 // the last location where '*' matched
2175 const wxChar *pszLastStarInText = NULL;
2176 const wxChar *pszLastStarInMask = NULL;
2177
2178match:
2179 for ( ; *pszMask != wxT('\0'); pszMask++, pszTxt++ ) {
097c080b 2180 switch ( *pszMask ) {
223d09f6
KB
2181 case wxT('?'):
2182 if ( *pszTxt == wxT('\0') )
d775fa82 2183 return false;
097c080b 2184
9a4232dc 2185 // pszTxt and pszMask will be incremented in the loop statement
0c5d3e1c 2186
097c080b
VZ
2187 break;
2188
223d09f6 2189 case wxT('*'):
097c080b 2190 {
9a4232dc
VZ
2191 // remember where we started to be able to backtrack later
2192 pszLastStarInText = pszTxt;
2193 pszLastStarInMask = pszMask;
2194
097c080b 2195 // ignore special chars immediately following this one
9a4232dc 2196 // (should this be an error?)
223d09f6 2197 while ( *pszMask == wxT('*') || *pszMask == wxT('?') )
097c080b
VZ
2198 pszMask++;
2199
2200 // if there is nothing more, match
223d09f6 2201 if ( *pszMask == wxT('\0') )
d775fa82 2202 return true;
097c080b
VZ
2203
2204 // are there any other metacharacters in the mask?
c86f1403 2205 size_t uiLenMask;
223d09f6 2206 const wxChar *pEndMask = wxStrpbrk(pszMask, wxT("*?"));
097c080b
VZ
2207
2208 if ( pEndMask != NULL ) {
2209 // we have to match the string between two metachars
2210 uiLenMask = pEndMask - pszMask;
2211 }
2212 else {
2213 // we have to match the remainder of the string
2bb67b80 2214 uiLenMask = wxStrlen(pszMask);
097c080b
VZ
2215 }
2216
2217 wxString strToMatch(pszMask, uiLenMask);
2bb67b80 2218 const wxChar* pMatch = wxStrstr(pszTxt, strToMatch);
097c080b 2219 if ( pMatch == NULL )
d775fa82 2220 return false;
097c080b
VZ
2221
2222 // -1 to compensate "++" in the loop
2223 pszTxt = pMatch + uiLenMask - 1;
2224 pszMask += uiLenMask - 1;
2225 }
2226 break;
2227
2228 default:
2229 if ( *pszMask != *pszTxt )
d775fa82 2230 return false;
097c080b
VZ
2231 break;
2232 }
2233 }
2234
2235 // match only if nothing left
9a4232dc 2236 if ( *pszTxt == wxT('\0') )
d775fa82 2237 return true;
9a4232dc
VZ
2238
2239 // if we failed to match, backtrack if we can
2240 if ( pszLastStarInText ) {
2241 pszTxt = pszLastStarInText + 1;
2242 pszMask = pszLastStarInMask;
2243
2244 pszLastStarInText = NULL;
2245
2246 // don't bother resetting pszLastStarInMask, it's unnecessary
2247
2248 goto match;
2249 }
2250
d775fa82 2251 return false;
706c2ac9 2252#endif // wxUSE_REGEX/!wxUSE_REGEX
097c080b
VZ
2253}
2254
1fc5dd6f 2255// Count the number of chars
c9f78968 2256int wxString::Freq(wxUniChar ch) const
1fc5dd6f
JS
2257{
2258 int count = 0;
8f93a29f 2259 for ( const_iterator i = begin(); i != end(); ++i )
1fc5dd6f 2260 {
8f93a29f 2261 if ( *i == ch )
1fc5dd6f
JS
2262 count ++;
2263 }
2264 return count;
2265}
4e79262f 2266