Implement comparisons between wxString::iterator and const_iterator.

Only comparisons between const_iterator and iterator worked before (because of
implicit conversion from the latter to the former), implement the ones in the
other direction explicitly now.

Closes #12594.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65857 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-10-21 19:22:33 +00:00
parent 53cdd2c11d
commit bdb40fa63e
2 changed files with 56 additions and 0 deletions

View File

@@ -1004,6 +1004,16 @@ public:
iterator operator-(ptrdiff_t n) const iterator operator-(ptrdiff_t n) const
{ return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } { 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: private:
iterator(wxString *wxstr, underlying_iterator ptr) iterator(wxString *wxstr, underlying_iterator ptr)
: m_cur(ptr), m_node(wxstr, &m_cur) {} : m_cur(ptr), m_node(wxstr, &m_cur) {}
@@ -1048,6 +1058,11 @@ public:
const_iterator operator-(ptrdiff_t n) const const_iterator operator-(ptrdiff_t n) const
{ return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } { 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: private:
// for internal wxString use only: // for internal wxString use only:
const_iterator(const wxString *wxstr, underlying_iterator ptr) const_iterator(const wxString *wxstr, underlying_iterator ptr)
@@ -4046,6 +4061,21 @@ inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsS
inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); } 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); } 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 // comparison with C string in Unicode build
#if wxUSE_UNICODE #if wxUSE_UNICODE

View File

@@ -34,6 +34,7 @@ private:
CPPUNIT_TEST_SUITE( StdStringTestCase ); CPPUNIT_TEST_SUITE( StdStringTestCase );
CPPUNIT_TEST( StdConstructors ); CPPUNIT_TEST( StdConstructors );
CPPUNIT_TEST( StdIterators ); CPPUNIT_TEST( StdIterators );
CPPUNIT_TEST( StdIteratorsCmp );
CPPUNIT_TEST( StdAppend ); CPPUNIT_TEST( StdAppend );
CPPUNIT_TEST( StdAssign ); CPPUNIT_TEST( StdAssign );
CPPUNIT_TEST( StdCompare ); CPPUNIT_TEST( StdCompare );
@@ -54,6 +55,7 @@ private:
void StdConstructors(); void StdConstructors();
void StdIterators(); void StdIterators();
void StdIteratorsCmp();
void StdAppend(); void StdAppend();
void StdAssign(); void StdAssign();
void StdCompare(); void StdCompare();
@@ -119,6 +121,30 @@ void StdStringTestCase::StdIterators()
wxString::const_reverse_iterator i4; wxString::const_reverse_iterator i4;
} }
void StdStringTestCase::StdIteratorsCmp()
{
wxString s("foobar");
wxString::iterator i = s.begin();
wxString::const_iterator ci = s.begin();
CPPUNIT_ASSERT( i == ci );
CPPUNIT_ASSERT( i >= ci );
CPPUNIT_ASSERT( i <= ci );
CPPUNIT_ASSERT( ci == i );
CPPUNIT_ASSERT( ci >= i );
CPPUNIT_ASSERT( ci <= i );
ci++;
CPPUNIT_ASSERT( i != ci );
CPPUNIT_ASSERT( i < ci );
CPPUNIT_ASSERT( !(i > ci) );
CPPUNIT_ASSERT( ci != i );
CPPUNIT_ASSERT( ci > i );
CPPUNIT_ASSERT( !(ci < i) );
}
void StdStringTestCase::StdAppend() void StdStringTestCase::StdAppend()
{ {
wxString s1, s2, s3, s4, s5, s6, s7, s8; wxString s1, s2, s3, s4, s5, s6, s7, s8;