]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
re-enabled using std::string for wxStringImpl if wxUSE_STL again
[wxWidgets.git] / include / wx / string.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/string.h
3 // Purpose: wxString and wxArrayString classes
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>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 /*
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
16 */
17
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 #include "wx/defs.h" // everybody should include this
26
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
28 #include <ctype.h>
29 #endif
30
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
34 # include <stdio.h>
35 # include <string.h>
36 # include <stdarg.h>
37 # include <limits.h>
38 #else
39 # include <string.h>
40 # include <stdio.h>
41 # include <stdarg.h>
42 # include <limits.h>
43 # include <stdlib.h>
44 #endif
45
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
49
50 #ifdef __WXPALMOS__
51 #include <StringMgr.h>
52 #endif
53
54 #include "wx/wxchar.h" // for wxChar, wxStrlen() etc.
55 #include "wx/strvararg.h"
56 #include "wx/buffer.h" // for wxCharBuffer
57 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58
59 class WXDLLIMPEXP_BASE wxString;
60
61 // ---------------------------------------------------------------------------
62 // macros
63 // ---------------------------------------------------------------------------
64
65 // casts [unfortunately!] needed to call some broken functions which require
66 // "char *" instead of "const char *"
67 #define WXSTRINGCAST (wxChar *)(const wxChar *)
68 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
69 #define wxMBSTRINGCAST (char *)(const char *)
70 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
71
72 // implementation only
73 #define wxASSERT_VALID_INDEX(i) \
74 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
75
76 // ----------------------------------------------------------------------------
77 // constants
78 // ----------------------------------------------------------------------------
79
80 #if WXWIN_COMPATIBILITY_2_6
81
82 // deprecated in favour of wxString::npos, don't use in new code
83 //
84 // maximum possible length for a string means "take all string" everywhere
85 #define wxSTRING_MAXLEN wxString::npos
86
87 #endif // WXWIN_COMPATIBILITY_2_6
88
89 // ----------------------------------------------------------------------------
90 // global data
91 // ----------------------------------------------------------------------------
92
93 // global pointer to empty string
94 extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
95
96 // ---------------------------------------------------------------------------
97 // global functions complementing standard C string library replacements for
98 // strlen() and portable strcasecmp()
99 //---------------------------------------------------------------------------
100
101 #if WXWIN_COMPATIBILITY_2_8
102 // Use wxXXX() functions from wxcrt.h instead! These functions are for
103 // backwards compatibility only.
104
105 // checks whether the passed in pointer is NULL and if the string is empty
106 wxDEPRECATED( inline bool IsEmpty(const char *p) );
107 inline bool IsEmpty(const char *p) { return (!p || !*p); }
108
109 // safe version of strlen() (returns 0 if passed NULL pointer)
110 wxDEPRECATED( inline size_t Strlen(const char *psz) );
111 inline size_t Strlen(const char *psz)
112 { return psz ? strlen(psz) : 0; }
113
114 // portable strcasecmp/_stricmp
115 wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) );
116 inline int Stricmp(const char *psz1, const char *psz2)
117 {
118 #if defined(__VISUALC__) && defined(__WXWINCE__)
119 register char c1, c2;
120 do {
121 c1 = tolower(*psz1++);
122 c2 = tolower(*psz2++);
123 } while ( c1 && (c1 == c2) );
124
125 return c1 - c2;
126 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
127 return _stricmp(psz1, psz2);
128 #elif defined(__SC__)
129 return _stricmp(psz1, psz2);
130 #elif defined(__SALFORDC__)
131 return stricmp(psz1, psz2);
132 #elif defined(__BORLANDC__)
133 return stricmp(psz1, psz2);
134 #elif defined(__WATCOMC__)
135 return stricmp(psz1, psz2);
136 #elif defined(__DJGPP__)
137 return stricmp(psz1, psz2);
138 #elif defined(__EMX__)
139 return stricmp(psz1, psz2);
140 #elif defined(__WXPM__)
141 return stricmp(psz1, psz2);
142 #elif defined(__WXPALMOS__) || \
143 defined(HAVE_STRCASECMP_IN_STRING_H) || \
144 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
145 defined(__GNUWIN32__)
146 return strcasecmp(psz1, psz2);
147 #elif defined(__MWERKS__) && !defined(__INTEL__)
148 register char c1, c2;
149 do {
150 c1 = tolower(*psz1++);
151 c2 = tolower(*psz2++);
152 } while ( c1 && (c1 == c2) );
153
154 return c1 - c2;
155 #else
156 // almost all compilers/libraries provide this function (unfortunately under
157 // different names), that's why we don't implement our own which will surely
158 // be more efficient than this code (uncomment to use):
159 /*
160 register char c1, c2;
161 do {
162 c1 = tolower(*psz1++);
163 c2 = tolower(*psz2++);
164 } while ( c1 && (c1 == c2) );
165
166 return c1 - c2;
167 */
168
169 #error "Please define string case-insensitive compare for your OS/compiler"
170 #endif // OS/compiler
171 }
172
173 #endif // WXWIN_COMPATIBILITY_2_8
174
175 // ----------------------------------------------------------------------------
176 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
177 // ----------------------------------------------------------------------------
178
179 #define wxUSE_STL_BASED_WXSTRING wxUSE_STL
180
181 // in both cases we need to define wxStdString
182 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
183
184 #include "wx/beforestd.h"
185 #include <string>
186 #include "wx/afterstd.h"
187
188 #if wxUSE_UNICODE_WCHAR
189 #ifdef HAVE_STD_WSTRING
190 typedef std::wstring wxStdString;
191 #else
192 typedef std::basic_string<wxChar> wxStdString;
193 #endif
194 #else
195 typedef std::string wxStdString;
196 #endif
197
198 #endif // need <string>
199
200 #if wxUSE_STL_BASED_WXSTRING
201
202 // we always want ctor from std::string when using std::string internally
203 #undef wxUSE_STD_STRING
204 #define wxUSE_STD_STRING 1
205
206 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
207 (defined(_MSC_VER) && (_MSC_VER <= 1200))
208 #define wxSTRING_BASE_HASNT_CLEAR
209 #endif
210
211 typedef wxStdString wxStringImpl;
212 #else // if !wxUSE_STL_BASED_WXSTRING
213
214 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
215 #undef HAVE_STD_STRING_COMPARE
216
217 // ---------------------------------------------------------------------------
218 // string data prepended with some housekeeping info (used by wxString class),
219 // is never used directly (but had to be put here to allow inlining)
220 // ---------------------------------------------------------------------------
221
222 struct WXDLLIMPEXP_BASE wxStringData
223 {
224 int nRefs; // reference count
225 size_t nDataLength, // actual string length
226 nAllocLength; // allocated memory size
227
228 // mimics declaration 'wxChar data[nAllocLength]'
229 wxChar* data() const { return (wxChar*)(this + 1); }
230
231 // empty string has a special ref count so it's never deleted
232 bool IsEmpty() const { return (nRefs == -1); }
233 bool IsShared() const { return (nRefs > 1); }
234
235 // lock/unlock
236 void Lock() { if ( !IsEmpty() ) nRefs++; }
237
238 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
239 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
240 __forceinline
241 #endif
242 // VC++ free must take place in same DLL as allocation when using non dll
243 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
244 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
245 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
246 // we must not inline deallocation since allocation is not inlined
247 void Free();
248 #else
249 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
250 #endif
251
252 // if we had taken control over string memory (GetWriteBuf), it's
253 // intentionally put in invalid state
254 void Validate(bool b) { nRefs = (b ? 1 : 0); }
255 bool IsValid() const { return (nRefs != 0); }
256 };
257
258 class WXDLLIMPEXP_BASE wxStringImpl
259 {
260 public:
261 // an 'invalid' value for string index, moved to this place due to a CW bug
262 static const size_t npos;
263
264 protected:
265 // points to data preceded by wxStringData structure with ref count info
266 wxStringCharType *m_pchData;
267
268 // accessor to string data
269 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
270
271 // string (re)initialization functions
272 // initializes the string to the empty value (must be called only from
273 // ctors, use Reinit() otherwise)
274 void Init() { m_pchData = (wxStringCharType *)wxEmptyString; }
275 // initializes the string with (a part of) C-string
276 void InitWith(const wxStringCharType *psz, size_t nPos = 0, size_t nLen = npos);
277 // as Init, but also frees old data
278 void Reinit() { GetStringData()->Unlock(); Init(); }
279
280 // memory allocation
281 // allocates memory for string of length nLen
282 bool AllocBuffer(size_t nLen);
283 // effectively copies data to string
284 bool AssignCopy(size_t, const wxStringCharType *);
285
286 // append a (sub)string
287 bool ConcatSelf(size_t nLen, const wxStringCharType *src, size_t nMaxLen);
288 bool ConcatSelf(size_t nLen, const wxStringCharType *src)
289 { return ConcatSelf(nLen, src, nLen); }
290
291 // functions called before writing to the string: they copy it if there
292 // are other references to our data (should be the only owner when writing)
293 bool CopyBeforeWrite();
294 bool AllocBeforeWrite(size_t);
295
296 // compatibility with wxString
297 bool Alloc(size_t nLen);
298
299 public:
300 // standard types
301 typedef wxStringCharType value_type;
302 typedef wxStringCharType char_type;
303 typedef size_t size_type;
304 typedef value_type& reference;
305 typedef const value_type& const_reference;
306 typedef value_type* pointer;
307 typedef const value_type* const_pointer;
308 typedef value_type *iterator;
309 typedef const value_type *const_iterator;
310
311 // constructors and destructor
312 // ctor for an empty string
313 wxStringImpl() { Init(); }
314 // copy ctor
315 wxStringImpl(const wxStringImpl& stringSrc)
316 {
317 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
318 _T("did you forget to call UngetWriteBuf()?") );
319
320 if ( stringSrc.empty() ) {
321 // nothing to do for an empty string
322 Init();
323 }
324 else {
325 m_pchData = stringSrc.m_pchData; // share same data
326 GetStringData()->Lock(); // => one more copy
327 }
328 }
329 // string containing nRepeat copies of ch
330 wxStringImpl(size_type nRepeat, wxStringCharType ch);
331 // ctor takes first nLength characters from C string
332 // (default value of npos means take all the string)
333 wxStringImpl(const wxStringCharType *psz)
334 { InitWith(psz, 0, npos); }
335 wxStringImpl(const wxStringCharType *psz, size_t nLength)
336 { InitWith(psz, 0, nLength); }
337 // take nLen chars starting at nPos
338 wxStringImpl(const wxStringImpl& str, size_t nPos, size_t nLen)
339 {
340 wxASSERT_MSG( str.GetStringData()->IsValid(),
341 _T("did you forget to call UngetWriteBuf()?") );
342 Init();
343 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
344 InitWith(str.c_str(), nPos, nLen);
345 }
346 // take all characters from pStart to pEnd
347 wxStringImpl(const void *pStart, const void *pEnd);
348
349 // dtor is not virtual, this class must not be inherited from!
350 ~wxStringImpl()
351 {
352 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
353 //RN - according to the above VC++ does indeed inline this,
354 //even though it spits out two warnings
355 #pragma warning (disable:4714)
356 #endif
357
358 GetStringData()->Unlock();
359 }
360
361 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
362 //re-enable inlining warning
363 #pragma warning (default:4714)
364 #endif
365 // overloaded assignment
366 // from another wxString
367 wxStringImpl& operator=(const wxStringImpl& stringSrc);
368 // from a character
369 wxStringImpl& operator=(wxStringCharType ch);
370 // from a C string
371 wxStringImpl& operator=(const wxStringCharType *psz);
372
373 // return the length of the string
374 size_type length() const { return GetStringData()->nDataLength; }
375 // return the length of the string
376 size_type size() const { return length(); }
377 // return the maximum size of the string
378 size_type max_size() const { return npos; }
379 // resize the string, filling the space with c if c != 0
380 void resize(size_t nSize, wxStringCharType ch = '\0');
381 // delete the contents of the string
382 void clear() { erase(0, npos); }
383 // returns true if the string is empty
384 bool empty() const { return length() == 0; }
385 // inform string about planned change in size
386 void reserve(size_t sz) { Alloc(sz); }
387 size_type capacity() const { return GetStringData()->nAllocLength; }
388
389 // lib.string.access
390 // return the character at position n
391 value_type at(size_type n) const
392 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
393 // returns the writable character at position n
394 reference at(size_type n)
395 {
396 wxASSERT_VALID_INDEX( n );
397 CopyBeforeWrite();
398 return m_pchData[n];
399 } // FIXME-UTF8: not useful for us...?
400
401 // lib.string.modifiers
402 // append elements str[pos], ..., str[pos+n]
403 wxStringImpl& append(const wxStringImpl& str, size_t pos, size_t n)
404 {
405 wxASSERT(pos <= str.length());
406 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
407 return *this;
408 }
409 // append a string
410 wxStringImpl& append(const wxStringImpl& str)
411 { ConcatSelf(str.length(), str.c_str()); return *this; }
412 // append first n (or all if n == npos) characters of sz
413 wxStringImpl& append(const wxStringCharType *sz)
414 { ConcatSelf(wxStrlen(sz), sz); return *this; }
415 wxStringImpl& append(const wxStringCharType *sz, size_t n)
416 { ConcatSelf(n, sz); return *this; }
417 // append n copies of ch
418 wxStringImpl& append(size_t n, wxStringCharType ch);
419 // append from first to last
420 wxStringImpl& append(const_iterator first, const_iterator last)
421 { ConcatSelf(last - first, first); return *this; }
422
423 // same as `this_string = str'
424 wxStringImpl& assign(const wxStringImpl& str)
425 { return *this = str; }
426 // same as ` = str[pos..pos + n]
427 wxStringImpl& assign(const wxStringImpl& str, size_t pos, size_t n)
428 { clear(); return append(str, pos, n); }
429 // same as `= first n (or all if n == npos) characters of sz'
430 wxStringImpl& assign(const wxStringCharType *sz)
431 { clear(); return append(sz, wxStrlen(sz)); }
432 wxStringImpl& assign(const wxStringCharType *sz, size_t n)
433 { clear(); return append(sz, n); }
434 // same as `= n copies of ch'
435 wxStringImpl& assign(size_t n, wxStringCharType ch)
436 { clear(); return append(n, ch); }
437 // assign from first to last
438 wxStringImpl& assign(const_iterator first, const_iterator last)
439 { clear(); return append(first, last); }
440
441 // first valid index position
442 const_iterator begin() const { return m_pchData; }
443 iterator begin();
444 // position one after the last valid one
445 const_iterator end() const { return m_pchData + length(); }
446 iterator end();
447
448 // insert another string
449 wxStringImpl& insert(size_t nPos, const wxStringImpl& str)
450 {
451 wxASSERT( str.GetStringData()->IsValid() );
452 return insert(nPos, str.c_str(), str.length());
453 }
454 // insert n chars of str starting at nStart (in str)
455 wxStringImpl& insert(size_t nPos, const wxStringImpl& str, size_t nStart, size_t n)
456 {
457 wxASSERT( str.GetStringData()->IsValid() );
458 wxASSERT( nStart < str.length() );
459 size_t strLen = str.length() - nStart;
460 n = strLen < n ? strLen : n;
461 return insert(nPos, str.c_str() + nStart, n);
462 }
463 // insert first n (or all if n == npos) characters of sz
464 wxStringImpl& insert(size_t nPos, const wxStringCharType *sz, size_t n = npos);
465 // insert n copies of ch
466 wxStringImpl& insert(size_t nPos, size_t n, wxStringCharType ch)// FIXME-UTF8: tricky
467 { return insert(nPos, wxStringImpl(n, ch)); }
468 iterator insert(iterator it, wxStringCharType ch) // FIXME-UTF8: tricky
469 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
470 void insert(iterator it, const_iterator first, const_iterator last)
471 { insert(it - begin(), first, last - first); }
472 void insert(iterator it, size_type n, wxStringCharType ch)
473 { insert(it - begin(), n, ch); }
474
475 // delete characters from nStart to nStart + nLen
476 wxStringImpl& erase(size_type pos = 0, size_type n = npos);
477 iterator erase(iterator first, iterator last)
478 {
479 size_t idx = first - begin();
480 erase(idx, last - first);
481 return begin() + idx;
482 }
483 iterator erase(iterator first);
484
485 // explicit conversion to C string (use this with printf()!)
486 const wxStringCharType* c_str() const { return m_pchData; }
487 const wxStringCharType* data() const { return m_pchData; }
488
489 // replaces the substring of length nLen starting at nStart
490 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringCharType* sz);
491 // replaces the substring of length nLen starting at nStart
492 wxStringImpl& replace(size_t nStart, size_t nLen, const wxStringImpl& str)
493 { return replace(nStart, nLen, str.c_str()); }
494 // replaces the substring with nCount copies of ch
495 wxStringImpl& replace(size_t nStart, size_t nLen, size_t nCount, wxStringCharType ch);
496 // replaces a substring with another substring
497 wxStringImpl& replace(size_t nStart, size_t nLen,
498 const wxStringImpl& str, size_t nStart2, size_t nLen2);
499 // replaces the substring with first nCount chars of sz
500 wxStringImpl& replace(size_t nStart, size_t nLen,
501 const wxStringCharType* sz, size_t nCount);
502 wxStringImpl& replace(iterator first, iterator last, const_pointer s)
503 { return replace(first - begin(), last - first, s); }
504 wxStringImpl& replace(iterator first, iterator last, const_pointer s,
505 size_type n)
506 { return replace(first - begin(), last - first, s, n); }
507 wxStringImpl& replace(iterator first, iterator last, const wxStringImpl& s)
508 { return replace(first - begin(), last - first, s); }
509 wxStringImpl& replace(iterator first, iterator last, size_type n, wxStringCharType c)
510 { return replace(first - begin(), last - first, n, c); }
511 wxStringImpl& replace(iterator first, iterator last,
512 const_iterator first1, const_iterator last1)
513 { return replace(first - begin(), last - first, first1, last1 - first1); }
514
515 // swap two strings
516 void swap(wxStringImpl& str);
517
518 // All find() functions take the nStart argument which specifies the
519 // position to start the search on, the default value is 0. All functions
520 // return npos if there were no match.
521
522 // find a substring
523 size_t find(const wxStringImpl& str, size_t nStart = 0) const;
524
525 // find first n characters of sz
526 size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
527
528 // find the first occurence of character ch after nStart
529 size_t find(wxStringCharType ch, size_t nStart = 0) const;
530
531 // rfind() family is exactly like find() but works right to left
532
533 // as find, but from the end
534 size_t rfind(const wxStringImpl& str, size_t nStart = npos) const;
535
536 // as find, but from the end
537 size_t rfind(const wxStringCharType* sz, size_t nStart = npos,
538 size_t n = npos) const;
539 // as find, but from the end
540 size_t rfind(wxStringCharType ch, size_t nStart = npos) const;
541
542 size_type copy(wxStringCharType* s, size_type n, size_type pos = 0);
543
544 // substring extraction
545 wxStringImpl substr(size_t nStart = 0, size_t nLen = npos) const;
546
547 // string += string
548 wxStringImpl& operator+=(const wxStringImpl& s) { return append(s); }
549 // string += C string
550 wxStringImpl& operator+=(const wxStringCharType *psz) { return append(psz); }
551 // string += char
552 wxStringImpl& operator+=(wxStringCharType ch) { return append(1, ch); }
553
554 #if !wxUSE_UNICODE_UTF8
555 // helpers for wxStringBuffer and wxStringBufferLength
556 wxStringCharType *DoGetWriteBuf(size_t nLen);
557 void DoUngetWriteBuf();
558 void DoUngetWriteBuf(size_t nLen);
559 #endif
560
561 friend class WXDLLIMPEXP_BASE wxString;
562 };
563
564 #endif // !wxUSE_STL_BASED_WXSTRING
565
566 // don't pollute the library user's name space
567 #undef wxASSERT_VALID_INDEX
568
569 // wx/unichar.h needs wxStringImpl, so it's only possible to include it here
570 // (it includes string.h if not included from string.h):
571 #include "wx/unichar.h"
572
573 // ----------------------------------------------------------------------------
574 // wxCStrData
575 // ----------------------------------------------------------------------------
576
577 // Lightweight object returned by wxString::c_str() and implicitly convertible
578 // to either const char* or const wchar_t*.
579 class wxCStrData
580 {
581 private:
582 // Ctors; for internal use by wxString and wxCStrData only
583 wxCStrData(const wxString *str, size_t offset = 0, bool owned = false)
584 : m_str(str), m_offset(offset), m_owned(owned) {}
585
586 public:
587 // Ctor constructs the object from char literal; they are needed to make
588 // operator?: compile and they intentionally take char*, not const char*
589 wxCStrData(char *buf);
590 wxCStrData(wchar_t *buf);
591
592 ~wxCStrData();
593
594 // FIXME: we'll need convertors for both char* and wchar_t* and NONE
595 // for wxChar*, but that's after completing the transition to
596 // "smart" wxUniChar class. For now, just have conversion to
597 // char* in ANSI build and wchar_t in Unicode build.
598 #if wxUSE_UNICODE
599 const wchar_t* AsWChar() const;
600 operator const wchar_t*() const { return AsWChar(); }
601 #else
602 const char* AsChar() const;
603 const unsigned char* AsUnsignedChar() const
604 { return (const unsigned char *) AsChar(); }
605 operator const void*() const { return AsChar(); }
606 operator const char*() const { return AsChar(); }
607 operator const unsigned char*() const { return AsUnsignedChar(); }
608 #endif
609
610 wxString AsString() const;
611
612 // allow expressions like "c_str()[0]":
613 wxUniChar operator[](int n) const { return operator[](size_t(n)); }
614 wxUniChar operator[](size_t n) const;
615 wxUniChar operator[](long n) const { return operator[](size_t(n)); }
616 #ifndef wxSIZE_T_IS_UINT
617 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
618 #endif // size_t != unsigned int
619
620 // these operators are needed to emulate the pointer semantics of c_str():
621 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
622 // (we need both versions to resolve ambiguities):
623 wxCStrData operator+(int n) const
624 { return wxCStrData(m_str, m_offset + n, m_owned); }
625 wxCStrData operator+(long n) const
626 { return wxCStrData(m_str, m_offset + n, m_owned); }
627 wxCStrData operator+(size_t n) const
628 { return wxCStrData(m_str, m_offset + n, m_owned); }
629
630 // this operator is needed to make expressions like "*c_str()" or
631 // "*(c_str() + 2)" work
632 wxUniChar operator*() const;
633
634 private:
635 const wxString *m_str;
636 size_t m_offset;
637 bool m_owned;
638
639 friend class WXDLLIMPEXP_BASE wxString;
640 };
641
642 // ----------------------------------------------------------------------------
643 // wxStringPrintfMixin
644 // ---------------------------------------------------------------------------
645
646 // NB: VC6 has a bug that causes linker errors if you have template methods
647 // in a class using __declspec(dllimport). The solution is to split such
648 // class into two classes, one that contains the template methods and does
649 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
650 // (with DLL linkage).
651 //
652 // We only do this for VC6 here, because the code is less efficient
653 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
654 // cannot compile this code.
655
656 #if defined(__VISUALC__) && __VISUALC__ < 1300
657 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
658 #endif
659
660 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
661 // this class contains implementation of wxString's vararg methods, it's
662 // exported from wxBase DLL
663 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
664 {
665 protected:
666 wxStringPrintfMixinBase() {}
667
668 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
669 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
670 };
671
672 // this class contains template wrappers for wxString's vararg methods, it's
673 // intentionally *not* exported from the DLL in order to fix the VC6 bug
674 // described above
675 class wxStringPrintfMixin : public wxStringPrintfMixinBase
676 {
677 private:
678 // to further complicate things, we can't return wxString from
679 // wxStringPrintfMixin::Format() because wxString is not yet declared at
680 // this point; the solution is to use this fake type trait template - this
681 // way the compiler won't know the return type until Format() is used
682 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
683 template<typename T> struct StringReturnType
684 {
685 typedef wxString type;
686 };
687
688 public:
689 // these are duplicated wxString methods, they're also declared below
690 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
691
692 // int Printf(const wxChar *pszFormat, ...);
693 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
694 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
695 WX_DEFINE_VARARG_FUNC(static typename StringReturnType<T1>::type,
696 Format, DoFormat)
697 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
698 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
699
700 protected:
701 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
702 };
703 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
704
705
706 // ----------------------------------------------------------------------------
707 // wxString: string class trying to be compatible with std::string, MFC
708 // CString and wxWindows 1.x wxString all at once
709 // ---------------------------------------------------------------------------
710
711 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
712 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
713 // for dll-interface class 'wxString'" -- this is OK in our case
714 #pragma warning (disable:4275)
715 #endif
716
717 class WXDLLIMPEXP_BASE wxString
718 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
719 : public wxStringPrintfMixin
720 #endif
721 {
722 // NB: special care was taken in arranging the member functions in such order
723 // that all inline functions can be effectively inlined, verify that all
724 // performance critical functions are still inlined if you change order!
725 public:
726 // an 'invalid' value for string index, moved to this place due to a CW bug
727 static const size_t npos;
728
729 private:
730 // if we hadn't made these operators private, it would be possible to
731 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
732 // converted to char in C and we do have operator=(char)
733 //
734 // NB: we don't need other versions (short/long and unsigned) as attempt
735 // to assign another numeric type to wxString will now result in
736 // ambiguity between operator=(char) and operator=(int)
737 wxString& operator=(int);
738
739 // these methods are not implemented - there is _no_ conversion from int to
740 // string, you're doing something wrong if the compiler wants to call it!
741 //
742 // try `s << i' or `s.Printf("%d", i)' instead
743 wxString(int);
744
745
746 // buffer for holding temporary substring when using any of the methods
747 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
748 // FIXME-UTF8: This will need changes when UTF8 build is introduced
749 template<typename T>
750 struct SubstrBufFromType
751 {
752 T data;
753 size_t len;
754
755 SubstrBufFromType() {}
756 SubstrBufFromType(const T& data_, size_t len_)
757 : data(data_), len(len_) {}
758 };
759
760 #if wxUSE_UNICODE_UTF8
761 // FIXME-UTF8: this will have to use slightly different type
762 #elif wxUSE_UNICODE_WCHAR
763 typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
764 typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB;
765 #else
766 typedef SubstrBufFromType<const char*> SubstrBufFromMB;
767 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
768 #endif
769
770
771 // Functions implementing primitive operations on string data; wxString
772 // methods and iterators are implemented in terms of it. The differences
773 // between UTF-8 and wchar_t* representations of the string are mostly
774 // contained here.
775
776 #if wxUSE_UNICODE
777 // FIXME-UTF8: This will need changes when UTF8 build is introduced
778 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
779 const wxMBConv& conv);
780 #else
781 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
782 const wxMBConv& conv);
783 #endif
784
785 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
786 // returns C string encoded as the implementation expects:
787 #if wxUSE_UNICODE
788 static const wchar_t* ImplStr(const wchar_t* str)
789 { return str; }
790 static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
791 { return SubstrBufFromWC(str, n == npos ? wxWcslen(str) : n); }
792 static wxWCharBuffer ImplStr(const char* str)
793 { return ConvertStr(str, npos, wxConvLibc).data; }
794 static SubstrBufFromMB ImplStr(const char* str, size_t n)
795 { return ConvertStr(str, n, wxConvLibc); }
796 #else
797 static const char* ImplStr(const char* str)
798 { return str; }
799 static const SubstrBufFromMB ImplStr(const char* str, size_t n)
800 { return SubstrBufFromMB(str, n == npos ? wxStrlen(str) : n); }
801 static wxCharBuffer ImplStr(const wchar_t* str)
802 { return ConvertStr(str, npos, wxConvLibc).data; }
803 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
804 { return ConvertStr(str, n, wxConvLibc); }
805 #endif
806
807 // moves the iterator to the next Unicode character
808 static void IncIter(wxStringImpl::iterator& i) { ++i; }
809 static void IncIter(wxStringImpl::const_iterator& i) { ++i; }
810 // moves the iterator to the previous Unicode character
811 static void DecIter(wxStringImpl::iterator& i) { --i; }
812 static void DecIter(wxStringImpl::const_iterator& i) { --i; }
813 // moves the iterator by n Unicode characters
814 static wxStringImpl::iterator AddToIter(wxStringImpl::iterator i, int n)
815 { return i + n; }
816 static wxStringImpl::const_iterator AddToIter(wxStringImpl::const_iterator i, int n)
817 { return i + n; }
818 // returns distance of the two iterators in Unicode characters
819 static int DiffIters(wxStringImpl::iterator i1, wxStringImpl::iterator i2)
820 { return i1 - i2; }
821 static int DiffIters(wxStringImpl::const_iterator i1, wxStringImpl::const_iterator i2)
822 { return i1 - i2; }
823
824 // encodes the character to a form used to represent it in internal
825 // representation (returns a string in UTF8 version)
826 static wxChar EncodeChar(wxUniChar ch) { return (wxChar)ch; }
827
828 // translates position index in wxString to/from index in underlying
829 // wxStringImpl:
830 static size_t PosToImpl(size_t pos) { return pos; }
831 static void PosLenToImpl(size_t pos, size_t len,
832 size_t *implPos, size_t *implLen)
833 { *implPos = pos; *implLen = len; }
834 static size_t PosFromImpl(size_t pos) { return pos; }
835
836 #else // wxUSE_UNICODE_UTF8
837
838 typedef char Utf8CharBuffer[5];
839 static Utf8CharBuffer EncodeChar(wxUniChar ch);
840 // returns n copies of ch encoded in UTF-8 string
841 static wxCharBuffer EncodeNChars(size_t n, wxUniChar ch);
842
843 size_t PosToImpl(size_t pos) const
844 {
845 if ( pos == 0 || pos == npos )
846 return pos;
847 else
848 return wxStringImpl::const_iterator(begin() + pos) - m_impl.begin();
849 }
850
851 size_t PosFromImpl(size_t pos) const
852 {
853 if ( pos == 0 || pos == npos )
854 return pos;
855 else
856 return const_iterator(m_impl.begin() + pos) - begin();
857 }
858
859 // FIXME: return as-is without copying under UTF8 locale, return
860 // converted string under other locales - needs wxCharBuffer
861 // changes
862 static wxCharBuffer ImplStr(const char* str);
863
864 static wxCharBuffer ImplStr(const wchar_t* str)
865 { return wxConvUTF8.cWC2MB(str); }
866 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
867
868
869 public:
870 // constructors and destructor
871 // ctor for an empty string
872 wxString() {}
873 // copy ctor
874 wxString(const wxStringImpl& stringSrc) : m_impl(stringSrc) { }
875 wxString(const wxString& stringSrc) : m_impl(stringSrc) { }
876 // string containing nRepeat copies of ch
877 wxString(wxUniChar ch, size_t nRepeat = 1)
878 : m_impl(nRepeat, ch) { }
879 wxString(size_t nRepeat, wxUniChar ch)
880 : m_impl(nRepeat, ch) { }
881 wxString(wxUniCharRef ch, size_t nRepeat = 1)
882 : m_impl(nRepeat, ch) { }
883 wxString(size_t nRepeat, wxUniCharRef ch)
884 : m_impl(nRepeat, ch) { }
885 wxString(char ch, size_t nRepeat = 1)
886 : m_impl(nRepeat, ch) { }
887 wxString(size_t nRepeat, char ch)
888 : m_impl(nRepeat, ch) { }
889 wxString(wchar_t ch, size_t nRepeat = 1)
890 : m_impl(nRepeat, ch) { }
891 wxString(size_t nRepeat, wchar_t ch)
892 : m_impl(nRepeat, ch) { }
893 // ctor takes first nLength characters from C string
894 // (default value of npos means take all the string)
895 wxString(const wxChar *psz)
896 : m_impl(psz ? psz : wxT("")) { }
897 wxString(const wxChar *psz, size_t nLength)
898 : m_impl(psz, nLength) { }
899 wxString(const wxChar *psz,
900 const wxMBConv& WXUNUSED(conv),
901 size_t nLength = npos)
902 : m_impl(psz, nLength == npos ? wxStrlen(psz) : nLength) { }
903
904 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
905 // implicit conversions from std::string to wxString as this allows to use
906 // the same strings in non-GUI and GUI code, however we don't want to
907 // unconditionally add this ctor as it would make wx lib dependent on
908 // libstdc++ on some Linux versions which is bad, so instead we ask the
909 // client code to define this wxUSE_STD_STRING symbol if they need it
910 #if wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
911 wxString(const wxStdString& s)
912 : m_impl(s.c_str()) { } // FIXME-UTF8: this is broken for embedded 0s
913 #endif // wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
914
915 #if wxUSE_UNICODE
916 // from multibyte string
917 wxString(const char *psz,
918 const wxMBConv& conv = wxConvLibc,
919 size_t nLength = npos);
920 // from multibyte string for ANSI compatibility, with wxConvLibc
921 wxString(const char *psz, size_t nLength);
922 // from wxWCharBuffer (i.e. return from wxGetString)
923 wxString(const wxWCharBuffer& psz) : m_impl(psz.data()) { }
924 #else // ANSI
925 // from C string (for compilers using unsigned char)
926 wxString(const unsigned char* psz)
927 : m_impl((const char*)psz) { }
928 // from part of C string (for compilers using unsigned char)
929 wxString(const unsigned char* psz, size_t nLength)
930 : m_impl((const char*)psz, nLength) { }
931
932 #if wxUSE_WCHAR_T
933 // from wide (Unicode) string
934 wxString(const wchar_t *pwz,
935 const wxMBConv& conv = wxConvLibc,
936 size_t nLength = npos);
937 // from wide string for Unicode compatibility, with wxConvLibc
938 wxString(const wchar_t *pwz, size_t nLength);
939 #endif // !wxUSE_WCHAR_T
940
941 // from wxCharBuffer
942 wxString(const wxCharBuffer& psz)
943 : m_impl(psz) { }
944 #endif // Unicode/ANSI
945
946 wxString(const wxCStrData& cstr)
947 : m_impl(cstr.AsString().m_impl) { }
948
949 // as we provide both ctors with this signature for both char and unsigned
950 // char string, we need to provide one for wxCStrData to resolve ambiguity
951 wxString(const wxCStrData& cstr, size_t nLength)
952 { assign(cstr.AsString(), nLength); }
953
954 // and because wxString is convertible to wxCStrData and const wxChar *
955 // we also need to provide this one
956 wxString(const wxString& str, size_t nLength)
957 { assign(str, nLength); }
958
959 public:
960 // standard types
961 typedef wxUniChar value_type;
962 typedef wxUniChar char_type;
963 typedef wxUniCharRef reference;
964 typedef wxChar* pointer;
965 typedef const wxChar* const_pointer;
966
967 typedef size_t size_type;
968 typedef wxUniChar const_reference;
969
970 #if wxUSE_STL
971 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
972 #else
973 #define WX_STR_ITERATOR_TAG void /* dummy type */
974 #endif
975
976 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
977 reference_type, reference_ctor) \
978 private: \
979 typedef wxStringImpl::iterator_name underlying_iterator; \
980 public: \
981 typedef WX_STR_ITERATOR_TAG iterator_category; \
982 typedef wxUniChar value_type; \
983 typedef int difference_type; \
984 typedef reference_type reference; \
985 typedef pointer_type pointer; \
986 \
987 iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \
988 \
989 reference operator*() const { return reference_ctor; } \
990 reference operator[](size_t n) const { return *(*this + n); } \
991 \
992 iterator_name& operator++() \
993 { wxString::IncIter(m_cur); return *this; } \
994 iterator_name& operator--() \
995 { wxString::DecIter(m_cur); return *this; } \
996 iterator_name operator++(int) \
997 { \
998 iterator_name tmp = *this; \
999 wxString::IncIter(m_cur); \
1000 return tmp; \
1001 } \
1002 iterator_name operator--(int) \
1003 { \
1004 iterator_name tmp = *this; \
1005 wxString::DecIter(m_cur); \
1006 return tmp; \
1007 } \
1008 \
1009 iterator_name operator+(int n) const \
1010 { return iterator_name(wxString::AddToIter(m_cur, n)); } \
1011 iterator_name operator+(size_t n) const \
1012 { return iterator_name(wxString::AddToIter(m_cur, (int)n)); } \
1013 iterator_name operator-(int n) const \
1014 { return iterator_name(wxString::AddToIter(m_cur, -n)); } \
1015 iterator_name operator-(size_t n) const \
1016 { return iterator_name(wxString::AddToIter(m_cur, -(int)n)); } \
1017 iterator_name operator+=(int n) \
1018 { m_cur = wxString::AddToIter(m_cur, n); return *this; } \
1019 iterator_name operator+=(size_t n) \
1020 { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \
1021 iterator_name operator-=(int n) \
1022 { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \
1023 iterator_name operator-=(size_t n) \
1024 { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \
1025 \
1026 unsigned operator-(const iterator_name& i) const \
1027 { return wxString::DiffIters(m_cur, i.m_cur); } \
1028 \
1029 bool operator==(const iterator_name&i) const \
1030 { return m_cur == i.m_cur; } \
1031 bool operator!=(const iterator_name& i) const \
1032 { return m_cur != i.m_cur; } \
1033 \
1034 bool operator<(const iterator_name& i) const \
1035 { return m_cur < i.m_cur; } \
1036 bool operator>(const iterator_name& i) const \
1037 { return m_cur > i.m_cur; } \
1038 bool operator<=(const iterator_name& i) const \
1039 { return m_cur <= i.m_cur; } \
1040 bool operator>=(const iterator_name& i) const \
1041 { return m_cur >= i.m_cur; } \
1042 \
1043 private: \
1044 /* for internal wxString use only: */ \
1045 iterator_name(underlying_iterator ptr) : m_cur(ptr) {} \
1046 operator underlying_iterator() const { return m_cur; } \
1047 \
1048 friend class WXDLLIMPEXP_BASE wxString; \
1049 friend class WXDLLIMPEXP_BASE wxCStrData; \
1050 \
1051 private: \
1052 underlying_iterator m_cur;
1053
1054 class const_iterator;
1055
1056 class iterator
1057 {
1058 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef,
1059 wxUniCharRef::CreateForString(m_cur))
1060
1061 friend class const_iterator;
1062 };
1063
1064 class const_iterator
1065 {
1066 // NB: reference_type is intentionally value, not reference, the character
1067 // may be encoded differently in wxString data:
1068 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar,
1069 wxUniChar(*m_cur))
1070
1071 public:
1072 const_iterator(const iterator& i) : m_cur(i.m_cur) {}
1073 };
1074
1075 #undef WX_STR_ITERATOR_TAG
1076 #undef WX_STR_ITERATOR_IMPL
1077
1078 friend class iterator;
1079 friend class const_iterator;
1080
1081 template <typename T>
1082 class reverse_iterator_impl
1083 {
1084 public:
1085 typedef T iterator_type;
1086
1087 typedef typename T::iterator_category iterator_category;
1088 typedef typename T::value_type value_type;
1089 typedef typename T::difference_type difference_type;
1090 typedef typename T::reference reference;
1091 typedef typename T::pointer *pointer;
1092
1093 reverse_iterator_impl(iterator_type i) : m_cur(i) {}
1094 reverse_iterator_impl(const reverse_iterator_impl& ri)
1095 : m_cur(ri.m_cur) {}
1096
1097 iterator_type base() const { return m_cur; }
1098
1099 reference operator*() const { return *(m_cur-1); }
1100 reference operator[](size_t n) const { return *(*this + n); }
1101
1102 reverse_iterator_impl& operator++()
1103 { --m_cur; return *this; }
1104 reverse_iterator_impl operator++(int)
1105 { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
1106 reverse_iterator_impl& operator--()
1107 { ++m_cur; return *this; }
1108 reverse_iterator_impl operator--(int)
1109 { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
1110
1111 reverse_iterator_impl operator+(int n) const
1112 { return reverse_iterator_impl(m_cur - n); }
1113 reverse_iterator_impl operator+(size_t n) const
1114 { return reverse_iterator_impl(m_cur - n); }
1115 reverse_iterator_impl operator-(int n) const
1116 { return reverse_iterator_impl(m_cur + n); }
1117 reverse_iterator_impl operator-(size_t n) const
1118 { return reverse_iterator_impl(m_cur + n); }
1119 reverse_iterator_impl operator+=(int n)
1120 { m_cur -= n; return *this; }
1121 reverse_iterator_impl operator+=(size_t n)
1122 { m_cur -= n; return *this; }
1123 reverse_iterator_impl operator-=(int n)
1124 { m_cur += n; return *this; }
1125 reverse_iterator_impl operator-=(size_t n)
1126 { m_cur += n; return *this; }
1127
1128 unsigned operator-(const reverse_iterator_impl& i) const
1129 { return i.m_cur - m_cur; }
1130
1131 bool operator==(const reverse_iterator_impl& ri) const
1132 { return m_cur == ri.m_cur; }
1133 bool operator!=(const reverse_iterator_impl& ri) const
1134 { return !(*this == ri); }
1135
1136 bool operator<(const reverse_iterator_impl& i) const
1137 { return m_cur > i.m_cur; }
1138 bool operator>(const reverse_iterator_impl& i) const
1139 { return m_cur < i.m_cur; }
1140 bool operator<=(const reverse_iterator_impl& i) const
1141 { return m_cur >= i.m_cur; }
1142 bool operator>=(const reverse_iterator_impl& i) const
1143 { return m_cur <= i.m_cur; }
1144
1145 private:
1146 iterator_type m_cur;
1147 };
1148
1149 typedef reverse_iterator_impl<iterator> reverse_iterator;
1150 typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
1151
1152 // first valid index position
1153 const_iterator begin() const { return const_iterator(m_impl.begin()); }
1154 iterator begin() { return iterator(m_impl.begin()); }
1155 // position one after the last valid one
1156 const_iterator end() const { return const_iterator(m_impl.end()); }
1157 iterator end() { return iterator(m_impl.end()); }
1158
1159 // first element of the reversed string
1160 const_reverse_iterator rbegin() const
1161 { return const_reverse_iterator(end()); }
1162 reverse_iterator rbegin()
1163 { return reverse_iterator(end()); }
1164 // one beyond the end of the reversed string
1165 const_reverse_iterator rend() const
1166 { return const_reverse_iterator(begin()); }
1167 reverse_iterator rend()
1168 { return reverse_iterator(begin()); }
1169
1170 // std::string methods:
1171 #if wxUSE_UNICODE_UTF8
1172 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1173 #else
1174 size_t length() const { return m_impl.length(); }
1175 #endif
1176
1177 size_type size() const { return length(); }
1178 size_type max_size() const { return npos; }
1179
1180 bool empty() const { return m_impl.empty(); }
1181
1182 size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8
1183 void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8
1184
1185 void resize(size_t nSize, wxUniChar ch = wxT('\0'))
1186 {
1187 #if wxUSE_UNICODE_UTF8
1188 if ( !ch.IsAscii() )
1189 {
1190 size_t len = length();
1191 if ( nSize == len)
1192 return;
1193 else if ( nSize < len )
1194 erase(nSize);
1195 else
1196 append(nSize - len, ch);
1197 }
1198 else
1199 #endif
1200 m_impl.resize(nSize, (wxStringCharType)ch);
1201 }
1202
1203 wxString substr(size_t nStart = 0, size_t nLen = npos) const
1204 {
1205 size_t pos, len;
1206 PosLenToImpl(nStart, nLen, &pos, &len);
1207 return m_impl.substr(pos, len);
1208 }
1209
1210 // generic attributes & operations
1211 // as standard strlen()
1212 size_t Len() const { return length(); }
1213 // string contains any characters?
1214 bool IsEmpty() const { return empty(); }
1215 // empty string is "false", so !str will return true
1216 bool operator!() const { return empty(); }
1217 // truncate the string to given length
1218 wxString& Truncate(size_t uiLen);
1219 // empty string contents
1220 void Empty()
1221 {
1222 Truncate(0);
1223
1224 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1225 }
1226 // empty the string and free memory
1227 void Clear()
1228 {
1229 wxString tmp(wxEmptyString);
1230 swap(tmp);
1231 }
1232
1233 // contents test
1234 // Is an ascii value
1235 bool IsAscii() const;
1236 // Is a number
1237 bool IsNumber() const;
1238 // Is a word
1239 bool IsWord() const;
1240
1241 // data access (all indexes are 0 based)
1242 // read access
1243 wxUniChar at(size_t n) const
1244 { return *(begin() + n); } // FIXME-UTF8: optimize?
1245 wxUniChar GetChar(size_t n) const
1246 { return at(n); }
1247 // read/write access
1248 wxUniCharRef at(size_t n)
1249 { return *(begin() + n); } // FIXME-UTF8: optimize?
1250 wxUniCharRef GetWritableChar(size_t n)
1251 { return at(n); }
1252 // write access
1253 void SetChar(size_t n, wxUniChar ch)
1254 { at(n) = ch; }
1255
1256 // get last character
1257 wxUniChar Last() const
1258 {
1259 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1260
1261 return at(length() - 1);
1262 }
1263
1264 // get writable last character
1265 wxUniCharRef Last()
1266 {
1267 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1268 return at(length() - 1);
1269 }
1270
1271 /*
1272 Note that we we must define all of the overloads below to avoid
1273 ambiguity when using str[0].
1274 */
1275 wxUniChar operator[](int n) const
1276 { return at(n); }
1277 wxUniChar operator[](long n) const
1278 { return at(n); }
1279 wxUniChar operator[](size_t n) const
1280 { return at(n); }
1281 #ifndef wxSIZE_T_IS_UINT
1282 wxUniChar operator[](unsigned int n) const
1283 { return at(n); }
1284 #endif // size_t != unsigned int
1285
1286 // operator versions of GetWriteableChar()
1287 wxUniCharRef operator[](int n)
1288 { return at(n); }
1289 wxUniCharRef operator[](long n)
1290 { return at(n); }
1291 wxUniCharRef operator[](size_t n)
1292 { return at(n); }
1293 #ifndef wxSIZE_T_IS_UINT
1294 wxUniCharRef operator[](unsigned int n)
1295 { return at(n); }
1296 #endif // size_t != unsigned int
1297
1298 // explicit conversion to C string (use this with printf()!)
1299 wxCStrData c_str() const { return wxCStrData(this); }
1300 wxCStrData data() const { return c_str(); }
1301
1302 // implicit conversion to C string
1303 operator wxCStrData() const { return c_str(); }
1304 operator const wxChar*() const { return c_str(); }
1305
1306 // identical to c_str(), for MFC compatibility
1307 const wxCStrData GetData() const { return c_str(); }
1308
1309 // explicit conversion to C string in internal representation (char*,
1310 // wchar_t*, UTF-8-encoded char*, depending on the build):
1311 const_pointer wx_str() const { return m_impl.c_str(); }
1312
1313 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1314 // converting numbers or strings which are certain not to contain special
1315 // chars (typically system functions, X atoms, environment variables etc.)
1316 //
1317 // the behaviour of these functions with the strings containing anything
1318 // else than 7 bit ASCII characters is undefined, use at your own risk.
1319 #if wxUSE_UNICODE
1320 static wxString FromAscii(const char *ascii); // string
1321 static wxString FromAscii(const char ascii); // char
1322 const wxCharBuffer ToAscii() const;
1323 #else // ANSI
1324 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1325 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
1326 const char *ToAscii() const { return c_str(); }
1327 #endif // Unicode/!Unicode
1328
1329 // conversions with (possible) format conversions: have to return a
1330 // buffer with temporary data
1331 //
1332 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1333 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1334 // fn_str() to return a string which should be used with the OS APIs
1335 // accepting the file names. The return value is always the same, but the
1336 // type differs because a function may either return pointer to the buffer
1337 // directly or have to use intermediate buffer for translation.
1338 #if wxUSE_UNICODE
1339 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1340
1341 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1342
1343 const wxChar* wc_str() const { return c_str(); }
1344
1345 // for compatibility with !wxUSE_UNICODE version
1346 const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
1347
1348 #if wxMBFILES
1349 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1350 #else // !wxMBFILES
1351 const wxChar* fn_str() const { return c_str(); }
1352 #endif // wxMBFILES/!wxMBFILES
1353 #else // ANSI
1354 const wxChar* mb_str() const { return c_str(); }
1355
1356 // for compatibility with wxUSE_UNICODE version
1357 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
1358
1359 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1360
1361 #if wxUSE_WCHAR_T
1362 const wxWCharBuffer wc_str(const wxMBConv& conv) const;
1363 #endif // wxUSE_WCHAR_T
1364 #ifdef __WXOSX__
1365 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1366 #else
1367 const wxChar* fn_str() const { return c_str(); }
1368 #endif
1369 #endif // Unicode/ANSI
1370
1371 // overloaded assignment
1372 // from another wxString
1373 wxString& operator=(const wxStringImpl& stringSrc)
1374 { m_impl = stringSrc; return *this; }
1375 wxString& operator=(const wxCStrData& cstr)
1376 { return *this = cstr.AsString(); }
1377 // from a character
1378 wxString& operator=(wxUniChar ch)
1379 { m_impl = EncodeChar(ch); return *this; }
1380 wxString& operator=(wxUniCharRef ch)
1381 { return operator=((wxUniChar)ch); }
1382 wxString& operator=(char ch)
1383 { return operator=(wxUniChar(ch)); }
1384 wxString& operator=(wchar_t ch)
1385 { return operator=(wxUniChar(ch)); }
1386 // from a C string - STL probably will crash on NULL,
1387 // so we need to compensate in that case
1388 #if wxUSE_STL_BASED_WXSTRING
1389 wxString& operator=(const wxChar *psz)
1390 { if(psz) m_impl = psz; else Clear(); return *this; }
1391 #else
1392 wxString& operator=(const wxChar *psz)
1393 { m_impl = psz; return *this; }
1394 #endif
1395
1396 #if wxUSE_UNICODE
1397 // from wxWCharBuffer
1398 wxString& operator=(const wxWCharBuffer& s)
1399 { (void) operator=((const wchar_t *)s); return *this; }
1400 // from C string
1401 wxString& operator=(const char* psz)
1402 { return operator=(wxString(psz)); }
1403 #else // ANSI
1404 // from another kind of C string
1405 wxString& operator=(const unsigned char* psz);
1406 #if wxUSE_WCHAR_T
1407 // from a wide string
1408 wxString& operator=(const wchar_t *pwz);
1409 #endif
1410 // from wxCharBuffer
1411 wxString& operator=(const wxCharBuffer& psz)
1412 { (void) operator=((const char *)psz); return *this; }
1413 #endif // Unicode/ANSI
1414
1415 // string concatenation
1416 // in place concatenation
1417 /*
1418 Concatenate and return the result. Note that the left to right
1419 associativity of << allows to write things like "str << str1 << str2
1420 << ..." (unlike with +=)
1421 */
1422 // string += string
1423 wxString& operator<<(const wxString& s)
1424 {
1425 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1426 wxASSERT_MSG( s.IsValid(),
1427 _T("did you forget to call UngetWriteBuf()?") );
1428 #endif
1429
1430 append(s);
1431 return *this;
1432 }
1433 // string += C string
1434 wxString& operator<<(const char *psz)
1435 { append(psz); return *this; }
1436 wxString& operator<<(const wchar_t *pwz)
1437 { append(pwz); return *this; }
1438 wxString& operator<<(const wxCStrData& psz)
1439 { append(psz.AsString()); return *this; }
1440 // string += char
1441 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1442 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1443 wxString& operator<<(char ch) { append(1, ch); return *this; }
1444 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1445
1446 // string += buffer (i.e. from wxGetString)
1447 wxString& operator<<(const wxWCharBuffer& s)
1448 { return operator<<((const wchar_t *)s); }
1449 wxString& operator+=(const wxWCharBuffer& s)
1450 { return operator<<((const wchar_t *)s); }
1451
1452 wxString& operator<<(const wxCharBuffer& s)
1453 { return operator<<((const char *)s); }
1454 wxString& operator+=(const wxCharBuffer& s)
1455 { return operator<<((const char *)s); }
1456
1457 // string += C string
1458 wxString& Append(const wxString& s)
1459 {
1460 // test for empty() to share the string if possible
1461 if ( empty() )
1462 *this = s;
1463 else
1464 append(s);
1465 return *this;
1466 }
1467 wxString& Append(const wxCStrData& psz)
1468 { append(psz); return *this; }
1469 wxString& Append(const char* psz)
1470 { append(psz); return *this; }
1471 wxString& Append(const wchar_t* pwz)
1472 { append(pwz); return *this; }
1473 // append count copies of given character
1474 wxString& Append(wxUniChar ch, size_t count = 1u)
1475 { append(count, ch); return *this; }
1476 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1477 { append(count, ch); return *this; }
1478 wxString& Append(char ch, size_t count = 1u)
1479 { append(count, ch); return *this; }
1480 wxString& Append(wchar_t ch, size_t count = 1u)
1481 { append(count, ch); return *this; }
1482 wxString& Append(const char* psz, size_t nLen)
1483 { append(psz, nLen); return *this; }
1484 wxString& Append(const wchar_t* pwz, size_t nLen)
1485 { append(pwz, nLen); return *this; }
1486
1487 // prepend a string, return the string itself
1488 wxString& Prepend(const wxString& str)
1489 { *this = str + *this; return *this; }
1490
1491 // non-destructive concatenation
1492 // two strings
1493 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1494 const wxString& string2);
1495 // string with a single char
1496 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1497 // char with a string
1498 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1499 // string with C string
1500 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1501 const char *psz);
1502 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1503 const wchar_t *pwz);
1504 // C string with string
1505 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1506 const wxString& string);
1507 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1508 const wxString& string);
1509
1510 // stream-like functions
1511 // insert an int into string
1512 wxString& operator<<(int i)
1513 { return (*this) << Format(_T("%d"), i); }
1514 // insert an unsigned int into string
1515 wxString& operator<<(unsigned int ui)
1516 { return (*this) << Format(_T("%u"), ui); }
1517 // insert a long into string
1518 wxString& operator<<(long l)
1519 { return (*this) << Format(_T("%ld"), l); }
1520 // insert an unsigned long into string
1521 wxString& operator<<(unsigned long ul)
1522 { return (*this) << Format(_T("%lu"), ul); }
1523 #if defined wxLongLong_t && !defined wxLongLongIsLong
1524 // insert a long long if they exist and aren't longs
1525 wxString& operator<<(wxLongLong_t ll)
1526 {
1527 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1528 return (*this) << Format(fmt, ll);
1529 }
1530 // insert an unsigned long long
1531 wxString& operator<<(wxULongLong_t ull)
1532 {
1533 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1534 return (*this) << Format(fmt , ull);
1535 }
1536 #endif
1537 // insert a float into string
1538 wxString& operator<<(float f)
1539 { return (*this) << Format(_T("%f"), f); }
1540 // insert a double into string
1541 wxString& operator<<(double d)
1542 { return (*this) << Format(_T("%g"), d); }
1543
1544 // string comparison
1545 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1546 int Cmp(const char *psz) const
1547 { return compare(psz); }
1548 int Cmp(const wchar_t *pwz) const
1549 { return compare(pwz); }
1550 int Cmp(const wxString& s) const
1551 { return compare(s); }
1552 // same as Cmp() but not case-sensitive
1553 int CmpNoCase(const wxString& s) const;
1554 int CmpNoCase(const char *psz) const
1555 { return CmpNoCase(wxString(psz)); }
1556 int CmpNoCase(const wchar_t *pwz) const
1557 { return CmpNoCase(wxString(pwz)); }
1558 // test for the string equality, either considering case or not
1559 // (if compareWithCase then the case matters)
1560 bool IsSameAs(const char *psz, bool compareWithCase = true) const
1561 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
1562 bool IsSameAs(const wchar_t *pwz, bool compareWithCase = true) const
1563 { return (compareWithCase ? Cmp(pwz) : CmpNoCase(pwz)) == 0; }
1564 // comparison with a single character: returns true if equal
1565 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const
1566 {
1567 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
1568 : wxToupper(GetChar(0u)) == wxToupper(c));
1569 }
1570
1571 // simple sub-string extraction
1572 // return substring starting at nFirst of length nCount (or till the end
1573 // if nCount = default value)
1574 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1575
1576 // operator version of Mid()
1577 wxString operator()(size_t start, size_t len) const
1578 { return Mid(start, len); }
1579
1580 // check if the string starts with the given prefix and return the rest
1581 // of the string in the provided pointer if it is not NULL; otherwise
1582 // return false
1583 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
1584 // check if the string ends with the given suffix and return the
1585 // beginning of the string before the suffix in the provided pointer if
1586 // it is not NULL; otherwise return false
1587 bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const;
1588
1589 // get first nCount characters
1590 wxString Left(size_t nCount) const;
1591 // get last nCount characters
1592 wxString Right(size_t nCount) const;
1593 // get all characters before the first occurance of ch
1594 // (returns the whole string if ch not found)
1595 wxString BeforeFirst(wxUniChar ch) const;
1596 // get all characters before the last occurence of ch
1597 // (returns empty string if ch not found)
1598 wxString BeforeLast(wxUniChar ch) const;
1599 // get all characters after the first occurence of ch
1600 // (returns empty string if ch not found)
1601 wxString AfterFirst(wxUniChar ch) const;
1602 // get all characters after the last occurence of ch
1603 // (returns the whole string if ch not found)
1604 wxString AfterLast(wxUniChar ch) const;
1605
1606 // for compatibility only, use more explicitly named functions above
1607 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1608 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1609
1610 // case conversion
1611 // convert to upper case in place, return the string itself
1612 wxString& MakeUpper();
1613 // convert to upper case, return the copy of the string
1614 // Here's something to remember: BC++ doesn't like returns in inlines.
1615 wxString Upper() const ;
1616 // convert to lower case in place, return the string itself
1617 wxString& MakeLower();
1618 // convert to lower case, return the copy of the string
1619 wxString Lower() const ;
1620
1621 // trimming/padding whitespace (either side) and truncating
1622 // remove spaces from left or from right (default) side
1623 wxString& Trim(bool bFromRight = true);
1624 // add nCount copies chPad in the beginning or at the end (default)
1625 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1626
1627 // searching and replacing
1628 // searching (return starting index, or -1 if not found)
1629 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1630 // searching (return starting index, or -1 if not found)
1631 int Find(const wxChar *pszSub) const; // like strstr
1632 // replace first (or all of bReplaceAll) occurences of substring with
1633 // another string, returns the number of replacements made
1634 size_t Replace(const wxChar *szOld,
1635 const wxChar *szNew,
1636 bool bReplaceAll = true);
1637
1638 // check if the string contents matches a mask containing '*' and '?'
1639 bool Matches(const wxChar *szMask) const;
1640
1641 // conversion to numbers: all functions return true only if the whole
1642 // string is a number and put the value of this number into the pointer
1643 // provided, the base is the numeric base in which the conversion should be
1644 // done and must be comprised between 2 and 36 or be 0 in which case the
1645 // standard C rules apply (leading '0' => octal, "0x" => hex)
1646 // convert to a signed integer
1647 bool ToLong(long *val, int base = 10) const;
1648 // convert to an unsigned integer
1649 bool ToULong(unsigned long *val, int base = 10) const;
1650 // convert to wxLongLong
1651 #if defined(wxLongLong_t)
1652 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1653 // convert to wxULongLong
1654 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1655 #endif // wxLongLong_t
1656 // convert to a double
1657 bool ToDouble(double *val) const;
1658
1659
1660 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1661 // formatted input/output
1662 // as sprintf(), returns the number of characters written or < 0 on error
1663 // (take 'this' into account in attribute parameter count)
1664 // int Printf(const wxChar *pszFormat, ...);
1665 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
1666 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1667 // as vprintf(), returns the number of characters written or < 0 on error
1668 int PrintfV(const wxString& format, va_list argptr);
1669
1670 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1671 // returns the string containing the result of Printf() to it
1672 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1673 WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat)
1674 #endif
1675 // the same as above, but takes a va_list
1676 static wxString FormatV(const wxString& format, va_list argptr);
1677
1678 // raw access to string memory
1679 // ensure that string has space for at least nLen characters
1680 // only works if the data of this string is not shared
1681 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1682 // minimize the string's memory
1683 // only works if the data of this string is not shared
1684 bool Shrink();
1685 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1686 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1687 //
1688 // get writable buffer of at least nLen bytes. Unget() *must* be called
1689 // a.s.a.p. to put string back in a reasonable state!
1690 wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) );
1691 // call this immediately after GetWriteBuf() has been used
1692 wxDEPRECATED( void UngetWriteBuf() );
1693 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1694 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1695
1696 // wxWidgets version 1 compatibility functions
1697
1698 // use Mid()
1699 wxString SubString(size_t from, size_t to) const
1700 { return Mid(from, (to - from + 1)); }
1701 // values for second parameter of CompareTo function
1702 enum caseCompare {exact, ignoreCase};
1703 // values for first parameter of Strip function
1704 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1705
1706 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1707 // use Printf()
1708 // (take 'this' into account in attribute parameter count)
1709 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1710 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
1711 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1712
1713 // use Cmp()
1714 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1715 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1716
1717 // use length()
1718 size_t Length() const { return length(); }
1719 // Count the number of characters
1720 int Freq(wxUniChar ch) const;
1721 // use MakeLower
1722 void LowerCase() { MakeLower(); }
1723 // use MakeUpper
1724 void UpperCase() { MakeUpper(); }
1725 // use Trim except that it doesn't change this string
1726 wxString Strip(stripType w = trailing) const;
1727
1728 // use Find (more general variants not yet supported)
1729 size_t Index(const wxChar* psz) const { return Find(psz); }
1730 size_t Index(wxUniChar ch) const { return Find(ch); }
1731 // use Truncate
1732 wxString& Remove(size_t pos) { return Truncate(pos); }
1733 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1734
1735 wxString& Remove(size_t nStart, size_t nLen)
1736 { return (wxString&)erase( nStart, nLen ); }
1737
1738 // use Find()
1739 int First( wxUniChar ch ) const { return Find(ch); }
1740 int First( char ch ) const { return Find(ch); }
1741 int First( wchar_t ch ) const { return Find(ch); }
1742 int First( const wxChar* psz ) const { return Find(psz); }
1743 int First( const wxString &str ) const { return Find(str); }
1744 int Last( wxUniChar ch ) const { return Find(ch, true); }
1745 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1746
1747 // use empty()
1748 bool IsNull() const { return empty(); }
1749
1750 // std::string compatibility functions
1751
1752 // take nLen chars starting at nPos
1753 wxString(const wxString& str, size_t nPos, size_t nLen)
1754 : m_impl(str.m_impl, nPos, nLen) { }
1755 // take all characters from pStart to pEnd
1756 wxString(const void *pStart, const void *pEnd)
1757 : m_impl((const wxChar*)pStart, (const wxChar*)pEnd) { }
1758 wxString(const_iterator first, const_iterator last)
1759 : m_impl(first, last) { }
1760 wxString(iterator first, iterator last)
1761 : m_impl(first, last) { }
1762
1763 // lib.string.modifiers
1764 // append elements str[pos], ..., str[pos+n]
1765 wxString& append(const wxString& str, size_t pos, size_t n)
1766 {
1767 size_t from, len;
1768 str.PosLenToImpl(pos, n, &from, &len);
1769 m_impl.append(str.m_impl, from, len);
1770 return *this;
1771 }
1772 // append a string
1773 wxString& append(const wxString& str)
1774 { m_impl.append(str.m_impl); return *this; }
1775 wxString& append(const wxCStrData& str)
1776 { m_impl.append(str.AsString().m_impl); return *this; }
1777 // append first n (or all if n == npos) characters of sz
1778 wxString& append(const char *sz)
1779 { m_impl.append(ImplStr(sz)); return *this; }
1780 wxString& append(const wchar_t *sz)
1781 { m_impl.append(ImplStr(sz)); return *this; }
1782 wxString& append(const char *sz, size_t n)
1783 {
1784 SubstrBufFromMB str(ImplStr(sz, n));
1785 m_impl.append(str.data, str.len);
1786 return *this;
1787 }
1788 wxString& append(const wchar_t *sz, size_t n)
1789 {
1790 SubstrBufFromWC str(ImplStr(sz, n));
1791 m_impl.append(str.data, str.len);
1792 return *this;
1793 }
1794 // append n copies of ch
1795 wxString& append(size_t n, wxUniChar ch)
1796 {
1797 #if wxUSE_UNICODE_UTF8
1798 if ( !ch.IsAscii() )
1799 m_impl.append(EncodeNChars(n, ch));
1800 else
1801 #endif
1802 m_impl.append(n, (wxStringCharType)ch);
1803 return *this;
1804 }
1805 // append from first to last
1806 wxString& append(const_iterator first, const_iterator last)
1807 { m_impl.append(first, last); return *this; }
1808
1809 // same as `this_string = str'
1810 wxString& assign(const wxString& str)
1811 { m_impl = str.m_impl; return *this; }
1812 // same as ` = str[pos..pos + n]
1813 wxString& assign(const wxString& str, size_t pos, size_t n)
1814 {
1815 size_t from, len;
1816 str.PosLenToImpl(pos, n, &from, &len);
1817 m_impl.assign(str.m_impl, from, len);
1818 return *this;
1819 }
1820 // same as `= first n (or all if n == npos) characters of sz'
1821 wxString& assign(const char *sz)
1822 { m_impl.assign(ImplStr(sz)); return *this; }
1823 wxString& assign(const wchar_t *sz)
1824 { m_impl.assign(ImplStr(sz)); return *this; }
1825 wxString& assign(const char *sz, size_t n)
1826 {
1827 SubstrBufFromMB str(ImplStr(sz, n));
1828 m_impl.assign(str.data, str.len);
1829 return *this;
1830 }
1831 wxString& assign(const wchar_t *sz, size_t n)
1832 {
1833 SubstrBufFromWC str(ImplStr(sz, n));
1834 m_impl.assign(str.data, str.len);
1835 return *this;
1836 }
1837 // same as `= n copies of ch'
1838 wxString& assign(size_t n, wxUniChar ch)
1839 {
1840 #if wxUSE_UNICODE_UTF8
1841 if ( !ch.IsAscii() )
1842 m_impl.assign(EncodeNChars(n, ch));
1843 else
1844 #endif
1845 m_impl.assign(n, (wxStringCharType)ch);
1846 return *this;
1847 }
1848 // assign from first to last
1849 wxString& assign(const_iterator first, const_iterator last)
1850 { m_impl.assign(first, last); return *this; }
1851
1852 // string comparison
1853 int compare(const wxString& str) const;
1854 // comparison with a substring
1855 int compare(size_t nStart, size_t nLen, const wxString& str) const;
1856 // comparison of 2 substrings
1857 int compare(size_t nStart, size_t nLen,
1858 const wxString& str, size_t nStart2, size_t nLen2) const;
1859 // just like strcmp()
1860 int compare(const char* sz) const;
1861 int compare(const wchar_t* sz) const;
1862 // substring comparison with first nCount characters of sz
1863 int compare(size_t nStart, size_t nLen,
1864 const char* sz, size_t nCount = npos) const;
1865 int compare(size_t nStart, size_t nLen,
1866 const wchar_t* sz, size_t nCount = npos) const;
1867
1868 // insert another string
1869 wxString& insert(size_t nPos, const wxString& str)
1870 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
1871 // insert n chars of str starting at nStart (in str)
1872 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1873 {
1874 size_t from, len;
1875 str.PosLenToImpl(nStart, n, &from, &len);
1876 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
1877 return *this;
1878 }
1879 // insert first n (or all if n == npos) characters of sz
1880 wxString& insert(size_t nPos, const char *sz)
1881 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1882 wxString& insert(size_t nPos, const wchar_t *sz)
1883 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1884 wxString& insert(size_t nPos, const char *sz, size_t n)
1885 {
1886 SubstrBufFromMB str(ImplStr(sz, n));
1887 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1888 return *this;
1889 }
1890 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
1891 {
1892 SubstrBufFromWC str(ImplStr(sz, n));
1893 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1894 return *this;
1895 }
1896 // insert n copies of ch
1897 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
1898 {
1899 #if wxUSE_UNICODE_UTF8
1900 if ( !ch.IsAscii() )
1901 m_impl.insert(begin() + nPos, EncodeNChars(n, ch));
1902 else
1903 #endif
1904 m_impl.insert(begin() + nPos, n, (wxStringCharType)ch);
1905 return *this;
1906 }
1907 iterator insert(iterator it, wxUniChar ch)
1908 { return iterator(m_impl.insert(it, EncodeChar(ch))); }
1909 void insert(iterator it, const_iterator first, const_iterator last)
1910 { m_impl.insert(it, first, last); }
1911 void insert(iterator it, size_type n, wxUniChar ch)
1912 {
1913 #if wxUSE_UNICODE_UTF8
1914 if ( !ch.IsAscii() )
1915 m_impl.insert(it, EncodeNChars(n, ch));
1916 else
1917 #endif
1918 m_impl.insert(it, n, (wxStringCharType)ch);
1919 }
1920
1921 // delete characters from nStart to nStart + nLen
1922 wxString& erase(size_type pos = 0, size_type n = npos)
1923 {
1924 size_t from, len;
1925 PosLenToImpl(pos, n, &from, &len);
1926 m_impl.erase(from, len);
1927 return *this;
1928 }
1929 iterator erase(iterator first, iterator last)
1930 { return iterator(m_impl.erase(first, last)); }
1931 iterator erase(iterator first)
1932 { return iterator(m_impl.erase(first)); }
1933
1934 #ifdef wxSTRING_BASE_HASNT_CLEAR
1935 void clear() { erase(); }
1936 #else
1937 void clear() { m_impl.clear(); }
1938 #endif
1939
1940 // replaces the substring of length nLen starting at nStart
1941 wxString& replace(size_t nStart, size_t nLen, const char* sz)
1942 {
1943 size_t from, len;
1944 PosLenToImpl(nStart, nLen, &from, &len);
1945 m_impl.replace(from, len, ImplStr(sz));
1946 return *this;
1947 }
1948 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
1949 {
1950 size_t from, len;
1951 PosLenToImpl(nStart, nLen, &from, &len);
1952 m_impl.replace(from, len, ImplStr(sz));
1953 return *this;
1954 }
1955 // replaces the substring of length nLen starting at nStart
1956 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1957 {
1958 size_t from, len;
1959 PosLenToImpl(nStart, nLen, &from, &len);
1960 m_impl.replace(from, len, str.m_impl);
1961 return *this;
1962 }
1963 // replaces the substring with nCount copies of ch
1964 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
1965 {
1966 size_t from, len;
1967 PosLenToImpl(nStart, nLen, &from, &len);
1968 #if wxUSE_UNICODE_UTF8
1969 if ( !ch.IsAscii() )
1970 m_impl.replace(from, len, EncodeNChars(nCount, ch));
1971 else
1972 #endif
1973 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
1974 return *this;
1975 }
1976 // replaces a substring with another substring
1977 wxString& replace(size_t nStart, size_t nLen,
1978 const wxString& str, size_t nStart2, size_t nLen2)
1979 {
1980 size_t from, len;
1981 PosLenToImpl(nStart, nLen, &from, &len);
1982
1983 size_t from2, len2;
1984 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
1985
1986 m_impl.replace(from, len, str.m_impl, from2, len2);
1987 return *this;
1988 }
1989 // replaces the substring with first nCount chars of sz
1990 wxString& replace(size_t nStart, size_t nLen,
1991 const char* sz, size_t nCount)
1992 {
1993 size_t from, len;
1994 PosLenToImpl(nStart, nLen, &from, &len);
1995
1996 SubstrBufFromMB str(ImplStr(sz, nCount));
1997
1998 m_impl.replace(from, len, str.data, str.len);
1999 return *this;
2000 }
2001 wxString& replace(size_t nStart, size_t nLen,
2002 const wchar_t* sz, size_t nCount)
2003 {
2004 size_t from, len;
2005 PosLenToImpl(nStart, nLen, &from, &len);
2006
2007 SubstrBufFromWC str(ImplStr(sz, nCount));
2008
2009 m_impl.replace(from, len, str.data, str.len);
2010 return *this;
2011 }
2012 wxString& replace(iterator first, iterator last, const char* s)
2013 { m_impl.replace(first, last, ImplStr(s)); return *this; }
2014 wxString& replace(iterator first, iterator last, const wchar_t* s)
2015 { m_impl.replace(first, last, ImplStr(s)); return *this; }
2016 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2017 {
2018 SubstrBufFromMB str(ImplStr(s, n));
2019 m_impl.replace(first, last, str.data, str.len);
2020 return *this;
2021 }
2022 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2023 {
2024 SubstrBufFromWC str(ImplStr(s, n));
2025 m_impl.replace(first, last, str.data, str.len);
2026 return *this;
2027 }
2028 wxString& replace(iterator first, iterator last, const wxString& s)
2029 { m_impl.replace(first, last, s.m_impl); return *this; }
2030 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2031 {
2032 #if wxUSE_UNICODE_UTF8
2033 if ( !ch.IsAscii() )
2034 m_impl.replace(first, last, EncodeNChars(n, ch));
2035 else
2036 #endif
2037 m_impl.replace(first, last, n, (wxStringCharType)ch);
2038 return *this;
2039 }
2040 wxString& replace(iterator first, iterator last,
2041 const_iterator first1, const_iterator last1)
2042 { m_impl.replace(first, last, first1, last1); return *this; }
2043
2044 // swap two strings
2045 void swap(wxString& str)
2046 { m_impl.swap(str.m_impl); }
2047
2048 // find a substring
2049 size_t find(const wxString& str, size_t nStart = 0) const
2050 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
2051
2052 // find first n characters of sz
2053 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
2054 {
2055 SubstrBufFromMB str(ImplStr(sz, n));
2056 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2057 }
2058 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
2059 {
2060 SubstrBufFromWC str(ImplStr(sz, n));
2061 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2062 }
2063
2064 // find the first occurence of character ch after nStart
2065 size_t find(wxUniChar ch, size_t nStart = 0) const
2066 { return PosFromImpl(m_impl.find(EncodeChar(ch), PosToImpl(nStart))); }
2067 size_t find(wxUniCharRef ch, size_t nStart = 0) const
2068 { return find(wxUniChar(ch), nStart); }
2069 size_t find(char ch, size_t nStart = 0) const
2070 { return find(wxUniChar(ch), nStart); }
2071 size_t find(wchar_t ch, size_t nStart = 0) const
2072 { return find(wxUniChar(ch), nStart); }
2073
2074 // rfind() family is exactly like find() but works right to left
2075
2076 // as find, but from the end
2077 size_t rfind(const wxString& str, size_t nStart = npos) const
2078 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
2079
2080 // as find, but from the end
2081 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
2082 {
2083 SubstrBufFromMB str(ImplStr(sz, n));
2084 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2085 }
2086 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
2087 {
2088 SubstrBufFromWC str(ImplStr(sz, n));
2089 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2090 }
2091 // as find, but from the end
2092 size_t rfind(wxUniChar ch, size_t nStart = npos) const
2093 { return PosFromImpl(m_impl.rfind(EncodeChar(ch), PosToImpl(nStart))); }
2094 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
2095 { return rfind(wxUniChar(ch), nStart); }
2096 size_t rfind(char ch, size_t nStart = npos) const
2097 { return rfind(wxUniChar(ch), nStart); }
2098 size_t rfind(wchar_t ch, size_t nStart = npos) const
2099 { return rfind(wxUniChar(ch), nStart); }
2100
2101 // find first/last occurence of any character (not) in the set:
2102 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2103 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2104 // sizeof(wchar_t)==2 and surrogates are present in the string;
2105 // should we care? Probably not.
2106 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2107 { return m_impl.find_first_of(str.m_impl, nStart); }
2108 size_t find_first_of(const char* sz, size_t nStart = 0) const
2109 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2110 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
2111 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2112 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
2113 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2114 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
2115 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2116 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2117 { return m_impl.find_first_of((wxChar)c, nStart); }
2118
2119 size_t find_last_of(const wxString& str, size_t nStart = npos) const
2120 { return m_impl.find_last_of(str.m_impl, nStart); }
2121 size_t find_last_of(const char* sz, size_t nStart = npos) const
2122 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2123 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
2124 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2125 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2126 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2127 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2128 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2129 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2130 { return m_impl.find_last_of((wxChar)c, nStart); }
2131
2132 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2133 { return m_impl.find_first_not_of(str.m_impl, nStart); }
2134 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
2135 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2136 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
2137 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2138 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
2139 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2140 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2141 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2142 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
2143 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2144
2145 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2146 { return m_impl.find_last_not_of(str.m_impl, nStart); }
2147 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
2148 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2149 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
2150 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2151 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
2152 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2153 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2154 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2155 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
2156 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2157 #else
2158 // we can't use std::string implementation in UTF-8 build, because the
2159 // character sets would be interpreted wrongly:
2160
2161 // as strpbrk() but starts at nStart, returns npos if not found
2162 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2163 { return find_first_of((const wxChar*)str.c_str(), nStart); }
2164 // same as above
2165 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2166 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2167 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2168 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2169 // same as find(char, size_t)
2170 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2171 { return find(c, nStart); }
2172 // find the last (starting from nStart) char from str in this string
2173 size_t find_last_of (const wxString& str, size_t nStart = npos) const
2174 { return find_last_of((const wxChar*)str.c_str(), nStart); }
2175 // same as above
2176 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2177 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2178 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2179 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2180 // same as above
2181 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2182 { return rfind(c, nStart); }
2183
2184 // find first/last occurence of any character not in the set
2185
2186 // as strspn() (starting from nStart), returns npos on failure
2187 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2188 { return find_first_not_of((const wxChar*)str.c_str(), nStart); }
2189 // same as above
2190 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2191 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2192 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2193 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2194 // same as above
2195 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2196 // as strcspn()
2197 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2198 { return find_last_not_of((const wxChar*)str.c_str(), nStart); }
2199 // same as above
2200 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2201 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2202 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2203 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2204 // same as above
2205 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2206 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2207
2208 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2209 // above to resolve ambiguities:
2210 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2211 { return find_first_of(wxUniChar(ch), nStart); }
2212 size_t find_first_of(char ch, size_t nStart = 0) const
2213 { return find_first_of(wxUniChar(ch), nStart); }
2214 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2215 { return find_first_of(wxUniChar(ch), nStart); }
2216 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2217 { return find_last_of(wxUniChar(ch), nStart); }
2218 size_t find_last_of(char ch, size_t nStart = npos) const
2219 { return find_last_of(wxUniChar(ch), nStart); }
2220 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2221 { return find_last_of(wxUniChar(ch), nStart); }
2222 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2223 { return find_first_not_of(wxUniChar(ch), nStart); }
2224 size_t find_first_not_of(char ch, size_t nStart = 0) const
2225 { return find_first_not_of(wxUniChar(ch), nStart); }
2226 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2227 { return find_first_not_of(wxUniChar(ch), nStart); }
2228 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2229 { return find_last_not_of(wxUniChar(ch), nStart); }
2230 size_t find_last_not_of(char ch, size_t nStart = npos) const
2231 { return find_last_not_of(wxUniChar(ch), nStart); }
2232 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2233 { return find_last_not_of(wxUniChar(ch), nStart); }
2234
2235 // string += string
2236 wxString& operator+=(const wxString& s)
2237 { m_impl += s.m_impl; return *this; }
2238 // string += C string
2239 wxString& operator+=(const char *psz)
2240 { m_impl += ImplStr(psz); return *this; }
2241 wxString& operator+=(const wchar_t *pwz)
2242 { m_impl += ImplStr(pwz); return *this; }
2243 wxString& operator+=(const wxCStrData& s)
2244 { m_impl += s.AsString().m_impl; return *this; }
2245 // string += char
2246 wxString& operator+=(wxUniChar ch)
2247 { m_impl += EncodeChar(ch); return *this; }
2248 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
2249 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
2250 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
2251 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
2252
2253 private:
2254 #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2255 // helpers for wxStringBuffer and wxStringBufferLength
2256 wxStringCharType *DoGetWriteBuf(size_t nLen)
2257 { return m_impl.DoGetWriteBuf(nLen); }
2258 void DoUngetWriteBuf()
2259 { m_impl.DoUngetWriteBuf(); }
2260 void DoUngetWriteBuf(size_t nLen)
2261 { m_impl.DoUngetWriteBuf(nLen); }
2262
2263 friend class WXDLLIMPEXP_BASE wxStringBuffer;
2264 friend class WXDLLIMPEXP_BASE wxStringBufferLength;
2265 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2266
2267 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2268 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
2269 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
2270 #endif
2271
2272 #if !wxUSE_STL_BASED_WXSTRING
2273 // check string's data validity
2274 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2275 #endif
2276
2277 private:
2278 wxStringImpl m_impl;
2279 };
2280
2281 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2282 #pragma warning (default:4275)
2283 #endif
2284
2285 // string iterator operators that satisfy STL Random Access Iterator
2286 // requirements:
2287 inline wxString::iterator operator+(int n, wxString::iterator i)
2288 { return i + n; }
2289 inline wxString::iterator operator+(size_t n, wxString::iterator i)
2290 { return i + n; }
2291 inline wxString::const_iterator operator+(int n, wxString::const_iterator i)
2292 { return i + n; }
2293 inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i)
2294 { return i + n; }
2295 inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
2296 { return i + n; }
2297 inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
2298 { return i + n; }
2299 inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
2300 { return i + n; }
2301 inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
2302 { return i + n; }
2303
2304 // notice that even though for many compilers the friend declarations above are
2305 // enough, from the point of view of C++ standard we must have the declarations
2306 // here as friend ones are not injected in the enclosing namespace and without
2307 // them the code fails to compile with conforming compilers such as xlC or g++4
2308 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
2309 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2310 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2311 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2312 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
2313
2314 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2315 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2316
2317 inline wxString operator+(const wxString& string, wxUniCharRef ch)
2318 { return string + (wxUniChar)ch; }
2319 inline wxString operator+(const wxString& string, char ch)
2320 { return string + wxUniChar(ch); }
2321 inline wxString operator+(const wxString& string, wchar_t ch)
2322 { return string + wxUniChar(ch); }
2323 inline wxString operator+(wxUniCharRef ch, const wxString& string)
2324 { return (wxUniChar)ch + string; }
2325 inline wxString operator+(char ch, const wxString& string)
2326 { return wxUniChar(ch) + string; }
2327 inline wxString operator+(wchar_t ch, const wxString& string)
2328 { return wxUniChar(ch) + string; }
2329
2330
2331 #if wxUSE_STL_BASED_WXSTRING
2332 // return an empty wxString (not very useful with wxUSE_STL == 1)
2333 inline const wxString wxGetEmptyString() { return wxString(); }
2334 #else // !wxUSE_STL_BASED_WXSTRING
2335 // return an empty wxString (more efficient than wxString() here)
2336 inline const wxString& wxGetEmptyString()
2337 {
2338 return *(wxString *)&wxEmptyString;
2339 }
2340 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2341
2342 // ----------------------------------------------------------------------------
2343 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2344 // ----------------------------------------------------------------------------
2345
2346 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2347
2348 class WXDLLIMPEXP_BASE wxStringBuffer
2349 {
2350 public:
2351 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
2352 : m_str(str), m_buf(lenWanted)
2353 { }
2354
2355 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
2356
2357 operator wxChar*() { return m_buf.data(); }
2358
2359 private:
2360 wxString& m_str;
2361 #if wxUSE_UNICODE
2362 wxWCharBuffer m_buf;
2363 #else
2364 wxCharBuffer m_buf;
2365 #endif
2366
2367 DECLARE_NO_COPY_CLASS(wxStringBuffer)
2368 };
2369
2370 class WXDLLIMPEXP_BASE wxStringBufferLength
2371 {
2372 public:
2373 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
2374 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
2375 { }
2376
2377 ~wxStringBufferLength()
2378 {
2379 wxASSERT(m_lenSet);
2380 m_str.assign(m_buf.data(), m_len);
2381 }
2382
2383 operator wxChar*() { return m_buf.data(); }
2384 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2385
2386 private:
2387 wxString& m_str;
2388 #if wxUSE_UNICODE
2389 wxWCharBuffer m_buf;
2390 #else
2391 wxCharBuffer m_buf;
2392 #endif
2393 size_t m_len;
2394 bool m_lenSet;
2395
2396 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
2397 };
2398
2399 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2400
2401 class WXDLLIMPEXP_BASE wxStringBuffer
2402 {
2403 public:
2404 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
2405 : m_str(str), m_buf(NULL)
2406 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2407
2408 ~wxStringBuffer() { m_str.DoUngetWriteBuf(); }
2409
2410 operator wxChar*() const { return m_buf; }
2411
2412 private:
2413 wxString& m_str;
2414 wxChar *m_buf;
2415
2416 DECLARE_NO_COPY_CLASS(wxStringBuffer)
2417 };
2418
2419 class WXDLLIMPEXP_BASE wxStringBufferLength
2420 {
2421 public:
2422 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
2423 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2424 {
2425 m_buf = m_str.DoGetWriteBuf(lenWanted);
2426 wxASSERT(m_buf != NULL);
2427 }
2428
2429 ~wxStringBufferLength()
2430 {
2431 wxASSERT(m_lenSet);
2432 m_str.DoUngetWriteBuf(m_len);
2433 }
2434
2435 operator wxChar*() const { return m_buf; }
2436 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2437
2438 private:
2439 wxString& m_str;
2440 wxChar *m_buf;
2441 size_t m_len;
2442 bool m_lenSet;
2443
2444 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
2445 };
2446
2447 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2448
2449 // ---------------------------------------------------------------------------
2450 // wxString comparison functions: operator versions are always case sensitive
2451 // ---------------------------------------------------------------------------
2452
2453 #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0
2454
2455 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2456
2457 #undef wxCMP_WXCHAR_STRING
2458
2459 // note that there is an optimization in operator==() and !=(): we (quickly)
2460 // checks the strings length first, before comparing their data
2461 inline bool operator==(const wxString& s1, const wxString& s2)
2462 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
2463 inline bool operator!=(const wxString& s1, const wxString& s2)
2464 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
2465 inline bool operator< (const wxString& s1, const wxString& s2)
2466 { return s1.Cmp(s2) < 0; }
2467 inline bool operator> (const wxString& s1, const wxString& s2)
2468 { return s1.Cmp(s2) > 0; }
2469 inline bool operator<=(const wxString& s1, const wxString& s2)
2470 { return s1.Cmp(s2) <= 0; }
2471 inline bool operator>=(const wxString& s1, const wxString& s2)
2472 { return s1.Cmp(s2) >= 0; }
2473
2474 #if wxUSE_UNICODE
2475 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
2476 { return (s1.Cmp((const wchar_t *)s2) == 0); }
2477 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
2478 { return (s2.Cmp((const wchar_t *)s1) == 0); }
2479 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2480 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2481 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2482 { return (s2.Cmp((const wchar_t *)s1) != 0); }
2483 #else // !wxUSE_UNICODE
2484 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
2485 { return (s1.Cmp((const char *)s2) == 0); }
2486 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
2487 { return (s2.Cmp((const char *)s1) == 0); }
2488 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2489 { return (s1.Cmp((const char *)s2) != 0); }
2490 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2491 { return (s2.Cmp((const char *)s1) != 0); }
2492 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2493
2494 #if wxUSE_UNICODE
2495 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
2496 { return string + (const wchar_t *)buf; }
2497 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
2498 { return (const wchar_t *)buf + string; }
2499 #else // !wxUSE_UNICODE
2500 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
2501 { return string + (const char *)buf; }
2502 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
2503 { return (const char *)buf + string; }
2504 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2505
2506 // comparison with char
2507 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2508 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2509 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2510 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2511 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2512 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2513 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2514 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2515 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2516 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2517 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2518 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2519 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2520 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2521 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2522 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2523 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2524 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
2525
2526 // comparison with C string in Unicode build
2527 #if wxUSE_UNICODE
2528
2529 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2530
2531 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
2532
2533 #undef wxCMP_CHAR_STRING
2534
2535 #endif // wxUSE_UNICODE
2536
2537 // we also need to provide the operators for comparison with wxCStrData to
2538 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2539 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2540 //
2541 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2542 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2543 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2544
2545 // FIXME: these ifdefs must be removed when wxCStrData has both conversions
2546 #if wxUSE_UNICODE
2547 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
2548 #else
2549 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
2550 #endif
2551
2552 #undef wxCMP_CHAR_CSTRDATA
2553 #undef wxCMP_WCHAR_CSTRDATA
2554
2555 // ---------------------------------------------------------------------------
2556 // Implementation only from here until the end of file
2557 // ---------------------------------------------------------------------------
2558
2559 #if wxUSE_STD_IOSTREAM
2560
2561 #include "wx/iosfwrap.h"
2562
2563 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
2564 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
2565
2566 #endif // wxSTD_STRING_COMPATIBILITY
2567
2568 // ---------------------------------------------------------------------------
2569 // wxCStrData implementation
2570 // ---------------------------------------------------------------------------
2571
2572 inline wxCStrData::wxCStrData(char *buf)
2573 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2574 inline wxCStrData::wxCStrData(wchar_t *buf)
2575 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2576
2577 inline wxCStrData::~wxCStrData()
2578 {
2579 if ( m_owned )
2580 delete m_str;
2581 }
2582
2583 #if wxUSE_UNICODE
2584 inline const wchar_t* wxCStrData::AsWChar() const
2585 #else
2586 inline const char* wxCStrData::AsChar() const
2587 #endif
2588 {
2589 // FIXME-UTF8: incorrect position, incorrect charset
2590 return m_str->wx_str() + m_offset;
2591 }
2592
2593 inline wxString wxCStrData::AsString() const
2594 {
2595 if ( m_offset == 0 )
2596 return *m_str;
2597 else
2598 return m_str->Mid(m_offset);
2599 }
2600
2601 inline wxUniChar wxCStrData::operator*() const
2602 {
2603 if ( m_str->empty() )
2604 return wxUniChar(_T('\0'));
2605 else
2606 return (*m_str)[m_offset];
2607 }
2608
2609 inline wxUniChar wxCStrData::operator[](size_t n) const
2610 {
2611 return m_str->at(m_offset + n);
2612 }
2613
2614 // ----------------------------------------------------------------------------
2615 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2616 // ----------------------------------------------------------------------------
2617
2618 // FIXME-UTF8: move this to buffer.h; provide versions for both variants
2619 inline wxWxCharBuffer::wxWxCharBuffer(const wxCStrData& cstr)
2620 : wxCharTypeBufferBase((const wxChar *)cstr)
2621 {
2622 }
2623
2624 #endif // _WX_WXSTRINGH__