You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/07/16 16:42:30 UTC
[trafficserver] branch master updated: TextView: More unit tests
about separators and tokens.
This is an automated email from the ASF dual-hosted git repository.
amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 22e273d TextView: More unit tests about separators and tokens.
22e273d is described below
commit 22e273d837970fb89b75c503d0e5ddbd83059cbe
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Fri Jul 13 12:40:54 2018 -0500
TextView: More unit tests about separators and tokens.
---
lib/ts/TextView.h | 28 +++++++++++++++++++++++++
lib/ts/unit-tests/test_TextView.cc | 43 +++++++++++++++++++++++++++++++++++++-
2 files changed, 70 insertions(+), 1 deletion(-)
diff --git a/lib/ts/TextView.h b/lib/ts/TextView.h
index d4494cd..1c63cae 100644
--- a/lib/ts/TextView.h
+++ b/lib/ts/TextView.h
@@ -289,6 +289,9 @@ public:
*/
template <typename F> self_type prefix_if(F const &pred) const;
+ /// Overload to provide better return type.
+ self_type &remove_prefix(size_t n);
+
/** Split a prefix from the view on the character at offset @a n.
The view is split in to two parts and the byte at offset @a n is discarded. @a this retains
@@ -380,6 +383,9 @@ public:
/// Get the prefix delimited by the first character for which @a pred is @c true.
template <typename F> self_type suffix_if(F const &pred) const;
+ /// Overload to provide better return type.
+ self_type &remove_suffix(size_t n);
+
/** Split the view to get a suffix of size @a n.
The view is split in to two parts, a suffix of size @a n and a remainder which is the original
@@ -642,6 +648,17 @@ TextView::prefix_if(F const &pred) const
return this->prefix(this->find(pred));
}
+inline auto
+TextView::remove_prefix(size_t n) -> self_type &
+{
+ if (n > this->size()) {
+ this->clear();
+ } else {
+ this->super_type::remove_prefix(n);
+ }
+ return *this;
+}
+
inline TextView
TextView::split_prefix_at(size_t n)
{
@@ -738,6 +755,17 @@ TextView::suffix_if(F const &pred) const
return this->suffix((this->size() - std::min(this->size(), this->rfind_if(pred))) - 1);
}
+inline auto
+TextView::remove_suffix(size_t n) -> self_type &
+{
+ if (n > this->size()) {
+ this->clear();
+ } else {
+ this->super_type::remove_suffix(n);
+ }
+ return *this;
+}
+
inline TextView
TextView::split_suffix(size_t n)
{
diff --git a/lib/ts/unit-tests/test_TextView.cc b/lib/ts/unit-tests/test_TextView.cc
index 0be0c73..9b93436 100644
--- a/lib/ts/unit-tests/test_TextView.cc
+++ b/lib/ts/unit-tests/test_TextView.cc
@@ -24,6 +24,7 @@
#include <ts/TextView.h>
#include <string>
#include <sstream>
+#include <iostream>
#include <iomanip>
#include <catch.hpp>
@@ -153,7 +154,47 @@ TEST_CASE("TextView Affixes", "[libts][TextView]")
s = t.take_suffix_at('Q');
REQUIRE(s == addr3);
REQUIRE(t.empty());
-}
+
+ auto is_sep{[](char c) { return isspace(c) || ',' == c || ';' == c; }};
+ ts::TextView token;
+ t = ";; , ;;one;two,th:ree four,, ; ,,f-ive="sv;
+ // Do an unrolled loop.
+ REQUIRE(!t.ltrim_if(is_sep).empty());
+ REQUIRE(t.take_prefix_if(is_sep) == "one");
+ REQUIRE(!t.ltrim_if(is_sep).empty());
+ REQUIRE(t.take_prefix_if(is_sep) == "two");
+ REQUIRE(!t.ltrim_if(is_sep).empty());
+ REQUIRE(t.take_prefix_if(is_sep) == "th:ree");
+ REQUIRE(!t.ltrim_if(is_sep).empty());
+ REQUIRE(t.take_prefix_if(is_sep) == "four");
+ REQUIRE(!t.ltrim_if(is_sep).empty());
+ REQUIRE(t.take_prefix_if(is_sep) == "f-ive=");
+ REQUIRE(t.empty());
+
+ // Simulate pulling off FQDN pieces in reverse order from a string_view.
+ // Simulates operations in HostLookup.cc, where the use of string_view necessitates this workaround of failures
+ // in the string_view API. With a TextView, it would just be repeated @c take_suffix_at('.')
+ std::string_view fqdn{"bob.ne1.corp.ngeo.com"};
+ ts::TextView elt{ts::TextView{fqdn}.suffix('.')};
+ REQUIRE(elt == "com");
+
+ // Unroll loop for testing.
+ fqdn.remove_suffix(std::min(fqdn.size(), elt.size() + 1));
+ elt = ts::TextView{fqdn}.suffix('.');
+ REQUIRE(elt == "ngeo");
+ fqdn.remove_suffix(std::min(fqdn.size(), elt.size() + 1));
+ elt = ts::TextView{fqdn}.suffix('.');
+ REQUIRE(elt == "corp");
+ fqdn.remove_suffix(std::min(fqdn.size(), elt.size() + 1));
+ elt = ts::TextView{fqdn}.suffix('.');
+ REQUIRE(elt == "ne1");
+ fqdn.remove_suffix(std::min(fqdn.size(), elt.size() + 1));
+ elt = ts::TextView{fqdn}.suffix('.');
+ REQUIRE(elt == "bob");
+ fqdn.remove_suffix(std::min(fqdn.size(), elt.size() + 1));
+ elt = ts::TextView{fqdn}.suffix('.');
+ REQUIRE(elt.empty());
+};
TEST_CASE("TextView Formatting", "[libts][TextView]")
{