X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/52988b5f63f90cf4f4bf844c31b1b0b1ee0b19c3..03773e400b6ff2692f2a9ba045d652c3b7b7109d:/include/wx/string.h?ds=inline diff --git a/include/wx/string.h b/include/wx/string.h index ec66dce85c..517abb1185 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -209,7 +209,7 @@ inline int Stricmp(const char *psz1, const char *psz2) // Lightweight object returned by wxString::c_str() and implicitly convertible // to either const char* or const wchar_t*. -class WXDLLIMPEXP_BASE wxCStrData +class wxCStrData { private: // Ctors; for internal use by wxString and wxCStrData only @@ -245,8 +245,16 @@ public: operator const void*() const { return AsChar(); } - inline const wxCharBuffer AsCharBuf() const; - inline const wxWCharBuffer AsWCharBuf() const; + // returns buffers that are valid as long as the associated wxString exists + const wxScopedCharBuffer AsCharBuf() const + { + return wxScopedCharBuffer::CreateNonOwned(AsChar()); + } + + const wxScopedWCharBuffer AsWCharBuf() const + { + return wxScopedWCharBuffer::CreateNonOwned(AsWChar()); + } inline wxString AsString() const; @@ -280,7 +288,7 @@ public: wxCStrData operator-(ptrdiff_t n) const { wxASSERT_MSG( n <= (ptrdiff_t)m_offset, - _T("attempt to construct address before the beginning of the string") ); + wxT("attempt to construct address before the beginning of the string") ); return wxCStrData(m_str, m_offset - n, m_owned); } @@ -400,6 +408,7 @@ protected: #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 (push) #pragma warning (disable:4275) #endif @@ -486,14 +495,14 @@ private: #if wxUSE_UNICODE_UTF8 // even char* -> char* needs conversion, from locale charset to UTF-8 - typedef SubstrBufFromType SubstrBufFromWC; - typedef SubstrBufFromType SubstrBufFromMB; + typedef SubstrBufFromType SubstrBufFromWC; + typedef SubstrBufFromType SubstrBufFromMB; #elif wxUSE_UNICODE_WCHAR - typedef SubstrBufFromType SubstrBufFromWC; - typedef SubstrBufFromType SubstrBufFromMB; + typedef SubstrBufFromType SubstrBufFromWC; + typedef SubstrBufFromType SubstrBufFromMB; #else - typedef SubstrBufFromType SubstrBufFromMB; - typedef SubstrBufFromType SubstrBufFromWC; + typedef SubstrBufFromType SubstrBufFromMB; + typedef SubstrBufFromType SubstrBufFromWC; #endif @@ -522,8 +531,8 @@ private: { return str ? str : wxT(""); } static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); } - static wxWCharBuffer ImplStr(const char* str, - const wxMBConv& conv = wxConvLibc) + static wxScopedWCharBuffer ImplStr(const char* str, + const wxMBConv& conv = wxConvLibc) { return ConvertStr(str, npos, conv).data; } static SubstrBufFromMB ImplStr(const char* str, size_t n, const wxMBConv& conv = wxConvLibc) @@ -535,7 +544,7 @@ private: static const SubstrBufFromMB ImplStr(const char* str, size_t n, const wxMBConv& WXUNUSED(conv) = wxConvLibc) { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); } - static wxCharBuffer ImplStr(const wchar_t* str) + static wxScopedCharBuffer ImplStr(const wchar_t* str) { return ConvertStr(str, npos, wxConvLibc).data; } static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) { return ConvertStr(str, n, wxConvLibc); } @@ -560,14 +569,14 @@ private: #else // wxUSE_UNICODE_UTF8 - static wxCharBuffer ImplStr(const char* str, - const wxMBConv& conv = wxConvLibc) + static wxScopedCharBuffer ImplStr(const char* str, + const wxMBConv& conv = wxConvLibc) { return ConvertStr(str, npos, conv).data; } static SubstrBufFromMB ImplStr(const char* str, size_t n, const wxMBConv& conv = wxConvLibc) { return ConvertStr(str, n, conv); } - static wxCharBuffer ImplStr(const wchar_t* str) + static wxScopedCharBuffer ImplStr(const wchar_t* str) { return ConvertStr(str, npos, wxMBConvUTF8()).data; } static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n) { return ConvertStr(str, n, wxMBConvUTF8()); } @@ -996,8 +1005,8 @@ public: { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } private: - iterator(wxString *str, underlying_iterator ptr) - : m_cur(ptr), m_node(str, &m_cur) {} + iterator(wxString *wxstr, underlying_iterator ptr) + : m_cur(ptr), m_node(wxstr, &m_cur) {} wxString* str() const { return const_cast(m_node.m_str); } @@ -1041,8 +1050,8 @@ public: private: // for internal wxString use only: - const_iterator(const wxString *str, underlying_iterator ptr) - : m_cur(ptr), m_node(str, &m_cur) {} + const_iterator(const wxString *wxstr, underlying_iterator ptr) + : m_cur(ptr), m_node(wxstr, &m_cur) {} const wxString* str() const { return m_node.m_str; } @@ -1275,10 +1284,10 @@ public: wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength) { assign(pwz, nLength); } - wxString(const wxCharBuffer& buf) - { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length - wxString(const wxWCharBuffer& buf) - { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length + wxString(const wxScopedCharBuffer& buf) + { assign(buf.data(), buf.length()); } + wxString(const wxScopedWCharBuffer& buf) + { assign(buf.data(), buf.length()); } // NB: this version uses m_impl.c_str() to force making a copy of the // string, so that "wxString(str.c_str())" idiom for passing strings @@ -1340,8 +1349,10 @@ public: #else // wxStringImpl is either not std::string or needs conversion operator wxStdWideString() const - // FIXME-UTF8: broken for embedded NULs - { return wxStdWideString(wc_str()); } + { + wxScopedWCharBuffer buf(wc_str()); + return wxStdWideString(buf.data(), buf.length()); + } #endif #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING @@ -1350,8 +1361,10 @@ public: #else // wxStringImpl is either not std::string or needs conversion operator std::string() const - // FIXME-UTF8: broken for embedded NULs - { return std::string(mb_str()); } + { + wxScopedCharBuffer buf(mb_str()); + return std::string(buf.data(), buf.length()); + } #endif #endif // wxUSE_STL @@ -1500,14 +1513,14 @@ public: // get last character wxUniChar Last() const { - wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); + wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") ); return *rbegin(); } // get writable last character wxUniCharRef Last() { - wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") ); + wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") ); return *rbegin(); } @@ -1636,7 +1649,7 @@ public: if ( len ) *len = length(); - return wxCharTypeBuffer::CreateNonOwned(wx_str()); + return wxCharTypeBuffer::CreateNonOwned(wx_str(), length()); #endif // Unicode build kind } @@ -1650,7 +1663,7 @@ public: static wxString FromAscii(const char *ascii, size_t len); static wxString FromAscii(const char *ascii); static wxString FromAscii(char ascii); - const wxCharBuffer ToAscii() const; + const wxScopedCharBuffer ToAscii() const; #else // ANSI static wxString FromAscii(const char *ascii) { return wxString( ascii ); } static wxString FromAscii(const char *ascii, size_t len) @@ -1705,8 +1718,8 @@ public: return FromImpl(wxStringImpl(utf8, len)); } - const char* utf8_str() const { return wx_str(); } - const char* ToUTF8() const { return wx_str(); } + const wxScopedCharBuffer utf8_str() const + { return wxCharBuffer::CreateNonOwned(m_impl.c_str(), m_impl.length()); } // this function exists in UTF-8 build only and returns the length of the // internal UTF-8 representation @@ -1721,33 +1734,39 @@ public: "string must be valid UTF-8" ); return s; } - const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); } - const wxCharBuffer ToUTF8() const { return utf8_str(); } + const wxScopedCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); } #else // ANSI static wxString FromUTF8(const char *utf8) { return wxString(wxMBConvUTF8().cMB2WC(utf8)); } static wxString FromUTF8(const char *utf8, size_t len) { size_t wlen; - wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen)); + wxScopedWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen)); return wxString(buf.data(), wlen); } static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos) { size_t wlen; - wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, - len == npos ? wxNO_LEN : len, - &wlen)); + wxScopedWCharBuffer buf + ( + wxMBConvUTF8().cMB2WC + ( + utf8, + len == npos ? wxNO_LEN : len, + &wlen + ) + ); wxASSERT_MSG( !utf8 || !*utf8 || wlen, "string must be valid UTF-8" ); return wxString(buf.data(), wlen); } - const wxCharBuffer utf8_str() const + const wxScopedCharBuffer utf8_str() const { return wxMBConvUTF8().cWC2MB(wc_str()); } - const wxCharBuffer ToUTF8() const { return utf8_str(); } #endif + const wxScopedCharBuffer ToUTF8() const { return utf8_str(); } + // functions for storing binary data in wxString: #if wxUSE_UNICODE static wxString From8BitData(const char *data, size_t len) @@ -1755,7 +1774,8 @@ public: // version for NUL-terminated data: static wxString From8BitData(const char *data) { return wxString(data, wxConvISO8859_1); } - const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); } + const wxScopedCharBuffer To8BitData() const + { return mb_str(wxConvISO8859_1); } #else // ANSI static wxString From8BitData(const char *data, size_t len) { return wxString(data, len); } @@ -1774,48 +1794,62 @@ public: // accepting the file names. The return value is always the same, but the // type differs because a function may either return pointer to the buffer // directly or have to use intermediate buffer for translation. + #if wxUSE_UNICODE + // this is an optimization: even though using mb_str(wxConvLibc) does the + // same thing (i.e. returns pointer to internal representation as locale is + // always an UTF-8 one) in wxUSE_UTF8_LOCALE_ONLY case, we can avoid the + // extra checks and the temporary buffer construction by providing a + // separate mb_str() overload #if wxUSE_UTF8_LOCALE_ONLY const char* mb_str() const { return wx_str(); } - const wxCharBuffer mb_str(const wxMBConv& conv) const; -#else - const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; -#endif + const wxScopedCharBuffer mb_str(const wxMBConv& conv) const + { + return AsCharBuf(conv); + } +#else // !wxUSE_UTF8_LOCALE_ONLY + const wxScopedCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const + { + return AsCharBuf(conv); + } +#endif // wxUSE_UTF8_LOCALE_ONLY/!wxUSE_UTF8_LOCALE_ONLY const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); } #if wxUSE_UNICODE_WCHAR const wchar_t* wc_str() const { return wx_str(); } #elif wxUSE_UNICODE_UTF8 - const wxWCharBuffer wc_str() const; + const wxScopedWCharBuffer wc_str() const + { return AsWCharBuf(wxMBConvStrictUTF8()); } #endif // for compatibility with !wxUSE_UNICODE version const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const { return wc_str(); } #if wxMBFILES - const wxCharBuffer fn_str() const { return mb_str(wxConvFile); } + const wxScopedCharBuffer fn_str() const { return mb_str(wxConvFile); } #else // !wxMBFILES const wxWX2WCbuf fn_str() const { return wc_str(); } #endif // wxMBFILES/!wxMBFILES #else // ANSI - const wxChar* mb_str() const { return wx_str(); } + const char* mb_str() const { return wx_str(); } // for compatibility with wxUSE_UNICODE version const char* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); } const wxWX2MBbuf mbc_str() const { return mb_str(); } -#if wxUSE_WCHAR_T - const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const; -#endif // wxUSE_WCHAR_T - const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); } + const wxScopedWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const + { return AsWCharBuf(conv); } + + const wxScopedCharBuffer fn_str() const + { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); } #endif // Unicode/ANSI #if wxUSE_UNICODE_UTF8 - const wxWCharBuffer t_str() const { return wc_str(); } + const wxScopedWCharBuffer t_str() const { return wc_str(); } #elif wxUSE_UNICODE_WCHAR const wchar_t* t_str() const { return wx_str(); } #else @@ -1910,12 +1944,12 @@ public: wxString& operator=(const unsigned char *psz) { return operator=((const char*)psz); } - // from wxWCharBuffer - wxString& operator=(const wxWCharBuffer& s) - { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs - // from wxCharBuffer - wxString& operator=(const wxCharBuffer& s) - { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs + // from wxScopedWCharBuffer + wxString& operator=(const wxScopedWCharBuffer& s) + { return assign(s); } + // from wxScopedCharBuffer + wxString& operator=(const wxScopedCharBuffer& s) + { return assign(s); } // string concatenation // in place concatenation @@ -1929,7 +1963,7 @@ public: { #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 wxASSERT_MSG( s.IsValid(), - _T("did you forget to call UngetWriteBuf()?") ); + wxT("did you forget to call UngetWriteBuf()?") ); #endif append(s); @@ -1950,10 +1984,10 @@ public: wxString& operator<<(wchar_t ch) { append(1, ch); return *this; } // string += buffer (i.e. from wxGetString) - wxString& operator<<(const wxWCharBuffer& s) - { return operator<<((const wchar_t *)s); } - wxString& operator<<(const wxCharBuffer& s) - { return operator<<((const char *)s); } + wxString& operator<<(const wxScopedWCharBuffer& s) + { return append(s); } + wxString& operator<<(const wxScopedCharBuffer& s) + { return append(s); } // string += C string wxString& Append(const wxString& s) @@ -1971,9 +2005,9 @@ public: { append(pwz); return *this; } wxString& Append(const wxCStrData& psz) { append(psz); return *this; } - wxString& Append(const wxCharBuffer& psz) + wxString& Append(const wxScopedCharBuffer& psz) { append(psz); return *this; } - wxString& Append(const wxWCharBuffer& psz) + wxString& Append(const wxScopedWCharBuffer& psz) { append(psz); return *this; } wxString& Append(const char* psz, size_t nLen) { append(psz, nLen); return *this; } @@ -1981,9 +2015,9 @@ public: { append(pwz, nLen); return *this; } wxString& Append(const wxCStrData& psz, size_t nLen) { append(psz, nLen); return *this; } - wxString& Append(const wxCharBuffer& psz, size_t nLen) + wxString& Append(const wxScopedCharBuffer& psz, size_t nLen) { append(psz, nLen); return *this; } - wxString& Append(const wxWCharBuffer& psz, size_t nLen) + wxString& Append(const wxScopedWCharBuffer& psz, size_t nLen) { append(psz, nLen); return *this; } // append count copies of given character wxString& Append(wxUniChar ch, size_t count = 1u) @@ -2023,36 +2057,34 @@ public: // stream-like functions // insert an int into string wxString& operator<<(int i) - { return (*this) << Format(_T("%d"), i); } + { return (*this) << Format(wxT("%d"), i); } // insert an unsigned int into string wxString& operator<<(unsigned int ui) - { return (*this) << Format(_T("%u"), ui); } + { return (*this) << Format(wxT("%u"), ui); } // insert a long into string wxString& operator<<(long l) - { return (*this) << Format(_T("%ld"), l); } + { return (*this) << Format(wxT("%ld"), l); } // insert an unsigned long into string wxString& operator<<(unsigned long ul) - { return (*this) << Format(_T("%lu"), ul); } + { return (*this) << Format(wxT("%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); + return (*this) << Format("%" wxLongLongFmtSpec "d", ll); } // insert an unsigned long long wxString& operator<<(wxULongLong_t ull) { - const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u"); - return (*this) << Format(fmt , ull); + return (*this) << Format("%" wxLongLongFmtSpec "u" , ull); } #endif // wxLongLong_t && !wxLongLongIsLong // insert a float into string wxString& operator<<(float f) - { return (*this) << Format(_T("%f"), f); } + { return (*this) << Format(wxT("%f"), f); } // insert a double into string wxString& operator<<(double d) - { return (*this) << Format(_T("%g"), d); } + { return (*this) << Format(wxT("%g"), d); } // string comparison // case-sensitive comparison (returns a value < 0, = 0 or > 0) @@ -2064,9 +2096,9 @@ public: { return compare(s); } int Cmp(const wxCStrData& s) const { return compare(s); } - int Cmp(const wxCharBuffer& s) const + int Cmp(const wxScopedCharBuffer& s) const { return compare(s); } - int Cmp(const wxWCharBuffer& s) const + int Cmp(const wxScopedWCharBuffer& s) const { return compare(s); } // same as Cmp() but not case-sensitive int CmpNoCase(const wxString& s) const; @@ -2089,9 +2121,9 @@ public: bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const { return IsSameAs(str.AsString(), compareWithCase); } - bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const + bool IsSameAs(const wxScopedCharBuffer& str, bool compareWithCase = true) const { return IsSameAs(str.data(), compareWithCase); } - bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const + bool IsSameAs(const wxScopedWCharBuffer& str, bool compareWithCase = true) const { return IsSameAs(str.data(), compareWithCase); } // comparison with a single character: returns true if equal bool IsSameAs(wxUniChar c, bool compareWithCase = true) const; @@ -2198,9 +2230,9 @@ public: int Find(const wxCStrData& sub) const { return Find(sub.AsString()); } - int Find(const wxCharBuffer& sub) const + int Find(const wxScopedCharBuffer& sub) const { return Find(sub.data()); } - int Find(const wxWCharBuffer& sub) const + int Find(const wxScopedWCharBuffer& sub) const { return Find(sub.data()); } // replace first (or all of bReplaceAll) occurrences of substring with @@ -2239,7 +2271,7 @@ public: // convert to a double bool ToCDouble(double *val) const; #endif - + #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN // formatted input/output // as sprintf(), returns the number of characters written or < 0 on error @@ -2394,7 +2426,7 @@ public: CreateConstIterator(last).impl()) { wxASSERT_MSG( first.m_str == last.m_str, - _T("pointers must be into the same string") ); + wxT("pointers must be into the same string") ); } #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER @@ -2454,15 +2486,15 @@ public: wxString& append(const wxCStrData& str) { return append(str.AsString()); } - wxString& append(const wxCharBuffer& str) - { return append(str.data()); } - wxString& append(const wxWCharBuffer& str) - { return append(str.data()); } + wxString& append(const wxScopedCharBuffer& str) + { return append(str.data(), str.length()); } + wxString& append(const wxScopedWCharBuffer& str) + { return append(str.data(), str.length()); } wxString& append(const wxCStrData& str, size_t n) { return append(str.AsString(), 0, n); } - wxString& append(const wxCharBuffer& str, size_t n) + wxString& append(const wxScopedCharBuffer& str, size_t n) { return append(str.data(), n); } - wxString& append(const wxWCharBuffer& str, size_t n) + wxString& append(const wxScopedWCharBuffer& str, size_t n) { return append(str.data(), n); } // append n copies of ch @@ -2586,15 +2618,15 @@ public: wxString& assign(const wxCStrData& str) { return assign(str.AsString()); } - wxString& assign(const wxCharBuffer& str) - { return assign(str.data()); } - wxString& assign(const wxWCharBuffer& str) - { return assign(str.data()); } + wxString& assign(const wxScopedCharBuffer& str) + { return assign(str.data(), str.length()); } + wxString& assign(const wxScopedWCharBuffer& str) + { return assign(str.data(), str.length()); } wxString& assign(const wxCStrData& str, size_t len) { return assign(str.AsString(), len); } - wxString& assign(const wxCharBuffer& str, size_t len) + wxString& assign(const wxScopedCharBuffer& str, size_t len) { return assign(str.data(), len); } - wxString& assign(const wxWCharBuffer& str, size_t len) + wxString& assign(const wxScopedWCharBuffer& str, size_t len) { return assign(str.data(), len); } // same as `= n copies of ch' @@ -2645,9 +2677,9 @@ public: int compare(const wchar_t* sz) const; int compare(const wxCStrData& str) const { return compare(str.AsString()); } - int compare(const wxCharBuffer& str) const + int compare(const wxScopedCharBuffer& str) const { return compare(str.data()); } - int compare(const wxWCharBuffer& str) const + int compare(const wxScopedWCharBuffer& str) const { return compare(str.data()); } // comparison with a substring int compare(size_t nStart, size_t nLen, const wxString& str) const; @@ -3028,9 +3060,9 @@ public: SubstrBufFromWC str(ImplStr(sz, n)); return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len)); } - size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const + size_t find(const wxScopedCharBuffer& s, size_t nStart = 0, size_t n = npos) const { return find(s.data(), nStart, n); } - size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const + size_t find(const wxScopedWCharBuffer& s, size_t nStart = 0, size_t n = npos) const { return find(s.data(), nStart, n); } size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const { return find(s.AsWChar(), nStart, n); } @@ -3074,9 +3106,9 @@ public: SubstrBufFromWC str(ImplStr(sz, n)); return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len)); } - size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const + size_t rfind(const wxScopedCharBuffer& s, size_t nStart = npos, size_t n = npos) const { return rfind(s.data(), nStart, n); } - size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const + size_t rfind(const wxScopedWCharBuffer& s, size_t nStart = npos, size_t n = npos) const { return rfind(s.data(), nStart, n); } size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const { return rfind(s.AsWChar(), nStart, n); } @@ -3262,54 +3294,54 @@ public: // and additional overloads for the versions taking strings: size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const { return find_first_of(sz.AsString(), nStart); } - size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const + size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const { return find_first_of(sz.data(), nStart); } - size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const + size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const { return find_first_of(sz.data(), nStart); } size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const { return find_first_of(sz.AsWChar(), nStart, n); } - size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const + size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const { return find_first_of(sz.data(), nStart, n); } - size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const + size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const { return find_first_of(sz.data(), nStart, n); } size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const { return find_last_of(sz.AsString(), nStart); } - size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const + size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const { return find_last_of(sz.data(), nStart); } - size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const + size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const { return find_last_of(sz.data(), nStart); } size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const { return find_last_of(sz.AsWChar(), nStart, n); } - size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const + size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const { return find_last_of(sz.data(), nStart, n); } - size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const + size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const { return find_last_of(sz.data(), nStart, n); } size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const { return find_first_not_of(sz.AsString(), nStart); } - size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const + size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const { return find_first_not_of(sz.data(), nStart); } - size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const + size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const { return find_first_not_of(sz.data(), nStart); } size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const { return find_first_not_of(sz.AsWChar(), nStart, n); } - size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const + size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const { return find_first_not_of(sz.data(), nStart, n); } - size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const + size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const { return find_first_not_of(sz.data(), nStart, n); } size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const { return find_last_not_of(sz.AsString(), nStart); } - size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const + size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const { return find_last_not_of(sz.data(), nStart); } - size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const + size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const { return find_last_not_of(sz.data(), nStart); } size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const { return find_last_not_of(sz.AsWChar(), nStart, n); } - size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const + size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const { return find_last_not_of(sz.data(), nStart, n); } - size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const + size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const { return find_last_not_of(sz.data(), nStart, n); } // string += string @@ -3342,10 +3374,10 @@ public: m_impl += s.AsString().m_impl; return *this; } - wxString& operator+=(const wxCharBuffer& s) - { return operator+=(s.data()); } - wxString& operator+=(const wxWCharBuffer& s) - { return operator+=(s.data()); } + wxString& operator+=(const wxScopedCharBuffer& s) + { return append(s); } + wxString& operator+=(const wxScopedWCharBuffer& s) + { return append(s); } // string += char wxString& operator+=(wxUniChar ch) { @@ -3408,36 +3440,117 @@ private: wxStringImpl m_impl; // buffers for compatibility conversion from (char*)c_str() and - // (wchar_t*)c_str(): - // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers + // (wchar_t*)c_str(): the pointers returned by these functions should remain + // valid until the string itself is modified for compatibility with the + // existing code and consistency with std::string::c_str() so returning a + // temporary buffer won't do and we need to cache the conversion results + + // TODO-UTF8: benchmark various approaches to keeping compatibility buffers template struct ConvertedBuffer { - ConvertedBuffer() : m_buf(NULL) {} + // notice that there is no need to initialize m_len here as it's unused + // as long as m_str is NULL + ConvertedBuffer() : m_str(NULL) {} ~ConvertedBuffer() - { free(m_buf); } + { free(m_str); } - operator T*() const { return m_buf; } + bool Extend(size_t len) + { + // add extra 1 for the trailing NUL + void * const str = realloc(m_str, sizeof(T)*(len + 1)); + if ( !str ) + return false; + + m_str = static_cast(str); + m_len = len; + + return true; + } - ConvertedBuffer& operator=(T *str) + const wxScopedCharTypeBuffer AsScopedBuffer() const { - free(m_buf); - m_buf = str; - return *this; + return wxScopedCharTypeBuffer::CreateNonOwned(m_str, m_len); } - T *m_buf; + T *m_str; // pointer to the string data + size_t m_len; // length, not size, i.e. in chars and without last NUL }; -#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY + + +#if wxUSE_UNICODE + // common mb_str() and wxCStrData::AsChar() helper: performs the conversion + // and returns either m_convertedToChar.m_str (in which case its m_len is + // also updated) or NULL if it failed + // + // there is an important exception: in wxUSE_UNICODE_UTF8 build if conv is a + // UTF-8 one, we return m_impl.c_str() directly, without doing any conversion + // as optimization and so the caller needs to check for this before using + // m_convertedToChar + // + // NB: AsChar() returns char* in any build, unlike mb_str() + const char *AsChar(const wxMBConv& conv) const; + + // mb_str() implementation helper + wxScopedCharBuffer AsCharBuf(const wxMBConv& conv) const + { +#if wxUSE_UNICODE_UTF8 + // avoid conversion if we can + if ( conv.IsUTF8() ) + { + return wxScopedCharBuffer::CreateNonOwned(m_impl.c_str(), + m_impl.length()); + } +#endif // wxUSE_UNICODE_UTF8 + + // call this solely in order to fill in m_convertedToChar as AsChar() + // updates it as a side effect: this is a bit ugly but it's a completely + // internal function so the users of this class shouldn't care or know + // about it and doing it like this, i.e. having a separate AsChar(), + // allows us to avoid the creation and destruction of a temporary buffer + // when using wxCStrData without duplicating any code + if ( !AsChar(conv) ) + { + // although it would be probably more correct to return NULL buffer + // from here if the conversion fails, a lot of existing code doesn't + // expect mb_str() (or wc_str()) to ever return NULL so return an + // empty string otherwise to avoid crashes in it + // + // also, some existing code does check for the conversion success and + // so asserting here would be bad too -- even if it does mean that + // silently losing data is possible for badly written code + return wxScopedCharBuffer::CreateNonOwned("", 0); + } + + return m_convertedToChar.AsScopedBuffer(); + } + ConvertedBuffer m_convertedToChar; -#endif +#endif // !wxUSE_UNICODE + #if !wxUSE_UNICODE_WCHAR + // common wc_str() and wxCStrData::AsWChar() helper for both UTF-8 and ANSI + // builds: converts the string contents into m_convertedToWChar and returns + // NULL if the conversion failed (this can only happen in ANSI build) + // + // NB: AsWChar() returns wchar_t* in any build, unlike wc_str() + const wchar_t *AsWChar(const wxMBConv& conv) const; + + // wc_str() implementation helper + wxScopedWCharBuffer AsWCharBuf(const wxMBConv& conv) const + { + if ( !AsWChar(conv) ) + return wxScopedWCharBuffer::CreateNonOwned(L"", 0); + + return m_convertedToWChar.AsScopedBuffer(); + } + ConvertedBuffer m_convertedToWChar; -#endif +#endif // !wxUSE_UNICODE_WCHAR #if wxUSE_UNICODE_UTF8 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently - // assigning to character pointer to by wxString::interator may + // assigning to character pointer to by wxString::iterator may // change the underlying wxStringImpl iterator, so we have to // keep track of all iterators and update them as necessary: struct wxStringIteratorNodeHead @@ -3462,7 +3575,7 @@ private: }; #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN - #pragma warning (default:4275) + #pragma warning (pop) #endif // string iterator operators that satisfy STL Random Access Iterator @@ -3517,9 +3630,9 @@ namespace wxPrivate template <> struct wxStringAsBufHelper { - static wxCharBuffer Get(const wxString& s, size_t *len) + static wxScopedCharBuffer Get(const wxString& s, size_t *len) { - wxCharBuffer buf(s.mb_str()); + wxScopedCharBuffer buf(s.mb_str()); if ( len ) *len = buf ? strlen(buf) : 0; return buf; @@ -3529,11 +3642,12 @@ struct wxStringAsBufHelper template <> struct wxStringAsBufHelper { - static wxWCharBuffer Get(const wxString& s, size_t *len) + static wxScopedWCharBuffer Get(const wxString& s, size_t *len) { + const size_t length = s.length(); if ( len ) - *len = s.length(); - return wxWCharBuffer::CreateNonOwned(s.wx_str()); + *len = length; + return wxScopedWCharBuffer::CreateNonOwned(s.wx_str(), length); } }; @@ -3542,20 +3656,21 @@ struct wxStringAsBufHelper template <> struct wxStringAsBufHelper { - static wxCharBuffer Get(const wxString& s, size_t *len) + static wxScopedCharBuffer Get(const wxString& s, size_t *len) { + const size_t length = s.utf8_length(); if ( len ) - *len = s.utf8_length(); - return wxCharBuffer::CreateNonOwned(s.wx_str()); + *len = length; + return wxScopedCharBuffer::CreateNonOwned(s.wx_str(), length); } }; template <> struct wxStringAsBufHelper { - static wxWCharBuffer Get(const wxString& s, size_t *len) + static wxScopedWCharBuffer Get(const wxString& s, size_t *len) { - wxWCharBuffer wbuf(s.wc_str()); + wxScopedWCharBuffer wbuf(s.wc_str()); if ( len ) *len = wxWcslen(wbuf); return wbuf; @@ -3859,32 +3974,32 @@ inline bool operator!=(const wxString& s1, const wxCStrData& s2) inline bool operator!=(const wxCStrData& s1, const wxString& s2) { return s1.AsString() != s2; } -inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) +inline bool operator==(const wxString& s1, const wxScopedWCharBuffer& s2) { return (s1.Cmp((const wchar_t *)s2) == 0); } -inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) +inline bool operator==(const wxScopedWCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const wchar_t *)s1) == 0); } -inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) +inline bool operator!=(const wxString& s1, const wxScopedWCharBuffer& s2) { return (s1.Cmp((const wchar_t *)s2) != 0); } -inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) +inline bool operator!=(const wxScopedWCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const wchar_t *)s1) != 0); } -inline bool operator==(const wxString& s1, const wxCharBuffer& s2) +inline bool operator==(const wxString& s1, const wxScopedCharBuffer& s2) { return (s1.Cmp((const char *)s2) == 0); } -inline bool operator==(const wxCharBuffer& s1, const wxString& s2) +inline bool operator==(const wxScopedCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const char *)s1) == 0); } -inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) +inline bool operator!=(const wxString& s1, const wxScopedCharBuffer& s2) { return (s1.Cmp((const char *)s2) != 0); } -inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) +inline bool operator!=(const wxScopedCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const char *)s1) != 0); } -inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) +inline wxString operator+(const wxString& string, const wxScopedWCharBuffer& buf) { return string + (const wchar_t *)buf; } -inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) +inline wxString operator+(const wxScopedWCharBuffer& buf, const wxString& string) { return (const wchar_t *)buf + string; } -inline wxString operator+(const wxString& string, const wxCharBuffer& buf) +inline wxString operator+(const wxString& string, const wxScopedCharBuffer& buf) { return string + (const char *)buf; } -inline wxString operator+(const wxCharBuffer& buf, const wxString& string) +inline wxString operator+(const wxScopedCharBuffer& buf, const wxString& string) { return (const char *)buf + string; } // comparison with char @@ -3942,16 +4057,16 @@ wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&); WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&); -WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&); +WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxScopedCharBuffer&); #ifndef __BORLANDC__ -WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&); +WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxScopedWCharBuffer&); #endif #if wxUSE_UNICODE && defined(HAVE_WOSTREAM) WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&); WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&); -WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxWCharBuffer&); +WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxScopedWCharBuffer&); #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM) @@ -3979,45 +4094,53 @@ inline wxCStrData::~wxCStrData() delete const_cast(m_str); // cast to silence warnings } -// simple cases for AsChar() and AsWChar(), the complicated ones are -// in string.cpp -#if wxUSE_UNICODE_WCHAR +// AsChar() and AsWChar() implementations simply forward to wxString methods + inline const wchar_t* wxCStrData::AsWChar() const { - return m_str->wx_str() + m_offset; -} -#endif // wxUSE_UNICODE_WCHAR + const wchar_t * const p = +#if wxUSE_UNICODE_WCHAR + m_str->wc_str(); +#elif wxUSE_UNICODE_UTF8 + m_str->AsWChar(wxMBConvStrictUTF8()); +#else + m_str->AsWChar(wxConvLibc); +#endif + // in Unicode build the string always has a valid Unicode representation + // and even if a conversion is needed (as in UTF8 case) it can't fail + // + // but in ANSI build the string contents might be not convertible to + // Unicode using the current locale encoding so we do need to check for + // errors #if !wxUSE_UNICODE -inline const char* wxCStrData::AsChar() const -{ - return m_str->wx_str() + m_offset; -} + if ( !p ) + { + // if conversion fails, return empty string and not NULL to avoid + // crashes in code written with either wxWidgets 2 wxString or + // std::string behaviour in mind: neither of them ever returns NULL + // from its c_str() and so we shouldn't neither + // + // notice that the same is done in AsChar() below and + // wxString::wc_str() and mb_str() for the same reasons + return L""; + } #endif // !wxUSE_UNICODE -#if wxUSE_UTF8_LOCALE_ONLY -inline const char* wxCStrData::AsChar() const -{ - return wxStringOperations::AddToIter(m_str->wx_str(), m_offset); + return p + m_offset; } -#endif // wxUSE_UTF8_LOCALE_ONLY -inline const wxCharBuffer wxCStrData::AsCharBuf() const -{ -#if !wxUSE_UNICODE - return wxCharBuffer::CreateNonOwned(AsChar()); -#else - return AsString().mb_str(); -#endif -} - -inline const wxWCharBuffer wxCStrData::AsWCharBuf() const +inline const char* wxCStrData::AsChar() const { -#if wxUSE_UNICODE_WCHAR - return wxWCharBuffer::CreateNonOwned(AsWChar()); -#else - return AsString().wc_str(); -#endif +#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY + const char * const p = m_str->AsChar(wxConvLibc); + if ( !p ) + return ""; +#else // !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY + const char * const p = m_str->mb_str(); +#endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY + + return p + m_offset; } inline wxString wxCStrData::AsString() const @@ -4040,7 +4163,7 @@ inline const wxStringCharType *wxCStrData::AsInternal() const inline wxUniChar wxCStrData::operator*() const { if ( m_str->empty() ) - return wxUniChar(_T('\0')); + return wxUniChar(wxT('\0')); else return (*m_str)[m_offset]; }