X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a0be6908526c8030fc8649b5d0a42c49ce4585ad..32efab35c14e1cf09cc0c3edd3d2345a66fcea3d:/include/wx/string.h diff --git a/include/wx/string.h b/include/wx/string.h index 4162fe4fc8..6cf19b5022 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 // ---------------------------------------------------------------------------- @@ -163,7 +159,12 @@ inline int Stricmp(const char *psz1, const char *psz2) #endif // OS/compiler } -#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 @@ -171,22 +172,33 @@ inline int Stricmp(const char *psz1, const char *psz2) #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 @@ -250,7 +262,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(); } @@ -313,7 +325,9 @@ public: { 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,9 +366,9 @@ public: 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; } // resize the string, filling the space with c if c != 0 @@ -362,7 +376,7 @@ public: // 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; } @@ -371,12 +385,9 @@ 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]; } // lib.string.modifiers // append elements str[pos], ..., str[pos+n] @@ -505,11 +516,8 @@ 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; @@ -519,7 +527,6 @@ public: // 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; @@ -610,7 +617,7 @@ friend class WXDLLIMPEXP_BASE wxArrayString; // 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 @@ -645,22 +652,40 @@ 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, 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 @@ -674,7 +699,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 @@ -682,7 +707,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() @@ -702,66 +727,58 @@ public: // data access (all indexes are 0 based) // read access wxChar GetChar(size_t n) const - { return operator[](n); } + { return at(n); } // read/write access wxChar& GetWritableChar(size_t n) - { return operator[](n); } + { return at(n); } // write access void SetChar(size_t n, wxChar ch) - { operator[](n) = ch; } + { at(n) = ch; } // get last character wxChar 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() { - 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]. 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. */ - // operator version of GetChar - wxChar operator[](int n) const - { return wxStringBase::operator[](n); } - wxChar& operator[](size_type n) - { return wxStringBase::operator[](n); } +#if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__) + wxChar operator[](int n) const + { return wxStringBase::at(n); } wxChar operator[](size_type n) const - { return wxStringBase::operator[](n); } + { return wxStringBase::at(n); } #ifndef wxSIZE_T_IS_UINT - // operator version of GetChar wxChar operator[](unsigned int n) const - { return wxStringBase::operator[](n); } + { return wxStringBase::at(n); } +#endif // size_t != unsigned int +#endif // broken compiler + - // operator version of GetWriteableChar + // operator versions of GetWriteableChar() + wxChar& operator[](int n) + { return wxStringBase::at(n); } + wxChar& operator[](size_type n) + { return wxStringBase::at(n); } +#ifndef wxSIZE_T_IS_UINT wxChar& operator[](unsigned int n) - { return wxStringBase::operator[](n); } + { return wxStringBase::at(n); } #endif // size_t != unsigned int // implicit conversion to C string @@ -798,14 +815,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); } @@ -816,12 +833,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 ) ); } @@ -837,9 +854,16 @@ public: // from a character wxString& operator=(wxChar ch) { return (wxString&)wxStringBase::operator=(ch); } - // from a C string + // 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) @@ -896,8 +920,8 @@ public: // 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); @@ -916,16 +940,19 @@ public: { *this = str + *this; return *this; } // non-destructive concatenation - // - friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2); - // + // 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, wxChar ch); - // + // char with a string 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); + // 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 @@ -940,6 +967,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); } @@ -958,7 +999,7 @@ 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 + // comparison with a single character: returns true if equal bool IsSameAs(wxChar c, bool compareWithCase = true) const { return (length() == 1) && (compareWithCase ? GetChar(0u) == c @@ -1043,7 +1084,7 @@ public: // convert to a double bool ToDouble(double *val) const; - // formated input/output + // 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; @@ -1117,8 +1158,8 @@ public: int Last( const wxChar 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 @@ -1171,7 +1212,7 @@ public: { 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; @@ -1260,6 +1301,17 @@ public: { return (wxString&)wxStringBase::operator+=(ch); } }; +// 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, 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); + + // define wxArrayString, for compatibility #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL #include "wx/arrstr.h" @@ -1449,12 +1501,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; }