From 04abe4bc8fbddf68ecc9950e2e743e3746ab26b6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 3 Apr 2007 20:29:42 +0000 Subject: [PATCH] cleanup of wxString ctors and operator=: made both char* and wchar_t* versions available, regardless of the build git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45242 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/string.h | 149 +++++++++++++++++++----------------------- src/common/string.cpp | 80 ++++++----------------- 2 files changed, 89 insertions(+), 140 deletions(-) diff --git a/include/wx/string.h b/include/wx/string.h index 3efcab7b9f..e598ccd5a1 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -483,78 +483,69 @@ public: // constructors and destructor // ctor for an empty string wxString() {} + // copy ctor wxString(const wxStringImpl& stringSrc) : m_impl(stringSrc) { } - wxString(const wxString& stringSrc) : m_impl(stringSrc) { } + wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { } + // string containing nRepeat copies of ch wxString(wxUniChar ch, size_t nRepeat = 1) - : m_impl(nRepeat, ch) { } + { assign(nRepeat, ch); } wxString(size_t nRepeat, wxUniChar ch) - : m_impl(nRepeat, ch) { } + { assign(nRepeat, ch); } wxString(wxUniCharRef ch, size_t nRepeat = 1) - : m_impl(nRepeat, ch) { } + { assign(nRepeat, ch); } wxString(size_t nRepeat, wxUniCharRef ch) - : m_impl(nRepeat, ch) { } + { assign(nRepeat, ch); } wxString(char ch, size_t nRepeat = 1) - : m_impl(nRepeat, ch) { } + { assign(nRepeat, ch); } wxString(size_t nRepeat, char ch) - : m_impl(nRepeat, ch) { } + { assign(nRepeat, ch); } wxString(wchar_t ch, size_t nRepeat = 1) - : m_impl(nRepeat, ch) { } + { assign(nRepeat, ch); } wxString(size_t nRepeat, wchar_t ch) - : m_impl(nRepeat, ch) { } - // ctor takes first nLength characters from C string - // (default value of npos means take all the string) - wxString(const wxChar *psz) - : m_impl(psz ? psz : wxT("")) { } - wxString(const wxChar *psz, size_t nLength) - : m_impl(psz, nLength) { } - wxString(const wxChar *psz, - const wxMBConv& WXUNUSED(conv), - size_t nLength = npos) - : m_impl(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 && !wxUSE_STL_BASED_WXSTRING - wxString(const wxStdString& s) - : m_impl(s.c_str()) { } // FIXME-UTF8: this is broken for embedded 0s -#endif // wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING - -#if wxUSE_UNICODE - // from multibyte string - wxString(const char *psz, - const wxMBConv& conv = wxConvLibc, - size_t nLength = npos); - // from multibyte string for ANSI compatibility, with wxConvLibc - wxString(const char *psz, size_t nLength); - // from wxWCharBuffer (i.e. return from wxGetString) - wxString(const wxWCharBuffer& psz) : m_impl(psz.data()) { } -#else // ANSI - // from C string (for compilers using unsigned char) - wxString(const unsigned char* psz) - : m_impl((const char*)psz) { } - // from part of C string (for compilers using unsigned char) - wxString(const unsigned char* psz, size_t nLength) - : m_impl((const char*)psz, nLength) { } + { assign(nRepeat, ch); } + + // ctors from char* strings: + wxString(const char *psz) + : m_impl(ImplStr(psz)) {} + wxString(const char *psz, const wxMBConv& conv) + : m_impl(ImplStr(psz, conv)) {} + wxString(const char *psz, size_t nLength) + { assign(psz, nLength); } + wxString(const char *psz, const wxMBConv& conv, size_t nLength) + { + SubstrBufFromMB str(ImplStr(psz, nLength, conv)); + m_impl.assign(str.data, str.len); + } -#if wxUSE_WCHAR_T - // from wide (Unicode) string - wxString(const wchar_t *pwz, - const wxMBConv& conv = wxConvLibc, - size_t nLength = npos); - // from wide string for Unicode compatibility, with wxConvLibc - wxString(const wchar_t *pwz, size_t nLength); -#endif // !wxUSE_WCHAR_T + // and unsigned char*: + wxString(const unsigned char *psz) + : m_impl(ImplStr((const char*)psz)) {} + wxString(const unsigned char *psz, const wxMBConv& conv) + : m_impl(ImplStr((const char*)psz, conv)) {} + wxString(const unsigned char *psz, size_t nLength) + { assign((const char*)psz, nLength); } + wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength) + { + SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv)); + m_impl.assign(str.data, str.len); + } - // from wxCharBuffer - wxString(const wxCharBuffer& psz) - : m_impl(psz) { } -#endif // Unicode/ANSI + // ctors from wchar_t* strings: + wxString(const wchar_t *pwz) + : m_impl(ImplStr(pwz)) {} + wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv)) + : m_impl(ImplStr(pwz)) {} + wxString(const wchar_t *pwz, size_t nLength) + { assign(pwz, nLength); } + 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 wxCStrData& cstr) : m_impl(cstr.AsString().m_impl) { } @@ -990,8 +981,8 @@ public: // overloaded assignment // from another wxString - wxString& operator=(const wxStringImpl& stringSrc) - { m_impl = stringSrc; return *this; } + wxString& operator=(const wxString& stringSrc) + { m_impl = stringSrc.m_impl; return *this; } wxString& operator=(const wxCStrData& cstr) { return *this = cstr.AsString(); } // from a character @@ -1008,31 +999,25 @@ public: // from a C string - STL probably will crash on NULL, // so we need to compensate in that case #if wxUSE_STL_BASED_WXSTRING - wxString& operator=(const wxChar *psz) - { if(psz) m_impl = psz; else Clear(); return *this; } + wxString& operator=(const char *psz) + { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; } + wxString& operator=(const wchar_t *pwz) + { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; } #else - wxString& operator=(const wxChar *psz) - { m_impl = psz; return *this; } + wxString& operator=(const char *psz) + { m_impl = ImplStr(psz); return *this; } + wxString& operator=(const wchar_t *pwz) + { m_impl = ImplStr(pwz); return *this; } #endif + wxString& operator=(const unsigned char *psz) + { return operator=((const char*)psz); } -#if wxUSE_UNICODE // from wxWCharBuffer wxString& operator=(const wxWCharBuffer& s) - { (void) operator=((const wchar_t *)s); 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); -#if wxUSE_WCHAR_T - // from a wide string - wxString& operator=(const wchar_t *pwz); -#endif + { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs // from wxCharBuffer - wxString& operator=(const wxCharBuffer& psz) - { (void) operator=((const char *)psz); return *this; } -#endif // Unicode/ANSI + wxString& operator=(const wxCharBuffer& s) + { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs // string concatenation // in place concatenation @@ -2201,6 +2186,10 @@ wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) 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&); +#ifndef __BORLANDC__ +WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&); +#endif #endif // wxSTD_STRING_COMPATIBILITY diff --git a/src/common/string.cpp b/src/common/string.cpp index 5ad43a3dea..6cdcaef003 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -75,13 +75,9 @@ const size_t wxString::npos = (size_t) -1; #include -wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str) -{ - return os << str.c_str(); -} - wxSTD ostream& operator<<(wxSTD ostream& os, const wxCStrData& str) { +// FIXME-UTF8: always, not only if wxUSE_UNICODE #if wxUSE_UNICODE && !defined(__BORLANDC__) return os << str.AsWChar(); #else @@ -89,6 +85,23 @@ wxSTD ostream& operator<<(wxSTD ostream& os, const wxCStrData& str) #endif } +wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str) +{ + return os << str.c_str(); +} + +wxSTD ostream& operator<<(wxSTD ostream& os, const wxCharBuffer& str) +{ + return os << str.data(); +} + +#ifndef __BORLANDC__ +wxSTD ostream& operator<<(wxSTD ostream& os, const wxWCharBuffer& str) +{ + return os << str.data(); +} +#endif + #endif // wxUSE_STD_IOSTREAM // =========================================================================== @@ -102,7 +115,7 @@ wxSTD ostream& operator<<(wxSTD ostream& os, const wxCStrData& str) #if wxUSE_UNICODE /* static */ wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength, - const wxMBConv& conv) + const wxMBConv& conv) { // anything to do? if ( !psz || nLength == 0 ) @@ -121,7 +134,7 @@ wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength, #else /* static */ wxString::SubstrBufFromWC wxString::ConvertStr(const wchar_t *pwz, size_t nLength, - const wxMBConv& conv) + const wxMBConv& conv) { // anything to do? if ( !pwz || nLength == 0 ) @@ -142,32 +155,6 @@ wxString::SubstrBufFromWC wxString::ConvertStr(const wchar_t *pwz, size_t nLengt #if wxUSE_UNICODE -// from multibyte string -wxString::wxString(const char *psz, const wxMBConv& conv, size_t nLength) -{ - // FIXME-UTF8: this will need changes - - // anything to do? - if ( psz && nLength != 0 ) - { - if ( nLength == npos ) - { - nLength = wxNO_LEN; - } - - size_t nLenWide; - wxWCharBuffer wbuf = conv.cMB2WC(psz, nLength, &nLenWide); - - if ( nLenWide ) - assign(wbuf, nLenWide); - } -} - -wxString::wxString(const char *psz, size_t nLength) -{ - assign(psz, nLength); -} - //Convert wxString in Unicode mode to a multi-byte string const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const { @@ -178,33 +165,6 @@ const wxCharBuffer wxString::mb_str(const wxMBConv& conv) const #if wxUSE_WCHAR_T -// from wide string -wxString::wxString(const wchar_t *pwz, const wxMBConv& conv, size_t nLength) -{ - // FIXME-UTF8: this will need changes - - // anything to do? - if ( pwz && nLength != 0 ) - { - if ( nLength == npos ) - { - nLength = wxNO_LEN; - } - - size_t nLenMB; - wxCharBuffer buf = conv.cWC2MB(pwz, nLength, &nLenMB); - - if ( nLenMB ) - assign(buf, nLenMB); - } - -} - -wxString::wxString(const wchar_t *pwz, size_t nLength) -{ - assign(pwz, nLength); -} - //Converts this string to a wide character string if unicode //mode is not enabled and wxUSE_WCHAR_T is enabled const wxWCharBuffer wxString::wc_str(const wxMBConv& conv) const -- 2.45.2