From a2e25444eb8848b8f190a75f6751c9b4bc801df1 Mon Sep 17 00:00:00 2001 From: "Alan M. Carroll" Date: Fri, 14 Jun 2019 17:51:07 -0500 Subject: [PATCH 1/2] TextView: fix bug in rtrim algorithms, add unit tests. --- swoc++/include/swoc/TextView.h | 12 +++++++----- unit_tests/test_TextView.cc | 13 +++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/swoc++/include/swoc/TextView.h b/swoc++/include/swoc/TextView.h index 38a40bac..8d667a1c 100644 --- a/swoc++/include/swoc/TextView.h +++ b/swoc++/include/swoc/TextView.h @@ -1288,7 +1288,7 @@ TextView::rtrim(std::string_view const &delimiters) const char *spot = this->data_end(); const char *limit = this->data(); - while (limit < spot && valid[static_cast(*--spot)]) + while (limit < spot-- && valid[static_cast(*spot)]) ; this->remove_suffix(this->data_end() - (spot + 1)); @@ -1308,7 +1308,9 @@ TextView::trim(std::string_view const &delimiters) ; this->remove_prefix(spot - this->data()); - for (spot = this->data_end(), limit = this->data(); limit < spot && valid[static_cast(*--spot)];) + spot = this->data_end(); + limit = this->data(); + while (limit < spot-- && valid[static_cast(*spot)]) ; this->remove_suffix(this->data_end() - (spot + 1)); @@ -1337,9 +1339,9 @@ template TextView::self_type & TextView::rtrim_if(F const &pred) { - const char *spot; - const char *limit; - for (spot = this->data_end(), limit = this->data(); limit < spot && pred(*--spot);) + const char *spot = this->data_end(); + const char *limit = this->data(); + while (limit < spot-- && pred(*spot)) ; this->remove_suffix(this->data_end() - (spot + 1)); return *this; diff --git a/unit_tests/test_TextView.cc b/unit_tests/test_TextView.cc index 4c90ca20..9fd4106d 100644 --- a/unit_tests/test_TextView.cc +++ b/unit_tests/test_TextView.cc @@ -28,6 +28,7 @@ using swoc::TextView; using namespace std::literals; +using namespace swoc::literals; TEST_CASE("TextView Constructor", "[libswoc][TextView]") { @@ -80,6 +81,18 @@ TEST_CASE("TextView Trimming", "[libswoc][TextView]") REQUIRE("More Text" == TextView{tv2}.rtrim_if(&isdigit)); REQUIRE(" Evil Dave Rulz " == TextView(tv).rtrim('.')); REQUIRE("Evil Dave Rulz" == TextView(tv).trim(" .")); + + tv.assign("\r\n"); + tv.rtrim_if([](char c) -> bool { return c == '\r' || c == '\n'; }); + REQUIRE(tv.size() == 0); + + tv.assign("..."); + tv.rtrim('.'); + REQUIRE(tv.size() == 0); + + tv.assign(".,,.;."); + tv.rtrim(";,."_tv); + REQUIRE(tv.size() == 0); } TEST_CASE("TextView Find", "[libswoc][TextView]") From 6c9af0e9fe77776dfceb7a8fb3fc38b65bab8fba Mon Sep 17 00:00:00 2001 From: "Alan M. Carroll" Date: Sat, 15 Jun 2019 07:57:42 -0500 Subject: [PATCH 2/2] Clang format. --- swoc++/include/swoc/bwf_fwd.h | 2 +- unit_tests/test_MemArena.cc | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/swoc++/include/swoc/bwf_fwd.h b/swoc++/include/swoc/bwf_fwd.h index f7d437b1..4d902628 100644 --- a/swoc++/include/swoc/bwf_fwd.h +++ b/swoc++/include/swoc/bwf_fwd.h @@ -24,7 +24,7 @@ namespace swoc { class BufferWriter; class FixedBufferWriter; -template < size_t N > class LocalBufferWriter; +template class LocalBufferWriter; namespace bwf { diff --git a/unit_tests/test_MemArena.cc b/unit_tests/test_MemArena.cc index cfe6f067..9a76fc7a 100644 --- a/unit_tests/test_MemArena.cc +++ b/unit_tests/test_MemArena.cc @@ -289,7 +289,8 @@ TEST_CASE("MemArena esoterica", "[libswoc][MemArena]") } { - std::unique_ptr arena(MemArena::construct_self_contained(), [](MemArena *arena) -> void { arena->~MemArena(); }); + std::unique_ptr arena(MemArena::construct_self_contained(), + [](MemArena *arena) -> void { arena->~MemArena(); }); static constexpr unsigned MAX = 512; std::uniform_int_distribution length_gen{6, MAX}; char buffer[MAX];