// use in DLL build under pre-Vista Windows so we disable this code for now, if
// anybody really needs to use UTF-8 build under Windows with this optimization
// it would have to be re-tested and probably corrected
-#if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__)
+// CS: under OSX release builds the string destructor/cache cleanup sometimes
+// crashes, disable until we find the true reason or a better workaround
+#if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__) && !defined(__WXOSX__)
#define wxUSE_STRING_POS_CACHE 1
#else
#define wxUSE_STRING_POS_CACHE 0
// 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
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;
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():
+ // 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):
+ // (we need both versions to resolve ambiguities). Note that this means
+ // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it
+ // is *not* number of Unicode characters in wxString.
wxCStrData operator+(int n) const
{ return wxCStrData(m_str, m_offset + n, m_owned); }
wxCStrData operator+(long n) const
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);
}
inline wxUniChar operator*() const;
private:
+ // the wxString this object was returned for
const wxString *m_str;
+ // Offset into c_str() return value. Note that this is *not* offset in
+ // m_str in Unicode characters. Instead, it is index into the
+ // char*/wchar_t* buffer returned by c_str(). It's interpretation depends
+ // on how is the wxCStrData instance used: if it is eventually cast to
+ // const char*, m_offset will be in bytes form string's start; if it is
+ // cast to const wchar_t*, it will be in wchar_t values.
size_t m_offset;
+ // should m_str be deleted, i.e. is it owned by us?
bool m_owned;
friend class WXDLLIMPEXP_FWD_BASE wxString;
// these are duplicated wxString methods, they're also declared below
// if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
- // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
+ // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1;
WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
Format, 1, (const wxFormatString&),
DoFormatWchar, DoFormatUtf8)
// int Printf(const wxString& format, ...);
WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
DoPrintfWchar, DoPrintfUtf8)
- // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
+ // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
DoPrintfWchar, DoPrintfUtf8)
#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
// the node belongs to a particular iterator instance, it's not copied
// when a copy of the iterator is made
- DECLARE_NO_COPY_CLASS(wxStringIteratorNode)
+ wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode);
};
#endif // wxUSE_UNICODE_UTF8
#if wxUSE_UNICODE_UTF8
// even char* -> char* needs conversion, from locale charset to UTF-8
- typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
- typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB;
+ typedef SubstrBufFromType<wxScopedCharBuffer> SubstrBufFromWC;
+ typedef SubstrBufFromType<wxScopedCharBuffer> SubstrBufFromMB;
#elif wxUSE_UNICODE_WCHAR
- typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
- typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB;
+ typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
+ typedef SubstrBufFromType<wxScopedWCharBuffer> SubstrBufFromMB;
#else
- typedef SubstrBufFromType<const char*> SubstrBufFromMB;
- typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
+ typedef SubstrBufFromType<const char*> SubstrBufFromMB;
+ typedef SubstrBufFromType<wxScopedCharBuffer> SubstrBufFromWC;
#endif
{ 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)
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); }
#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()); }
return wxTLS_VALUE(s_cache);
}
-
+
// this helper struct is used to ensure that GetCache() is called during
// static initialization time, i.e. before any threads creation, as otherwise
// the static s_cache construction inside GetCache() wouldn't be MT-safe
// a lot of misses in this function...)
Cache::Element * const cacheBegin = GetCacheBegin();
#ifndef wxHAS_COMPILER_TLS
- // during destruction tls calls may return NULL, in this case return NULL
+ // during destruction tls calls may return NULL, in this case return NULL
// immediately without accessing anything else
if ( cacheBegin == NULL )
return NULL;
{ 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 wx_const_cast(wxString*, m_node.m_str); }
+ wxString* str() const { return const_cast<wxString*>(m_node.m_str); }
wxStringIteratorNode m_node;
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; }
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
#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
#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
// 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();
}
if ( len )
*len = length();
- return wxCharTypeBuffer<T>::CreateNonOwned(wx_str());
+ return wxCharTypeBuffer<T>::CreateNonOwned(wx_str(), length());
#endif // Unicode build kind
}
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)
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
"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)
// 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); }
// 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
const char* t_str() const { return wx_str(); }
-#endif
+#endif
// overloaded assignment
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
{
#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);
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)
{ 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; }
{ 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)
// 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");
+ const wxChar *fmt = wxT("%") wxLongLongFmtSpec wxT("d");
return (*this) << Format(fmt, ll);
}
// insert an unsigned long long
wxString& operator<<(wxULongLong_t ull)
{
- const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
+ const wxChar *fmt = wxT("%") wxLongLongFmtSpec wxT("u");
return (*this) << Format(fmt , 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)
{ 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;
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;
wxString Left(size_t nCount) const;
// get last nCount characters
wxString Right(size_t nCount) const;
- // get all characters before the first occurance of ch
+ // get all characters before the first occurrence of ch
// (returns the whole string if ch not found)
wxString BeforeFirst(wxUniChar ch) const;
- // get all characters before the last occurence of ch
+ // get all characters before the last occurrence of ch
// (returns empty string if ch not found)
wxString BeforeLast(wxUniChar ch) const;
- // get all characters after the first occurence of ch
+ // get all characters after the first occurrence of ch
// (returns empty string if ch not found)
wxString AfterFirst(wxUniChar ch) const;
- // get all characters after the last occurence of ch
+ // get all characters after the last occurrence of ch
// (returns the whole string if ch not found)
wxString AfterLast(wxUniChar ch) const;
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) occurences of substring with
+ // replace first (or all of bReplaceAll) occurrences of substring with
// another string, returns the number of replacements made
size_t Replace(const wxString& strOld,
const wxString& strNew,
// check if the string contents matches a mask containing '*' and '?'
bool Matches(const wxString& mask) const;
- // 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
- // standard C rules apply (leading '0' => octal, "0x" => hex)
- // convert to a signed integer
- 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
+ // 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
+ // standard C rules apply (leading '0' => octal, "0x" => hex)
+ // convert to a signed integer
+ 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;
+ 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;
-
+ // convert to a double
+ bool ToDouble(double *val) const;
+
+#if wxUSE_XLOCALE
+ // conversions to numbers using C locale
+ // convert to a signed integer
+ bool ToCLong(long *val, int base = 10) const;
+ // convert to an unsigned integer
+ bool ToCULong(unsigned long *val, int base = 10) const;
+ // convert to a double
+ bool ToCDouble(double *val) const;
+#endif
#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
// formatted input/output
#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
// returns the string containing the result of Printf() to it
- // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
+ // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
DoFormatWchar, DoFormatUtf8)
#ifdef __WATCOMC__
#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
// use Printf()
// (take 'this' into account in attribute parameter count)
- // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
+ // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
DoPrintfWchar, DoPrintfUtf8)
#ifdef __WATCOMC__
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
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
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'
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;
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); }
- // find the first occurence of character ch after nStart
+ // find the first occurrence of character ch after nStart
size_t find(wxUniChar ch, size_t nStart = 0) const
{
#if wxUSE_UNICODE_UTF8
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); }
size_t rfind(wchar_t ch, size_t nStart = npos) const
{ return rfind(wxUniChar(ch), nStart); }
- // find first/last occurence of any character (not) in the set:
+ // find first/last occurrence of any character (not) in the set:
#if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
// FIXME-UTF8: this is not entirely correct, because it doesn't work if
// sizeof(wchar_t)==2 and surrogates are present in the string;
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
+ // find first/last occurrence of any character not in the set
// as strspn() (starting from nStart), returns npos on failure
size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
// 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
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)
{
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<typename T>
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); }
+
+ 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;
- operator T*() const { return m_buf; }
+ m_str = static_cast<T *>(str);
+ m_len = len;
- ConvertedBuffer& operator=(T *str)
+ return true;
+ }
+
+ const wxScopedCharTypeBuffer<T> AsScopedBuffer() const
{
- free(m_buf);
- m_buf = str;
- return *this;
+ return wxScopedCharTypeBuffer<T>::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<char> 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<wchar_t> 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
// copying is disallowed as it would result in more than one pointer into
// the same linked list
- DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead)
+ wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead);
};
wxStringIteratorNodeHead m_iterators;
};
#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
- #pragma warning (default:4275)
+ #pragma warning (pop)
#endif
// string iterator operators that satisfy STL Random Access Iterator
template <>
struct wxStringAsBufHelper<char>
{
- 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;
template <>
struct wxStringAsBufHelper<wchar_t>
{
- 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);
}
};
template <>
struct wxStringAsBufHelper<char>
{
- 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<wchar_t>
{
- 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;
wxString& m_str;
wxStringCharType *m_buf;
- DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
};
class wxStringInternalBufferLength
size_t m_len;
bool m_lenSet;
- DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
};
#endif // !wxUSE_STL_BASED_WXSTRING
template<typename T>
-class WXDLLIMPEXP_BASE wxStringTypeBufferBase
+class wxStringTypeBufferBase
{
public:
typedef T CharType;
};
template<typename T>
-class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
- : public wxStringTypeBufferBase<T>
+class wxStringTypeBufferLengthBase : public wxStringTypeBufferBase<T>
{
public:
wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
this->m_str.assign(this->m_buf.data());
}
- DECLARE_NO_COPY_CLASS(wxStringTypeBuffer)
+ wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer);
};
template<typename T>
this->m_str.assign(this->m_buf.data(), this->m_len);
}
- DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength)
+ wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength);
};
#if wxUSE_STL_BASED_WXSTRING
~wxStringInternalBuffer()
{ m_str.m_impl.assign(m_buf.data()); }
- DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
};
WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
m_str.m_impl.assign(m_buf.data(), m_len);
}
- DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
};
#endif // wxUSE_STL_BASED_WXSTRING
WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
-class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
+// Note about inlined dtors in the classes below: this is done not for
+// performance reasons but just to avoid linking errors in the MSVC DLL build
+// under Windows: if a class has non-inline methods it must be declared as
+// being DLL-exported but, due to an extremely interesting feature of MSVC 7
+// and later, any template class which is used as a base of a DLL-exported
+// class is implicitly made DLL-exported too, as explained at the bottom of
+// http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
+// _inheriting_ from a class can change whether it is being exported from DLL)
+//
+// But this results in link errors because the base template class is not DLL-
+// exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
+// does have only inline functions. So the simplest fix is to just make all the
+// functions of these classes inline too.
+
+class wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
{
public:
wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
: wxStringTypeBufferBase<char>(str, lenWanted) {}
- ~wxUTF8StringBuffer();
+ ~wxUTF8StringBuffer()
+ {
+ wxMBConvStrictUTF8 conv;
+ size_t wlen = conv.ToWChar(NULL, 0, m_buf);
+ wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
+
+ wxStringInternalBuffer wbuf(m_str, wlen);
+ conv.ToWChar(wbuf, wlen, m_buf);
+ }
- DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer)
+ wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer);
};
WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
-class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
- : public wxStringTypeBufferLengthBase<char>
+class wxUTF8StringBufferLength : public wxStringTypeBufferLengthBase<char>
{
public:
wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
: wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
- ~wxUTF8StringBufferLength();
+ ~wxUTF8StringBufferLength()
+ {
+ wxCHECK_RET(m_lenSet, "length not set");
+
+ wxMBConvStrictUTF8 conv;
+ size_t wlen = conv.ToWChar(NULL, 0, m_buf, m_len);
+ wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
- DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength)
+ wxStringInternalBufferLength wbuf(m_str, wlen);
+ conv.ToWChar(wbuf, wlen, m_buf, m_len);
+ wbuf.SetLength(wlen);
+ }
+
+ wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength);
};
#endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
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
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)
inline wxCStrData::~wxCStrData()
{
if ( m_owned )
- delete wx_const_cast(wxString*, m_str); // cast to silence warnings
+ delete const_cast<wxString*>(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
inline wxUniChar wxCStrData::operator*() const
{
if ( m_str->empty() )
- return wxUniChar(_T('\0'));
+ return wxUniChar(wxT('\0'));
else
return (*m_str)[m_offset];
}
if ( str )
{
m_next = str->m_iterators.ptr;
- wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
+ const_cast<wxString*>(m_str)->m_iterators.ptr = this;
if ( m_next )
m_next->m_prev = this;
}
if ( m_prev )
m_prev->m_next = m_next;
else if ( m_str ) // first in the list
- wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next;
+ const_cast<wxString*>(m_str)->m_iterators.ptr = m_next;
m_next = m_prev = NULL;
m_citer = NULL;
#include "wx/crt.h"
#endif
+// ----------------------------------------------------------------------------
+// Checks on wxString characters
+// ----------------------------------------------------------------------------
+
+template<bool (T)(const wxUniChar& c)>
+ inline bool wxStringCheck(const wxString& val)
+ {
+ for ( wxString::const_iterator i = val.begin();
+ i != val.end();
+ ++i )
+ if (T(*i) == 0)
+ return false;
+ return true;
+ }
+
#endif // _WX_WXSTRING_H_