X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/051aa330fe60ecd17d77a92993f61bf3e05862f6..6580df1b72539453610530247a0b3bea8dad172a:/include/wx/string.h?ds=sidebyside diff --git a/include/wx/string.h b/include/wx/string.h index 2daf622c90..71d0b84807 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -18,10 +18,6 @@ #ifndef _WX_WXSTRINGH__ #define _WX_WXSTRINGH__ -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma interface "string.h" -#endif - // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- @@ -51,10 +47,18 @@ #include // for strcasecmp() #endif // HAVE_STRCASECMP_IN_STRINGS_H -#include "wx/wxchar.h" // for wxChar +#ifdef __WXPALMOS__ + #include +#endif + +#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 +class WXDLLIMPEXP_BASE wxString; + // --------------------------------------------------------------------------- // macros // --------------------------------------------------------------------------- @@ -74,16 +78,14 @@ // constants // ---------------------------------------------------------------------------- -#if defined(__VISAGECPP__) && __IBMCPP__ >= 400 -// must define this static for VA or else you get multiply defined symbols everywhere -extern const unsigned int wxSTRING_MAXLEN; +#if WXWIN_COMPATIBILITY_2_6 -#else +// deprecated in favour of wxString::npos, don't use in new code +// // maximum possible length for a string means "take all string" everywhere -// (as sizeof(StringData) is unknown here, we substract 100) -const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100; +#define wxSTRING_MAXLEN wxStringBase::npos -#endif +#endif // WXWIN_COMPATIBILITY_2_6 // ---------------------------------------------------------------------------- // global data @@ -97,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__) @@ -134,7 +140,8 @@ inline int Stricmp(const char *psz1, const char *psz2) return stricmp(psz1, psz2); #elif defined(__WXPM__) return stricmp(psz1, psz2); -#elif defined(HAVE_STRCASECMP_IN_STRING_H) || \ +#elif defined(__WXPALMOS__) || \ + defined(HAVE_STRCASECMP_IN_STRING_H) || \ defined(HAVE_STRCASECMP_IN_STRINGS_H) || \ defined(__GNUWIN32__) return strcasecmp(psz1, psz2); @@ -164,11 +171,14 @@ inline int Stricmp(const char *psz1, const char *psz2) #endif // OS/compiler } -// return an empty wxString -class WXDLLIMPEXP_BASE wxString; // not yet defined -inline const wxString& wxGetEmptyString() { return *(wxString *)&wxEmptyString; } +#endif // WXWIN_COMPATIBILITY_2_8 -#if wxUSE_STL +// ---------------------------------------------------------------------------- +// deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING +// ---------------------------------------------------------------------------- + +// in both cases we need to define wxStdString +#if wxUSE_STL || wxUSE_STD_STRING #include "wx/beforestd.h" #include @@ -176,22 +186,33 @@ inline const wxString& wxGetEmptyString() { return *(wxString *)&wxEmptyString; #if wxUSE_UNICODE #ifdef HAVE_STD_WSTRING - typedef std::wstring wxStringBase; + typedef std::wstring wxStdString; #else - typedef std::basic_string wxStringBase; + typedef std::basic_string wxStdString; #endif #else - typedef std::string wxStringBase; + typedef std::string wxStdString; #endif -#if (defined(__GNUG__) && (__GNUG__ < 3)) || \ - (defined(_MSC_VER) && (_MSC_VER <= 1200)) - #define wxSTRING_BASE_HASNT_CLEAR -#endif +#endif // need + +#if wxUSE_STL + // 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)) + #define wxSTRING_BASE_HASNT_CLEAR + #endif + + 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 @@ -238,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; @@ -255,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(); } @@ -282,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 @@ -311,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) @@ -352,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; } @@ -376,12 +523,13 @@ public: // return the character at position n value_type at(size_type n) const { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; } - value_type operator[](size_type n) const { return at(n); } // returns the writable character at position n reference at(size_type n) - { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; } - reference operator[](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] @@ -400,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; } @@ -417,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) @@ -425,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) { @@ -451,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 @@ -480,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); @@ -494,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) @@ -510,26 +662,22 @@ public: // find a substring size_t find(const wxStringBase& str, size_t nStart = 0) const; - // VC++ 1.5 can't cope with this syntax. -#if !defined(__VISUALC__) || defined(__WIN32__) // find first n characters of sz size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const; -#endif // VC++ 1.5 // 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 // as find, but from the end size_t rfind(const wxStringBase& str, size_t nStart = npos) const; - // VC++ 1.5 can't cope with this syntax. // as find, but from the end 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 @@ -540,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 @@ -549,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 @@ -561,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); } @@ -569,22 +717,20 @@ 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. - // just like strcmp() - int compare(const wxStringBase& str) const - { return wxStrcmp(c_str(), str.c_str()); } + // comparison with another string + int compare(const wxStringBase& str) const; // comparison with a substring int compare(size_t nStart, size_t nLen, const wxStringBase& str) const; // comparison of 2 substrings int compare(size_t nStart, size_t nLen, const wxStringBase& str, size_t nStart2, size_t nLen2) const; - // just like strcmp() - int compare(const wxChar* sz) const - { return wxStrcmp(c_str(), sz); } + // comparison with a c string + int compare(const wxChar* sz) const; // substring comparison with first nCount characters of sz int compare(size_t nStart, size_t nLen, const wxChar* sz, size_t nCount = npos) const; @@ -599,43 +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 // --------------------------------------------------------------------------- -// This is (yet another one) String class for C++ programmers. It doesn't use -// any of "advanced" C++ features (i.e. templates, exceptions, namespaces...) -// thus you should be able to compile it with practicaly any C++ compiler. -// This class uses copy-on-write technique, i.e. identical strings share the -// same memory as long as neither of them is changed. -// -// This class aims to be as compatible as possible with the new standard -// std::string class, but adds some additional functions and should be at -// least as efficient than the standard implementation. -// -// Performance note: it's more efficient to write functions which take "const -// String&" arguments than "const char *" if you assign the argument to -// another string. -// -// It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5. + +// 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). // -// To do: -// - ressource support (string tables in ressources) -// - more wide character (UNICODE) support -// - regular expressions support -// --------------------------------------------------------------------------- +// 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. -class WXDLLIMPEXP_BASE wxString : public wxStringBase +#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 { -#if !wxUSE_STL -friend class WXDLLIMPEXP_BASE wxArrayString; +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 +// --------------------------------------------------------------------------- + +#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 @@ -660,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(char ch, size_t nRepeat = 1) + : wxStringBase(nRepeat, ch) { } + wxString(size_t nRepeat, char ch) : wxStringBase(nRepeat, ch) { } - wxString(size_t nRepeat, wxChar 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) @@ -670,22 +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 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 +#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 @@ -698,8 +996,8 @@ public: size_t Len() const { return length(); } // string contains any characters? bool IsEmpty() const { return empty(); } - // empty string is "FALSE", so !str will return TRUE - bool operator!() const { return IsEmpty(); } + // empty string is "false", so !str will return true + bool operator!() const { return empty(); } // truncate the string to given length wxString& Truncate(size_t uiLen); // empty string contents @@ -707,7 +1005,7 @@ public: { Truncate(0); - wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") ); + wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") ); } // empty the string and free memory void Clear() @@ -726,76 +1024,70 @@ public: // data access (all indexes are 0 based) // read access - wxChar GetChar(size_t n) const - { return operator[](n); } + wxUniChar GetChar(size_t n) const + { return at(n); } // read/write access - wxChar& GetWritableChar(size_t n) - { return operator[](n); } + wxUniCharRef GetWritableChar(size_t n) + { return at(n); } // write access - void SetChar(size_t n, wxChar ch) - { operator[](n) = ch; } + void SetChar(size_t n, wxUniChar ch) + { at(n) = ch; } // get last character - wxChar Last() const + wxUniChar Last() const { - wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") ); + wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); - return operator[](length() - 1); + return at(length() - 1); } // get writable last character - wxChar& Last() + wxUniCharRef Last() { - wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") ); - return operator[](length() - 1); + wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); + return at(length() - 1); } /* - So why do we have all these overloaded operator[]s? A bit of history: - initially there was only one of them, taking size_t. Then people - started complaining because they wanted to use ints as indices (I - wonder why) and compilers were giving warnings about it, so we had to - add the operator[](int). Then it became apparent that you couldn't - write str[0] any longer because there was ambiguity between two - overloads and so you now had to write str[0u] (or, of course, use the - explicit casts to either int or size_t but nobody did this). - - Finally, someone decided to compile wxWin on an Alpha machine and got - a surprize: str[0u] didn't compile there because it is of type - unsigned int and size_t is unsigned _long_ on Alpha and so there was - ambiguity between converting uint to int or ulong. To fix this one we - now add operator[](uint) for the machines where size_t is not already - the same as unsigned int - hopefully this fixes the problem (for some - time) - - The only real fix is, of course, to remove all versions but the one - taking size_t... + Note that we we must define all of the overloads below to avoid + ambiguity when using str[0]. */ - - // operator version of GetChar - wxChar operator[](int n) const - { return wxStringBase::operator[](n); } - wxChar& operator[](size_type n) - { return wxStringBase::operator[](n); } - wxChar operator[](size_type n) const - { return wxStringBase::operator[](n); } + wxUniChar operator[](int n) const + { return wxStringBase::at(n); } + wxUniChar operator[](long n) const + { return wxStringBase::at(n); } + wxUniChar operator[](size_t n) const + { return wxStringBase::at(n); } #ifndef wxSIZE_T_IS_UINT - // operator version of GetChar - wxChar operator[](unsigned int n) const - { return wxStringBase::operator[](n); } + wxUniChar operator[](unsigned int n) const + { return wxStringBase::at(n); } +#endif // size_t != unsigned int - // operator version of GetWriteableChar - wxChar& operator[](unsigned int n) - { return wxStringBase::operator[](n); } + // operator versions of GetWriteableChar() + wxUniCharRef operator[](int n) + { return wxStringBase::at(n); } + wxUniCharRef operator[](long n) + { return wxStringBase::at(n); } + wxUniCharRef operator[](size_t n) + { return wxStringBase::at(n); } +#ifndef wxSIZE_T_IS_UINT + 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 @@ -823,15 +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 - { return conv.cWC2MB(c_str()); } + 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); } @@ -842,32 +1133,51 @@ 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 - { return conv.cMB2WC(c_str()); } + 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 ) ); } +#else const wxChar* fn_str() const { return c_str(); } +#endif #endif // Unicode/ANSI // overloaded assignment // 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); } - // from a C string + 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 + wxString& operator=(const wxChar *psz) + { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; } +#else wxString& operator=(const wxChar *psz) { return (wxString&)wxStringBase::operator=(psz); } +#endif + #if wxUSE_UNICODE // 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); @@ -890,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 @@ -900,37 +1210,58 @@ 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) { - // test for IsEmpty() to share the string if possible - if ( IsEmpty() ) + // test for empty() to share the string if possible + if ( empty() ) *this = s; else 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; } @@ -940,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 @@ -964,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); } @@ -973,15 +1321,17 @@ public: // string comparison // case-sensitive comparison (returns a value < 0, = 0 or > 0) - int Cmp(const wxChar *psz) const { return wxStrcmp(c_str(), psz); } + int Cmp(const wxChar *psz) const; + int Cmp(const wxString& s) const; // same as Cmp() but not case-sensitive - int CmpNoCase(const wxChar *psz) const { return wxStricmp(c_str(), psz); } + int CmpNoCase(const wxChar *psz) const; + int CmpNoCase(const wxString& s) const; // test for the string equality, either considering case or not // (if compareWithCase then the case matters) - bool IsSameAs(const wxChar *psz, bool compareWithCase = TRUE) const + 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)); @@ -996,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; @@ -1007,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 @@ -1035,25 +1389,25 @@ public: // trimming/padding whitespace (either side) and truncating // remove spaces from left or from right (default) side - wxString& Trim(bool bFromRight = TRUE); + 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 // another string, returns the number of replacements made size_t Replace(const wxChar *szOld, const wxChar *szNew, - bool bReplaceAll = TRUE); + bool bReplaceAll = true); // check if the string contents matches a mask containing '*' and '?' bool Matches(const wxChar *szMask) const; - // conversion to numbers: all functions return TRUE only if the whole + // conversion to numbers: all functions return true only if the whole // string is a number and put the value of this number into the pointer // provided, the base is the numeric base in which the conversion should be // done and must be comprised between 2 and 36 or be 0 in which case the @@ -1062,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 @@ -1084,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 @@ -1103,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 @@ -1114,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 @@ -1124,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); } @@ -1133,14 +1505,16 @@ 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); } - bool Contains(const wxString& str) const { return Find(str) != -1; } + int Last( const wxUniChar ch ) const { return Find(ch, true); } + bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; } - // use IsEmpty() - bool IsNull() const { return IsEmpty(); } + // use empty() + bool IsNull() const { return empty(); } // std::string compatibility functions @@ -1150,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] @@ -1162,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) @@ -1186,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; @@ -1219,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 @@ -1247,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, @@ -1265,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) @@ -1277,16 +1653,72 @@ 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(); } +#else // !wxUSE_STL + // return an empty wxString (more efficient than wxString() here) + inline const wxString& wxGetEmptyString() + { + return *(wxString *)&wxEmptyString; + } +#endif // wxUSE_STL/!wxUSE_STL + // ---------------------------------------------------------------------------- // wxStringBuffer: a tiny class allowing to get a writable pointer into string // ---------------------------------------------------------------------------- @@ -1351,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; } @@ -1369,12 +1801,15 @@ class WXDLLIMPEXP_BASE wxStringBufferLength 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; } @@ -1395,46 +1830,10 @@ private: // wxString comparison functions: operator versions are always case sensitive // --------------------------------------------------------------------------- -#if wxUSE_STL - -inline bool operator==(const wxString& s1, const wxString& s2) - { return s1.compare(s2) == 0; } -inline bool operator==(const wxString& s1, const wxChar * s2) - { return s1.compare(s2) == 0; } -inline bool operator==(const wxChar * s1, const wxString& s2) - { return s2.compare(s1) == 0; } -inline bool operator!=(const wxString& s1, const wxString& s2) - { return s1.compare(s2) != 0; } -inline bool operator!=(const wxString& s1, const wxChar * s2) - { return s1.compare(s2) != 0; } -inline bool operator!=(const wxChar * s1, const wxString& s2) - { return s2.compare(s1) != 0; } -inline bool operator< (const wxString& s1, const wxString& s2) - { return s1.compare(s2) < 0; } -inline bool operator< (const wxString& s1, const wxChar * s2) - { return s1.compare(s2) < 0; } -inline bool operator< (const wxChar * s1, const wxString& s2) - { return s2.compare(s1) > 0; } -inline bool operator> (const wxString& s1, const wxString& s2) - { return s1.compare(s2) > 0; } -inline bool operator> (const wxString& s1, const wxChar * s2) - { return s1.compare(s2) > 0; } -inline bool operator> (const wxChar * s1, const wxString& s2) - { return s2.compare(s1) < 0; } -inline bool operator<=(const wxString& s1, const wxString& s2) - { return s1.compare(s2) <= 0; } -inline bool operator<=(const wxString& s1, const wxChar * s2) - { return s1.compare(s2) <= 0; } -inline bool operator<=(const wxChar * s1, const wxString& s2) - { return s2.compare(s1) >= 0; } -inline bool operator>=(const wxString& s1, const wxString& s2) - { return s1.compare(s2) >= 0; } -inline bool operator>=(const wxString& s1, const wxChar * s2) - { return s1.compare(s2) >= 0; } -inline bool operator>=(const wxChar * s1, const wxString& s2) - { return s2.compare(s1) <= 0; } - -#else // if !wxUSE_STL +// note that when wxUSE_STL == 1 the comparison operators taking std::string +// are used and defining them also for wxString would only result in +// compilation ambiguities when comparing std::string and wxString +#if !wxUSE_STL inline bool operator==(const wxString& s1, const wxString& s2) { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } @@ -1473,14 +1872,6 @@ inline bool operator>=(const wxString& s1, const wxChar * s2) inline bool operator>=(const wxChar * s1, const wxString& s2) { return s2.Cmp(s1) <= 0; } -#endif // !wxUSE_STL - -// comparison with char -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); } - #if wxUSE_UNICODE inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) { return (s1.Cmp((const wchar_t *)s2) == 0); } @@ -1501,16 +1892,6 @@ inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const char *)s1) != 0); } #endif // wxUSE_UNICODE/!wxUSE_UNICODE -#if !wxUSE_STL - -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); - -#endif // !wxUSE_STL - #if wxUSE_UNICODE inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) { return string + (const wchar_t *)buf; } @@ -1523,6 +1904,57 @@ inline wxString operator+(const wxCharBuffer& buf, const wxString& string) { return (const char *)buf + string; } #endif // wxUSE_UNICODE/!wxUSE_UNICODE +#endif // !wxUSE_STL + +// comparison with char (those are not defined by std::[w]string and so should +// be always available) +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 // --------------------------------------------------------------------------- @@ -1534,9 +1966,88 @@ inline wxString operator+(const wxCharBuffer& buf, const wxString& string) #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__