#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
-
-#ifdef __WXPALMOS__
- #include <StringMgr.h>
-#endif
+#endif // ! __WXPALMOS5__
#include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
#include "wx/strvararg.h"
#define wxMBSTRINGCAST (char *)(const char *)
#define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
-// like _T(), but for literals in wxString's internal representation, i.e.
-// char* in UTF-8 build and wxChar* otherwise:
-#if wxUSE_UNICODE_UTF8
- #define wxSTRING_TEXT(str) str
-#else
- #define wxSTRING_TEXT(str) _T(str)
-#endif
-
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
return _stricmp(psz1, psz2);
#elif defined(__SC__)
return _stricmp(psz1, psz2);
-#elif defined(__SALFORDC__)
- return stricmp(psz1, psz2);
#elif defined(__BORLANDC__)
return stricmp(psz1, psz2);
#elif defined(__WATCOMC__)
wxCStrData operator+(size_t n) const
{ return wxCStrData(m_str, m_offset + n, m_owned); }
- // and these for "str.c_str() + n - 2":
- wxCStrData operator-(int n) const
+ // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
+ // expression but it must be ptrdiff_t and not e.g. int to work in this
+ // example):
+ wxCStrData operator-(ptrdiff_t n) const
{
- wxASSERT_MSG( n <= (int)m_offset,
- _T("attempt to construct address before the beginning of the string") );
- return wxCStrData(m_str, m_offset - n, m_owned);
- }
- wxCStrData operator-(long n) const
- {
- wxASSERT_MSG( n <= (int)m_offset,
- _T("attempt to construct address before the beginning of the string") );
- return wxCStrData(m_str, m_offset - n, m_owned);
- }
- wxCStrData operator-(size_t n) const
- {
- wxASSERT_MSG( n <= m_offset,
+ wxASSERT_MSG( n <= (ptrdiff_t)m_offset,
_T("attempt to construct address before the beginning of the string") );
return wxCStrData(m_str, m_offset - n, m_owned);
}
#if wxUSE_UNICODE_UTF8
// see the comment near wxString::iterator for why we need this
-struct WXDLLIMPEXP_BASE wxStringIteratorNode
+class WXDLLIMPEXP_BASE wxStringIteratorNode
{
+public:
wxStringIteratorNode()
: m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {}
wxStringIteratorNode(const wxString *str,
return tmp; \
} \
\
- iterator_name& operator+=(int n) \
+ iterator_name& operator+=(ptrdiff_t n) \
{ \
m_cur = wxStringOperations::AddToIter(m_cur, n); \
return *this; \
} \
- iterator_name& operator+=(size_t n) \
- { \
- m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
- return *this; \
- } \
- iterator_name& operator-=(int n) \
+ iterator_name& operator-=(ptrdiff_t n) \
{ \
m_cur = wxStringOperations::AddToIter(m_cur, -n); \
return *this; \
- } \
- iterator_name& operator-=(size_t n) \
- { \
- m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
- return *this; \
} \
\
difference_type operator-(const iterator_name& i) const \
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; }
+ {
+ if (&i != this)
+ {
+ m_cur = i.m_cur;
+ m_node.set(i.str(), &m_cur);
+ }
+ return *this;
+ }
reference operator*()
{ return wxUniCharRef::CreateForString(m_node, m_cur); }
- iterator operator+(int n) const
+ iterator operator+(ptrdiff_t n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
- iterator operator+(size_t n) const
- { return iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); }
- iterator operator-(int n) const
+ iterator operator-(ptrdiff_t n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
- iterator operator-(size_t n) const
- { return iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); }
private:
iterator(wxString *str, underlying_iterator ptr)
: 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; }
+ {
+ if (&i != this)
+ {
+ 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); }
- const_iterator operator+(int n) const
+ const_iterator operator+(ptrdiff_t n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
- const_iterator operator+(size_t n) const
- { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); }
- const_iterator operator-(int n) const
+ const_iterator operator-(ptrdiff_t n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
- const_iterator operator-(size_t n) const
- { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); }
private:
// for internal wxString use only:
reference operator*()
{ return wxUniCharRef::CreateForString(m_cur); }
- iterator operator+(int n) const
+ iterator operator+(ptrdiff_t n) const
{ return iterator(wxStringOperations::AddToIter(m_cur, n)); }
- iterator operator+(size_t n) const
- { return iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
- iterator operator-(int n) const
+ iterator operator-(ptrdiff_t n) const
{ return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
- iterator operator-(size_t n) const
- { return iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
private:
// for internal wxString use only:
reference operator*() const
{ return wxStringOperations::DecodeChar(m_cur); }
- const_iterator operator+(int n) const
+ const_iterator operator+(ptrdiff_t n) const
{ return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
- const_iterator operator+(size_t n) const
- { return const_iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
- const_iterator operator-(int n) const
+ const_iterator operator-(ptrdiff_t n) const
{ return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
- const_iterator operator-(size_t n) const
- { return const_iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
private:
// for internal wxString use only:
{ reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
// NB: explicit <T> in the functions below is to keep BCC 5.5 happy
- reverse_iterator_impl operator+(int n) const
- { return reverse_iterator_impl<T>(m_cur - n); }
- reverse_iterator_impl operator+(size_t n) const
+ reverse_iterator_impl operator+(ptrdiff_t n) const
{ return reverse_iterator_impl<T>(m_cur - n); }
- reverse_iterator_impl operator-(int n) const
+ reverse_iterator_impl operator-(ptrdiff_t n) const
{ return reverse_iterator_impl<T>(m_cur + n); }
- reverse_iterator_impl operator-(size_t n) const
- { return reverse_iterator_impl<T>(m_cur + n); }
- reverse_iterator_impl operator+=(int n)
- { m_cur -= n; return *this; }
- reverse_iterator_impl operator+=(size_t n)
+ reverse_iterator_impl operator+=(ptrdiff_t n)
{ m_cur -= n; return *this; }
- reverse_iterator_impl operator-=(int n)
- { m_cur += n; return *this; }
- reverse_iterator_impl operator-=(size_t n)
+ reverse_iterator_impl operator-=(ptrdiff_t n)
{ m_cur += n; return *this; }
unsigned operator-(const reverse_iterator_impl& i) const
wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
// string containing nRepeat copies of ch
- wxString(wxUniChar ch, size_t nRepeat = 1)
+ wxString(wxUniChar ch, size_t nRepeat = 1 )
{ assign(nRepeat, ch); }
wxString(size_t nRepeat, wxUniChar ch)
{ assign(nRepeat, ch); }
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);
}
#if wxUSE_WCHAR_T
const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
#endif // wxUSE_WCHAR_T
-#ifdef __WXOSX__
- const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
-#else
- const wxChar* fn_str() const { return c_str(); }
-#endif
+ const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); }
#endif // Unicode/ANSI
// overloaded assignment
// from another wxString
wxString& operator=(const wxString& stringSrc)
- { m_impl = stringSrc.m_impl; return *this; }
+ { if (&stringSrc != this) m_impl = stringSrc.m_impl; return *this; }
wxString& operator=(const wxCStrData& cstr)
{ return *this = cstr.AsString(); }
// from a character
wxString& operator=(wxUniChar ch)
- { m_impl = wxStringOperations::EncodeChar(ch); return *this; }
+ {
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ m_impl = wxStringOperations::EncodeChar(ch);
+ else
+#endif
+ m_impl = (wxStringCharType)ch;
+ return *this;
+ }
wxString& operator=(wxUniCharRef ch)
{ return operator=((wxUniChar)ch); }
wxString& operator=(char ch)
{ return compare(s); }
// same as Cmp() but not case-sensitive
int CmpNoCase(const wxString& s) const;
+
// test for the string equality, either considering case or not
// (if compareWithCase then the case matters)
bool IsSameAs(const wxString& str, bool compareWithCase = true) const
- { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
+ {
+#if !wxUSE_UNICODE_UTF8
+ // in UTF-8 build, length() is O(n) and doing this would be _slower_
+ if ( length() != str.length() )
+ return false;
+#endif
+ return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0;
+ }
bool IsSameAs(const char *str, bool compareWithCase = true) const
{ return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
{ return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
+
bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
{ return IsSameAs(str.AsString(), compareWithCase); }
bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
// find the first occurence of character ch after nStart
size_t find(wxUniChar ch, size_t nStart = 0) const
{
- return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
- PosToImpl(nStart)));
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
+ PosToImpl(nStart)));
+ else
+#endif
+ return PosFromImpl(m_impl.find((wxStringCharType)ch,
+ PosToImpl(nStart)));
+
}
size_t find(wxUniCharRef ch, size_t nStart = 0) const
{ return find(wxUniChar(ch), nStart); }
// as find, but from the end
size_t rfind(wxUniChar ch, size_t nStart = npos) const
{
- return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
- PosToImpl(nStart)));
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
+ PosToImpl(nStart)));
+ else
+#endif
+ return PosFromImpl(m_impl.rfind((wxStringCharType)ch,
+ PosToImpl(nStart)));
}
size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
{ return rfind(wxUniChar(ch), nStart); }
{ return operator+=(s.data()); }
// string += char
wxString& operator+=(wxUniChar ch)
- { m_impl += wxStringOperations::EncodeChar(ch); return *this; }
+ {
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ m_impl += wxStringOperations::EncodeChar(ch);
+ else
+#endif
+ m_impl += (wxStringCharType)ch;
+ return *this;
+ }
wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
private:
wxStringImpl m_impl;
-#ifdef __VISUALC__
- // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by
- // clients of class 'wxString'" - this is private, we don't care
- #pragma warning (disable:4251)
-#endif
-
// buffers for compatibility conversion from (char*)c_str() and
// (wchar_t*)c_str():
// FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
ConvertedBuffer<wchar_t> m_convertedToWChar;
#endif
-#ifdef __VISUALC__
- #pragma warning (default:4251)
-#endif
-
#if wxUSE_UNICODE_UTF8
// FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
// assigning to character pointer to by wxString::interator may
// string iterator operators that satisfy STL Random Access Iterator
// requirements:
-inline wxString::iterator operator+(int n, wxString::iterator i)
- { return i + n; }
-inline wxString::iterator operator+(size_t n, wxString::iterator i)
- { return i + n; }
-inline wxString::const_iterator operator+(int n, wxString::const_iterator i)
+inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
{ return i + n; }
-inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i)
+inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
{ return i + n; }
-inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
+inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
{ return i + n; }
-inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
- { return i + n; }
-inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
- { return i + n; }
-inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
+inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
{ return i + n; }
// notice that even though for many compilers the friend declarations above are
#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
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
+
class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
{
public:
DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
};
-class wxStringInternalBufferLength : public wxStringTypeBufferLengthBase<wxStringCharType>
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
+ wxStringTypeBufferLengthBase<wxStringCharType> )
+
+class wxStringInternalBufferLength
+ : public wxStringTypeBufferLengthBase<wxStringCharType>
{
public:
wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
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
// ---------------------------------------------------------------------------
#undef wxCMP_WXCHAR_STRING
-// note that there is an optimization in operator==() and !=(): we (quickly)
-// checks the strings length first, before comparing their data
inline bool operator==(const wxString& s1, const wxString& s2)
- { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
+ { return s1.IsSameAs(s2); }
inline bool operator!=(const wxString& s1, const wxString& s2)
- { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
+ { return !s1.IsSameAs(s2); }
inline bool operator< (const wxString& s1, const wxString& s2)
{ return s1.Cmp(s2) < 0; }
inline bool operator> (const wxString& s1, const wxString& s2)
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