view: simplify logical comparator implementation
we remove some duplicate and specific logical comparator specialisations and instead rely on a base view/view comparator implementation by way of make_view.
This commit is contained in:
parent
cd69bb684a
commit
202c22eee8
@ -30,7 +30,7 @@ main (int, char**)
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (const auto &[tok, expected]: util::zip (util::make_tokeniser (csv, ','), TESTS))
|
for (const auto &[tok, expected]: util::zip (util::make_tokeniser (csv, ','), TESTS))
|
||||||
tap.expect (equal (tok, expected.value), "%s", expected.message);
|
tap.expect_eq (tok, expected.value, "%s", expected.message);
|
||||||
|
|
||||||
return tap.status ();
|
return tap.status ();
|
||||||
}
|
}
|
||||||
|
18
test/uri.cpp
18
test/uri.cpp
@ -144,15 +144,15 @@ main (void)
|
|||||||
tap.expect_nothrow ([t] (void) { util::uri foo (t.src); }, "nothrow parsing '%s'", t.src);
|
tap.expect_nothrow ([t] (void) { util::uri foo (t.src); }, "nothrow parsing '%s'", t.src);
|
||||||
util::uri u (t.src);
|
util::uri u (t.src);
|
||||||
|
|
||||||
tap.expect (equal (u.scheme (), t.scheme), "scheme for '%s'", t.src);
|
tap.expect_eq (u.scheme (), t.scheme, "scheme for '%s'", t.src);
|
||||||
tap.expect (equal (u.heirarchical (), t.hierarchical), "hierarchical for '%s'", t.src);
|
tap.expect_eq (u.heirarchical (), t.hierarchical, "hierarchical for '%s'", t.src);
|
||||||
tap.expect (equal (u.authority (), t.authority), "authority for '%s'", t.src);
|
tap.expect_eq (u.authority (), t.authority, "authority for '%s'", t.src);
|
||||||
tap.expect (equal (u.host (), t.host), "host for '%s'", t.src);
|
tap.expect_eq (u.host (), t.host, "host for '%s'", t.src);
|
||||||
tap.expect (equal (u.user (), t.user), "user for '%s'", t.src);
|
tap.expect_eq (u.user (), t.user, "user for '%s'", t.src);
|
||||||
tap.expect (equal (u.port (), t.port), "port for '%s'", t.src);
|
tap.expect_eq (u.port (), t.port, "port for '%s'", t.src);
|
||||||
tap.expect (equal (u.path (), t.path), "path for '%s'", t.src);
|
tap.expect_eq (u.path (), t.path, "path for '%s'", t.src);
|
||||||
tap.expect (equal (u.query (), t.query), "query for '%s'", t.src);
|
tap.expect_eq (u.query (), t.query, "query for '%s'", t.src);
|
||||||
tap.expect (equal (u.fragment (), t.fragment), "fragment for '%s'", t.src);
|
tap.expect_eq (u.fragment (), t.fragment, "fragment for '%s'", t.src);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char* BAD[] = {
|
static const char* BAD[] = {
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
#include "tap.hpp"
|
#include "tap.hpp"
|
||||||
#include "view.hpp"
|
#include "view.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
int
|
int
|
||||||
main (int, char**)
|
main (int, char**)
|
||||||
{
|
{
|
||||||
@ -9,10 +13,35 @@ main (int, char**)
|
|||||||
const std::string s = "this is a test string";
|
const std::string s = "this is a test string";
|
||||||
const std::string t = "not the same string";
|
const std::string t = "not the same string";
|
||||||
|
|
||||||
tap.expect (equal (s, util::make_view(s)), "string/view equality");
|
tap.expect_eq (s, util::make_view(s), "string/view equality");
|
||||||
tap.expect (!equal (s, util::make_view(t)), "string/view inequality");
|
tap.expect_neq (s, util::make_view(t), "string/view inequality");
|
||||||
tap.expect (equal (s.data (), util::make_view (s)), "c-str/view equality");
|
tap.expect_eq (s.data (), util::make_view (s), "c-str/view equality");
|
||||||
tap.expect (equal (s, util::view<const char*> (&*s.cbegin (), &*s.cend ())), "string/pointer-view equality");
|
tap.expect_eq (
|
||||||
|
s,
|
||||||
|
util::view<const char*> (&*s.cbegin (), &*s.cend ()),
|
||||||
|
"string/pointer-view equality");
|
||||||
|
|
||||||
|
{
|
||||||
|
// comparator tests
|
||||||
|
static const struct {
|
||||||
|
util::view<const char*> a;
|
||||||
|
util::view<const char*> b;
|
||||||
|
bool less;
|
||||||
|
} TESTS[] = {
|
||||||
|
{ "b", "b", false },
|
||||||
|
{ "b", "a", false },
|
||||||
|
{ "b", "c", true },
|
||||||
|
{ "foo", "bar", false },
|
||||||
|
{ "bar", "foo", true },
|
||||||
|
{ "banana", "banana", false },
|
||||||
|
{ "banana", "banan", false },
|
||||||
|
{ "banan", "banana", true },
|
||||||
|
};
|
||||||
|
|
||||||
|
tap.expect (std::all_of (std::begin (TESTS), std::end (TESTS), [] (const auto &x) {
|
||||||
|
return (x.a < x.b) == x.less;
|
||||||
|
}), "comparator less-than");
|
||||||
|
};
|
||||||
|
|
||||||
return tap.status ();
|
return tap.status ();
|
||||||
}
|
}
|
||||||
|
26
view.cpp
26
view.cpp
@ -20,32 +20,6 @@
|
|||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
#define EQUALITY(A,B) \
|
|
||||||
bool \
|
|
||||||
util::equal (A a, B b) \
|
|
||||||
{ \
|
|
||||||
return a.size () == b.size () && \
|
|
||||||
std::equal (a.cbegin (), \
|
|
||||||
a.cend (), \
|
|
||||||
b.cbegin ()); \
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
EQUALITY(const std::string&, view<const char*>)
|
|
||||||
EQUALITY(const std::string&, view<char*>)
|
|
||||||
EQUALITY(const std::string&, view<std::string::const_iterator>)
|
|
||||||
EQUALITY(const std::string&, view<std::string::iterator>)
|
|
||||||
|
|
||||||
EQUALITY(view<const char*>, const std::string&)
|
|
||||||
EQUALITY(view<char*>, const std::string&)
|
|
||||||
EQUALITY(view<std::string::const_iterator>, const std::string&)
|
|
||||||
EQUALITY(view<std::string::iterator>, const std::string&)
|
|
||||||
|
|
||||||
#undef EQUALITY
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
namespace util {
|
namespace util {
|
||||||
#define OSTREAM(A) \
|
#define OSTREAM(A) \
|
||||||
|
87
view.hpp
87
view.hpp
@ -46,6 +46,12 @@ namespace util {
|
|||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
template <std::size_t N, typename ValueT>
|
||||||
|
view (const ValueT(&value)[N]):
|
||||||
|
view (std::begin (value), std::end (value))
|
||||||
|
{ ; }
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
constexpr
|
constexpr
|
||||||
view (const view &rhs) noexcept:
|
view (const view &rhs) noexcept:
|
||||||
@ -291,55 +297,50 @@ namespace util {
|
|||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
template <typename IteratorT>
|
template <typename IteratorA, typename IteratorB>
|
||||||
constexpr bool
|
constexpr bool
|
||||||
operator== (const view<IteratorT> &a, const view<IteratorT> &b)
|
operator== (const view<IteratorA> &a, const view<IteratorB> &b)
|
||||||
{
|
{
|
||||||
return std::cbegin (a) == std::cbegin (b) &&
|
return a.size () == b.size () &&
|
||||||
std::cend (a) == std::cend (b);
|
std::equal (std::begin (a), std::end (a), std::begin (b));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
template <typename IteratorT>
|
// defer equality to the view/view operator by way of make_view
|
||||||
|
template <typename IteratorT, typename ValueT>
|
||||||
constexpr bool
|
constexpr bool
|
||||||
operator!= (const view<IteratorT> &a, const view<IteratorT> &b)
|
operator== (const view<IteratorT> &a, const ValueT &b)
|
||||||
{
|
|
||||||
return !(a == b);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
|
||||||
template <typename IteratorT, typename ContainerT>
|
|
||||||
constexpr bool
|
|
||||||
operator== (const ContainerT &a, const view<IteratorT> &b)
|
|
||||||
{
|
|
||||||
return make_view (a) == b;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
|
||||||
template <typename IteratorT, typename ContainerT>
|
|
||||||
constexpr bool
|
|
||||||
operator!= (const ContainerT &b, const view<IteratorT> &a)
|
|
||||||
{
|
|
||||||
return !(a == b);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
|
||||||
template <typename IteratorT, typename ContainerT>
|
|
||||||
constexpr bool
|
|
||||||
operator== (const view<IteratorT> &a, const ContainerT &b)
|
|
||||||
{
|
{
|
||||||
return a == make_view (b);
|
return a == make_view (b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
template <typename IteratorT, typename ContainerT>
|
// reverse the arguments and forward to the above operator. we formumlate
|
||||||
|
// equality this way to avoid implementing the operator twice for each
|
||||||
|
// weird case.
|
||||||
|
template <typename IteratorT, typename ValueT>
|
||||||
constexpr bool
|
constexpr bool
|
||||||
operator!= (const view<IteratorT> &a, const ContainerT &b)
|
operator== (const ValueT &a, const view<IteratorT> &b)
|
||||||
|
{
|
||||||
|
return b == a;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename IteratorT, typename ValueT>
|
||||||
|
constexpr bool
|
||||||
|
operator!= (const util::view<IteratorT> &a, const ValueT &b)
|
||||||
|
{
|
||||||
|
return !(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename IteratorT, typename ValueT>
|
||||||
|
constexpr bool
|
||||||
|
operator!= (const ValueT &a, const util::view<IteratorT> &b)
|
||||||
{
|
{
|
||||||
return !(a == b);
|
return !(a == b);
|
||||||
}
|
}
|
||||||
@ -363,24 +364,10 @@ namespace util {
|
|||||||
util::min (la, lb)
|
util::min (la, lb)
|
||||||
);
|
);
|
||||||
|
|
||||||
return res < 0 || la < lb;
|
return res < 0 || (res == 0 && la < lb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
|
||||||
// equality operations for string-like views against std::strings
|
|
||||||
bool equal (const std::string&, view<const char*>);
|
|
||||||
bool equal (const std::string&, view<char*>);
|
|
||||||
bool equal (const std::string&, view<std::string::const_iterator>);
|
|
||||||
bool equal (const std::string&, view<std::string::iterator>);
|
|
||||||
|
|
||||||
|
|
||||||
bool equal (view<const char*>, const std::string&);
|
|
||||||
bool equal (view<char*>, const std::string&);
|
|
||||||
bool equal (view<std::string::const_iterator>, const std::string&);
|
|
||||||
bool equal (view<std::string::iterator>, const std::string&);
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
template <typename IteratorT>
|
template <typename IteratorT>
|
||||||
std::ostream&
|
std::ostream&
|
||||||
|
Loading…
Reference in New Issue
Block a user