From 2d2a850a61dbe5695ba92dfc4dcb107c10d3bce6 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 8 Jan 2026 09:38:31 +0100 Subject: feat: `operator<<` for const crope/wrope. API improvements. Signed-off-by: Amlal El Mahrouss --- example/simple_example/example.cpp | 7 ++++--- include/ocl/tproc/detail/rope_fwd.hpp | 8 ++++---- include/ocl/tproc/detail/rope_fwd.inl | 10 +++++----- include/ocl/tproc/rope.hpp | 26 ++++++++++++++++++++------ test/rope_test/crope.pred.test.cpp | 22 +++++++++++----------- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/example/simple_example/example.cpp b/example/simple_example/example.cpp index da543a9..3feb686 100644 --- a/example/simple_example/example.cpp +++ b/example/simple_example/example.cpp @@ -7,6 +7,7 @@ #include #include +#include #ifndef STANDALONE using namespace ocl; @@ -18,9 +19,9 @@ int main() { auto rope = tproc::crope("The Quick Brown Fox Jumps Over The Lazy Dog"); - std::unique_ptr new_elem(new tproc::crope(", and Jumps again.")); + std::unique_ptr new_elem = std::make_unique(", and Jumps again."); std::unique_ptr res(rope.concat(new_elem.get())); - std::cout << ++rope; - std::cout << rope; + std::cout << *++rope << std::endl; + std::cout << rope << std::endl; } diff --git a/include/ocl/tproc/detail/rope_fwd.hpp b/include/ocl/tproc/detail/rope_fwd.hpp index 9fe6212..0aabf86 100644 --- a/include/ocl/tproc/detail/rope_fwd.hpp +++ b/include/ocl/tproc/detail/rope_fwd.hpp @@ -42,8 +42,8 @@ namespace ocl::tproc rope_ptr operator++(); rope_ptr operator++(int); - const rope_ptr cbegin(); - const rope_ptr cend(); + const rope_ptr cbegin() const; + const rope_ptr cend() const; rope_ptr concat(rope_ptr right); @@ -56,7 +56,7 @@ namespace ocl::tproc bool starts_with(const boost::core::basic_string_view&); bool ends_with(const boost::core::basic_string_view&); - size_type size(); + size_type size() const; bool empty() const; rope_ptr insert(size_type pos, @@ -64,7 +64,7 @@ namespace ocl::tproc rope_ptr) const; boost::core::basic_string_view data(); - const boost::core::basic_string_view c_str(); + const boost::core::basic_string_view c_str() const; public: basic_rope(const boost::core::basic_string_view& in = {}); diff --git a/include/ocl/tproc/detail/rope_fwd.inl b/include/ocl/tproc/detail/rope_fwd.inl index 5ce38f8..4fd6e9a 100644 --- a/include/ocl/tproc/detail/rope_fwd.inl +++ b/include/ocl/tproc/detail/rope_fwd.inl @@ -396,7 +396,7 @@ namespace ocl::tproc } template - const basic_rope::rope_ptr basic_rope::cbegin() + const basic_rope::rope_ptr basic_rope::cbegin() const { if (impl_->is_leaf()) return this; @@ -405,14 +405,14 @@ namespace ocl::tproc } template - const basic_rope::rope_ptr basic_rope::cend() + const basic_rope::rope_ptr basic_rope::cend() const { return nullptr; } template basic_rope::size_type - basic_rope::size() + basic_rope::size() const { return impl_->size(); } @@ -567,7 +567,7 @@ namespace ocl::tproc template const boost::core::basic_string_view::value_type> - basic_rope::c_str() + basic_rope::c_str() const { return {impl_->blob_, impl_->capacity_}; } @@ -603,7 +603,7 @@ namespace ocl::tproc { return data(); } - + template basic_rope::rope_ptr basic_rope::operator++() { diff --git a/include/ocl/tproc/rope.hpp b/include/ocl/tproc/rope.hpp index 05e5399..fba755a 100644 --- a/include/ocl/tproc/rope.hpp +++ b/include/ocl/tproc/rope.hpp @@ -194,14 +194,28 @@ namespace ocl::tproc } // namespace ocl::tproc -inline std::ostream & operator<<(std::ostream& os, ocl::tproc::crope& r){ - os << r.data(); - return os; +inline std::ostream& operator<<(std::ostream& os, ocl::tproc::crope& r) +{ + os << r.data(); + return os; +} + +inline std::wostream& operator<<(std::wostream& os, ocl::tproc::wrope& r) +{ + os << r.data(); + return os; } -inline std::wostream & operator<<(std::wostream& os, ocl::tproc::wrope& r){ - os << r.data(); - return os; +inline std::ostream& operator<<(std::ostream& os, const ocl::tproc::crope& r) +{ + os << r.c_str(); + return os; +} + +inline std::wostream& operator<<(std::wostream& os, const ocl::tproc::wrope& r) +{ + os << r.c_str(); + return os; } #include "rope.inl" diff --git a/test/rope_test/crope.pred.test.cpp b/test/rope_test/crope.pred.test.cpp index afbc9c5..a967adb 100644 --- a/test/rope_test/crope.pred.test.cpp +++ b/test/rope_test/crope.pred.test.cpp @@ -7,6 +7,7 @@ #include #include +#include #define BOOST_TEST_MODULE crope_pred #include @@ -20,11 +21,11 @@ using namespace boost; BOOST_AUTO_TEST_CASE(rope_should_succeed_in_find_pred) { auto rope = tproc::crope("Exact Sentence"); - auto it = tproc::rope::exact_pred{"Exact Sentence"}(rope.cbegin(), rope.cend()); + auto it = tproc::rope::exact_pred{"Exact Sentence"}(rope.begin(), rope.end()); BOOST_TEST(it != rope.cend()); - ocl::io::println(it->data()); + std::cout << rope << std::endl; } BOOST_AUTO_TEST_CASE(rope_should_succeed_in_at) @@ -32,27 +33,26 @@ BOOST_AUTO_TEST_CASE(rope_should_succeed_in_at) auto rope = tproc::crope("Exact Sentence"); auto it = std::move(rope.substr(rope.at("Exact"), rope.size())); - ocl::io::println(it.data()); + BOOST_ASSERT(it == "Exact Sentence"); } BOOST_AUTO_TEST_CASE(rope_should_succeed_in_starts_with) { auto rope = tproc::crope("The Quick Brown Fox Jumps Over The Lazy Dog"); - + // find the leaf with the starting value 'foo' - auto it = tproc::rope::starts_with_pred{"The Quick"}(rope.cbegin(), rope.cend()); + auto it = tproc::rope::starts_with_pred{"The Quick"}(rope.begin(), rope.end()); BOOST_TEST(it != rope.cend()); - auto it_end = tproc::rope::ends_with_pred{"Lazy Dog"}(rope.cbegin(), rope.cend()); + auto it_end = tproc::rope::ends_with_pred{"Lazy Dog"}(rope.begin(), rope.end()); BOOST_TEST(it_end != rope.cend()); - io::println(it_end->data()); - io::println(it->data()); - - std::unique_ptr new_elem(new tproc::crope(", and Jumps again.")); + std::unique_ptr new_elem = std::make_unique(", and Jumps again."); std::unique_ptr ret_elem(rope.concat(new_elem.get())); - io::println(ret_elem->data()); + BOOST_TEST((*ret_elem == "The Quick Brown Fox Jumps Over The Lazy Dog, and Jumps again.")); + + std::cout << *ret_elem; } -- cgit v1.2.3