X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c57e3bd53e52b819c2cb42cb3d5b222f3b6b495a..6580df1b72539453610530247a0b3bea8dad172a:/include/wx/string.h?ds=inline diff --git a/include/wx/string.h b/include/wx/string.h index 94dc733c6c..71d0b84807 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -51,7 +51,9 @@ #include #endif -#include "wx/wxchar.h" // for wxChar +#include "wx/wxchar.h" // for wxChar, wxStrlen() etc. +#include "wx/unichar.h" +#include "wx/strvararg.h" #include "wx/buffer.h" // for wxCharBuffer #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes @@ -76,9 +78,15 @@ class WXDLLIMPEXP_BASE wxString; // constants // ---------------------------------------------------------------------------- +#if WXWIN_COMPATIBILITY_2_6 + +// deprecated in favour of wxString::npos, don't use in new code +// // maximum possible length for a string means "take all string" everywhere #define wxSTRING_MAXLEN wxStringBase::npos +#endif // WXWIN_COMPATIBILITY_2_6 + // ---------------------------------------------------------------------------- // global data // ---------------------------------------------------------------------------- @@ -91,17 +99,21 @@ extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString; // strlen() and portable strcasecmp() //--------------------------------------------------------------------------- -// Use wxXXX() functions from wxchar.h instead! These functions are for +#if WXWIN_COMPATIBILITY_2_8 +// Use wxXXX() functions from wxcrt.h instead! These functions are for // backwards compatibility only. // checks whether the passed in pointer is NULL and if the string is empty +wxDEPRECATED( inline bool IsEmpty(const char *p) ); inline bool IsEmpty(const char *p) { return (!p || !*p); } // safe version of strlen() (returns 0 if passed NULL pointer) +wxDEPRECATED( inline size_t Strlen(const char *psz) ); inline size_t Strlen(const char *psz) { return psz ? strlen(psz) : 0; } // portable strcasecmp/_stricmp +wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) ); inline int Stricmp(const char *psz1, const char *psz2) { #if defined(__VISUALC__) && defined(__WXWINCE__) @@ -159,12 +171,14 @@ inline int Stricmp(const char *psz1, const char *psz2) #endif // OS/compiler } +#endif // WXWIN_COMPATIBILITY_2_8 + // ---------------------------------------------------------------------------- // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING // ---------------------------------------------------------------------------- // in both cases we need to define wxStdString -#if wxUSE_STL || defined(wxUSE_STD_STRING) +#if wxUSE_STL || wxUSE_STD_STRING #include "wx/beforestd.h" #include @@ -187,6 +201,7 @@ inline int Stricmp(const char *psz1, const char *psz2) // we don't need an extra ctor from std::string when copy ctor already does // the work #undef wxUSE_STD_STRING + #define wxUSE_STD_STRING 0 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \ (defined(_MSC_VER) && (_MSC_VER <= 1200)) @@ -196,7 +211,8 @@ inline int Stricmp(const char *psz1, const char *psz2) typedef wxStdString wxStringBase; #else // if !wxUSE_STL -#ifndef HAVE_STD_STRING_COMPARE +#if !defined(HAVE_STD_STRING_COMPARE) && \ + (!defined(__WX_SETUP_H__) || wxUSE_STL == 0) #define HAVE_STD_STRING_COMPARE #endif @@ -243,9 +259,6 @@ struct WXDLLIMPEXP_BASE wxStringData class WXDLLIMPEXP_BASE wxStringBase { -#if !wxUSE_STL -friend class WXDLLIMPEXP_BASE wxArrayString; -#endif public : // an 'invalid' value for string index, moved to this place due to a CW bug static const size_t npos; @@ -260,7 +273,7 @@ protected: // initializes the string to the empty value (must be called only from // ctors, use Reinit() otherwise) void Init() { m_pchData = (wxChar *)wxEmptyString; } - // initializaes the string with (a part of) C-string + // initializes the string with (a part of) C-string void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = npos); // as Init, but also frees old data void Reinit() { GetStringData()->Unlock(); Init(); } @@ -287,15 +300,142 @@ protected: bool Alloc(size_t nLen); public: // standard types - typedef wxChar value_type; - typedef wxChar char_type; + typedef wxUniChar value_type; + typedef wxUniChar char_type; + typedef wxUniCharRef reference; + typedef wxChar* pointer; + typedef const wxChar* const_pointer; + typedef size_t size_type; - typedef value_type& reference; - typedef const value_type& const_reference; - typedef value_type* pointer; - typedef const value_type* const_pointer; - typedef value_type *iterator; - typedef const value_type *const_iterator; + typedef wxUniChar const_reference; + + #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \ + reference_type, reference_ctor) \ + public: \ + typedef wxUniChar value_type; \ + typedef reference_type reference; \ + typedef pointer_type pointer; \ + \ + iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \ + \ + reference operator*() const { return reference_ctor; } \ + \ + iterator_name& operator++() \ + { ++m_cur; return *this; } \ + iterator_name operator++(int) \ + { iterator_name tmp = *this; ++m_cur; return tmp; } \ + iterator_name& operator--() \ + { --m_cur; return *this; } \ + iterator_name operator--(int) \ + { iterator_name tmp = *this; --m_cur; return tmp; } \ + \ + iterator_name operator+(int n) const \ + { return iterator_name(m_cur + n); } \ + iterator_name operator+(size_t n) const \ + { return iterator_name(m_cur + n); } \ + iterator_name operator-(int n) const \ + { return iterator_name(m_cur - n); } \ + iterator_name operator-(size_t n) const \ + { return iterator_name(m_cur - n); } \ + iterator_name operator+=(int n) \ + { m_cur += n; return *this; } \ + iterator_name operator+=(size_t n) \ + { m_cur += n; return *this; } \ + iterator_name operator-=(int n) \ + { m_cur -= n; return *this; } \ + iterator_name operator-=(size_t n) \ + { m_cur -= n; return *this; } \ + \ + unsigned operator-(const iterator_name& i) const \ + { return m_cur - i.m_cur; } \ + \ + bool operator==(const iterator_name&i) const \ + { return m_cur == i.m_cur; } \ + bool operator!=(const iterator_name& i) const \ + { return m_cur != i.m_cur; } \ + \ + bool operator<(const iterator_name& i) const \ + { return m_cur < i.m_cur; } \ + bool operator>(const iterator_name& i) const \ + { return m_cur > i.m_cur; } \ + bool operator<=(const iterator_name& i) const \ + { return m_cur <= i.m_cur; } \ + bool operator>=(const iterator_name& i) const \ + { return m_cur >= i.m_cur; } \ + \ + protected: \ + /* for internal wxString use only: */ \ + iterator_name(pointer ptr) : m_cur(ptr) {} \ + operator pointer() const { return m_cur; } \ + \ + friend class WXDLLIMPEXP_BASE wxString; \ + friend class WXDLLIMPEXP_BASE wxStringBase; \ + friend class WXDLLIMPEXP_BASE wxCStrData; \ + \ + protected: \ + pointer m_cur; + + class const_iterator; + + class iterator + { + WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, + wxUniCharRef::CreateForString(m_cur)) + + friend class const_iterator; + }; + + class const_iterator + { + // NB: reference_type is intentionally value, not reference, the character + // may be encoded differently in wxString data: + WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar, + wxUniChar(*m_cur)) + + public: + const_iterator(const iterator& i) : m_cur(i.m_cur) {} + }; + + #undef WX_STR_ITERATOR + + template + class reverse_iterator_impl + { + public: + typedef T iterator_type; + typedef typename T::value_type value_type; + typedef typename T::reference reference; + typedef typename T::pointer *pointer; + + reverse_iterator_impl(iterator_type i) : m_cur(i) {} + reverse_iterator_impl(const reverse_iterator_impl& ri) + : m_cur(ri.m_cur) {} + + iterator_type base() const { return m_cur; } + + reference operator*() const { return *(m_cur-1); } + + reverse_iterator_impl& operator++() + { --m_cur; return *this; } + reverse_iterator_impl operator++(int) + { reverse_iterator_impl tmp = *this; --m_cur; return tmp; } + reverse_iterator_impl& operator--() + { ++m_cur; return *this; } + reverse_iterator_impl operator--(int) + { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; } + + bool operator==(const reverse_iterator_impl& ri) const + { return m_cur == ri.m_cur; } + bool operator!=(const reverse_iterator_impl& ri) const + { return !(*this == ri); } + + private: + iterator_type m_cur; + }; + + typedef reverse_iterator_impl reverse_iterator; + typedef reverse_iterator_impl const_reverse_iterator; + // constructors and destructor // ctor for an empty string @@ -316,14 +456,16 @@ public: } } // string containing nRepeat copies of ch - wxStringBase(size_type nRepeat, wxChar ch); + wxStringBase(size_type nRepeat, wxUniChar ch); // ctor takes first nLength characters from C string // (default value of npos means take all the string) wxStringBase(const wxChar *psz) { InitWith(psz, 0, npos); } wxStringBase(const wxChar *psz, size_t nLength) { InitWith(psz, 0, nLength); } - wxStringBase(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = npos) + wxStringBase(const wxChar *psz, + const wxMBConv& WXUNUSED(conv), + size_t nLength = npos) { InitWith(psz, 0, nLength); } // take nLen chars starting at nPos wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen) @@ -357,22 +499,22 @@ public: // from another wxString wxStringBase& operator=(const wxStringBase& stringSrc); // from a character - wxStringBase& operator=(wxChar ch); + wxStringBase& operator=(wxUniChar ch); // from a C string wxStringBase& operator=(const wxChar *psz); // return the length of the string - size_type size() const { return GetStringData()->nDataLength; } + size_type length() const { return GetStringData()->nDataLength; } // return the length of the string - size_type length() const { return size(); } + size_type size() const { return length(); } // return the maximum size of the string - size_type max_size() const { return wxSTRING_MAXLEN; } + size_type max_size() const { return npos; } // resize the string, filling the space with c if c != 0 - void resize(size_t nSize, wxChar ch = wxT('\0')); + void resize(size_t nSize, wxUniChar ch = wxT('\0')); // delete the contents of the string void clear() { erase(0, npos); } // returns true if the string is empty - bool empty() const { return size() == 0; } + bool empty() const { return length() == 0; } // inform string about planned change in size void reserve(size_t sz) { Alloc(sz); } size_type capacity() const { return GetStringData()->nAllocLength; } @@ -383,7 +525,11 @@ public: { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } // returns the writable character at position n reference at(size_type n) - { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } + { + wxASSERT_VALID_INDEX( n ); + CopyBeforeWrite(); + return wxUniCharRef::CreateForString(&m_pchData[n]); + } // lib.string.modifiers // append elements str[pos], ..., str[pos+n] @@ -402,7 +548,7 @@ public: wxStringBase& append(const wxChar *sz, size_t n) { ConcatSelf(n, sz); return *this; } // append n copies of ch - wxStringBase& append(size_t n, wxChar ch); + wxStringBase& append(size_t n, wxUniChar ch); // append from first to last wxStringBase& append(const_iterator first, const_iterator last) { ConcatSelf(last - first, first); return *this; } @@ -419,7 +565,7 @@ public: wxStringBase& assign(const wxChar *sz, size_t n) { clear(); return append(sz, n); } // same as `= n copies of ch' - wxStringBase& assign(size_t n, wxChar ch) + wxStringBase& assign(size_t n, wxUniChar ch) { clear(); return append(n, ch); } // assign from first to last wxStringBase& assign(const_iterator first, const_iterator last) @@ -427,14 +573,18 @@ public: // first valid index position const_iterator begin() const { return m_pchData; } + iterator begin(); // position one after the last valid one const_iterator end() const { return m_pchData + length(); } - - // first valid index position - iterator begin(); - // position one after the last valid one iterator end(); + // first element of the reversed string + const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } + reverse_iterator rbegin() { return reverse_iterator(end()); } + // one beyond the end of the reversed string + const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } + reverse_iterator rend() { return reverse_iterator(begin()); } + // insert another string wxStringBase& insert(size_t nPos, const wxStringBase& str) { @@ -453,13 +603,13 @@ public: // insert first n (or all if n == npos) characters of sz wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos); // insert n copies of ch - wxStringBase& insert(size_t nPos, size_t n, wxChar ch) + wxStringBase& insert(size_t nPos, size_t n, wxUniChar ch) { return insert(nPos, wxStringBase(n, ch)); } - iterator insert(iterator it, wxChar ch) + iterator insert(iterator it, wxUniChar ch) { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; } void insert(iterator it, const_iterator first, const_iterator last) { insert(it - begin(), first, last - first); } - void insert(iterator it, size_type n, wxChar ch) + void insert(iterator it, size_type n, wxUniChar ch) { insert(it - begin(), n, ch); } // delete characters from nStart to nStart + nLen @@ -482,7 +632,7 @@ public: wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str) { return replace(nStart, nLen, str.c_str()); } // replaces the substring with nCount copies of ch - wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch); + wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch); // replaces a substring with another substring wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str, size_t nStart2, size_t nLen2); @@ -496,7 +646,7 @@ public: { return replace(first - begin(), last - first, s, n); } wxStringBase& replace(iterator first, iterator last, const wxStringBase& s) { return replace(first - begin(), last - first, s); } - wxStringBase& replace(iterator first, iterator last, size_type n, wxChar c) + wxStringBase& replace(iterator first, iterator last, size_type n, wxUniChar c) { return replace(first - begin(), last - first, n, c); } wxStringBase& replace(iterator first, iterator last, const_iterator first1, const_iterator last1) @@ -516,7 +666,7 @@ public: size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; // find the first occurence of character ch after nStart - size_t find(wxChar ch, size_t nStart = 0) const; + size_t find(wxUniChar ch, size_t nStart = 0) const; // rfind() family is exactly like find() but works right to left @@ -527,7 +677,7 @@ public: size_t rfind(const wxChar* sz, size_t nStart = npos, size_t n = npos) const; // as find, but from the end - size_t rfind(wxChar ch, size_t nStart = npos) const; + size_t rfind(wxUniChar ch, size_t nStart = npos) const; // find first/last occurence of any character in the set @@ -538,7 +688,7 @@ public: size_t find_first_of(const wxChar* sz, size_t nStart = 0) const; size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const; // same as find(char, size_t) - size_t find_first_of(wxChar c, size_t nStart = 0) const + size_t find_first_of(wxUniChar c, size_t nStart = 0) const { return find(c, nStart); } // find the last (starting from nStart) char from str in this string size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const @@ -547,7 +697,7 @@ public: size_t find_last_of (const wxChar* sz, size_t nStart = npos) const; size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const; // same as above - size_t find_last_of(wxChar c, size_t nStart = npos) const + size_t find_last_of(wxUniChar c, size_t nStart = npos) const { return rfind(c, nStart); } // find first/last occurence of any character not in the set @@ -559,7 +709,7 @@ public: size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const; size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const; // same as above - size_t find_first_not_of(wxChar ch, size_t nStart = 0) const; + size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const; // as strcspn() size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const { return find_last_not_of(str.c_str(), nStart); } @@ -567,7 +717,7 @@ public: size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const; size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const; // same as above - size_t find_last_not_of(wxChar ch, size_t nStart = npos) const; + size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const; // All compare functions return -1, 0 or 1 if the [sub]string is less, // equal or greater than the compare() argument. @@ -595,25 +745,163 @@ public: // string += C string wxStringBase& operator+=(const wxChar *psz) { return append(psz); } // string += char - wxStringBase& operator+=(wxChar ch) { return append(1, ch); } + wxStringBase& operator+=(wxUniChar ch) { return append(1, ch); } + wxStringBase& operator+=(wxUniCharRef ch) { return append(1, ch); } + wxStringBase& operator+=(char ch) { return append(1, ch); } + wxStringBase& operator+=(wchar_t ch) { return append(1, ch); } }; #endif // !wxUSE_STL +// ---------------------------------------------------------------------------- +// wxCStrData +// ---------------------------------------------------------------------------- + +// Lightweight object returned by wxString::c_str() and implicitly convertible +// to either const char* or const wchar_t*. +class WXDLLIMPEXP_BASE wxCStrData +{ +private: + // Ctors; for internal use by wxString and wxCStrData only + wxCStrData(const wxString *str, size_t offset = 0, bool owned = false) + : m_str(str), m_offset(offset), m_owned(owned) {} + +public: + // Ctor constructs the object from char literal; they are needed to make + // operator?: compile and they intentionally take char*, not const char* + wxCStrData(char *buf); + wxCStrData(wchar_t *buf); + + ~wxCStrData(); + + // FIXME: we'll need convertors for both char* and wchar_t* and NONE + // for wxChar*, but that's after completing the transition to + // "smart" wxUniChar class. For now, just have conversion to + // char* in ANSI build and wchar_t in Unicode build. +#if wxUSE_UNICODE + const wchar_t* AsWChar() const; + operator const wchar_t*() const { return AsWChar(); } +#else + const char* AsChar() const; + operator const char*() const { return AsChar(); } +#endif + + wxString AsString() const; + operator wxString() const; + + // allow expressions like "c_str()[0]": + wxUniChar operator[](int n) const { return operator[](size_t(n)); } + wxUniChar operator[](size_t n) const; + wxUniChar operator[](long n) const { return operator[](size_t(n)); } +#ifndef wxSIZE_T_IS_UINT + wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); } +#endif // size_t != unsigned int + + // these operators are needed to emulate the pointer semantics of c_str(): + // expressions like "wxChar *p = str.c_str() + 1;" should continue to work + // (we need both versions to resolve ambiguities): + wxCStrData operator+(int n) const + { return wxCStrData(m_str, m_offset + n, m_owned); } + wxCStrData operator+(long n) const + { return wxCStrData(m_str, m_offset + n, m_owned); } + wxCStrData operator+(size_t n) const + { return wxCStrData(m_str, m_offset + n, m_owned); } + + // this operator is need to make expressions like "*c_str()" or + // "*(c_str() + 2)" work + wxUniChar operator*() const; + +private: + const wxString *m_str; + size_t m_offset; + bool m_owned; + + friend class WXDLLIMPEXP_BASE wxString; +}; + +// ---------------------------------------------------------------------------- +// wxStringPrintfMixin +// --------------------------------------------------------------------------- + +// NB: VC6 has a bug that causes linker errors if you have template methods +// in a class using __declspec(dllimport). The solution is to split such +// class into two classes, one that contains the template methods and does +// *not* use WXDLLIMPEXP_BASE and another class that contains the rest +// (with DLL linkage). +// +// We only do this for VC6 here, because the code is less efficient +// (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler +// cannot compile this code. + +#if defined(__VISUALC__) && __VISUALC__ < 1300 + #define wxNEEDS_WXSTRING_PRINTF_MIXIN +#endif + +#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN +// this class contains implementation of wxString's vararg methods, it's +// exported from wxBase DLL +class WXDLLIMPEXP_BASE wxStringPrintfMixinBase +{ +protected: + wxStringPrintfMixinBase() {} + + int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; + static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; +}; + +// this class contains template wrappers for wxString's vararg methods, it's +// intentionally *not* exported from the DLL in order to fix the VC6 bug +// described above +class wxStringPrintfMixin : public wxStringPrintfMixinBase +{ +private: + // to further complicate things, we can't return wxString from + // wxStringPrintfMixin::Format() because wxString is not yet declared at + // this point; the solution is to use this fake type trait template - this + // way the compiler won't know the return type until Format() is used + // (this doesn't compile with Watcom, but VC6 compiles it just fine): + template struct StringReturnType + { + typedef wxString type; + }; + +public: + // these are duplicated wxString methods, they're also declared below + // if !wxNEEDS_WXSTRING_PRINTF_MIXIN: + + // int Printf(const wxChar *pszFormat, ...); + WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) + // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; + WX_DEFINE_VARARG_FUNC(static typename StringReturnType::type, + Format, DoFormat) + // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; + WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) + +protected: + wxStringPrintfMixin() : wxStringPrintfMixinBase() {} +}; +#endif // wxNEEDS_WXSTRING_PRINTF_MIXIN + + // ---------------------------------------------------------------------------- // wxString: string class trying to be compatible with std::string, MFC // CString and wxWindows 1.x wxString all at once // --------------------------------------------------------------------------- -class WXDLLIMPEXP_BASE wxString : public wxStringBase -{ -#if !wxUSE_STL -friend class WXDLLIMPEXP_BASE wxArrayString; +#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN + // "non dll-interface class 'wxStringPrintfMixin' used as base interface + // for dll-interface class 'wxString'" -- this is OK in our case + #pragma warning (disable:4275) #endif +class WXDLLIMPEXP_BASE wxString : public wxStringBase +#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN + ,public wxStringPrintfMixin +#endif +{ // NB: special care was taken in arranging the member functions in such order // that all inline functions can be effectively inlined, verify that all - // performace critical functions are still inlined if you change order! + // performance critical functions are still inlined if you change order! private: // if we hadn't made these operators private, it would be possible to // compile "wxString s; s = 17;" without any warnings as 17 is implicitly @@ -638,9 +926,21 @@ public: wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { } wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { } // string containing nRepeat copies of ch - wxString(wxChar ch, size_t nRepeat = 1) + wxString(wxUniChar ch, size_t nRepeat = 1) + : wxStringBase(nRepeat, ch) { } + wxString(size_t nRepeat, wxUniChar ch) + : wxStringBase(nRepeat, ch) { } + wxString(wxUniCharRef ch, size_t nRepeat = 1) + : wxStringBase(nRepeat, ch) { } + wxString(size_t nRepeat, wxUniCharRef ch) : wxStringBase(nRepeat, ch) { } - wxString(size_t nRepeat, wxChar ch) + wxString(char ch, size_t nRepeat = 1) + : wxStringBase(nRepeat, ch) { } + wxString(size_t nRepeat, char ch) + : wxStringBase(nRepeat, ch) { } + wxString(wchar_t ch, size_t nRepeat = 1) + : wxStringBase(nRepeat, ch) { } + wxString(size_t nRepeat, wchar_t ch) : wxStringBase(nRepeat, ch) { } // ctor takes first nLength characters from C string // (default value of npos means take all the string) @@ -648,33 +948,42 @@ public: : wxStringBase(psz ? psz : wxT("")) { } wxString(const wxChar *psz, size_t nLength) : wxStringBase(psz, nLength) { } - wxString(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = npos) + wxString(const wxChar *psz, + const wxMBConv& WXUNUSED(conv), + size_t nLength = npos) : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { } - // even we're not build with wxUSE_STL == 1 it is very convenient to allow + // even if we're not built with wxUSE_STL == 1 it is very convenient to allow // implicit conversions from std::string to wxString as this allows to use // the same strings in non-GUI and GUI code, however we don't want to // unconditionally add this ctor as it would make wx lib dependent on // libstdc++ on some Linux versions which is bad, so instead we ask the // client code to define this wxUSE_STD_STRING symbol if they need it -#ifdef wxUSE_STD_STRING +#if wxUSE_STD_STRING wxString(const wxStdString& s) : wxStringBase(s.c_str()) { } #endif // wxUSE_STD_STRING #if wxUSE_UNICODE // from multibyte string - wxString(const char *psz, wxMBConv& conv, size_t nLength = npos); + wxString(const char *psz, + const wxMBConv& conv = wxConvLibc, + size_t nLength = npos); // from wxWCharBuffer (i.e. return from wxGetString) wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { } #else // ANSI // from C string (for compilers using unsigned char) - wxString(const unsigned char* psz, size_t nLength = npos) + wxString(const unsigned char* psz) + : wxStringBase((const char*)psz) { } + // from part of C string (for compilers using unsigned char) + wxString(const unsigned char* psz, size_t nLength) : wxStringBase((const char*)psz, nLength) { } #if wxUSE_WCHAR_T // from wide (Unicode) string - wxString(const wchar_t *pwz, wxMBConv& conv = wxConvLibc, size_t nLength = npos); + wxString(const wchar_t *pwz, + const wxMBConv& conv = wxConvLibc, + size_t nLength = npos); #endif // !wxUSE_WCHAR_T // from wxCharBuffer @@ -688,7 +997,7 @@ public: // string contains any characters? bool IsEmpty() const { return empty(); } // empty string is "false", so !str will return true - bool operator!() const { return IsEmpty(); } + bool operator!() const { return empty(); } // truncate the string to given length wxString& Truncate(size_t uiLen); // empty string contents @@ -715,17 +1024,17 @@ public: // data access (all indexes are 0 based) // read access - wxChar GetChar(size_t n) const + wxUniChar GetChar(size_t n) const { return at(n); } // read/write access - wxChar& GetWritableChar(size_t n) + wxUniCharRef GetWritableChar(size_t n) { return at(n); } // write access - void SetChar(size_t n, wxChar ch) + void SetChar(size_t n, wxUniChar ch) { at(n) = ch; } // get last character - wxChar Last() const + wxUniChar Last() const { wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); @@ -733,7 +1042,7 @@ public: } // get writable last character - wxChar& Last() + wxUniCharRef Last() { wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); return at(length() - 1); @@ -741,42 +1050,44 @@ public: /* Note that we we must define all of the overloads below to avoid - ambiguity when using str[0]. Also note that for a conforming compiler we - don't need const version of operatorp[] at all as indexed access to - const string is provided by implicit conversion to "const wxChar *" - below and defining them would only result in ambiguities, but some other - compilers refuse to compile "str[0]" without them. + ambiguity when using str[0]. */ - -#if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__) - wxChar operator[](int n) const + wxUniChar operator[](int n) const + { return wxStringBase::at(n); } + wxUniChar operator[](long n) const { return wxStringBase::at(n); } - wxChar operator[](size_type n) const + wxUniChar operator[](size_t n) const { return wxStringBase::at(n); } #ifndef wxSIZE_T_IS_UINT - wxChar operator[](unsigned int n) const + wxUniChar operator[](unsigned int n) const { return wxStringBase::at(n); } #endif // size_t != unsigned int -#endif // broken compiler - // operator versions of GetWriteableChar() - wxChar& operator[](int n) + wxUniCharRef operator[](int n) + { return wxStringBase::at(n); } + wxUniCharRef operator[](long n) { return wxStringBase::at(n); } - wxChar& operator[](size_type n) + wxUniCharRef operator[](size_t n) { return wxStringBase::at(n); } #ifndef wxSIZE_T_IS_UINT - wxChar& operator[](unsigned int n) + wxUniCharRef operator[](unsigned int n) { return wxStringBase::at(n); } #endif // size_t != unsigned int + // explicit conversion to C string (use this with printf()!) + wxCStrData c_str() const { return wxCStrData(this); } + // implicit conversion to C string + operator wxCStrData() const { return c_str(); } operator const wxChar*() const { return c_str(); } - // identical to c_str(), for wxWin 1.6x compatibility - const wxChar* wx_str() const { return c_str(); } // identical to c_str(), for MFC compatibility - const wxChar* GetData() const { return c_str(); } + const wxCStrData GetData() const { return c_str(); } + + // explicit conversion to C string in internal representation (char*, + // wchar_t*, UTF-8-encoded char*, depending on the build): + const_pointer wx_str() const { return data(); } // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for // converting numbers or strings which are certain not to contain special @@ -804,14 +1115,14 @@ public: // type differs because a function may either return pointer to the buffer // directly or have to use intermediate buffer for translation. #if wxUSE_UNICODE - const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const; + const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } const wxChar* wc_str() const { return c_str(); } // for compatibility with !wxUSE_UNICODE version - const wxChar* wc_str(wxMBConv& WXUNUSED(conv)) const { return c_str(); } + const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } #if wxMBFILES const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } @@ -822,12 +1133,12 @@ public: const wxChar* mb_str() const { return c_str(); } // for compatibility with wxUSE_UNICODE version - const wxChar* mb_str(wxMBConv& WXUNUSED(conv)) const { return c_str(); } + const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); } const wxWX2MBbuf mbc_str() const { return mb_str(); } #if wxUSE_WCHAR_T - const wxWCharBuffer wc_str(wxMBConv& conv) const; + const wxWCharBuffer wc_str(const wxMBConv& conv) const; #endif // wxUSE_WCHAR_T #ifdef __WXOSX__ const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); } @@ -840,9 +1151,16 @@ public: // from another wxString wxString& operator=(const wxStringBase& stringSrc) { return (wxString&)wxStringBase::operator=(stringSrc); } + wxString& operator=(const wxCStrData& cstr); // from a character - wxString& operator=(wxChar ch) + wxString& operator=(wxUniChar ch) { return (wxString&)wxStringBase::operator=(ch); } + wxString& operator=(wxUniCharRef ch) + { return (wxString&)wxStringBase::operator=((wxUniChar)ch); } + wxString& operator=(char ch) + { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); } + wxString& operator=(wchar_t ch) + { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); } // from a C string - STL probably will crash on NULL, // so we need to compensate in that case #if wxUSE_STL @@ -857,6 +1175,9 @@ public: // from wxWCharBuffer wxString& operator=(const wxWCharBuffer& psz) { (void) operator=((const wchar_t *)psz); return *this; } + // from C string + wxString& operator=(const char* psz) + { return operator=(wxString(psz)); } #else // ANSI // from another kind of C string wxString& operator=(const unsigned char* psz); @@ -879,7 +1200,7 @@ public: // string += string wxString& operator<<(const wxString& s) { -#if !wxUSE_STL +#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL wxASSERT_MSG( s.GetStringData()->IsValid(), _T("did you forget to call UngetWriteBuf()?") ); #endif @@ -889,23 +1210,36 @@ public: } // string += C string wxString& operator<<(const wxChar *psz) + { append(psz); return *this; } + wxString& operator<<(const wxCStrData& psz) { append(psz); return *this; } // string += char - wxString& operator<<(wxChar ch) { append(1, ch); return *this; } + wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; } + wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; } + wxString& operator<<(char ch) { append(1, ch); return *this; } + wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } // string += buffer (i.e. from wxGetString) #if wxUSE_UNICODE wxString& operator<<(const wxWCharBuffer& s) - { (void)operator<<((const wchar_t *)s); return *this; } - void operator+=(const wxWCharBuffer& s) - { (void)operator<<((const wchar_t *)s); } + { return operator<<((const wchar_t *)s); } + wxString& operator+=(const wxWCharBuffer& s) + { return operator<<((const wchar_t *)s); } #else // !wxUSE_UNICODE wxString& operator<<(const wxCharBuffer& s) - { (void)operator<<((const char *)s); return *this; } - void operator+=(const wxCharBuffer& s) - { (void)operator<<((const char *)s); } + { return operator<<((const char *)s); } + wxString& operator+=(const wxCharBuffer& s) + { return operator<<((const char *)s); } #endif // wxUSE_UNICODE/!wxUSE_UNICODE +#if wxUSE_UNICODE + // string += C string in Unicode build (with conversion) + wxString& operator<<(const char *s) + { return operator<<(wxString(s)); } + wxString& operator+=(const char *s) + { return operator+=(wxString(s)); } +#endif // wxUSE_UNICODE + // string += C string wxString& Append(const wxString& s) { @@ -916,10 +1250,18 @@ public: append(s); return *this; } + wxString& Append(const wxCStrData& psz) + { append(psz); return *this; } wxString& Append(const wxChar* psz) { append(psz); return *this; } // append count copies of given character - wxString& Append(wxChar ch, size_t count = 1u) + wxString& Append(wxUniChar ch, size_t count = 1u) + { append(count, ch); return *this; } + wxString& Append(wxUniCharRef ch, size_t count = 1u) + { append(count, ch); return *this; } + wxString& Append(char ch, size_t count = 1u) + { append(count, ch); return *this; } + wxString& Append(wchar_t ch, size_t count = 1u) { append(count, ch); return *this; } wxString& Append(const wxChar* psz, size_t nLen) { append(psz, nLen); return *this; } @@ -929,16 +1271,19 @@ public: { *this = str + *this; return *this; } // non-destructive concatenation - // - friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); - // - friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); - // - friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); - // - friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); - // - friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); + // two strings + friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, + const wxString& string2); + // string with a single char + friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); + // char with a string + friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); + // string with C string + friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, + const wxChar *psz); + // C string with string + friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, + const wxString& string); // stream-like functions // insert an int into string @@ -953,6 +1298,20 @@ public: // insert an unsigned long into string wxString& operator<<(unsigned long ul) { return (*this) << Format(_T("%lu"), ul); } +#if defined wxLongLong_t && !defined wxLongLongIsLong + // insert a long long if they exist and aren't longs + wxString& operator<<(wxLongLong_t ll) + { + const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d"); + return (*this) << Format(fmt, ll); + } + // insert an unsigned long long + wxString& operator<<(wxULongLong_t ull) + { + const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); + return (*this) << Format(fmt , ull); + } +#endif // insert a float into string wxString& operator<<(float f) { return (*this) << Format(_T("%f"), f); } @@ -971,8 +1330,8 @@ public: // (if compareWithCase then the case matters) bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; } - // comparison with a signle character: returns true if equal - bool IsSameAs(wxChar c, bool compareWithCase = true) const + // comparison with a single character: returns true if equal + bool IsSameAs(wxUniChar c, bool compareWithCase = true) const { return (length() == 1) && (compareWithCase ? GetChar(0u) == c : wxToupper(GetChar(0u)) == wxToupper(c)); @@ -987,10 +1346,14 @@ public: wxString operator()(size_t start, size_t len) const { return Mid(start, len); } - // check that the string starts with prefix and return the rest of the - // string in the provided pointer if it is not NULL, otherwise return - // false + // check if the string starts with the given prefix and return the rest + // of the string in the provided pointer if it is not NULL; otherwise + // return false bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const; + // check if the string ends with the given suffix and return the + // beginning of the string before the suffix in the provided pointer if + // it is not NULL; otherwise return false + bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const; // get first nCount characters wxString Left(size_t nCount) const; @@ -998,20 +1361,20 @@ public: wxString Right(size_t nCount) const; // get all characters before the first occurance of ch // (returns the whole string if ch not found) - wxString BeforeFirst(wxChar ch) const; + wxString BeforeFirst(wxUniChar ch) const; // get all characters before the last occurence of ch // (returns empty string if ch not found) - wxString BeforeLast(wxChar ch) const; + wxString BeforeLast(wxUniChar ch) const; // get all characters after the first occurence of ch // (returns empty string if ch not found) - wxString AfterFirst(wxChar ch) const; + wxString AfterFirst(wxUniChar ch) const; // get all characters after the last occurence of ch // (returns the whole string if ch not found) - wxString AfterLast(wxChar ch) const; + wxString AfterLast(wxUniChar ch) const; // for compatibility only, use more explicitly named functions above - wxString Before(wxChar ch) const { return BeforeLast(ch); } - wxString After(wxChar ch) const { return AfterFirst(ch); } + wxString Before(wxUniChar ch) const { return BeforeLast(ch); } + wxString After(wxUniChar ch) const { return AfterFirst(ch); } // case conversion // convert to upper case in place, return the string itself @@ -1028,11 +1391,11 @@ public: // remove spaces from left or from right (default) side wxString& Trim(bool bFromRight = true); // add nCount copies chPad in the beginning or at the end (default) - wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = true); + wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true); // searching and replacing // searching (return starting index, or -1 if not found) - int Find(wxChar ch, bool bFromEnd = false) const; // like strchr/strrchr + int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr // searching (return starting index, or -1 if not found) int Find(const wxChar *pszSub) const; // like strstr // replace first (or all of bReplaceAll) occurences of substring with @@ -1053,20 +1416,33 @@ public: bool ToLong(long *val, int base = 10) const; // convert to an unsigned integer bool ToULong(unsigned long *val, int base = 10) const; + // convert to wxLongLong +#if defined(wxLongLong_t) + bool ToLongLong(wxLongLong_t *val, int base = 10) const; + // convert to wxULongLong + bool ToULongLong(wxULongLong_t *val, int base = 10) const; +#endif // wxLongLong_t // convert to a double bool ToDouble(double *val) const; - // formated input/output + +#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN + // formatted input/output // as sprintf(), returns the number of characters written or < 0 on error // (take 'this' into account in attribute parameter count) - int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; + // int Printf(const wxChar *pszFormat, ...); + WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf) +#endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN // as vprintf(), returns the number of characters written or < 0 on error - int PrintfV(const wxChar* pszFormat, va_list argptr); + int PrintfV(const wxString& format, va_list argptr); +#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN // returns the string containing the result of Printf() to it - static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; + // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1; + WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat) +#endif // the same as above, but takes a va_list - static wxString FormatV(const wxChar *pszFormat, va_list argptr); + static wxString FormatV(const wxString& format, va_list argptr); // raw access to string memory // ensure that string has space for at least nLen characters @@ -1075,14 +1451,16 @@ public: // minimize the string's memory // only works if the data of this string is not shared bool Shrink(); -#if !wxUSE_STL +#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL + // These are deprecated, use wxStringBuffer or wxStringBufferLength instead + // // get writable buffer of at least nLen bytes. Unget() *must* be called // a.s.a.p. to put string back in a reasonable state! - wxChar *GetWriteBuf(size_t nLen); + wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) ); // call this immediately after GetWriteBuf() has been used - void UngetWriteBuf(); - void UngetWriteBuf(size_t nLen); -#endif + wxDEPRECATED( void UngetWriteBuf() ); + wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); +#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL // wxWidgets version 1 compatibility functions @@ -1094,9 +1472,12 @@ public: // values for first parameter of Strip function enum stripType {leading = 0x1, trailing = 0x2, both = 0x3}; +#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN // use Printf() // (take 'this' into account in attribute parameter count) - int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; + // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2; + WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf) +#endif // wxNEEDS_WXSTRING_PRINTF_MIXIN // use Cmp() inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const @@ -1105,7 +1486,7 @@ public: // use Len size_t Length() const { return length(); } // Count the number of characters - int Freq(wxChar ch) const; + int Freq(wxUniChar ch) const; // use MakeLower void LowerCase() { MakeLower(); } // use MakeUpper @@ -1115,7 +1496,7 @@ public: // use Find (more general variants not yet supported) size_t Index(const wxChar* psz) const { return Find(psz); } - size_t Index(wxChar ch) const { return Find(ch); } + size_t Index(wxUniChar ch) const { return Find(ch); } // use Truncate wxString& Remove(size_t pos) { return Truncate(pos); } wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); } @@ -1124,10 +1505,12 @@ public: { return (wxString&)erase( nStart, nLen ); } // use Find() - int First( const wxChar ch ) const { return Find(ch); } + int First( const wxUniChar ch ) const { return Find(ch); } + int First( char ch ) const { return Find(ch); } + int First( wchar_t ch ) const { return Find(ch); } int First( const wxChar* psz ) const { return Find(psz); } int First( const wxString &str ) const { return Find(str); } - int Last( const wxChar ch ) const { return Find(ch, true); } + int Last( const wxUniChar ch ) const { return Find(ch, true); } bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } // use empty() @@ -1141,10 +1524,10 @@ public: // take all characters from pStart to pEnd wxString(const void *pStart, const void *pEnd) : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { } -#if wxUSE_STL wxString(const_iterator first, const_iterator last) : wxStringBase(first, last) { } -#endif + wxString(iterator first, iterator last) + : wxStringBase(first, last) { } // lib.string.modifiers // append elements str[pos], ..., str[pos+n] @@ -1153,13 +1536,15 @@ public: // append a string wxString& append(const wxString& str) { return (wxString&)wxStringBase::append(str); } + wxString& append(const wxCStrData& str) + { return (wxString&)wxStringBase::append(str.AsString()); } // append first n (or all if n == npos) characters of sz wxString& append(const wxChar *sz) { return (wxString&)wxStringBase::append(sz); } wxString& append(const wxChar *sz, size_t n) { return (wxString&)wxStringBase::append(sz, n); } // append n copies of ch - wxString& append(size_t n, wxChar ch) + wxString& append(size_t n, wxUniChar ch) { return (wxString&)wxStringBase::append(n, ch); } // append from first to last wxString& append(const_iterator first, const_iterator last) @@ -1177,14 +1562,14 @@ public: wxString& assign(const wxChar *sz, size_t n) { return (wxString&)wxStringBase::assign(sz, n); } // same as `= n copies of ch' - wxString& assign(size_t n, wxChar ch) + wxString& assign(size_t n, wxUniChar ch) { return (wxString&)wxStringBase::assign(n, ch); } // assign from first to last wxString& assign(const_iterator first, const_iterator last) { return (wxString&)wxStringBase::assign(first, last); } // string comparison -#ifndef HAVE_STD_STRING_COMPARE +#if !defined(HAVE_STD_STRING_COMPARE) int compare(const wxStringBase& str) const; // comparison with a substring int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; @@ -1210,13 +1595,13 @@ public: wxString& insert(size_t nPos, const wxChar *sz, size_t n) { return (wxString&)wxStringBase::insert(nPos, sz, n); } // insert n copies of ch - wxString& insert(size_t nPos, size_t n, wxChar ch) + wxString& insert(size_t nPos, size_t n, wxUniChar ch) { return (wxString&)wxStringBase::insert(nPos, n, ch); } - iterator insert(iterator it, wxChar ch) + iterator insert(iterator it, wxUniChar ch) { return wxStringBase::insert(it, ch); } void insert(iterator it, const_iterator first, const_iterator last) { wxStringBase::insert(it, first, last); } - void insert(iterator it, size_type n, wxChar ch) + void insert(iterator it, size_type n, wxUniChar ch) { wxStringBase::insert(it, n, ch); } // delete characters from nStart to nStart + nLen @@ -1238,7 +1623,7 @@ public: wxString& replace(size_t nStart, size_t nLen, const wxString& str) { return (wxString&)wxStringBase::replace(nStart, nLen, str); } // replaces the substring with nCount copies of ch - wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch) + wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch) { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); } // replaces a substring with another substring wxString& replace(size_t nStart, size_t nLen, @@ -1256,7 +1641,7 @@ public: { return (wxString&)wxStringBase::replace(first, last, s, n); } wxString& replace(iterator first, iterator last, const wxString& s) { return (wxString&)wxStringBase::replace(first, last, s); } - wxString& replace(iterator first, iterator last, size_type n, wxChar c) + wxString& replace(iterator first, iterator last, size_type n, wxUniChar c) { return (wxString&)wxStringBase::replace(first, last, n, c); } wxString& replace(iterator first, iterator last, const_iterator first1, const_iterator last1) @@ -1268,16 +1653,61 @@ public: // string += C string wxString& operator+=(const wxChar *psz) { return (wxString&)wxStringBase::operator+=(psz); } + wxString& operator+=(const wxCStrData& s) + { return (wxString&)wxStringBase::operator+=(s.AsString()); } // string += char - wxString& operator+=(wxChar ch) + wxString& operator+=(wxUniChar ch) { return (wxString&)wxStringBase::operator+=(ch); } + wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); } + wxString& operator+=(char ch) { return *this += wxUniChar(ch); } + wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); } + +private: +#if !wxUSE_STL + // helpers for wxStringBuffer and wxStringBufferLength + wxChar *DoGetWriteBuf(size_t nLen); + void DoUngetWriteBuf(); + void DoUngetWriteBuf(size_t nLen); + + friend class WXDLLIMPEXP_BASE wxStringBuffer; + friend class WXDLLIMPEXP_BASE wxStringBufferLength; +#endif + +#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN + int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2; + static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1; +#endif }; -// define wxArrayString, for compatibility -#if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL - #include "wx/arrstr.h" +#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN + #pragma warning (default:4275) #endif +// notice that even though for many compilers the friend declarations above are +// enough, from the point of view of C++ standard we must have the declarations +// here as friend ones are not injected in the enclosing namespace and without +// them the code fails to compile with conforming compilers such as xlC or g++4 +wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); +wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); +wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); + +wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch); +wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string); + +inline wxString operator+(const wxString& string, wxUniCharRef ch) + { return string + (wxUniChar)ch; } +inline wxString operator+(const wxString& string, char ch) + { return string + wxUniChar(ch); } +inline wxString operator+(const wxString& string, wchar_t ch) + { return string + wxUniChar(ch); } +inline wxString operator+(wxUniCharRef ch, const wxString& string) + { return (wxUniChar)ch + string; } +inline wxString operator+(char ch, const wxString& string) + { return wxUniChar(ch) + string; } +inline wxString operator+(wchar_t ch, const wxString& string) + { return wxUniChar(ch) + string; } + + #if wxUSE_STL // return an empty wxString (not very useful with wxUSE_STL == 1) inline const wxString wxGetEmptyString() { return wxString(); } @@ -1353,9 +1783,9 @@ class WXDLLIMPEXP_BASE wxStringBuffer public: wxStringBuffer(wxString& str, size_t lenWanted = 1024) : m_str(str), m_buf(NULL) - { m_buf = m_str.GetWriteBuf(lenWanted); } + { m_buf = m_str.DoGetWriteBuf(lenWanted); } - ~wxStringBuffer() { m_str.UngetWriteBuf(); } + ~wxStringBuffer() { m_str.DoUngetWriteBuf(); } operator wxChar*() const { return m_buf; } @@ -1372,14 +1802,14 @@ public: wxStringBufferLength(wxString& str, size_t lenWanted = 1024) : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) { - m_buf = m_str.GetWriteBuf(lenWanted); + m_buf = m_str.DoGetWriteBuf(lenWanted); wxASSERT(m_buf != NULL); } ~wxStringBufferLength() { wxASSERT(m_lenSet); - m_str.UngetWriteBuf(m_len); + m_str.DoUngetWriteBuf(m_len); } operator wxChar*() const { return m_buf; } @@ -1462,12 +1892,6 @@ inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const char *)s1) != 0); } #endif // wxUSE_UNICODE/!wxUSE_UNICODE -wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); -wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch); -wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string); -wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz); -wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string); - #if wxUSE_UNICODE inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) { return string + (const wchar_t *)buf; } @@ -1484,10 +1908,52 @@ inline wxString operator+(const wxCharBuffer& buf, const wxString& string) // comparison with char (those are not defined by std::[w]string and so should // be always available) -inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); } -inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); } -inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); } -inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } +inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } +inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); } +inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); } +inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); } +inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); } +inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); } +inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); } +inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); } +inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); } +inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); } +inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); } +inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); } +inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); } +inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); } +inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); } +inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); } +inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } +inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } + +// comparison with C string in Unicode build +#if wxUSE_UNICODE +inline bool operator==(const wxString& s1, const char* s2) + { return s1 == wxString(s2); } +inline bool operator==(const char* s1, const wxString& s2) + { return wxString(s1) == s2; } +inline bool operator!=(const wxString& s1, const char* s2) + { return s1 != wxString(s2); } +inline bool operator!=(const char* s1, const wxString& s2) + { return wxString(s1) != s2; } +inline bool operator< (const wxString& s1, const char* s2) + { return s1 < wxString(s2); } +inline bool operator< (const char* s1, const wxString& s2) + { return wxString(s1) < s2; } +inline bool operator> (const wxString& s1, const char* s2) + { return s1 > wxString(s2); } +inline bool operator> (const char* s1, const wxString& s2) + { return wxString(s1) > s2; } +inline bool operator<=(const wxString& s1, const char* s2) + { return s1 <= wxString(s2); } +inline bool operator<=(const char* s1, const wxString& s2) + { return wxString(s1) <= s2; } +inline bool operator>=(const wxString& s1, const char* s2) + { return s1 >= wxString(s2); } +inline bool operator>=(const char* s1, const wxString& s2) + { return wxString(s1) >= s2; } +#endif // wxUSE_UNICODE // --------------------------------------------------------------------------- // Implementation only from here until the end of file @@ -1500,9 +1966,88 @@ inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } #include "wx/iosfwrap.h" -WXDLLIMPEXP_BASE wxSTD istream& operator>>(wxSTD istream&, wxString&); WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); +WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); #endif // wxSTD_STRING_COMPATIBILITY +// --------------------------------------------------------------------------- +// wxCStrData implementation +// --------------------------------------------------------------------------- + +inline wxCStrData::wxCStrData(char *buf) + : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} +inline wxCStrData::wxCStrData(wchar_t *buf) + : m_str(new wxString(buf)), m_offset(0), m_owned(true) {} + +inline wxCStrData::~wxCStrData() +{ + if ( m_owned ) + delete m_str; +} + +#if wxUSE_UNICODE +inline const wchar_t* wxCStrData::AsWChar() const +#else +inline const char* wxCStrData::AsChar() const +#endif +{ + if ( m_offset == 0 ) + return m_str->wx_str(); // FIXME + else + return (const wxChar*)(m_str->begin() + m_offset); +} + +inline wxString wxCStrData::AsString() const +{ + if ( m_offset == 0 ) + return *m_str; + else + return m_str->Mid(m_offset); +} + +inline wxCStrData::operator wxString() const { return AsString(); } + +inline wxUniChar wxCStrData::operator*() const +{ + if ( m_str->empty() ) + return wxUniChar(_T('\0')); + else + return (*m_str)[m_offset]; +} + +inline wxUniChar wxCStrData::operator[](size_t n) const +{ + return m_str->at(m_offset + n); +} + +// ---------------------------------------------------------------------------- +// implementation of wxString inline methods using wxCStrData +// ---------------------------------------------------------------------------- + +inline wxString& wxString::operator=(const wxCStrData& cstr) +{ + return *this = cstr.AsString(); +} + +// ---------------------------------------------------------------------------- +// implementation of wx[W]CharBuffer inline methods using wxCStrData +// ---------------------------------------------------------------------------- + +#if wxUSE_UNICODE + +inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr) + : m_str(wxStrdupW(cstr)) +{ +} + +#else // !wxUSE_UNICODE + +inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr) + : m_str(wxStrdupA(cstr)) +{ +} + +#endif // wxUSE_UNICODE/!wxUSE_UNICODE + #endif // _WX_WXSTRINGH__