unsigned operator-(const iterator_name& i) const \
{ return wxString::DiffIters(m_cur, i.m_cur); } \
\
- bool operator==(const iterator_name&i) const \
+ bool operator==(const iterator_name& i) const \
{ return m_cur == i.m_cur; } \
bool operator!=(const iterator_name& i) const \
{ return m_cur != i.m_cur; } \
// take nLen chars starting at nPos
wxString(const wxString& str, size_t nPos, size_t nLen)
: m_impl(str.m_impl, nPos, nLen) { }
- // take all characters from pStart to pEnd
- wxString(const void *pStart, const void *pEnd)
- : m_impl((const wxChar*)pStart, (const wxChar*)pEnd) { }
+ // take all characters from first to last
wxString(const_iterator first, const_iterator last)
: m_impl(first, last) { }
- wxString(iterator first, iterator last)
- : m_impl(first, last) { }
+ wxString(const char *first, const char *last)
+ {
+ SubstrBufFromMB str(ImplStr(first, last - first));
+ m_impl.assign(str.data, str.len);
+ }
+ wxString(const wchar_t *first, const wchar_t *last)
+ {
+ SubstrBufFromWC str(ImplStr(first, last - first));
+ m_impl.assign(str.data, str.len);
+ }
// lib.string.modifiers
// append elements str[pos], ..., str[pos+n]
// append from first to last
wxString& append(const_iterator first, const_iterator last)
{ m_impl.append(first, last); return *this; }
+ wxString& append(const char *first, const char *last)
+ { return append(first, last - first); }
+ wxString& append(const wchar_t *first, const wchar_t *last)
+ { return append(first, last - first); }
// same as `this_string = str'
wxString& assign(const wxString& str)
// assign from first to last
wxString& assign(const_iterator first, const_iterator last)
{ m_impl.assign(first, last); return *this; }
+ wxString& assign(const char *first, const char *last)
+ { return assign(first, last - first); }
+ wxString& assign(const wchar_t *first, const wchar_t *last)
+ { return assign(first, last - first); }
// string comparison
int compare(const wxString& str) const;
{ return iterator(m_impl.insert(it, EncodeChar(ch))); }
void insert(iterator it, const_iterator first, const_iterator last)
{ m_impl.insert(it, first, last); }
+ void insert(iterator it, const char *first, const char *last)
+ { insert(it - begin(), first, last - first); }
+ void append(iterator it, const wchar_t *first, const wchar_t *last)
+ { insert(it - begin(), first, last - first); }
void insert(iterator it, size_type n, wxUniChar ch)
{
#if wxUSE_UNICODE_UTF8
m_impl.erase(from, len);
return *this;
}
+ // delete characters from first up to last
iterator erase(iterator first, iterator last)
{ return iterator(m_impl.erase(first, last)); }
iterator erase(iterator first)
wxString& replace(iterator first, iterator last,
const_iterator first1, const_iterator last1)
{ m_impl.replace(first, last, first1, last1); return *this; }
+ wxString& replace(iterator first, iterator last,
+ const char *first1, const char *last1)
+ { replace(first, last, first1, last1 - first1); return *this; }
+ wxString& replace(iterator first, iterator last,
+ const wchar_t *first1, const wchar_t *last1)
+ { replace(first, last, first1, last1 - first1); return *this; }
// swap two strings
void swap(wxString& str)
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
- typedef value_type *iterator;
- typedef const value_type *const_iterator;
+
+ // macro to define the bulk of iterator and const_iterator classes
+ #define WX_DEFINE_STRINGIMPL_ITERATOR(iterator_name, ref_type, ptr_type) \
+ public: \
+ typedef wxStringCharType value_type; \
+ typedef ref_type reference; \
+ typedef ptr_type pointer; \
+ \
+ iterator_name(pointer ptr) : m_ptr(ptr) { } \
+ \
+ reference operator*() const { return *m_ptr; } \
+ \
+ iterator_name& operator++() { m_ptr++; return *this; } \
+ iterator_name operator++(int) \
+ { \
+ const iterator_name tmp(*this); \
+ m_ptr++; \
+ return tmp; \
+ } \
+ \
+ iterator_name& operator--() { m_ptr--; return *this; } \
+ iterator_name operator--(int) \
+ { \
+ const iterator_name tmp(*this); \
+ m_ptr--; \
+ return tmp; \
+ } \
+ \
+ iterator_name operator+(int n) const \
+ { return iterator_name(m_ptr + n); } \
+ iterator_name operator-(int n) const \
+ { return iterator_name(m_ptr - n); } \
+ iterator_name& operator+=(int n) \
+ { m_ptr += n; return *this; } \
+ iterator_name& operator-=(int n) \
+ { m_ptr -= n; return *this; } \
+ \
+ size_t operator-(const iterator_name& i) const \
+ { return m_ptr - i.m_ptr; } \
+ \
+ bool operator==(const iterator_name& i) const \
+ { return m_ptr == i.m_ptr; } \
+ bool operator!=(const iterator_name& i) const \
+ { return m_ptr != i.m_ptr; } \
+ \
+ bool operator<(const iterator_name& i) const \
+ { return m_ptr < i.m_ptr; } \
+ bool operator>(const iterator_name& i) const \
+ { return m_ptr > i.m_ptr; } \
+ bool operator<=(const iterator_name& i) const \
+ { return m_ptr <= i.m_ptr; } \
+ bool operator>=(const iterator_name& i) const \
+ { return m_ptr >= i.m_ptr; } \
+ \
+ private: \
+ /* for wxStringImpl use only */ \
+ operator pointer() const { return m_ptr; } \
+ \
+ friend class WXDLLIMPEXP_BASE wxStringImpl; \
+ \
+ pointer m_ptr
+
+ class iterator
+ {
+ WX_DEFINE_STRINGIMPL_ITERATOR(iterator,
+ wxStringCharType&,
+ wxStringCharType*);
+
+ friend class const_iterator;
+ };
+
+ class const_iterator
+ {
+ public:
+ const_iterator(iterator i) : m_ptr(i.m_ptr) { }
+
+ WX_DEFINE_STRINGIMPL_ITERATOR(const_iterator,
+ const wxStringCharType&,
+ const wxStringCharType*);
+ };
+
// constructors and destructor
// ctor for an empty string
size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
InitWith(str.c_str(), nPos, nLen);
}
- // take all characters from pStart to pEnd
- wxStringImpl(const void *pStart, const void *pEnd);
+ // take everything between start and end
+ wxStringImpl(const_iterator start, const_iterator end);
// dtor is not virtual, this class must not be inherited from!
~wxStringImpl()
// find first n characters of sz
size_t find(const wxStringCharType* sz, size_t nStart = 0, size_t n = npos) const;
- // find the first occurence of character ch after nStart
+ // find the first occurrence of character ch after nStart
size_t find(wxStringCharType ch, size_t nStart = 0) const;
// rfind() family is exactly like find() but works right to left
}
}
-// poor man's iterators are "void *" pointers
-wxStringImpl::wxStringImpl(const void *pStart, const void *pEnd)
+wxStringImpl::wxStringImpl(const_iterator first, const_iterator last)
{
- if ( pEnd >= pStart )
+ if ( last >= first )
{
- InitWith((const wxChar *)pStart, 0,
- (const wxChar *)pEnd - (const wxChar *)pStart);
+ InitWith(first, 0, last - first);
}
else
{
- wxFAIL_MSG( _T("pStart is not before pEnd") );
+ wxFAIL_MSG( _T("first must be before last") );
Init();
}
}
CPPUNIT_ASSERT( s6 == s1 );
CPPUNIT_ASSERT( s7 == s1 );
CPPUNIT_ASSERT( s8 == _T("efgh") );
+
+ const char *pc = s1;
+ WX_ASSERT_STR_EQUAL( "bcd", wxString(pc + 1, pc + 4) );
+
+ const wchar_t *pw = s2.c_str();
+ WX_ASSERT_STR_EQUAL( "a", wxString(pw, pw + 1) );
}
void StdStringTestCase::StdAppend()
{
wxString s1, s2, s3, s4, s5, s6, s7, s8;
- s1 = s2 = s3 = s4 = s5 = s6 = s7 = _T("abc");
+ s1 = s2 = s3 = s4 = s5 = s6 = _T("abc");
s1.append(_T("def"));
s2.append(_T("defgh"), 3);
s3.append(wxString(_T("abcdef")), 3, 6);
CPPUNIT_ASSERT( s5 == _T("abcaaaxxy") );
CPPUNIT_ASSERT( s6 == _T("abcdef") );
+ const char *pc = s1.c_str() + 2;
+ s7.append(pc, pc + 4);
+ WX_ASSERT_STR_EQUAL( "cdef", s7 );
+
+ const wchar_t *pw = s2.c_str() + 2;
+ s8.append(pw, pw + 4);
+ WX_ASSERT_STR_EQUAL( "cdef", s8 );
+
s7 = s8 = wxString(_T("null\0time"), 9);
s7.append(_T("def"));
CPPUNIT_ASSERT( s4 == _T("def") );
CPPUNIT_ASSERT( s5 == _T("aaa") );
CPPUNIT_ASSERT( s6 == _T("ef") );
+
+ const char *pc = s1;
+ s7.assign(pc, pc + 2);
+ WX_ASSERT_STR_EQUAL( "de", s7 );
+
+ const wchar_t *pw = s1.c_str();
+ s8.assign(pw + 2, pw + 3);
+ WX_ASSERT_STR_EQUAL( "f", s8 );
}
void StdStringTestCase::StdCompare()