Repository: dtolnay/cxx (rust)

Issue:
Provide comparison operators for rust::Str and rust::String
The C++ types should come with operators for `==`, `!=`, `<`, `>`, `<=`, `>=`.

Pull request:
Add comparison operators for Str and String
Closes #57.

Patch:
diff --git a/book/src/binding/str.md b/book/src/binding/str.md
index 93e97502..999727c0 100644
--- a/book/src/binding/str.md
+++ b/book/src/binding/str.md
@@ -37,6 +37,13 @@ public:
   const_iterator end() const noexcept;
   const_iterator cbegin() const noexcept;
   const_iterator cend() const noexcept;
+
+  bool operator==(const Str &) const noexcept;
+  bool operator!=(const Str &) const noexcept;
+  bool operator<(const Str &) const noexcept;
+  bool operator<=(const Str &) const noexcept;
+  bool operator>(const Str &) const noexcept;
+  bool operator>=(const Str &) const noexcept;
 };
 
 std::ostream &operator<<(std::ostream &, const Str &);
diff --git a/book/src/binding/string.md b/book/src/binding/string.md
index 47a2b964..c4a1b6ad 100644
--- a/book/src/binding/string.md
+++ b/book/src/binding/string.md
@@ -42,6 +42,13 @@ public:
   const_iterator end() const noexcept;
   const_iterator cbegin() const noexcept;
   const_iterator cend() const noexcept;
+
+  bool operator==(const String &) const noexcept;
+  bool operator!=(const String &) const noexcept;
+  bool operator<(const String &) const noexcept;
+  bool operator<=(const String &) const noexcept;
+  bool operator>(const String &) const noexcept;
+  bool operator>=(const String &) const noexcept;
 };
 
 std::ostream &operator<<(std::ostream &, const String &);
diff --git a/include/cxx.h b/include/cxx.h
index e40876dd..31b34fc7 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -59,6 +59,13 @@ public:
   const_iterator cbegin() const noexcept;
   const_iterator cend() const noexcept;
 
+  bool operator==(const String &) const noexcept;
+  bool operator!=(const String &) const noexcept;
+  bool operator<(const String &) const noexcept;
+  bool operator<=(const String &) const noexcept;
+  bool operator>(const String &) const noexcept;
+  bool operator>=(const String &) const noexcept;
+
   // Internal API only intended for the cxxbridge code generator.
   String(unsafe_bitcopy_t, const String &) noexcept;
 
@@ -98,6 +105,13 @@ public:
   const_iterator cbegin() const noexcept;
   const_iterator cend() const noexcept;
 
+  bool operator==(const Str &) const noexcept;
+  bool operator!=(const Str &) const noexcept;
+  bool operator<(const Str &) const noexcept;
+  bool operator<=(const Str &) const noexcept;
+  bool operator>(const Str &) const noexcept;
+  bool operator>=(const Str &) const noexcept;
+
 private:
   friend impl<Str>;
   // Not necessarily ABI compatible with &str. Codegen will translate to
diff --git a/src/cxx.cc b/src/cxx.cc
index c9e21516..ac10df18 100644
--- a/src/cxx.cc
+++ b/src/cxx.cc
@@ -1,4 +1,5 @@
 #include "../include/cxx.h"
+#include <algorithm>
 #include <cassert>
 #include <cstring>
 #include <exception>
@@ -140,6 +141,30 @@ String::const_iterator String::cend() const noexcept {
   return this->data() + this->size();
 }
 
