Skip to content

Add comparison operators for Str and String #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions book/src/binding/str.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 &);
Expand Down
7 changes: 7 additions & 0 deletions book/src/binding/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 &);
Expand Down
14 changes: 14 additions & 0 deletions include/cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class String final {
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;

Expand Down Expand Up @@ -98,6 +105,13 @@ class Str final {
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
Expand Down
58 changes: 58 additions & 0 deletions src/cxx.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../include/cxx.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <exception>
Expand Down Expand Up @@ -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) {}

Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down