#include "wx/stringops.h"
#include "wx/unichar.h"
-class WXDLLIMPEXP_BASE wxString;
+class WXDLLIMPEXP_FWD_BASE wxString;
// unless this symbol is predefined to disable the compatibility functions, do
// use them
size_t m_offset;
bool m_owned;
- friend class WXDLLIMPEXP_BASE wxString;
+ friend class WXDLLIMPEXP_FWD_BASE wxString;
};
// ----------------------------------------------------------------------------
// We have to implement the version without template arguments manually
// because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
// normally does it itself. It has to be a template so that we can use
- // the hack, even though there's no real template parameter:
- struct FormatDummyArg {} ;
-
+ // the hack, even though there's no real template parameter. We can't move
+ // it to wxStrig, because it would shadow these versions of Format() then.
template<typename T>
inline static typename StringReturnType<T>::type
- Format(const wxFormatString& fmt, FormatDummyArg dummy = FormatDummyArg())
+ Format(const T& fmt)
{
- return DoFormatWchar(fmt);
+ // NB: this doesn't compile if T is not (some form of) a string;
+ // this makes Format's prototype equivalent to
+ // Format(const wxFormatString& fmt)
+ return DoFormatWchar(wxFormatString(fmt));
}
// int Printf(const wxString& format, ...);
// see the comment near wxString::iterator for why we need this
struct WXDLLIMPEXP_BASE wxStringIteratorNode
{
- inline wxStringIteratorNode(const wxString *str,
- wxStringImpl::const_iterator *citer);
- inline wxStringIteratorNode(const wxString *str,
- wxStringImpl::iterator *iter);
- inline ~wxStringIteratorNode();
+ wxStringIteratorNode()
+ : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {}
+ wxStringIteratorNode(const wxString *str,
+ wxStringImpl::const_iterator *citer)
+ { DoSet(str, citer, NULL); }
+ wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter)
+ { DoSet(str, NULL, iter); }
+ ~wxStringIteratorNode()
+ { clear(); }
+
+ inline void set(const wxString *str, wxStringImpl::const_iterator *citer)
+ { clear(); DoSet(str, citer, NULL); }
+ inline void set(const wxString *str, wxStringImpl::iterator *iter)
+ { clear(); DoSet(str, NULL, iter); }
const wxString *m_str;
wxStringImpl::const_iterator *m_citer;
wxStringImpl::iterator *m_iter;
wxStringIteratorNode *m_prev, *m_next;
+
+private:
+ inline void clear();
+ inline void DoSet(const wxString *str,
+ wxStringImpl::const_iterator *citer,
+ wxStringImpl::iterator *iter);
+
+ // 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)
};
#endif // wxUSE_UNICODE_UTF8
/* for internal wxString use only: */ \
underlying_iterator impl() const { return m_cur; } \
\
- friend class WXDLLIMPEXP_BASE wxString; \
- friend class WXDLLIMPEXP_BASE wxCStrData; \
+ friend class wxString; \
+ friend class wxCStrData; \
\
private: \
underlying_iterator m_cur
- class WXDLLIMPEXP_BASE const_iterator;
+ class WXDLLIMPEXP_FWD_BASE const_iterator;
#if wxUSE_UNICODE_UTF8
// NB: In UTF-8 build, (non-const) iterator needs to keep reference
WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
public:
+ iterator() {}
iterator(const iterator& i)
: m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
+ iterator& operator=(const iterator& i)
+ { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
reference operator*()
{ return wxUniCharRef::CreateForString(m_node, m_cur); }
WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
public:
+ const_iterator() {}
const_iterator(const const_iterator& i)
: m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
const_iterator(const iterator& i)
: m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
+ const_iterator& operator=(const const_iterator& i)
+ { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
+ const_iterator& operator=(const iterator& i)
+ { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
+
reference operator*() const
{ return wxStringOperations::DecodeChar(m_cur); }
WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
public:
+ iterator() {}
iterator(const iterator& i) : m_cur(i.m_cur) {}
reference operator*()
WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
public:
+ const_iterator() {}
const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
const_iterator(const iterator& i) : m_cur(i.m_cur) {}
typedef typename T::reference reference;
typedef typename T::pointer *pointer;
+ reverse_iterator_impl() {}
reverse_iterator_impl(iterator_type i) : m_cur(i) {}
reverse_iterator_impl(const reverse_iterator_impl& ri)
: m_cur(ri.m_cur) {}
wxString(const std::string& str)
{ assign(str.c_str(), str.length()); }
#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
#if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
// wxStringImpl is std::string in the encoding we want
operator const wxStdWideString&() const { return m_impl; }
void resize(size_t nSize, wxUniChar ch = wxT('\0'))
{
+ const size_t len = length();
+ if ( nSize == len)
+ return;
+
#if wxUSE_UNICODE_UTF8
- if ( !ch.IsAscii() )
+ if ( nSize < len )
{
- size_t len = length();
- if ( nSize == len)
- return;
- else if ( nSize < len )
- erase(nSize);
- else
- append(nSize - len, ch);
+ // we can't use wxStringImpl::resize() for truncating the string as it
+ // counts in bytes, not characters
+ erase(nSize);
+ return;
}
+
+ // we also can't use (presumably more efficient) resize() if we have to
+ // append characters taking more than one byte
+ if ( !ch.IsAscii() )
+ append(nSize - len, ch);
else
-#endif
+#endif // wxUSE_UNICODE_UTF8
m_impl.resize(nSize, (wxStringCharType)ch);
}
// implicit conversion to C string
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
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
// identical to c_str(), for MFC compatibility
const wxCStrData GetData() const { return c_str(); }
// the behaviour of these functions with the strings containing anything
// else than 7 bit ASCII characters is undefined, use at your own risk.
#if wxUSE_UNICODE
- static wxString FromAscii(const char *ascii); // string
- static wxString FromAscii(const char ascii); // char
+ static wxString FromAscii(const char *ascii, size_t len);
+ static wxString FromAscii(const char *ascii);
+ static wxString FromAscii(char ascii);
const wxCharBuffer ToAscii() const;
#else // ANSI
static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
- static wxString FromAscii(const char ascii) { return wxString( ascii ); }
+ static wxString FromAscii(const char *ascii, size_t len)
+ { return wxString( ascii, len ); }
+ static wxString FromAscii(char ascii) { return wxString( ascii ); }
const char *ToAscii() const { return c_str(); }
#endif // Unicode/!Unicode
+ // also provide unsigned char overloads as signed/unsigned doesn't matter
+ // for 7 bit ASCII characters
+ static wxString FromAscii(const unsigned char *ascii)
+ { return FromAscii((const char *)ascii); }
+ static wxString FromAscii(const unsigned char *ascii, size_t len)
+ { return FromAscii((const char *)ascii, len); }
+
// conversion to/from UTF-8:
#if wxUSE_UNICODE_UTF8
static wxString FromUTF8(const char *utf8)
{
+ if ( !utf8 )
+ return wxEmptyString;
+
wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
return FromImpl(wxStringImpl(utf8));
}
static wxString FromUTF8(const char *utf8, size_t len)
{
+ if ( !utf8 )
+ return wxEmptyString;
+ if ( len == npos )
+ return FromUTF8(utf8);
+
wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
return FromImpl(wxStringImpl(utf8, len));
}
{
wxStringIteratorNodeHead() : ptr(NULL) {}
wxStringIteratorNode *ptr;
+
+ // copying is disallowed as it would result in more than one pointer into
+ // the same linked list
+ DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead)
};
wxStringIteratorNodeHead m_iterators;
- friend class WXDLLIMPEXP_BASE wxStringIteratorNode;
- friend class WXDLLIMPEXP_BASE wxUniCharRef;
+ friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
+ friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
#endif // wxUSE_UNICODE_UTF8
- friend class WXDLLIMPEXP_BASE wxCStrData;
- friend class wxImplStringBuffer;
- friend class wxImplStringBufferLength;
+ friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
+ friend class wxStringInternalBuffer;
+ friend class wxStringInternalBufferLength;
};
#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
{ return wxUniChar(ch) + string; }
-#if wxUSE_STL_BASED_WXSTRING
- // return an empty wxString (not very useful with wxUSE_STL == 1)
- inline const wxString wxGetEmptyString() { return wxString(); }
-#else // !wxUSE_STL_BASED_WXSTRING
- // return an empty wxString (more efficient than wxString() here)
- inline const wxString& wxGetEmptyString()
- {
- return *(wxString *)&wxEmptyString;
- }
-#endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
+#define wxGetEmptyString() wxString()
// ----------------------------------------------------------------------------
// wxStringBuffer: a tiny class allowing to get a writable pointer into string
#if !wxUSE_STL_BASED_WXSTRING
// string buffer for direct access to string data in their native
// representation:
-class wxImplStringBuffer
+class wxStringInternalBuffer
{
public:
typedef wxStringCharType CharType;
- wxImplStringBuffer(wxString& str, size_t lenWanted = 1024)
+ wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
: m_str(str), m_buf(NULL)
{ m_buf = m_str.DoGetWriteBuf(lenWanted); }
- ~wxImplStringBuffer() { m_str.DoUngetWriteBuf(); }
+ ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
operator wxStringCharType*() const { return m_buf; }
wxString& m_str;
wxStringCharType *m_buf;
- DECLARE_NO_COPY_CLASS(wxImplStringBuffer)
+ DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
};
-class wxImplStringBufferLength
+class wxStringInternalBufferLength
{
public:
typedef wxStringCharType CharType;
- wxImplStringBufferLength(wxString& str, size_t lenWanted = 1024)
+ wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
: m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
{
m_buf = m_str.DoGetWriteBuf(lenWanted);
wxASSERT(m_buf != NULL);
}
- ~wxImplStringBufferLength()
+ ~wxStringInternalBufferLength()
{
wxASSERT(m_lenSet);
m_str.DoUngetWriteBuf(m_len);
size_t m_len;
bool m_lenSet;
- DECLARE_NO_COPY_CLASS(wxImplStringBufferLength)
+ DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
};
#endif // !wxUSE_STL_BASED_WXSTRING
template<typename T>
-class wxStringTypeBufferBase
+class WXDLLIMPEXP_BASE wxStringTypeBufferBase
{
public:
typedef T CharType;
};
template<typename T>
-class wxStringTypeBufferLengthBase
+class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
{
public:
typedef T CharType;
: m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
{ }
- ~wxStringTypeBufferLengthBase()
- {
- wxASSERT(m_lenSet);
- m_str.assign(m_buf.data(), m_len);
- }
-
operator CharType*() { return m_buf.data(); }
void SetLength(size_t length) { m_len = length; m_lenSet = true; }
};
#if wxUSE_STL_BASED_WXSTRING
-class wxImplStringBuffer : public wxStringTypeBufferBase<wxStringCharType>
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
+
+class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
{
public:
- wxImplStringBuffer(wxString& str, size_t lenWanted = 1024)
+ wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
: wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
- ~wxImplStringBuffer()
+ ~wxStringInternalBuffer()
{ m_str.m_impl.assign(m_buf.data()); }
- DECLARE_NO_COPY_CLASS(wxImplStringBuffer)
+ DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
};
-class wxImplStringBufferLength : public wxStringTypeBufferLengthBase<wxStringCharType>
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
+ wxStringTypeBufferLengthBase<wxStringCharType> )
+
+class wxStringInternalBufferLength
+ : public wxStringTypeBufferLengthBase<wxStringCharType>
{
public:
- wxImplStringBufferLength(wxString& str, size_t lenWanted = 1024)
+ wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
: wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
- ~wxImplStringBufferLength()
+ ~wxStringInternalBufferLength()
{
wxASSERT(m_lenSet);
m_str.m_impl.assign(m_buf.data(), m_len);
}
- DECLARE_NO_COPY_CLASS(wxImplStringBufferLength)
+ DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
};
#endif // wxUSE_STL_BASED_WXSTRING
typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
#else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
-typedef wxImplStringBuffer wxStringBuffer;
-typedef wxImplStringBufferLength wxStringBufferLength;
+typedef wxStringInternalBuffer wxStringBuffer;
+typedef wxStringInternalBufferLength wxStringBufferLength;
#endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
+#if wxUSE_UNICODE_UTF8
+typedef wxStringInternalBuffer wxUTF8StringBuffer;
+typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
+#elif wxUSE_UNICODE_WCHAR
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
+
+class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
+{
+public:
+ wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferBase<char>(str, lenWanted) {}
+ ~wxUTF8StringBuffer();
+
+ DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer)
+};
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
+
+class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
+ : public wxStringTypeBufferLengthBase<char>
+{
+public:
+ wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
+ ~wxUTF8StringBufferLength();
+
+ DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength)
+};
+#endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
+
+
// ---------------------------------------------------------------------------
// wxString comparison functions: operator versions are always case sensitive
// ---------------------------------------------------------------------------
WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
#endif
-#endif // wxSTD_STRING_COMPATIBILITY
+#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&);
+
+#endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
+
+#endif // wxUSE_STD_IOSTREAM
// ---------------------------------------------------------------------------
// wxCStrData implementation
inline wxCStrData::~wxCStrData()
{
if ( m_owned )
- delete m_str;
+ delete wx_const_cast(wxString*, m_str); // cast to silence warnings
}
// simple cases for AsChar() and AsWChar(), the complicated ones are
// implementation of wxStringIteratorNode inline methods
// ----------------------------------------------------------------------------
-wxStringIteratorNode::wxStringIteratorNode(const wxString *str,
- wxStringImpl::const_iterator *citer)
- : m_str(str),
- m_citer(citer),
- m_iter(NULL),
- m_prev(NULL),
- m_next(str->m_iterators.ptr)
-{
- wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
- if ( m_next )
- m_next->m_prev = this;
-}
-
-wxStringIteratorNode::wxStringIteratorNode(const wxString *str,
- wxStringImpl::iterator *iter)
- : m_str(str),
- m_citer(NULL),
- m_iter(iter),
- m_prev(NULL),
- m_next(str->m_iterators.ptr)
+void wxStringIteratorNode::DoSet(const wxString *str,
+ wxStringImpl::const_iterator *citer,
+ wxStringImpl::iterator *iter)
{
- wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
- if ( m_next)
- m_next->m_prev = this;
+ m_prev = NULL;
+ m_iter = iter;
+ m_citer = citer;
+ m_str = str;
+ if ( str )
+ {
+ m_next = str->m_iterators.ptr;
+ wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
+ if ( m_next )
+ m_next->m_prev = this;
+ }
+ else
+ {
+ m_next = NULL;
+ }
}
-wxStringIteratorNode::~wxStringIteratorNode()
+void wxStringIteratorNode::clear()
{
if ( m_next )
m_next->m_prev = m_prev;
if ( m_prev )
m_prev->m_next = m_next;
- else // first in the list
+ else if ( m_str ) // first in the list
wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next;
+
+ m_next = m_prev = NULL;
+ m_citer = NULL;
+ m_iter = NULL;
+ m_str = NULL;
}
#endif // wxUSE_UNICODE_UTF8