]> git.saurik.com Git - wxWidgets.git/commitdiff
Implement comparisons between wxString::iterator and const_iterator.
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 21 Oct 2010 19:22:33 +0000 (19:22 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 21 Oct 2010 19:22:33 +0000 (19:22 +0000)
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
tests/strings/stdstrings.cpp

index 1ab7bd3446ce76aec850a55c2ead0dc55b834300..02cdb6609995381ab4c619d9079714d25c9120e7 100644 (file)
@@ -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
 
index 13f5e2728e6c02c904f1dac2078803a6cb29c991..e4d19cc01dfb4caf2edbfcad66f04dbb8bf3b544 100644 (file)
@@ -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;