+bool String::operator==(const String &rhs) const noexcept {
+  return rust::Str(*this) == rust::Str(rhs);
+}
+
+bool String::operator!=(const String &rhs) const noexcept {
+  return rust::Str(*this) != rust::Str(rhs);
+}
+
+bool String::operator<(const String &rhs) const noexcept {
+  return rust::Str(*this) < rust::Str(rhs);
+}
+
+bool String::operator<=(const String &rhs) const noexcept {
+  return rust::Str(*this) <= rust::Str(rhs);
+}
+
+bool String::operator>(const String &rhs) const noexcept {
+  return rust::Str(*this) > rust::Str(rhs);
+}
+
+bool String::operator>=(const String &rhs) const noexcept {
+  return rust::Str(*this) >= rust::Str(rhs);
+}
+
 String::String(unsafe_bitcopy_t, const String &bits) noexcept
     : repr(bits.repr) {}
 
@@ -186,6 +211,39 @@ Str::const_iterator Str::cbegin() const noexcept { return this->ptr; }
 
 Str::const_iterator Str::cend() const noexcept { return this->ptr + this->len; }
 
+bool Str::operator==(const Str &rhs) const noexcept {
+  return this->len == rhs.len &&
+         std::equal(this->begin(), this->end(), rhs.begin());
+}
+
+bool Str::operator!=(const Str &rhs) const noexcept { return !(*this == rhs); }
+
+bool Str::operator<(const Str &rhs) const noexcept {
+  return std::lexicographical_compare(this->begin(), this->end(), rhs.begin(),
+                                      rhs.end());
+}
+
+bool Str::operator<=(const Str &rhs) const noexcept {
+  // std::mismatch(this->begin(), this->end(), rhs.begin(), rhs.end()), except
+  // without Undefined Behavior on C++11 if rhs is shorter than *this.
+  const_iterator liter = this->begin(), lend = this->end(), riter = rhs.begin(),
+                 rend = rhs.end();
+  while (liter != lend && riter != rend && *liter == *riter) {
+    ++liter, ++riter;
+  }
+  if (liter == lend) {
+    return true; // equal or *this is a prefix of rhs
+  } else if (riter == rend) {
+    return false; // rhs is a prefix of *this
+  } else {
+    return *liter <= *riter;
+  }
+}
+
+bool Str::operator>(const Str &rhs) const noexcept { return rhs < *this; }
+
+bool Str::operator>=(const Str &rhs) const noexcept { return rhs <= *this; }
+
 std::ostream &operator<<(std::ostream &os, const Str &s) {
   os.write(s.data(), s.size());
   return os;


Test patch:
diff --git a/tests/ffi/tests.cc b/tests/ffi/tests.cc
index 340dc9c1..244c6c01 100644
--- a/tests/ffi/tests.cc
+++ b/tests/ffi/tests.cc
@@ -6,6 +6,7 @@
 #include <numeric>
 #include <stdexcept>
 #include <string>
+#include <tuple>
 
 extern "C" void cxx_test_suite_set_correct() noexcept;
 extern "C" tests::R *cxx_test_suite_get_box() noexcept;
@@ -656,6 +657,25 @@ extern "C" const char *cxx_run_test() noexcept {
   ASSERT(Shared{1} == Shared{1});
   ASSERT(Shared{1} != Shared{2});
 
+  rust::String first = "first", second = "second", sec = "sec";
+  bool (rust::String::*cmp)(const rust::String &) const;
+  bool first_first, first_second, sec_second, second_sec;
+  for (auto test : {
+    std::tuple<decltype(cmp), bool, bool, bool, bool>
+    {&rust::String::operator==, true, false, false, false},
+    {&rust::String::operator!=, false, true, true, true},
+    {&rust::String::operator<, false, true, true, false},
+    {&rust::String::operator<=, true, true, true, false},
+    {&rust::String::operator>, false, false, false, true},
+    {&rust::String::operator>=, true, false, false, true},
+  }) {
+    std::tie(cmp, first_first, first_second, sec_second, second_sec) = test;
+    ASSERT((first.*cmp)(first) == first_first);
+    ASSERT((first.*cmp)(second) == first_second);
+    ASSERT((sec.*cmp)(second) == sec_second);
+    ASSERT((second.*cmp)(sec) == second_sec);
+  }
+
   cxx_test_suite_set_correct();
   return nullptr;
 }