#define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
#endif
+namespace wxPrivate
+{
+ template <typename T> struct wxStringAsBufHelper;
+}
+
// ---------------------------------------------------------------------------
// macros
// ---------------------------------------------------------------------------
{ return mb_str(conv); }
wxWritableWCharBuffer wchar_str() const { return wc_str(); }
+ // conversion to the buffer of the given type T (= char or wchar_t) and
+ // also optionally return the buffer length
+ //
+ // this is mostly/only useful for the template functions
+ //
+ // FIXME-VC6: the second argument only exists for VC6 which doesn't support
+ // explicit template function selection, do not use it unless
+ // you must support VC6!
+ template <typename T>
+ wxCharTypeBuffer<T> tchar_str(size_t *len = NULL,
+ T * WXUNUSED(dummy) = NULL) const
+ {
+#if wxUSE_UNICODE
+ // we need a helper dispatcher depending on type
+ return wxPrivate::wxStringAsBufHelper<T>::Get(*this, len);
+#else // ANSI
+ // T can only be char in ANSI build
+ if ( len )
+ *len = length();
+
+ return wxCharTypeBuffer<T>::CreateNonOwned(wx_str());
+#endif // Unicode build kind
+ }
+
// conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
// converting numbers or strings which are certain not to contain special
// chars (typically system functions, X atoms, environment variables etc.)
// conversion to/from UTF-8:
#if wxUSE_UNICODE_UTF8
- static wxString FromUTF8(const char *utf8)
+ static wxString FromUTF8Unchecked(const char *utf8)
{
if ( !utf8 )
return wxEmptyString;
wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
return FromImpl(wxStringImpl(utf8));
}
- static wxString FromUTF8(const char *utf8, size_t len)
+ static wxString FromUTF8Unchecked(const char *utf8, size_t len)
{
if ( !utf8 )
return wxEmptyString;
if ( len == npos )
- return FromUTF8(utf8);
+ return FromUTF8Unchecked(utf8);
wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
return FromImpl(wxStringImpl(utf8, len));
}
+
+ static wxString FromUTF8(const char *utf8)
+ {
+ if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8) )
+ return "";
+
+ return FromImpl(wxStringImpl(utf8));
+ }
+ static wxString FromUTF8(const char *utf8, size_t len)
+ {
+ if ( len == npos )
+ return FromUTF8(utf8);
+
+ if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8, len) )
+ return "";
+
+ return FromImpl(wxStringImpl(utf8, len));
+ }
+
const char* utf8_str() const { return wx_str(); }
const char* ToUTF8() const { return wx_str(); }
+
+ // this function exists in UTF-8 build only and returns the length of the
+ // internal UTF-8 representation
+ size_t utf8_length() const { return m_impl.length(); }
#elif wxUSE_UNICODE_WCHAR
- static wxString FromUTF8(const char *utf8)
- { return wxString(utf8, wxMBConvUTF8()); }
- static wxString FromUTF8(const char *utf8, size_t len)
+ static wxString FromUTF8(const char *utf8, size_t len = npos)
{ return wxString(utf8, wxMBConvUTF8(), len); }
+ static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
+ {
+ const wxString s(utf8, wxMBConvUTF8(), len);
+ wxASSERT_MSG( !utf8 || !*utf8 || !s.empty(),
+ "string must be valid UTF-8" );
+ return s;
+ }
const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
const wxCharBuffer ToUTF8() const { return utf8_str(); }
#else // ANSI
{ 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));
- return wxString(buf.data(), wlen);
+ size_t wlen;
+ wxWCharBuffer 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));
+ wxASSERT_MSG( !utf8 || !*utf8 || wlen,
+ "string must be valid UTF-8" );
+
+ return wxString(buf.data(), wlen);
}
const wxCharBuffer utf8_str() const
{ return wxMBConvUTF8().cWC2MB(wc_str()); }
const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
#if wxUSE_UNICODE_WCHAR
- const wxChar* wc_str() const { return wx_str(); }
+ const wchar_t* wc_str() const { return wx_str(); }
#elif wxUSE_UNICODE_UTF8
const wxWCharBuffer wc_str() const;
#endif
const wxChar* mb_str() const { return wx_str(); }
// for compatibility with wxUSE_UNICODE version
- const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
+ const char* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
const wxWX2MBbuf mbc_str() const { return mb_str(); }
const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); }
#endif // Unicode/ANSI
+#if wxUSE_UNICODE_UTF8
+ const wxWCharBuffer 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
+
+
// overloaded assignment
// from another wxString
wxString& operator=(const wxString& stringSrc)
#define wxGetEmptyString() wxString()
+// ----------------------------------------------------------------------------
+// helper functions which couldn't be defined inline
+// ----------------------------------------------------------------------------
+
+namespace wxPrivate
+{
+
+#if wxUSE_UNICODE_WCHAR
+
+template <>
+struct wxStringAsBufHelper<char>
+{
+ static wxCharBuffer Get(const wxString& s, size_t *len)
+ {
+ wxCharBuffer 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)
+ {
+ if ( len )
+ *len = s.length();
+ return wxWCharBuffer::CreateNonOwned(s.wx_str());
+ }
+};
+
+#elif wxUSE_UNICODE_UTF8
+
+template <>
+struct wxStringAsBufHelper<char>
+{
+ static wxCharBuffer Get(const wxString& s, size_t *len)
+ {
+ if ( len )
+ *len = s.utf8_length();
+ return wxCharBuffer::CreateNonOwned(s.wx_str());
+ }
+};
+
+template <>
+struct wxStringAsBufHelper<wchar_t>
+{
+ static wxWCharBuffer Get(const wxString& s, size_t *len)
+ {
+ wxWCharBuffer wbuf(s.wc_str());
+ if ( len )
+ *len = wxWcslen(wbuf);
+ return wbuf;
+ }
+};
+
+#endif // Unicode build kind
+
+} // namespace wxPrivate
+
// ----------------------------------------------------------------------------
// wxStringBuffer: a tiny class allowing to get a writable pointer into string
// ----------------------------------------------------------------------------
wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
: m_str(str), m_buf(lenWanted)
- { }
-
+ {
+ // for compatibility with old wxStringBuffer which provided direct
+ // access to wxString internal buffer, initialize ourselves with the
+ // string initial contents
+
+ // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
+ // tchar_str<CharType>
+ size_t len;
+ const wxCharTypeBuffer<CharType> buf(str.tchar_str(&len, (CharType *)NULL));
+ if ( buf )
+ {
+ if ( len > lenWanted )
+ {
+ // in this case there is not enough space for terminating NUL,
+ // ensure that we still put it there
+ m_buf.data()[lenWanted] = 0;
+ len = lenWanted - 1;
+ }
+
+ memcpy(m_buf.data(), buf, (len + 1)*sizeof(CharType));
+ }
+ //else: conversion failed, this can happen when trying to get Unicode
+ // string contents into a char string
+ }
operator CharType*() { return m_buf.data(); }
template<typename T>
class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
+ : public wxStringTypeBufferBase<T>
{
public:
- typedef T CharType;
-
wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
- : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
+ : wxStringTypeBufferBase<T>(str, lenWanted),
+ m_len(0),
+ m_lenSet(false)
{ }
- operator CharType*() { return m_buf.data(); }
+ ~wxStringTypeBufferLengthBase()
+ {
+ wxASSERT_MSG( this->m_lenSet, "forgot to call SetLength()" );
+ }
+
void SetLength(size_t length) { m_len = length; m_lenSet = true; }
protected:
- wxString& m_str;
- wxCharTypeBuffer<CharType> m_buf;
- size_t m_len;
- bool m_lenSet;
+ size_t m_len;
+ bool m_lenSet;
};
template<typename T>
{
public:
wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
- : wxStringTypeBufferBase<T>(str, lenWanted) {}
+ : wxStringTypeBufferBase<T>(str, lenWanted)
+ { }
+
~wxStringTypeBuffer()
{
this->m_str.assign(this->m_buf.data());
{
public:
wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
- : wxStringTypeBufferLengthBase<T>(str, lenWanted) {}
+ : wxStringTypeBufferLengthBase<T>(str, lenWanted)
+ { }
~wxStringTypeBufferLength()
{
- wxASSERT(this->m_lenSet);
this->m_str.assign(this->m_buf.data(), this->m_len);
}
~wxStringInternalBufferLength()
{
- wxASSERT(m_lenSet);
m_str.m_impl.assign(m_buf.data(), m_len);
}
DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
};
+
#endif // wxUSE_STL_BASED_WXSTRING