#include "wx/defs.h" // everybody should include this
-#ifndef __WXPALMOS5__
#if defined(__WXMAC__) || defined(__VISAGECPP__)
#include <ctype.h>
#endif
#ifdef HAVE_STRCASECMP_IN_STRINGS_H
#include <strings.h> // for strcasecmp()
#endif // HAVE_STRCASECMP_IN_STRINGS_H
-#endif // ! __WXPALMOS5__
#include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
#include "wx/strvararg.h"
// it would have to be re-tested and probably corrected
// 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__)
+#if wxUSE_UNICODE_UTF8 && !defined(__WINDOWS__) && !defined(__WXOSX__)
#define wxUSE_STRING_POS_CACHE 1
#else
#define wxUSE_STRING_POS_CACHE 0
return stricmp(psz1, psz2);
#elif defined(__WXPM__)
return stricmp(psz1, psz2);
-#elif defined(__WXPALMOS__) || \
- defined(HAVE_STRCASECMP_IN_STRING_H) || \
+#elif defined(HAVE_STRCASECMP_IN_STRING_H) || \
defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
defined(__GNUWIN32__)
return strcasecmp(psz1, 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
inline ~wxCStrData();
- // methods defined inline below must be declared inline or mingw32 3.4.5
- // warns about "<symbol> defined locally after being referenced with
- // dllimport linkage"
-#if wxUSE_UNICODE_WCHAR
- inline
-#endif
- const wchar_t* AsWChar() const;
+ // AsWChar() and AsChar() can't be defined here as they use wxString and so
+ // must come after it and because of this won't be inlined when called from
+ // wxString methods (without a lot of work to extract these wxString methods
+ // from inside the class itself). But we still define them being inline
+ // below to let compiler inline them from elsewhere. And because of this we
+ // must declare them as inline here because otherwise some compilers give
+ // warnings about them, e.g. mingw32 3.4.5 warns about "<symbol> defined
+ // locally after being referenced with dllimport linkage" while IRIX
+ // mipsPro 7.4 warns about "function declared inline after being called".
+ inline const wchar_t* AsWChar() const;
operator const wchar_t*() const { return AsWChar(); }
-#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
- inline
-#endif
- const char* AsChar() const;
+ inline const char* AsChar() const;
const unsigned char* AsUnsignedChar() const
{ return (const unsigned char *) AsChar(); }
operator const char*() const { return AsChar(); }
operator const void*() const { return AsChar(); }
// returns buffers that are valid as long as the associated wxString exists
- inline const wxScopedCharBuffer AsCharBuf() const;
- inline const wxScopedWCharBuffer AsWCharBuf() const;
+ const wxScopedCharBuffer AsCharBuf() const
+ {
+ return wxScopedCharBuffer::CreateNonOwned(AsChar());
+ }
+
+ const wxScopedWCharBuffer AsWCharBuf() const
+ {
+ return wxScopedWCharBuffer::CreateNonOwned(AsWChar());
+ }
inline wxString AsString() 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);
}
#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
iterator operator-(ptrdiff_t n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
+ // Normal iterators need to be comparable with the const_iterators so
+ // declare the comparison operators and implement them below after the
+ // full const_iterator declaration.
+ bool operator==(const const_iterator& i) const;
+ bool operator!=(const const_iterator& i) const;
+ bool operator<(const const_iterator& i) const;
+ bool operator>(const const_iterator& i) const;
+ bool operator<=(const const_iterator& i) const;
+ bool operator>=(const const_iterator& i) const;
+
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<wxString*>(m_node.m_str); }
const_iterator operator-(ptrdiff_t n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
+ // Notice that comparison operators taking non-const iterator are not
+ // needed here because of the implicit conversion from non-const iterator
+ // to const ones ensure that the versions for const_iterator declared
+ // inside WX_STR_ITERATOR_IMPL can be used.
+
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; }
iterator operator-(ptrdiff_t n) const
{ return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
+ // As in UTF-8 case above, define comparison operators taking
+ // const_iterator too.
+ bool operator==(const const_iterator& i) const;
+ bool operator!=(const const_iterator& i) const;
+ bool operator<(const const_iterator& i) const;
+ bool operator>(const const_iterator& i) const;
+ bool operator<=(const const_iterator& i) const;
+ bool operator>=(const const_iterator& i) const;
+
private:
// for internal wxString use only:
iterator(underlying_iterator ptr) : m_cur(ptr) {}
const_iterator operator-(ptrdiff_t n) const
{ return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
+ // As in UTF-8 case above, we don't need comparison operators taking
+ // iterator because we have an implicit conversion from iterator to
+ // const_iterator so the operators declared by WX_STR_ITERATOR_IMPL will
+ // be used.
+
private:
// for internal wxString use only:
const_iterator(underlying_iterator ptr) : m_cur(ptr) {}
{ assign(pwz, nLength); }
wxString(const wxScopedCharBuffer& buf)
- { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
+ { assign(buf.data(), buf.length()); }
wxString(const wxScopedWCharBuffer& buf)
- { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
+ { 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
}
#endif // wxUSE_STRING_POS_CACHE
- // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
- // implicit conversions from std::string to wxString and vice verse 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
+ // even if we're not built with wxUSE_STD_STRING_CONV_IN_WXSTRING == 1 it is
+ // very convenient to allow implicit conversions from std::string to wxString
+ // and vice verse 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
#if wxUSE_UNICODE_WCHAR
wxString(const wxStdWideString& str) : m_impl(str) {}
#endif
#endif // wxUSE_STD_STRING
- // Unlike ctor from std::string, we provide conversion to std::string only
- // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
- // because it conflicts with operator const char/wchar_t*:
-#if wxUSE_STL
+ // Also always provide explicit conversions to std::[w]string in any case,
+ // see below for the implicit ones.
+#if wxUSE_STD_STRING
+ // We can avoid a copy if we already use this string type internally,
+ // otherwise we create a copy on the fly:
#if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
- // wxStringImpl is std::string in the encoding we want
- operator const wxStdWideString&() const { return m_impl; }
+ #define wxStringToStdWstringRetType const wxStdWideString&
+ const wxStdWideString& ToStdWstring() const { return m_impl; }
#else
// wxStringImpl is either not std::string or needs conversion
- operator wxStdWideString() const
- // FIXME-UTF8: broken for embedded NULs
- { return wxStdWideString(wc_str()); }
+ #define wxStringToStdWstringRetType wxStdWideString
+ wxStdWideString ToStdWstring() const
+ {
+#if wxUSE_UNICODE_WCHAR
+ wxScopedWCharBuffer buf =
+ wxScopedWCharBuffer::CreateNonOwned(m_impl.c_str(), m_impl.length());
+#else // !wxUSE_UNICODE_WCHAR
+ wxScopedWCharBuffer buf(wc_str());
+#endif
+
+ return wxStdWideString(buf.data(), buf.length());
+ }
#endif
#if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
// wxStringImpl is std::string in the encoding we want
- operator const std::string&() const { return m_impl; }
+ #define wxStringToStdStringRetType const std::string&
+ const std::string& ToStdString() const { return m_impl; }
#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()); }
+ #define wxStringToStdStringRetType std::string
+ std::string ToStdString() const
+ {
+ wxScopedCharBuffer buf(mb_str());
+ return std::string(buf.data(), buf.length());
+ }
#endif
-#endif // wxUSE_STL
+
+#if wxUSE_STD_STRING_CONV_IN_WXSTRING
+ // Implicit conversions to std::[w]string are not provided by default as
+ // they conflict with the implicit conversions to "const char/wchar_t *"
+ // which we use for backwards compatibility but do provide them if
+ // explicitly requested.
+ operator wxStringToStdStringRetType() const { return ToStdString(); }
+ operator wxStringToStdWstringRetType() const { return ToStdWstring(); }
+#endif // wxUSE_STD_STRING_CONV_IN_WXSTRING
+
+#undef wxStringToStdStringRetType
+#undef wxStringToStdWstringRetType
+
+#endif // wxUSE_STD_STRING
wxString Clone() const
{
// 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();
}
operator wxCStrData() const { return c_str(); }
// the first two operators conflict with operators for conversion to
- // std::string and they must be disabled in STL build; the next one only
- // makes sense if conversions to char* are also defined and not defining it
- // in STL build also helps us to get more clear error messages for the code
- // which relies on implicit conversion to char* in STL build
-#if !wxUSE_STL
+ // std::string and they must be disabled if those conversions are enabled;
+ // the next one only makes sense if conversions to char* are also defined
+ // and not defining it in STL build also helps us to get more clear error
+ // messages for the code which relies on implicit conversion to char* in
+ // STL build
+#if !wxUSE_STD_STRING_CONV_IN_WXSTRING
operator const char*() const { return c_str(); }
operator const wchar_t*() const { return c_str(); }
// wxWidgets versions: this is the same as conversion to const char * so it
// may fail!
operator const void*() const { return c_str(); }
-#endif // wxUSE_STL
+#endif // !wxUSE_STD_STRING_CONV_IN_WXSTRING
// identical to c_str(), for MFC compatibility
const wxCStrData GetData() const { return c_str(); }
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
return s;
}
const wxScopedCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
- const wxScopedCharBuffer ToUTF8() const { return utf8_str(); }
#else // ANSI
static wxString FromUTF8(const char *utf8)
{ return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
}
const wxScopedCharBuffer utf8_str() const
{ return wxMBConvUTF8().cWC2MB(wc_str()); }
- const wxScopedCharBuffer 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); }
- const char *To8BitData() const { return c_str(); }
+ const wxScopedCharBuffer To8BitData() const
+ { return wxScopedCharBuffer::CreateNonOwned(wx_str(), length()); }
#endif // Unicode/ANSI
// conversions with (possible) format conversions: have to return a
// 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 wxScopedCharBuffer mb_str(const wxMBConv& conv) const;
-#else
- const wxScopedCharBuffer 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 wxScopedWCharBuffer 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
#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 wxScopedWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
-#endif // wxUSE_WCHAR_T
+ 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
// from wxScopedWCharBuffer
wxString& operator=(const wxScopedWCharBuffer& s)
- { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
+ { return assign(s); }
// from wxScopedCharBuffer
wxString& operator=(const wxScopedCharBuffer& s)
- { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
+ { 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);
// string += buffer (i.e. from wxGetString)
wxString& operator<<(const wxScopedWCharBuffer& s)
- { return operator<<((const wchar_t *)s); }
+ { return append(s); }
wxString& operator<<(const wxScopedCharBuffer& s)
- { return operator<<((const char *)s); }
+ { return append(s); }
// string += C string
wxString& Append(const wxString& s)
// 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); }
-#if defined wxLongLong_t && !defined wxLongLongIsLong
+ { return (*this) << Format(wxT("%lu"), ul); }
+#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
// 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
+#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
// 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)
// get last nCount characters
wxString Right(size_t nCount) const;
// get all characters before the first occurrence of ch
- // (returns the whole string if ch not found)
- wxString BeforeFirst(wxUniChar ch) const;
+ // (returns the whole string if ch not found) and also put everything
+ // following the first occurrence of ch into rest if it's non-NULL
+ wxString BeforeFirst(wxUniChar ch, wxString *rest = NULL) const;
// get all characters before the last occurrence of ch
- // (returns empty string if ch not found)
- wxString BeforeLast(wxUniChar ch) const;
+ // (returns empty string if ch not found) and also put everything
+ // following the last occurrence of ch into rest if it's non-NULL
+ wxString BeforeLast(wxUniChar ch, wxString *rest = NULL) const;
// get all characters after the first occurrence of ch
// (returns empty string if ch not found)
wxString AfterFirst(wxUniChar ch) 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;
bool ToCULong(unsigned long *val, int base = 10) const;
// convert to a double
bool ToCDouble(double *val) const;
-#endif
-
+
+ // create a string representing the given floating point number with the
+ // default (like %g) or fixed (if precision >=0) precision
+ // in the current locale
+ static wxString FromDouble(double val, int precision = -1);
+ // in C locale
+ static wxString FromCDouble(double val, int precision = -1);
+
#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
// formatted input/output
// as sprintf(), returns the number of characters written or < 0 on error
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 wxScopedCharBuffer& str)
- { return append(str.data()); }
+ { return append(str.data(), str.length()); }
wxString& append(const wxScopedWCharBuffer& str)
- { return append(str.data()); }
+ { return append(str.data(), str.length()); }
wxString& append(const wxCStrData& str, size_t n)
{ return append(str.AsString(), 0, n); }
wxString& append(const wxScopedCharBuffer& str, size_t n)
wxString& assign(const wxCStrData& str)
{ return assign(str.AsString()); }
wxString& assign(const wxScopedCharBuffer& str)
- { return assign(str.data()); }
+ { return assign(str.data(), str.length()); }
wxString& assign(const wxScopedWCharBuffer& str)
- { return assign(str.data()); }
+ { return assign(str.data(), str.length()); }
wxString& assign(const wxCStrData& str, size_t len)
{ return assign(str.AsString(), len); }
wxString& assign(const wxScopedCharBuffer& str, size_t len)
return *this;
}
wxString& operator+=(const wxScopedCharBuffer& s)
- { return operator+=(s.data()); }
+ { return append(s); }
wxString& operator+=(const wxScopedWCharBuffer& s)
- { return operator+=(s.data()); }
+ { 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); }
- 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<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
};
#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
- #pragma warning (default:4275)
+ #pragma warning (pop)
#endif
// string iterator operators that satisfy STL Random Access Iterator
inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
+
+// wxString iterators comparisons
+inline bool wxString::iterator::operator==(const const_iterator& i) const
+ { return i == *this; }
+inline bool wxString::iterator::operator!=(const const_iterator& i) const
+ { return i != *this; }
+inline bool wxString::iterator::operator<(const const_iterator& i) const
+ { return i > *this; }
+inline bool wxString::iterator::operator>(const const_iterator& i) const
+ { return i < *this; }
+inline bool wxString::iterator::operator<=(const const_iterator& i) const
+ { return i >= *this; }
+inline bool wxString::iterator::operator>=(const const_iterator& i) const
+ { return i <= *this; }
+
// comparison with C string in Unicode build
#if wxUSE_UNICODE
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 wxScopedCharBuffer wxCStrData::AsCharBuf() const
-{
-#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
- return wxScopedCharBuffer::CreateNonOwned(AsChar());
-#else
- return AsString().mb_str();
-#endif
-}
-
-inline const wxScopedWCharBuffer wxCStrData::AsWCharBuf() const
+inline const char* wxCStrData::AsChar() const
{
-#if wxUSE_UNICODE_WCHAR
- return wxScopedWCharBuffer::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];
}