From bdb40fa63eafd1b6b0b25e2d8142bff75a92421d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 21 Oct 2010 19:22:33 +0000 Subject: [PATCH] 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 --- include/wx/string.h | 30 ++++++++++++++++++++++++++++++ tests/strings/stdstrings.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/include/wx/string.h b/include/wx/string.h index 1ab7bd3446..02cdb66099 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1004,6 +1004,16 @@ public: iterator operator-(ptrdiff_t n) const { 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: iterator(wxString *wxstr, underlying_iterator ptr) : m_cur(ptr), m_node(wxstr, &m_cur) {} @@ -1048,6 +1058,11 @@ public: const_iterator operator-(ptrdiff_t n) const { 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: // for internal wxString use only: 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, 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 #if wxUSE_UNICODE diff --git a/tests/strings/stdstrings.cpp b/tests/strings/stdstrings.cpp index 13f5e2728e..e4d19cc01d 100644 --- a/tests/strings/stdstrings.cpp +++ b/tests/strings/stdstrings.cpp @@ -34,6 +34,7 @@ private: CPPUNIT_TEST_SUITE( StdStringTestCase ); CPPUNIT_TEST( StdConstructors ); CPPUNIT_TEST( StdIterators ); + CPPUNIT_TEST( StdIteratorsCmp ); CPPUNIT_TEST( StdAppend ); CPPUNIT_TEST( StdAssign ); CPPUNIT_TEST( StdCompare ); @@ -54,6 +55,7 @@ private: void StdConstructors(); void StdIterators(); + void StdIteratorsCmp(); void StdAppend(); void StdAssign(); void StdCompare(); @@ -119,6 +121,30 @@ void StdStringTestCase::StdIterators() 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() { wxString s1, s2, s3, s4, s5, s6, s7, s8; -- 2.45.2