1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString and wxArrayString classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
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.
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
51 #include <StringMgr.h>
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
59 class WXDLLIMPEXP_BASE wxString
;
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
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 *)
72 // implementation only
73 #define wxASSERT_VALID_INDEX(i) \
74 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 #if WXWIN_COMPATIBILITY_2_6
82 // deprecated in favour of wxString::npos, don't use in new code
84 // maximum possible length for a string means "take all string" everywhere
85 #define wxSTRING_MAXLEN wxString::npos
87 #endif // WXWIN_COMPATIBILITY_2_6
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // global pointer to empty string
94 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
96 // ---------------------------------------------------------------------------
97 // global functions complementing standard C string library replacements for
98 // strlen() and portable strcasecmp()
99 //---------------------------------------------------------------------------
101 #if WXWIN_COMPATIBILITY_2_8
102 // Use wxXXX() functions from wxcrt.h instead! These functions are for
103 // backwards compatibility only.
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
); }
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; }
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
)
118 #if defined(__VISUALC__) && defined(__WXWINCE__)
119 register char c1
, c2
;
121 c1
= tolower(*psz1
++);
122 c2
= tolower(*psz2
++);
123 } while ( c1
&& (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
;
150 c1
= tolower(*psz1
++);
151 c2
= tolower(*psz2
++);
152 } while ( c1
&& (c1
== c2
) );
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):
160 register char c1, c2;
162 c1 = tolower(*psz1++);
163 c2 = tolower(*psz2++);
164 } while ( c1 && (c1 == c2) );
169 #error "Please define string case-insensitive compare for your OS/compiler"
170 #endif // OS/compiler
173 #endif // WXWIN_COMPATIBILITY_2_8
175 // ----------------------------------------------------------------------------
176 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
177 // ----------------------------------------------------------------------------
179 // FIXME-UTF8: using std::string as wxString base class is currently broken,
180 // so we use the standard wxString with std::string conversion
181 // enabled, this is API-compatible.
183 #define wxUSE_STL_BASED_WXSTRING 0
185 #undef wxUSE_STD_STRING
186 #define wxUSE_STD_STRING 1
189 #define wxUSE_STL_BASED_WXSTRING wxUSE_STL
192 // in both cases we need to define wxStdString
193 #if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
195 #include "wx/beforestd.h"
197 #include "wx/afterstd.h"
199 #if wxUSE_UNICODE_WCHAR
200 #ifdef HAVE_STD_WSTRING
201 typedef std::wstring wxStdString
;
203 typedef std::basic_string
<wxChar
> wxStdString
;
206 typedef std::string wxStdString
;
209 #endif // need <string>
211 #if wxUSE_STL_BASED_WXSTRING
213 // we always want ctor from std::string when using std::string internally
214 #undef wxUSE_STD_STRING
215 #define wxUSE_STD_STRING 1
217 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
218 (defined(_MSC_VER) && (_MSC_VER <= 1200))
219 #define wxSTRING_BASE_HASNT_CLEAR
222 typedef wxStdString wxStringImpl
;
223 #else // if !wxUSE_STL_BASED_WXSTRING
225 // in non-STL mode, compare() is implemented in wxString and not wxStringImpl
226 #undef HAVE_STD_STRING_COMPARE
228 // ---------------------------------------------------------------------------
229 // string data prepended with some housekeeping info (used by wxString class),
230 // is never used directly (but had to be put here to allow inlining)
231 // ---------------------------------------------------------------------------
233 struct WXDLLIMPEXP_BASE wxStringData
235 int nRefs
; // reference count
236 size_t nDataLength
, // actual string length
237 nAllocLength
; // allocated memory size
239 // mimics declaration 'wxChar data[nAllocLength]'
240 wxChar
* data() const { return (wxChar
*)(this + 1); }
242 // empty string has a special ref count so it's never deleted
243 bool IsEmpty() const { return (nRefs
== -1); }
244 bool IsShared() const { return (nRefs
> 1); }
247 void Lock() { if ( !IsEmpty() ) nRefs
++; }
249 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
250 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
253 // VC++ free must take place in same DLL as allocation when using non dll
254 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
255 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
256 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
257 // we must not inline deallocation since allocation is not inlined
260 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
263 // if we had taken control over string memory (GetWriteBuf), it's
264 // intentionally put in invalid state
265 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
266 bool IsValid() const { return (nRefs
!= 0); }
269 class WXDLLIMPEXP_BASE wxStringImpl
272 // an 'invalid' value for string index, moved to this place due to a CW bug
273 static const size_t npos
;
276 // points to data preceded by wxStringData structure with ref count info
277 wxStringCharType
*m_pchData
;
279 // accessor to string data
280 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
282 // string (re)initialization functions
283 // initializes the string to the empty value (must be called only from
284 // ctors, use Reinit() otherwise)
285 void Init() { m_pchData
= (wxStringCharType
*)wxEmptyString
; }
286 // initializes the string with (a part of) C-string
287 void InitWith(const wxStringCharType
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
288 // as Init, but also frees old data
289 void Reinit() { GetStringData()->Unlock(); Init(); }
292 // allocates memory for string of length nLen
293 bool AllocBuffer(size_t nLen
);
294 // effectively copies data to string
295 bool AssignCopy(size_t, const wxStringCharType
*);
297 // append a (sub)string
298 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
, size_t nMaxLen
);
299 bool ConcatSelf(size_t nLen
, const wxStringCharType
*src
)
300 { return ConcatSelf(nLen
, src
, nLen
); }
302 // functions called before writing to the string: they copy it if there
303 // are other references to our data (should be the only owner when writing)
304 bool CopyBeforeWrite();
305 bool AllocBeforeWrite(size_t);
307 // compatibility with wxString
308 bool Alloc(size_t nLen
);
312 typedef wxStringCharType value_type
;
313 typedef wxStringCharType char_type
;
314 typedef size_t size_type
;
315 typedef value_type
& reference
;
316 typedef const value_type
& const_reference
;
317 typedef value_type
* pointer
;
318 typedef const value_type
* const_pointer
;
319 typedef value_type
*iterator
;
320 typedef const value_type
*const_iterator
;
322 // constructors and destructor
323 // ctor for an empty string
324 wxStringImpl() { Init(); }
326 wxStringImpl(const wxStringImpl
& stringSrc
)
328 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
329 _T("did you forget to call UngetWriteBuf()?") );
331 if ( stringSrc
.empty() ) {
332 // nothing to do for an empty string
336 m_pchData
= stringSrc
.m_pchData
; // share same data
337 GetStringData()->Lock(); // => one more copy
340 // string containing nRepeat copies of ch
341 wxStringImpl(size_type nRepeat
, wxStringCharType ch
);
342 // ctor takes first nLength characters from C string
343 // (default value of npos means take all the string)
344 wxStringImpl(const wxStringCharType
*psz
)
345 { InitWith(psz
, 0, npos
); }
346 wxStringImpl(const wxStringCharType
*psz
, size_t nLength
)
347 { InitWith(psz
, 0, nLength
); }
348 // take nLen chars starting at nPos
349 wxStringImpl(const wxStringImpl
& str
, size_t nPos
, size_t nLen
)
351 wxASSERT_MSG( str
.GetStringData()->IsValid(),
352 _T("did you forget to call UngetWriteBuf()?") );
354 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
355 InitWith(str
.c_str(), nPos
, nLen
);
357 // take all characters from pStart to pEnd
358 wxStringImpl(const void *pStart
, const void *pEnd
);
360 // dtor is not virtual, this class must not be inherited from!
363 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
364 //RN - according to the above VC++ does indeed inline this,
365 //even though it spits out two warnings
366 #pragma warning (disable:4714)
369 GetStringData()->Unlock();
372 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
373 //re-enable inlining warning
374 #pragma warning (default:4714)
376 // overloaded assignment
377 // from another wxString
378 wxStringImpl
& operator=(const wxStringImpl
& stringSrc
);
380 wxStringImpl
& operator=(wxStringCharType ch
);
382 wxStringImpl
& operator=(const wxStringCharType
*psz
);
384 // return the length of the string
385 size_type
length() const { return GetStringData()->nDataLength
; }
386 // return the length of the string
387 size_type
size() const { return length(); }
388 // return the maximum size of the string
389 size_type
max_size() const { return npos
; }
390 // resize the string, filling the space with c if c != 0
391 void resize(size_t nSize
, wxStringCharType ch
= '\0');
392 // delete the contents of the string
393 void clear() { erase(0, npos
); }
394 // returns true if the string is empty
395 bool empty() const { return length() == 0; }
396 // inform string about planned change in size
397 void reserve(size_t sz
) { Alloc(sz
); }
398 size_type
capacity() const { return GetStringData()->nAllocLength
; }
401 // return the character at position n
402 value_type
at(size_type n
) const
403 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
404 // returns the writable character at position n
405 reference
at(size_type n
)
407 wxASSERT_VALID_INDEX( n
);
410 } // FIXME-UTF8: not useful for us...?
412 // lib.string.modifiers
413 // append elements str[pos], ..., str[pos+n]
414 wxStringImpl
& append(const wxStringImpl
& str
, size_t pos
, size_t n
)
416 wxASSERT(pos
<= str
.length());
417 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
421 wxStringImpl
& append(const wxStringImpl
& str
)
422 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
423 // append first n (or all if n == npos) characters of sz
424 wxStringImpl
& append(const wxStringCharType
*sz
)
425 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
426 wxStringImpl
& append(const wxStringCharType
*sz
, size_t n
)
427 { ConcatSelf(n
, sz
); return *this; }
428 // append n copies of ch
429 wxStringImpl
& append(size_t n
, wxStringCharType ch
);
430 // append from first to last
431 wxStringImpl
& append(const_iterator first
, const_iterator last
)
432 { ConcatSelf(last
- first
, first
); return *this; }
434 // same as `this_string = str'
435 wxStringImpl
& assign(const wxStringImpl
& str
)
436 { return *this = str
; }
437 // same as ` = str[pos..pos + n]
438 wxStringImpl
& assign(const wxStringImpl
& str
, size_t pos
, size_t n
)
439 { clear(); return append(str
, pos
, n
); }
440 // same as `= first n (or all if n == npos) characters of sz'
441 wxStringImpl
& assign(const wxStringCharType
*sz
)
442 { clear(); return append(sz
, wxStrlen(sz
)); }
443 wxStringImpl
& assign(const wxStringCharType
*sz
, size_t n
)
444 { clear(); return append(sz
, n
); }
445 // same as `= n copies of ch'
446 wxStringImpl
& assign(size_t n
, wxStringCharType ch
)
447 { clear(); return append(n
, ch
); }
448 // assign from first to last
449 wxStringImpl
& assign(const_iterator first
, const_iterator last
)
450 { clear(); return append(first
, last
); }
452 // first valid index position
453 const_iterator
begin() const { return m_pchData
; }
455 // position one after the last valid one
456 const_iterator
end() const { return m_pchData
+ length(); }
459 // insert another string
460 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
)
462 wxASSERT( str
.GetStringData()->IsValid() );
463 return insert(nPos
, str
.c_str(), str
.length());
465 // insert n chars of str starting at nStart (in str)
466 wxStringImpl
& insert(size_t nPos
, const wxStringImpl
& str
, size_t nStart
, size_t n
)
468 wxASSERT( str
.GetStringData()->IsValid() );
469 wxASSERT( nStart
< str
.length() );
470 size_t strLen
= str
.length() - nStart
;
471 n
= strLen
< n
? strLen
: n
;
472 return insert(nPos
, str
.c_str() + nStart
, n
);
474 // insert first n (or all if n == npos) characters of sz
475 wxStringImpl
& insert(size_t nPos
, const wxStringCharType
*sz
, size_t n
= npos
);
476 // insert n copies of ch
477 wxStringImpl
& insert(size_t nPos
, size_t n
, wxStringCharType ch
)// FIXME-UTF8: tricky
478 { return insert(nPos
, wxStringImpl(n
, ch
)); }
479 iterator
insert(iterator it
, wxStringCharType ch
) // FIXME-UTF8: tricky
480 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
481 void insert(iterator it
, const_iterator first
, const_iterator last
)
482 { insert(it
- begin(), first
, last
- first
); }
483 void insert(iterator it
, size_type n
, wxStringCharType ch
)
484 { insert(it
- begin(), n
, ch
); }
486 // delete characters from nStart to nStart + nLen
487 wxStringImpl
& erase(size_type pos
= 0, size_type n
= npos
);
488 iterator
erase(iterator first
, iterator last
)
490 size_t idx
= first
- begin();
491 erase(idx
, last
- first
);
492 return begin() + idx
;
494 iterator
erase(iterator first
);
496 // explicit conversion to C string (use this with printf()!)
497 const wxStringCharType
* c_str() const { return m_pchData
; }
498 const wxStringCharType
* data() const { return m_pchData
; }
500 // replaces the substring of length nLen starting at nStart
501 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringCharType
* sz
);
502 // replaces the substring of length nLen starting at nStart
503 wxStringImpl
& replace(size_t nStart
, size_t nLen
, const wxStringImpl
& str
)
504 { return replace(nStart
, nLen
, str
.c_str()); }
505 // replaces the substring with nCount copies of ch
506 wxStringImpl
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxStringCharType ch
);
507 // replaces a substring with another substring
508 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
509 const wxStringImpl
& str
, size_t nStart2
, size_t nLen2
);
510 // replaces the substring with first nCount chars of sz
511 wxStringImpl
& replace(size_t nStart
, size_t nLen
,
512 const wxStringCharType
* sz
, size_t nCount
);
513 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
)
514 { return replace(first
- begin(), last
- first
, s
); }
515 wxStringImpl
& replace(iterator first
, iterator last
, const_pointer s
,
517 { return replace(first
- begin(), last
- first
, s
, n
); }
518 wxStringImpl
& replace(iterator first
, iterator last
, const wxStringImpl
& s
)
519 { return replace(first
- begin(), last
- first
, s
); }
520 wxStringImpl
& replace(iterator first
, iterator last
, size_type n
, wxStringCharType c
)
521 { return replace(first
- begin(), last
- first
, n
, c
); }
522 wxStringImpl
& replace(iterator first
, iterator last
,
523 const_iterator first1
, const_iterator last1
)
524 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
527 void swap(wxStringImpl
& str
);
529 // All find() functions take the nStart argument which specifies the
530 // position to start the search on, the default value is 0. All functions
531 // return npos if there were no match.
534 size_t find(const wxStringImpl
& str
, size_t nStart
= 0) const;
536 // find first n characters of sz
537 size_t find(const wxStringCharType
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
539 // find the first occurence of character ch after nStart
540 size_t find(wxStringCharType ch
, size_t nStart
= 0) const;
542 // rfind() family is exactly like find() but works right to left
544 // as find, but from the end
545 size_t rfind(const wxStringImpl
& str
, size_t nStart
= npos
) const;
547 // as find, but from the end
548 size_t rfind(const wxStringCharType
* sz
, size_t nStart
= npos
,
549 size_t n
= npos
) const;
550 // as find, but from the end
551 size_t rfind(wxStringCharType ch
, size_t nStart
= npos
) const;
553 size_type
copy(wxStringCharType
* s
, size_type n
, size_type pos
= 0);
555 // substring extraction
556 wxStringImpl
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
559 wxStringImpl
& operator+=(const wxStringImpl
& s
) { return append(s
); }
560 // string += C string
561 wxStringImpl
& operator+=(const wxStringCharType
*psz
) { return append(psz
); }
563 wxStringImpl
& operator+=(wxStringCharType ch
) { return append(1, ch
); }
565 #if !wxUSE_UNICODE_UTF8
566 // helpers for wxStringBuffer and wxStringBufferLength
567 wxStringCharType
*DoGetWriteBuf(size_t nLen
);
568 void DoUngetWriteBuf();
569 void DoUngetWriteBuf(size_t nLen
);
572 friend class WXDLLIMPEXP_BASE wxString
;
575 #endif // !wxUSE_STL_BASED_WXSTRING
577 // don't pollute the library user's name space
578 #undef wxASSERT_VALID_INDEX
580 // wx/unichar.h needs wxStringImpl, so it's only possible to include it here
581 // (it includes string.h if not included from string.h):
582 #include "wx/unichar.h"
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 // Lightweight object returned by wxString::c_str() and implicitly convertible
589 // to either const char* or const wchar_t*.
593 // Ctors; for internal use by wxString and wxCStrData only
594 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
595 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
598 // Ctor constructs the object from char literal; they are needed to make
599 // operator?: compile and they intentionally take char*, not const char*
600 wxCStrData(char *buf
);
601 wxCStrData(wchar_t *buf
);
605 // FIXME: we'll need convertors for both char* and wchar_t* and NONE
606 // for wxChar*, but that's after completing the transition to
607 // "smart" wxUniChar class. For now, just have conversion to
608 // char* in ANSI build and wchar_t in Unicode build.
610 const wchar_t* AsWChar() const;
611 operator const wchar_t*() const { return AsWChar(); }
613 const char* AsChar() const;
614 const unsigned char* AsUnsignedChar() const
615 { return (const unsigned char *) AsChar(); }
616 operator const void*() const { return AsChar(); }
617 operator const char*() const { return AsChar(); }
618 operator const unsigned char*() const { return AsUnsignedChar(); }
621 wxString
AsString() const;
622 operator wxString() const;
624 // allow expressions like "c_str()[0]":
625 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
626 wxUniChar
operator[](size_t n
) const;
627 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
628 #ifndef wxSIZE_T_IS_UINT
629 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
630 #endif // size_t != unsigned int
632 // these operators are needed to emulate the pointer semantics of c_str():
633 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
634 // (we need both versions to resolve ambiguities):
635 wxCStrData
operator+(int n
) const
636 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
637 wxCStrData
operator+(long n
) const
638 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
639 wxCStrData
operator+(size_t n
) const
640 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
642 // this operator is needed to make expressions like "*c_str()" or
643 // "*(c_str() + 2)" work
644 wxUniChar
operator*() const;
647 const wxString
*m_str
;
651 friend class WXDLLIMPEXP_BASE wxString
;
654 // ----------------------------------------------------------------------------
655 // wxStringPrintfMixin
656 // ---------------------------------------------------------------------------
658 // NB: VC6 has a bug that causes linker errors if you have template methods
659 // in a class using __declspec(dllimport). The solution is to split such
660 // class into two classes, one that contains the template methods and does
661 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
662 // (with DLL linkage).
664 // We only do this for VC6 here, because the code is less efficient
665 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
666 // cannot compile this code.
668 #if defined(__VISUALC__) && __VISUALC__ < 1300
669 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
672 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
673 // this class contains implementation of wxString's vararg methods, it's
674 // exported from wxBase DLL
675 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
678 wxStringPrintfMixinBase() {}
680 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
681 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
684 // this class contains template wrappers for wxString's vararg methods, it's
685 // intentionally *not* exported from the DLL in order to fix the VC6 bug
687 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
690 // to further complicate things, we can't return wxString from
691 // wxStringPrintfMixin::Format() because wxString is not yet declared at
692 // this point; the solution is to use this fake type trait template - this
693 // way the compiler won't know the return type until Format() is used
694 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
695 template<typename T
> struct StringReturnType
697 typedef wxString type
;
701 // these are duplicated wxString methods, they're also declared below
702 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
704 // int Printf(const wxChar *pszFormat, ...);
705 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
706 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
707 WX_DEFINE_VARARG_FUNC(static typename StringReturnType
<T1
>::type
,
709 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
710 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
713 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
715 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
718 // ----------------------------------------------------------------------------
719 // wxString: string class trying to be compatible with std::string, MFC
720 // CString and wxWindows 1.x wxString all at once
721 // ---------------------------------------------------------------------------
723 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
724 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
725 // for dll-interface class 'wxString'" -- this is OK in our case
726 #pragma warning (disable:4275)
729 class WXDLLIMPEXP_BASE wxString
730 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
731 : public wxStringPrintfMixin
734 // NB: special care was taken in arranging the member functions in such order
735 // that all inline functions can be effectively inlined, verify that all
736 // performance critical functions are still inlined if you change order!
738 #if !wxUSE_STL_BASED_WXSTRING
739 // an 'invalid' value for string index, moved to this place due to a CW bug
740 static const size_t npos
;
744 // if we hadn't made these operators private, it would be possible to
745 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
746 // converted to char in C and we do have operator=(char)
748 // NB: we don't need other versions (short/long and unsigned) as attempt
749 // to assign another numeric type to wxString will now result in
750 // ambiguity between operator=(char) and operator=(int)
751 wxString
& operator=(int);
753 // these methods are not implemented - there is _no_ conversion from int to
754 // string, you're doing something wrong if the compiler wants to call it!
756 // try `s << i' or `s.Printf("%d", i)' instead
760 // buffer for holding temporary substring when using any of the methods
761 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
762 // FIXME-UTF8: This will need changes when UTF8 build is introduced
764 struct SubstrBufFromType
769 SubstrBufFromType() {}
770 SubstrBufFromType(const T
& data_
, size_t len_
)
771 : data(data_
), len(len_
) {}
774 #if wxUSE_UNICODE_UTF8
775 // FIXME-UTF8: this will have to use slightly different type
776 #elif wxUSE_UNICODE_WCHAR
777 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
778 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
779 typedef SubstrBufFromWC SubstrBufFrom
;
781 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
782 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
783 typedef SubstrBufFromMB SubstrBufFrom
;
787 // Functions implementing primitive operations on string data; wxString
788 // methods and iterators are implemented in terms of it. The differences
789 // between UTF-8 and wchar_t* representations of the string are mostly
793 // FIXME-UTF8: This will need changes when UTF8 build is introduced
794 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
795 const wxMBConv
& conv
);
797 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
798 const wxMBConv
& conv
);
801 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
802 // returns C string encoded as the implementation expects (version for
803 // the same char type as used internally)
804 static const wxStringCharType
* ImplStr(const wxStringCharType
* str
)
806 static const SubstrBufFrom
ImplStr(const wxStringCharType
* str
, size_t n
)
807 { return SubstrBufFrom(str
, n
); }
809 // returns C string encoded as the implementation expects (version for
810 // the other char type than the one used internally)
811 static wxWCharBuffer
ImplStr(const char* str
)
812 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
813 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
)
814 { return ConvertStr(str
, n
, wxConvLibc
); }
816 static wxCharBuffer
ImplStr(const wchar_t* str
)
817 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
818 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
819 { return ConvertStr(str
, n
, wxConvLibc
); }
822 // moves the iterator to the next Unicode character
823 static void IncIter(wxStringImpl::iterator
& i
) { ++i
; }
824 static void IncIter(wxStringImpl::const_iterator
& i
) { ++i
; }
825 // moves the iterator to the previous Unicode character
826 static void DecIter(wxStringImpl::iterator
& i
) { --i
; }
827 static void DecIter(wxStringImpl::const_iterator
& i
) { --i
; }
828 // moves the iterator by n Unicode characters
829 static wxStringImpl::iterator
AddToIter(wxStringImpl::iterator i
, int n
)
831 static wxStringImpl::const_iterator
AddToIter(wxStringImpl::const_iterator i
, int n
)
833 // returns distance of the two iterators in Unicode characters
834 static int DiffIters(wxStringImpl::iterator i1
, wxStringImpl::iterator i2
)
836 static int DiffIters(wxStringImpl::const_iterator i1
, wxStringImpl::const_iterator i2
)
839 // encodes the character to a form used to represent it in internal
840 // representation (returns a string in UTF8 version)
841 static wxChar
EncodeChar(wxUniChar ch
) { return (wxChar
)ch
; }
843 // translates position index in wxString to/from index in underlying
845 static size_t PosToImpl(size_t pos
) { return pos
; }
846 static void PosLenToImpl(size_t pos
, size_t len
,
847 size_t *implPos
, size_t *implLen
)
848 { *implPos
= pos
; *implLen
= len
; }
849 static size_t PosFromImpl(size_t pos
) { return pos
; }
851 #else // wxUSE_UNICODE_UTF8
853 typedef char Utf8CharBuffer
[5];
854 static Utf8CharBuffer
EncodeChar(wxUniChar ch
);
855 // returns n copies of ch encoded in UTF-8 string
856 static wxCharBuffer
EncodeNChars(size_t n
, wxUniChar ch
);
858 size_t PosToImpl(size_t pos
) const
860 if ( pos
== 0 || pos
== npos
)
863 return wxStringImpl::const_iterator(begin() + pos
) - m_impl
.begin();
866 size_t PosFromImpl(size_t pos
) const
868 if ( pos
== 0 || pos
== npos
)
871 return const_iterator(m_impl
.begin() + pos
) - begin();
874 // FIXME: return as-is without copying under UTF8 locale, return
875 // converted string under other locales - needs wxCharBuffer
877 static wxCharBuffer
ImplStr(const char* str
);
879 static wxCharBuffer
ImplStr(const wchar_t* str
)
880 { return wxConvUTF8
.cWC2MB(str
); }
881 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
885 // constructors and destructor
886 // ctor for an empty string
889 wxString(const wxStringImpl
& stringSrc
) : m_impl(stringSrc
) { }
890 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
) { }
891 // string containing nRepeat copies of ch
892 wxString(wxUniChar ch
, size_t nRepeat
= 1)
893 : m_impl(nRepeat
, ch
) { }
894 wxString(size_t nRepeat
, wxUniChar ch
)
895 : m_impl(nRepeat
, ch
) { }
896 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
897 : m_impl(nRepeat
, ch
) { }
898 wxString(size_t nRepeat
, wxUniCharRef ch
)
899 : m_impl(nRepeat
, ch
) { }
900 wxString(char ch
, size_t nRepeat
= 1)
901 : m_impl(nRepeat
, ch
) { }
902 wxString(size_t nRepeat
, char ch
)
903 : m_impl(nRepeat
, ch
) { }
904 wxString(wchar_t ch
, size_t nRepeat
= 1)
905 : m_impl(nRepeat
, ch
) { }
906 wxString(size_t nRepeat
, wchar_t ch
)
907 : m_impl(nRepeat
, ch
) { }
908 // ctor takes first nLength characters from C string
909 // (default value of npos means take all the string)
910 wxString(const wxChar
*psz
)
911 : m_impl(psz
? psz
: wxT("")) { }
912 wxString(const wxChar
*psz
, size_t nLength
)
913 : m_impl(psz
, nLength
) { }
914 wxString(const wxChar
*psz
,
915 const wxMBConv
& WXUNUSED(conv
),
916 size_t nLength
= npos
)
917 : m_impl(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
919 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
920 // implicit conversions from std::string to wxString as this allows to use
921 // the same strings in non-GUI and GUI code, however we don't want to
922 // unconditionally add this ctor as it would make wx lib dependent on
923 // libstdc++ on some Linux versions which is bad, so instead we ask the
924 // client code to define this wxUSE_STD_STRING symbol if they need it
926 wxString(const wxStdString
& s
)
927 : m_impl(s
.c_str()) { }
928 #endif // wxUSE_STD_STRING
931 // from multibyte string
932 wxString(const char *psz
,
933 const wxMBConv
& conv
= wxConvLibc
,
934 size_t nLength
= npos
);
935 // from multibyte string for ANSI compatibility, with wxConvLibc
936 wxString(const char *psz
, size_t nLength
);
937 // from wxWCharBuffer (i.e. return from wxGetString)
938 wxString(const wxWCharBuffer
& psz
) : m_impl(psz
.data()) { }
940 // from C string (for compilers using unsigned char)
941 wxString(const unsigned char* psz
)
942 : m_impl((const char*)psz
) { }
943 // from part of C string (for compilers using unsigned char)
944 wxString(const unsigned char* psz
, size_t nLength
)
945 : m_impl((const char*)psz
, nLength
) { }
948 // from wide (Unicode) string
949 wxString(const wchar_t *pwz
,
950 const wxMBConv
& conv
= wxConvLibc
,
951 size_t nLength
= npos
);
952 // from wide string for Unicode compatibility, with wxConvLibc
953 wxString(const wchar_t *pwz
, size_t nLength
);
954 #endif // !wxUSE_WCHAR_T
957 wxString(const wxCharBuffer
& psz
)
959 #endif // Unicode/ANSI
961 // as we provide both ctors with this signature for both char and unsigned
962 // char string, we need to provide one for wxCStrData to resolve ambiguity
963 wxString(const wxCStrData
& cstr
, size_t nLength
)
964 { assign(cstr
.AsString(), nLength
); }
966 // and because wxString is convertible to wxCStrData and const wxChar *
967 // we also need to provide this one
968 wxString(const wxString
& str
, size_t nLength
)
969 { assign(str
, nLength
); }
973 typedef wxUniChar value_type
;
974 typedef wxUniChar char_type
;
975 typedef wxUniCharRef reference
;
976 typedef wxChar
* pointer
;
977 typedef const wxChar
* const_pointer
;
979 typedef size_t size_type
;
980 typedef wxUniChar const_reference
;
982 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
983 reference_type, reference_ctor) \
985 typedef wxStringImpl::iterator_name underlying_iterator; \
987 typedef wxUniChar value_type; \
988 typedef reference_type reference; \
989 typedef pointer_type pointer; \
991 iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \
993 reference operator*() const { return reference_ctor; } \
995 iterator_name& operator++() \
996 { wxString::IncIter(m_cur); return *this; } \
997 iterator_name& operator--() \
998 { wxString::DecIter(m_cur); return *this; } \
999 iterator_name operator++(int) \
1001 iterator_name tmp = *this; \
1002 wxString::IncIter(m_cur); \
1005 iterator_name operator--(int) \
1007 iterator_name tmp = *this; \
1008 wxString::DecIter(m_cur); \
1012 iterator_name operator+(int n) const \
1013 { return iterator_name(wxString::AddToIter(m_cur, n)); } \
1014 iterator_name operator+(size_t n) const \
1015 { return iterator_name(wxString::AddToIter(m_cur, (int)n)); } \
1016 iterator_name operator-(int n) const \
1017 { return iterator_name(wxString::AddToIter(m_cur, -n)); } \
1018 iterator_name operator-(size_t n) const \
1019 { return iterator_name(wxString::AddToIter(m_cur, -(int)n)); } \
1020 iterator_name operator+=(int n) \
1021 { m_cur = wxString::AddToIter(m_cur, n); return *this; } \
1022 iterator_name operator+=(size_t n) \
1023 { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \
1024 iterator_name operator-=(int n) \
1025 { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \
1026 iterator_name operator-=(size_t n) \
1027 { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \
1029 unsigned operator-(const iterator_name& i) const \
1030 { return wxString::DiffIters(m_cur, i.m_cur); } \
1032 bool operator==(const iterator_name&i) const \
1033 { return m_cur == i.m_cur; } \
1034 bool operator!=(const iterator_name& i) const \
1035 { return m_cur != i.m_cur; } \
1037 bool operator<(const iterator_name& i) const \
1038 { return m_cur < i.m_cur; } \
1039 bool operator>(const iterator_name& i) const \
1040 { return m_cur > i.m_cur; } \
1041 bool operator<=(const iterator_name& i) const \
1042 { return m_cur <= i.m_cur; } \
1043 bool operator>=(const iterator_name& i) const \
1044 { return m_cur >= i.m_cur; } \
1047 /* for internal wxString use only: */ \
1048 iterator_name(underlying_iterator ptr) : m_cur(ptr) {} \
1049 operator underlying_iterator() const { return m_cur; } \
1051 friend class WXDLLIMPEXP_BASE wxString; \
1052 friend class WXDLLIMPEXP_BASE wxStringImpl; \
1053 friend class WXDLLIMPEXP_BASE wxCStrData; \
1056 underlying_iterator m_cur;
1058 class const_iterator
;
1062 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
1063 wxUniCharRef::CreateForString(m_cur
))
1065 friend class const_iterator
;
1068 class const_iterator
1070 // NB: reference_type is intentionally value, not reference, the character
1071 // may be encoded differently in wxString data:
1072 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
,
1076 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1079 #undef WX_STR_ITERATOR_IMPL
1081 friend class iterator
;
1082 friend class const_iterator
;
1084 template <typename T
>
1085 class reverse_iterator_impl
1088 typedef T iterator_type
;
1089 typedef typename
T::value_type value_type
;
1090 typedef typename
T::reference reference
;
1091 typedef typename
T::pointer
*pointer
;
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
) {}
1097 iterator_type
base() const { return m_cur
; }
1099 reference
operator*() const { return *(m_cur
-1); }
1101 reverse_iterator_impl
& operator++()
1102 { --m_cur
; return *this; }
1103 reverse_iterator_impl
operator++(int)
1104 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1105 reverse_iterator_impl
& operator--()
1106 { ++m_cur
; return *this; }
1107 reverse_iterator_impl
operator--(int)
1108 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1110 reverse_iterator_impl
operator+(int n
) const
1111 { return reverse_iterator_impl(m_cur
- n
); }
1112 reverse_iterator_impl
operator+(size_t n
) const
1113 { return reverse_iterator_impl(m_cur
- n
); }
1114 reverse_iterator_impl
operator-(int n
) const
1115 { return reverse_iterator_impl(m_cur
+ n
); }
1116 reverse_iterator_impl
operator-(size_t n
) const
1117 { return reverse_iterator_impl(m_cur
+ n
); }
1118 reverse_iterator_impl
operator+=(int n
)
1119 { m_cur
-= n
; return *this; }
1120 reverse_iterator_impl
operator+=(size_t n
)
1121 { m_cur
-= n
; return *this; }
1122 reverse_iterator_impl
operator-=(int n
)
1123 { m_cur
+= n
; return *this; }
1124 reverse_iterator_impl
operator-=(size_t n
)
1125 { m_cur
+= n
; return *this; }
1127 unsigned operator-(const reverse_iterator_impl
& i
) const
1128 { return i
.m_cur
- m_cur
; }
1130 bool operator==(const reverse_iterator_impl
& ri
) const
1131 { return m_cur
== ri
.m_cur
; }
1132 bool operator!=(const reverse_iterator_impl
& ri
) const
1133 { return !(*this == ri
); }
1135 bool operator<(const reverse_iterator_impl
& i
) const
1136 { return m_cur
> i
.m_cur
; }
1137 bool operator>(const reverse_iterator_impl
& i
) const
1138 { return m_cur
< i
.m_cur
; }
1139 bool operator<=(const reverse_iterator_impl
& i
) const
1140 { return m_cur
>= i
.m_cur
; }
1141 bool operator>=(const reverse_iterator_impl
& i
) const
1142 { return m_cur
<= i
.m_cur
; }
1145 iterator_type m_cur
;
1148 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1149 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1151 // first valid index position
1152 const_iterator
begin() const { return const_iterator(m_impl
.begin()); }
1153 iterator
begin() { return iterator(m_impl
.begin()); }
1154 // position one after the last valid one
1155 const_iterator
end() const { return const_iterator(m_impl
.end()); }
1156 iterator
end() { return iterator(m_impl
.end()); }
1158 // first element of the reversed string
1159 const_reverse_iterator
rbegin() const
1160 { return const_reverse_iterator(end()); }
1161 reverse_iterator
rbegin()
1162 { return reverse_iterator(end()); }
1163 // one beyond the end of the reversed string
1164 const_reverse_iterator
rend() const
1165 { return const_reverse_iterator(begin()); }
1166 reverse_iterator
rend()
1167 { return reverse_iterator(begin()); }
1169 // std::string methods:
1170 #if wxUSE_UNICODE_UTF8
1171 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1173 size_t length() const { return m_impl
.length(); }
1176 size_type
size() const { return length(); }
1177 size_type
max_size() const { return npos
; }
1179 bool empty() const { return m_impl
.empty(); }
1181 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
1182 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
1184 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1186 #if wxUSE_UNICODE_UTF8
1187 if ( !ch
.IsAscii() )
1189 size_t len
= length();
1192 else if ( nSize
< len
)
1195 append(nSize
- len
, ch
);
1199 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1202 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1205 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1206 return m_impl
.substr(pos
, len
);
1209 // generic attributes & operations
1210 // as standard strlen()
1211 size_t Len() const { return length(); }
1212 // string contains any characters?
1213 bool IsEmpty() const { return empty(); }
1214 // empty string is "false", so !str will return true
1215 bool operator!() const { return empty(); }
1216 // truncate the string to given length
1217 wxString
& Truncate(size_t uiLen
);
1218 // empty string contents
1223 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1225 // empty the string and free memory
1228 wxString
tmp(wxEmptyString
);
1233 // Is an ascii value
1234 bool IsAscii() const;
1236 bool IsNumber() const;
1238 bool IsWord() const;
1240 // data access (all indexes are 0 based)
1242 wxUniChar
at(size_t n
) const
1243 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1244 wxUniChar
GetChar(size_t n
) const
1246 // read/write access
1247 wxUniCharRef
at(size_t n
)
1248 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1249 wxUniCharRef
GetWritableChar(size_t n
)
1252 void SetChar(size_t n
, wxUniChar ch
)
1255 // get last character
1256 wxUniChar
Last() const
1258 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1260 return at(length() - 1);
1263 // get writable last character
1266 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1267 return at(length() - 1);
1271 Note that we we must define all of the overloads below to avoid
1272 ambiguity when using str[0].
1274 wxUniChar
operator[](int n
) const
1276 wxUniChar
operator[](long n
) const
1278 wxUniChar
operator[](size_t n
) const
1280 #ifndef wxSIZE_T_IS_UINT
1281 wxUniChar
operator[](unsigned int n
) const
1283 #endif // size_t != unsigned int
1285 // operator versions of GetWriteableChar()
1286 wxUniCharRef
operator[](int n
)
1288 wxUniCharRef
operator[](long n
)
1290 wxUniCharRef
operator[](size_t n
)
1292 #ifndef wxSIZE_T_IS_UINT
1293 wxUniCharRef
operator[](unsigned int n
)
1295 #endif // size_t != unsigned int
1297 // explicit conversion to C string (use this with printf()!)
1298 wxCStrData
c_str() const { return wxCStrData(this); }
1299 wxCStrData
data() const { return c_str(); }
1301 // implicit conversion to C string
1302 operator wxCStrData() const { return c_str(); }
1303 operator const wxChar
*() const { return c_str(); }
1305 // identical to c_str(), for MFC compatibility
1306 const wxCStrData
GetData() const { return c_str(); }
1308 // explicit conversion to C string in internal representation (char*,
1309 // wchar_t*, UTF-8-encoded char*, depending on the build):
1310 const_pointer
wx_str() const { return m_impl
.c_str(); }
1312 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1313 // converting numbers or strings which are certain not to contain special
1314 // chars (typically system functions, X atoms, environment variables etc.)
1316 // the behaviour of these functions with the strings containing anything
1317 // else than 7 bit ASCII characters is undefined, use at your own risk.
1319 static wxString
FromAscii(const char *ascii
); // string
1320 static wxString
FromAscii(const char ascii
); // char
1321 const wxCharBuffer
ToAscii() const;
1323 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1324 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1325 const char *ToAscii() const { return c_str(); }
1326 #endif // Unicode/!Unicode
1328 // conversions with (possible) format conversions: have to return a
1329 // buffer with temporary data
1331 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1332 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1333 // fn_str() to return a string which should be used with the OS APIs
1334 // accepting the file names. The return value is always the same, but the
1335 // type differs because a function may either return pointer to the buffer
1336 // directly or have to use intermediate buffer for translation.
1338 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1340 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1342 const wxChar
* wc_str() const { return c_str(); }
1344 // for compatibility with !wxUSE_UNICODE version
1345 const wxChar
* wc_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
1348 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1350 const wxChar
* fn_str() const { return c_str(); }
1351 #endif // wxMBFILES/!wxMBFILES
1353 const wxChar
* mb_str() const { return c_str(); }
1355 // for compatibility with wxUSE_UNICODE version
1356 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
1358 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1361 const wxWCharBuffer
wc_str(const wxMBConv
& conv
) const;
1362 #endif // wxUSE_WCHAR_T
1364 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1366 const wxChar
* fn_str() const { return c_str(); }
1368 #endif // Unicode/ANSI
1370 // overloaded assignment
1371 // from another wxString
1372 wxString
& operator=(const wxStringImpl
& stringSrc
)
1373 { m_impl
= stringSrc
; return *this; }
1374 wxString
& operator=(const wxCStrData
& cstr
)
1375 { return *this = cstr
.AsString(); }
1377 wxString
& operator=(wxUniChar ch
)
1378 { m_impl
= EncodeChar(ch
); return *this; }
1379 wxString
& operator=(wxUniCharRef ch
)
1380 { return operator=((wxUniChar
)ch
); }
1381 wxString
& operator=(char ch
)
1382 { return operator=(wxUniChar(ch
)); }
1383 wxString
& operator=(wchar_t ch
)
1384 { return operator=(wxUniChar(ch
)); }
1385 // from a C string - STL probably will crash on NULL,
1386 // so we need to compensate in that case
1387 #if wxUSE_STL_BASED_WXSTRING
1388 wxString
& operator=(const wxChar
*psz
)
1389 { if(psz
) m_impl
= psz
; else Clear(); return *this; }
1391 wxString
& operator=(const wxChar
*psz
)
1392 { m_impl
= psz
; return *this; }
1396 // from wxWCharBuffer
1397 wxString
& operator=(const wxWCharBuffer
& s
)
1398 { (void) operator=((const wchar_t *)s
); return *this; }
1400 wxString
& operator=(const char* psz
)
1401 { return operator=(wxString(psz
)); }
1403 // from another kind of C string
1404 wxString
& operator=(const unsigned char* psz
);
1406 // from a wide string
1407 wxString
& operator=(const wchar_t *pwz
);
1409 // from wxCharBuffer
1410 wxString
& operator=(const wxCharBuffer
& psz
)
1411 { (void) operator=((const char *)psz
); return *this; }
1412 #endif // Unicode/ANSI
1414 // string concatenation
1415 // in place concatenation
1417 Concatenate and return the result. Note that the left to right
1418 associativity of << allows to write things like "str << str1 << str2
1419 << ..." (unlike with +=)
1422 wxString
& operator<<(const wxString
& s
)
1424 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1425 wxASSERT_MSG( s
.IsValid(),
1426 _T("did you forget to call UngetWriteBuf()?") );
1432 // string += C string
1433 wxString
& operator<<(const char *psz
)
1434 { append(psz
); return *this; }
1435 wxString
& operator<<(const wchar_t *pwz
)
1436 { append(pwz
); return *this; }
1437 wxString
& operator<<(const wxCStrData
& psz
)
1438 { append(psz
.AsString()); return *this; }
1440 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1441 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1442 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1443 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1445 // string += buffer (i.e. from wxGetString)
1446 wxString
& operator<<(const wxWCharBuffer
& s
)
1447 { return operator<<((const wchar_t *)s
); }
1448 wxString
& operator+=(const wxWCharBuffer
& s
)
1449 { return operator<<((const wchar_t *)s
); }
1451 wxString
& operator<<(const wxCharBuffer
& s
)
1452 { return operator<<((const char *)s
); }
1453 wxString
& operator+=(const wxCharBuffer
& s
)
1454 { return operator<<((const char *)s
); }
1456 // string += C string
1457 wxString
& Append(const wxString
& s
)
1459 // test for empty() to share the string if possible
1466 wxString
& Append(const wxCStrData
& psz
)
1467 { append(psz
); return *this; }
1468 wxString
& Append(const char* psz
)
1469 { append(psz
); return *this; }
1470 wxString
& Append(const wchar_t* pwz
)
1471 { append(pwz
); return *this; }
1472 // append count copies of given character
1473 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1474 { append(count
, ch
); return *this; }
1475 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1476 { append(count
, ch
); return *this; }
1477 wxString
& Append(char ch
, size_t count
= 1u)
1478 { append(count
, ch
); return *this; }
1479 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1480 { append(count
, ch
); return *this; }
1481 wxString
& Append(const char* psz
, size_t nLen
)
1482 { append(psz
, nLen
); return *this; }
1483 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1484 { append(pwz
, nLen
); return *this; }
1486 // prepend a string, return the string itself
1487 wxString
& Prepend(const wxString
& str
)
1488 { *this = str
+ *this; return *this; }
1490 // non-destructive concatenation
1492 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1493 const wxString
& string2
);
1494 // string with a single char
1495 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1496 // char with a string
1497 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1498 // string with C string
1499 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1501 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1502 const wchar_t *pwz
);
1503 // C string with string
1504 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1505 const wxString
& string
);
1506 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1507 const wxString
& string
);
1509 // stream-like functions
1510 // insert an int into string
1511 wxString
& operator<<(int i
)
1512 { return (*this) << Format(_T("%d"), i
); }
1513 // insert an unsigned int into string
1514 wxString
& operator<<(unsigned int ui
)
1515 { return (*this) << Format(_T("%u"), ui
); }
1516 // insert a long into string
1517 wxString
& operator<<(long l
)
1518 { return (*this) << Format(_T("%ld"), l
); }
1519 // insert an unsigned long into string
1520 wxString
& operator<<(unsigned long ul
)
1521 { return (*this) << Format(_T("%lu"), ul
); }
1522 #if defined wxLongLong_t && !defined wxLongLongIsLong
1523 // insert a long long if they exist and aren't longs
1524 wxString
& operator<<(wxLongLong_t ll
)
1526 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1527 return (*this) << Format(fmt
, ll
);
1529 // insert an unsigned long long
1530 wxString
& operator<<(wxULongLong_t ull
)
1532 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1533 return (*this) << Format(fmt
, ull
);
1536 // insert a float into string
1537 wxString
& operator<<(float f
)
1538 { return (*this) << Format(_T("%f"), f
); }
1539 // insert a double into string
1540 wxString
& operator<<(double d
)
1541 { return (*this) << Format(_T("%g"), d
); }
1543 // string comparison
1544 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1545 int Cmp(const char *psz
) const
1546 { return compare(psz
); }
1547 int Cmp(const wchar_t *pwz
) const
1548 { return compare(pwz
); }
1549 int Cmp(const wxString
& s
) const
1550 { return compare(s
); }
1551 // same as Cmp() but not case-sensitive
1552 int CmpNoCase(const wxString
& s
) const;
1553 int CmpNoCase(const char *psz
) const
1554 { return CmpNoCase(wxString(psz
)); }
1555 int CmpNoCase(const wchar_t *pwz
) const
1556 { return CmpNoCase(wxString(pwz
)); }
1557 // test for the string equality, either considering case or not
1558 // (if compareWithCase then the case matters)
1559 bool IsSameAs(const char *psz
, bool compareWithCase
= true) const
1560 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
1561 bool IsSameAs(const wchar_t *pwz
, bool compareWithCase
= true) const
1562 { return (compareWithCase
? Cmp(pwz
) : CmpNoCase(pwz
)) == 0; }
1563 // comparison with a single character: returns true if equal
1564 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1566 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1567 : wxToupper(GetChar(0u)) == wxToupper(c
));
1570 // simple sub-string extraction
1571 // return substring starting at nFirst of length nCount (or till the end
1572 // if nCount = default value)
1573 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1575 // operator version of Mid()
1576 wxString
operator()(size_t start
, size_t len
) const
1577 { return Mid(start
, len
); }
1579 // check if the string starts with the given prefix and return the rest
1580 // of the string in the provided pointer if it is not NULL; otherwise
1582 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1583 // check if the string ends with the given suffix and return the
1584 // beginning of the string before the suffix in the provided pointer if
1585 // it is not NULL; otherwise return false
1586 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1588 // get first nCount characters
1589 wxString
Left(size_t nCount
) const;
1590 // get last nCount characters
1591 wxString
Right(size_t nCount
) const;
1592 // get all characters before the first occurance of ch
1593 // (returns the whole string if ch not found)
1594 wxString
BeforeFirst(wxUniChar ch
) const;
1595 // get all characters before the last occurence of ch
1596 // (returns empty string if ch not found)
1597 wxString
BeforeLast(wxUniChar ch
) const;
1598 // get all characters after the first occurence of ch
1599 // (returns empty string if ch not found)
1600 wxString
AfterFirst(wxUniChar ch
) const;
1601 // get all characters after the last occurence of ch
1602 // (returns the whole string if ch not found)
1603 wxString
AfterLast(wxUniChar ch
) const;
1605 // for compatibility only, use more explicitly named functions above
1606 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1607 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1610 // convert to upper case in place, return the string itself
1611 wxString
& MakeUpper();
1612 // convert to upper case, return the copy of the string
1613 // Here's something to remember: BC++ doesn't like returns in inlines.
1614 wxString
Upper() const ;
1615 // convert to lower case in place, return the string itself
1616 wxString
& MakeLower();
1617 // convert to lower case, return the copy of the string
1618 wxString
Lower() const ;
1620 // trimming/padding whitespace (either side) and truncating
1621 // remove spaces from left or from right (default) side
1622 wxString
& Trim(bool bFromRight
= true);
1623 // add nCount copies chPad in the beginning or at the end (default)
1624 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1626 // searching and replacing
1627 // searching (return starting index, or -1 if not found)
1628 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1629 // searching (return starting index, or -1 if not found)
1630 int Find(const wxChar
*pszSub
) const; // like strstr
1631 // replace first (or all of bReplaceAll) occurences of substring with
1632 // another string, returns the number of replacements made
1633 size_t Replace(const wxChar
*szOld
,
1634 const wxChar
*szNew
,
1635 bool bReplaceAll
= true);
1637 // check if the string contents matches a mask containing '*' and '?'
1638 bool Matches(const wxChar
*szMask
) const;
1640 // conversion to numbers: all functions return true only if the whole
1641 // string is a number and put the value of this number into the pointer
1642 // provided, the base is the numeric base in which the conversion should be
1643 // done and must be comprised between 2 and 36 or be 0 in which case the
1644 // standard C rules apply (leading '0' => octal, "0x" => hex)
1645 // convert to a signed integer
1646 bool ToLong(long *val
, int base
= 10) const;
1647 // convert to an unsigned integer
1648 bool ToULong(unsigned long *val
, int base
= 10) const;
1649 // convert to wxLongLong
1650 #if defined(wxLongLong_t)
1651 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1652 // convert to wxULongLong
1653 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1654 #endif // wxLongLong_t
1655 // convert to a double
1656 bool ToDouble(double *val
) const;
1659 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1660 // formatted input/output
1661 // as sprintf(), returns the number of characters written or < 0 on error
1662 // (take 'this' into account in attribute parameter count)
1663 // int Printf(const wxChar *pszFormat, ...);
1664 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
1665 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1666 // as vprintf(), returns the number of characters written or < 0 on error
1667 int PrintfV(const wxString
& format
, va_list argptr
);
1669 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1670 // returns the string containing the result of Printf() to it
1671 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1672 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, DoFormat
)
1674 // the same as above, but takes a va_list
1675 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1677 // raw access to string memory
1678 // ensure that string has space for at least nLen characters
1679 // only works if the data of this string is not shared
1680 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1681 // minimize the string's memory
1682 // only works if the data of this string is not shared
1684 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1685 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1687 // get writable buffer of at least nLen bytes. Unget() *must* be called
1688 // a.s.a.p. to put string back in a reasonable state!
1689 wxDEPRECATED( wxChar
*GetWriteBuf(size_t nLen
) );
1690 // call this immediately after GetWriteBuf() has been used
1691 wxDEPRECATED( void UngetWriteBuf() );
1692 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1693 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1695 // wxWidgets version 1 compatibility functions
1698 wxString
SubString(size_t from
, size_t to
) const
1699 { return Mid(from
, (to
- from
+ 1)); }
1700 // values for second parameter of CompareTo function
1701 enum caseCompare
{exact
, ignoreCase
};
1702 // values for first parameter of Strip function
1703 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1705 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1707 // (take 'this' into account in attribute parameter count)
1708 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1709 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
1710 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1713 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1714 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1717 size_t Length() const { return length(); }
1718 // Count the number of characters
1719 int Freq(wxUniChar ch
) const;
1721 void LowerCase() { MakeLower(); }
1723 void UpperCase() { MakeUpper(); }
1724 // use Trim except that it doesn't change this string
1725 wxString
Strip(stripType w
= trailing
) const;
1727 // use Find (more general variants not yet supported)
1728 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1729 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1731 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1732 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1734 wxString
& Remove(size_t nStart
, size_t nLen
)
1735 { return (wxString
&)erase( nStart
, nLen
); }
1738 int First( wxUniChar ch
) const { return Find(ch
); }
1739 int First( char ch
) const { return Find(ch
); }
1740 int First( wchar_t ch
) const { return Find(ch
); }
1741 int First( const wxChar
* psz
) const { return Find(psz
); }
1742 int First( const wxString
&str
) const { return Find(str
); }
1743 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1744 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1747 bool IsNull() const { return empty(); }
1749 // std::string compatibility functions
1751 // take nLen chars starting at nPos
1752 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1753 : m_impl(str
.m_impl
, nPos
, nLen
) { }
1754 // take all characters from pStart to pEnd
1755 wxString(const void *pStart
, const void *pEnd
)
1756 : m_impl((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1757 wxString(const_iterator first
, const_iterator last
)
1758 : m_impl(first
, last
) { }
1759 wxString(iterator first
, iterator last
)
1760 : m_impl(first
, last
) { }
1762 // lib.string.modifiers
1763 // append elements str[pos], ..., str[pos+n]
1764 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1767 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1768 m_impl
.append(str
.m_impl
, from
, len
);
1772 wxString
& append(const wxString
& str
)
1773 { m_impl
.append(str
.m_impl
); return *this; }
1774 wxString
& append(const wxCStrData
& str
)
1775 { m_impl
.append(str
.AsString().m_impl
); return *this; }
1776 // append first n (or all if n == npos) characters of sz
1777 wxString
& append(const char *sz
)
1778 { m_impl
.append(ImplStr(sz
)); return *this; }
1779 wxString
& append(const wchar_t *sz
)
1780 { m_impl
.append(ImplStr(sz
)); return *this; }
1781 wxString
& append(const char *sz
, size_t n
)
1783 SubstrBufFromMB
str(ImplStr(sz
, n
));
1784 m_impl
.append(str
.data
, str
.len
);
1787 wxString
& append(const wchar_t *sz
, size_t n
)
1789 SubstrBufFromWC
str(ImplStr(sz
, n
));
1790 m_impl
.append(str
.data
, str
.len
);
1793 // append n copies of ch
1794 wxString
& append(size_t n
, wxUniChar ch
)
1796 #if wxUSE_UNICODE_UTF8
1797 if ( !ch
.IsAscii() )
1798 m_impl
.append(EncodeNChars(n
, ch
));
1801 m_impl
.append(n
, (wxStringCharType
)ch
);
1804 // append from first to last
1805 wxString
& append(const_iterator first
, const_iterator last
)
1806 { m_impl
.append(first
, last
); return *this; }
1808 // same as `this_string = str'
1809 wxString
& assign(const wxString
& str
)
1810 { m_impl
= str
.m_impl
; return *this; }
1811 // same as ` = str[pos..pos + n]
1812 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1815 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1816 m_impl
.assign(str
.m_impl
, from
, len
);
1819 // same as `= first n (or all if n == npos) characters of sz'
1820 wxString
& assign(const char *sz
)
1821 { m_impl
.assign(ImplStr(sz
)); return *this; }
1822 wxString
& assign(const wchar_t *sz
)
1823 { m_impl
.assign(ImplStr(sz
)); return *this; }
1824 wxString
& assign(const char *sz
, size_t n
)
1826 SubstrBufFromMB
str(ImplStr(sz
, n
));
1827 m_impl
.assign(str
.data
, str
.len
);
1830 wxString
& assign(const wchar_t *sz
, size_t n
)
1832 SubstrBufFromWC
str(ImplStr(sz
, n
));
1833 m_impl
.assign(str
.data
, str
.len
);
1836 // same as `= n copies of ch'
1837 wxString
& assign(size_t n
, wxUniChar ch
)
1839 #if wxUSE_UNICODE_UTF8
1840 if ( !ch
.IsAscii() )
1841 m_impl
.assign(EncodeNChars(n
, ch
));
1844 m_impl
.assign(n
, (wxStringCharType
)ch
);
1847 // assign from first to last
1848 wxString
& assign(const_iterator first
, const_iterator last
)
1849 { m_impl
.assign(first
, last
); return *this; }
1851 // string comparison
1852 int compare(const wxString
& str
) const;
1853 // comparison with a substring
1854 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1855 // comparison of 2 substrings
1856 int compare(size_t nStart
, size_t nLen
,
1857 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1858 // just like strcmp()
1859 int compare(const char* sz
) const;
1860 int compare(const wchar_t* sz
) const;
1861 // substring comparison with first nCount characters of sz
1862 int compare(size_t nStart
, size_t nLen
,
1863 const char* sz
, size_t nCount
= npos
) const;
1864 int compare(size_t nStart
, size_t nLen
,
1865 const wchar_t* sz
, size_t nCount
= npos
) const;
1867 // insert another string
1868 wxString
& insert(size_t nPos
, const wxString
& str
)
1869 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
1870 // insert n chars of str starting at nStart (in str)
1871 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1874 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
1875 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
1878 // insert first n (or all if n == npos) characters of sz
1879 wxString
& insert(size_t nPos
, const char *sz
)
1880 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1881 wxString
& insert(size_t nPos
, const wchar_t *sz
)
1882 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1883 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
1885 SubstrBufFromMB
str(ImplStr(sz
, n
));
1886 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1889 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
1891 SubstrBufFromWC
str(ImplStr(sz
, n
));
1892 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1895 // insert n copies of ch
1896 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
1898 #if wxUSE_UNICODE_UTF8
1899 if ( !ch
.IsAscii() )
1900 m_impl
.insert(begin() + nPos
, EncodeNChars(n
, ch
));
1903 m_impl
.insert(begin() + nPos
, n
, (wxStringCharType
)ch
);
1906 iterator
insert(iterator it
, wxUniChar ch
)
1907 { return iterator(m_impl
.insert(it
, EncodeChar(ch
))); }
1908 void insert(iterator it
, const_iterator first
, const_iterator last
)
1909 { m_impl
.insert(it
, first
, last
); }
1910 void insert(iterator it
, size_type n
, wxUniChar ch
)
1912 #if wxUSE_UNICODE_UTF8
1913 if ( !ch
.IsAscii() )
1914 m_impl
.insert(it
, EncodeNChars(n
, ch
));
1917 m_impl
.insert(it
, n
, (wxStringCharType
)ch
);
1920 // delete characters from nStart to nStart + nLen
1921 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1924 PosLenToImpl(pos
, n
, &from
, &len
);
1925 m_impl
.erase(from
, len
);
1928 iterator
erase(iterator first
, iterator last
)
1929 { return iterator(m_impl
.erase(first
, last
)); }
1930 iterator
erase(iterator first
)
1931 { return iterator(m_impl
.erase(first
)); }
1933 #ifdef wxSTRING_BASE_HASNT_CLEAR
1934 void clear() { erase(); }
1936 void clear() { m_impl
.clear(); }
1939 // replaces the substring of length nLen starting at nStart
1940 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
1943 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1944 m_impl
.replace(from
, len
, ImplStr(sz
));
1947 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
1950 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1951 m_impl
.replace(from
, len
, ImplStr(sz
));
1954 // replaces the substring of length nLen starting at nStart
1955 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1958 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1959 m_impl
.replace(from
, len
, str
.m_impl
);
1962 // replaces the substring with nCount copies of ch
1963 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
1966 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1967 #if wxUSE_UNICODE_UTF8
1968 if ( !ch
.IsAscii() )
1969 m_impl
.replace(from
, len
, EncodeNChars(nCount
, ch
));
1972 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
1975 // replaces a substring with another substring
1976 wxString
& replace(size_t nStart
, size_t nLen
,
1977 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1980 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1983 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
1985 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
1988 // replaces the substring with first nCount chars of sz
1989 wxString
& replace(size_t nStart
, size_t nLen
,
1990 const char* sz
, size_t nCount
)
1993 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1995 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
1997 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2000 wxString
& replace(size_t nStart
, size_t nLen
,
2001 const wchar_t* sz
, size_t nCount
)
2004 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2006 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2008 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2011 wxString
& replace(iterator first
, iterator last
, const char* s
)
2012 { m_impl
.replace(first
, last
, ImplStr(s
)); return *this; }
2013 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2014 { m_impl
.replace(first
, last
, ImplStr(s
)); return *this; }
2015 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2017 SubstrBufFromMB
str(ImplStr(s
, n
));
2018 m_impl
.replace(first
, last
, str
.data
, str
.len
);
2021 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2023 SubstrBufFromWC
str(ImplStr(s
, n
));
2024 m_impl
.replace(first
, last
, str
.data
, str
.len
);
2027 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2028 { m_impl
.replace(first
, last
, s
.m_impl
); return *this; }
2029 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2031 #if wxUSE_UNICODE_UTF8
2032 if ( !ch
.IsAscii() )
2033 m_impl
.replace(first
, last
, EncodeNChars(n
, ch
));
2036 m_impl
.replace(first
, last
, n
, (wxStringCharType
)ch
);
2039 wxString
& replace(iterator first
, iterator last
,
2040 const_iterator first1
, const_iterator last1
)
2041 { m_impl
.replace(first
, last
, first1
, last1
); return *this; }
2044 void swap(wxString
& str
)
2045 { m_impl
.swap(str
.m_impl
); }
2048 size_t find(const wxString
& str
, size_t nStart
= 0) const
2049 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2051 // find first n characters of sz
2052 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2054 SubstrBufFromMB
str(ImplStr(sz
, n
));
2055 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2057 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2059 SubstrBufFromWC
str(ImplStr(sz
, n
));
2060 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2063 // find the first occurence of character ch after nStart
2064 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2065 { return PosFromImpl(m_impl
.find(EncodeChar(ch
), PosToImpl(nStart
))); }
2066 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2067 { return find(wxUniChar(ch
), nStart
); }
2068 size_t find(char ch
, size_t nStart
= 0) const
2069 { return find(wxUniChar(ch
), nStart
); }
2070 size_t find(wchar_t ch
, size_t nStart
= 0) const
2071 { return find(wxUniChar(ch
), nStart
); }
2073 // rfind() family is exactly like find() but works right to left
2075 // as find, but from the end
2076 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2077 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2079 // as find, but from the end
2080 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2082 SubstrBufFromMB
str(ImplStr(sz
, n
));
2083 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2085 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2087 SubstrBufFromWC
str(ImplStr(sz
, n
));
2088 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2090 // as find, but from the end
2091 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2092 { return PosFromImpl(m_impl
.rfind(EncodeChar(ch
), PosToImpl(nStart
))); }
2093 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2094 { return rfind(wxUniChar(ch
), nStart
); }
2095 size_t rfind(char ch
, size_t nStart
= npos
) const
2096 { return rfind(wxUniChar(ch
), nStart
); }
2097 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2098 { return rfind(wxUniChar(ch
), nStart
); }
2100 // find first/last occurence of any character (not) in the set:
2101 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2102 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2103 // sizeof(wchar_t)==2 and surrogates are present in the string;
2104 // should we care? Probably not.
2105 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2106 { return m_impl
.find_first_of(str
.impl
, nStart
); }
2107 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2108 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2109 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2110 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2111 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2112 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2113 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2114 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2115 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2116 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2118 size_t find_last_of(const wxStringImpl
& str
, size_t nStart
= npos
) const
2119 { return m_impl
.find_last_of(str
.impl
, nStart
); }
2120 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2121 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2122 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2123 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2124 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2125 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2126 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2127 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2128 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2129 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2131 size_t find_first_not_of(const wxStringImpl
& str
, size_t nStart
= 0) const
2132 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2133 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2134 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2135 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2136 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2137 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2138 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2139 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2140 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2141 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const;
2142 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2144 size_t find_last_not_of(const wxStringImpl
& str
, size_t nStart
= npos
) const
2145 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2146 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2147 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2148 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2149 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2150 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2151 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2152 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2153 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2154 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const;
2155 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2157 // we can't use std::string implementation in UTF-8 build, because the
2158 // character sets would be interpreted wrongly:
2160 // as strpbrk() but starts at nStart, returns npos if not found
2161 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2162 { return find_first_of((const wxChar
*)str
.c_str(), nStart
); }
2164 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2165 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2166 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2167 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2168 // same as find(char, size_t)
2169 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2170 { return find(c
, nStart
); }
2171 // find the last (starting from nStart) char from str in this string
2172 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2173 { return find_last_of((const wxChar
*)str
.c_str(), nStart
); }
2175 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2176 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2177 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2178 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2180 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2181 { return rfind(c
, nStart
); }
2183 // find first/last occurence of any character not in the set
2185 // as strspn() (starting from nStart), returns npos on failure
2186 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2187 { return find_first_not_of((const wxChar
*)str
.c_str(), nStart
); }
2189 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2190 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2191 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2192 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2194 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2196 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2197 { return find_last_not_of((const wxChar
*)str
.c_str(), nStart
); }
2199 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2200 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2201 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2202 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2204 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2205 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2207 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2208 // above to resolve ambiguities:
2209 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2210 { return find_first_of(wxUniChar(ch
), nStart
); }
2211 size_t find_first_of(char ch
, size_t nStart
= 0) const
2212 { return find_first_of(wxUniChar(ch
), nStart
); }
2213 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2214 { return find_first_of(wxUniChar(ch
), nStart
); }
2215 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2216 { return find_last_of(wxUniChar(ch
), nStart
); }
2217 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2218 { return find_last_of(wxUniChar(ch
), nStart
); }
2219 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2220 { return find_last_of(wxUniChar(ch
), nStart
); }
2221 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2222 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2223 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2224 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2225 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2226 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2227 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2228 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2229 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2230 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2231 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2232 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2235 wxString
& operator+=(const wxString
& s
)
2236 { m_impl
+= s
.m_impl
; return *this; }
2237 // string += C string
2238 wxString
& operator+=(const char *psz
)
2239 { m_impl
+= ImplStr(psz
); return *this; }
2240 wxString
& operator+=(const wchar_t *pwz
)
2241 { m_impl
+= ImplStr(pwz
); return *this; }
2242 wxString
& operator+=(const wxCStrData
& s
)
2243 { m_impl
+= s
.AsString().m_impl
; return *this; }
2245 wxString
& operator+=(wxUniChar ch
)
2246 { m_impl
+= EncodeChar(ch
); return *this; }
2247 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2248 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2249 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2250 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2253 #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2254 // helpers for wxStringBuffer and wxStringBufferLength
2255 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2256 { return m_impl
.DoGetWriteBuf(nLen
); }
2257 void DoUngetWriteBuf()
2258 { m_impl
.DoUngetWriteBuf(); }
2259 void DoUngetWriteBuf(size_t nLen
)
2260 { m_impl
.DoUngetWriteBuf(nLen
); }
2262 friend class WXDLLIMPEXP_BASE wxStringBuffer
;
2263 friend class WXDLLIMPEXP_BASE wxStringBufferLength
;
2264 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2266 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2267 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
2268 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
2271 #if !wxUSE_STL_BASED_WXSTRING
2272 // check string's data validity
2273 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2277 wxStringImpl m_impl
;
2280 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2281 #pragma warning (default:4275)
2284 // notice that even though for many compilers the friend declarations above are
2285 // enough, from the point of view of C++ standard we must have the declarations
2286 // here as friend ones are not injected in the enclosing namespace and without
2287 // them the code fails to compile with conforming compilers such as xlC or g++4
2288 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2289 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2290 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2291 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2292 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2294 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2295 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2297 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2298 { return string
+ (wxUniChar
)ch
; }
2299 inline wxString
operator+(const wxString
& string
, char ch
)
2300 { return string
+ wxUniChar(ch
); }
2301 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2302 { return string
+ wxUniChar(ch
); }
2303 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2304 { return (wxUniChar
)ch
+ string
; }
2305 inline wxString
operator+(char ch
, const wxString
& string
)
2306 { return wxUniChar(ch
) + string
; }
2307 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2308 { return wxUniChar(ch
) + string
; }
2311 #if wxUSE_STL_BASED_WXSTRING
2312 // return an empty wxString (not very useful with wxUSE_STL == 1)
2313 inline const wxString
wxGetEmptyString() { return wxString(); }
2314 #else // !wxUSE_STL_BASED_WXSTRING
2315 // return an empty wxString (more efficient than wxString() here)
2316 inline const wxString
& wxGetEmptyString()
2318 return *(wxString
*)&wxEmptyString
;
2320 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2322 // ----------------------------------------------------------------------------
2323 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2324 // ----------------------------------------------------------------------------
2326 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2328 class WXDLLIMPEXP_BASE wxStringBuffer
2331 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2332 : m_str(str
), m_buf(lenWanted
)
2335 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
2337 operator wxChar
*() { return m_buf
.data(); }
2342 wxWCharBuffer m_buf
;
2347 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
2350 class WXDLLIMPEXP_BASE wxStringBufferLength
2353 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2354 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2357 ~wxStringBufferLength()
2360 m_str
.assign(m_buf
.data(), m_len
);
2363 operator wxChar
*() { return m_buf
.data(); }
2364 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2369 wxWCharBuffer m_buf
;
2376 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
2379 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2381 class WXDLLIMPEXP_BASE wxStringBuffer
2384 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2385 : m_str(str
), m_buf(NULL
)
2386 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2388 ~wxStringBuffer() { m_str
.DoUngetWriteBuf(); }
2390 operator wxChar
*() const { return m_buf
; }
2396 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
2399 class WXDLLIMPEXP_BASE wxStringBufferLength
2402 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2403 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2405 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2406 wxASSERT(m_buf
!= NULL
);
2409 ~wxStringBufferLength()
2412 m_str
.DoUngetWriteBuf(m_len
);
2415 operator wxChar
*() const { return m_buf
; }
2416 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2424 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
2427 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2429 // ---------------------------------------------------------------------------
2430 // wxString comparison functions: operator versions are always case sensitive
2431 // ---------------------------------------------------------------------------
2433 // note that when wxUSE_STL_BASED_WXSTRING == 1 the comparison operators taking
2434 // std::string are used and defining them also for wxString would only result
2435 // in compilation ambiguities when comparing std::string and wxString
2436 #if !wxUSE_STL_BASED_WXSTRING
2438 #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0
2440 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2442 #undef wxCMP_WXCHAR_STRING
2444 // note that there is an optimization in operator==() and !=(): we (quickly)
2445 // checks the strings length first, before comparing their data
2446 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2447 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2448 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2449 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2450 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2451 { return s1
.Cmp(s2
) < 0; }
2452 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2453 { return s1
.Cmp(s2
) > 0; }
2454 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2455 { return s1
.Cmp(s2
) <= 0; }
2456 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2457 { return s1
.Cmp(s2
) >= 0; }
2460 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2461 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2462 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2463 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2464 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2465 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2466 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2467 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2468 #else // !wxUSE_UNICODE
2469 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2470 { return (s1
.Cmp((const char *)s2
) == 0); }
2471 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2472 { return (s2
.Cmp((const char *)s1
) == 0); }
2473 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2474 { return (s1
.Cmp((const char *)s2
) != 0); }
2475 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2476 { return (s2
.Cmp((const char *)s1
) != 0); }
2477 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2480 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2481 { return string
+ (const wchar_t *)buf
; }
2482 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2483 { return (const wchar_t *)buf
+ string
; }
2484 #else // !wxUSE_UNICODE
2485 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2486 { return string
+ (const char *)buf
; }
2487 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2488 { return (const char *)buf
+ string
; }
2489 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2491 #endif // !wxUSE_STL_BASED_WXSTRING
2493 // comparison with char (those are not defined by std::[w]string and so should
2494 // be always available)
2495 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2496 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2497 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2498 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2499 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2500 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2501 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2502 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2503 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2504 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2505 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2506 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2507 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2508 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2509 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2510 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2511 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2512 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2514 // comparison with C string in Unicode build
2517 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2519 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2521 #undef wxCMP_CHAR_STRING
2523 #endif // wxUSE_UNICODE
2525 // we also need to provide the operators for comparison with wxCStrData to
2526 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2527 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2529 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2530 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2531 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2533 // FIXME: these ifdefs must be removed when wxCStrData has both conversions
2535 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2537 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2540 #undef wxCMP_CHAR_CSTRDATA
2541 #undef wxCMP_WCHAR_CSTRDATA
2543 // ---------------------------------------------------------------------------
2544 // Implementation only from here until the end of file
2545 // ---------------------------------------------------------------------------
2547 #if wxUSE_STD_IOSTREAM
2549 #include "wx/iosfwrap.h"
2551 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2552 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2554 #endif // wxSTD_STRING_COMPATIBILITY
2556 // ---------------------------------------------------------------------------
2557 // wxCStrData implementation
2558 // ---------------------------------------------------------------------------
2560 inline wxCStrData::wxCStrData(char *buf
)
2561 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2562 inline wxCStrData::wxCStrData(wchar_t *buf
)
2563 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2565 inline wxCStrData::~wxCStrData()
2572 inline const wchar_t* wxCStrData::AsWChar() const
2574 inline const char* wxCStrData::AsChar() const
2577 if ( m_offset
== 0 )
2578 return m_str
->wx_str(); // FIXME-UTF8
2580 return (const wxChar
*)(m_str
->begin() + m_offset
);
2583 inline wxString
wxCStrData::AsString() const
2585 if ( m_offset
== 0 )
2588 return m_str
->Mid(m_offset
);
2591 inline wxCStrData::operator wxString() const { return AsString(); }
2593 inline wxUniChar
wxCStrData::operator*() const
2595 if ( m_str
->empty() )
2596 return wxUniChar(_T('\0'));
2598 return (*m_str
)[m_offset
];
2601 inline wxUniChar
wxCStrData::operator[](size_t n
) const
2603 return m_str
->at(m_offset
+ n
);
2606 // ----------------------------------------------------------------------------
2607 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2608 // ----------------------------------------------------------------------------
2610 // FIXME-UTF8: move this to buffer.h; provide versions for both variants
2611 inline wxWxCharBuffer::wxWxCharBuffer(const wxCStrData
& cstr
)
2612 : wxCharTypeBufferBase((const wxChar
*)cstr
)
2616 #endif // _WX_WXSTRINGH__