string: fixup tokeniser::contains

This commit is contained in:
Danny Robson 2019-02-20 15:51:54 +11:00
parent 2ce92cb2c8
commit 2f6c845409
2 changed files with 80 additions and 15 deletions

View File

@ -23,12 +23,19 @@ strbegins(const char *restrict str,
namespace cruft {
/// Provides an iterator interface over a string, breaking at each
/// occurence of a specific character.
///
/// It is up to the constructor to ensure the lifetime of the string.
/// This class simply provides an interface for iteration and doesn't
/// concern itself with lifetimes.
template <typename Iterator>
struct tokeniser {
public:
using value_type = typename std::iterator_traits<Iterator>::value_type;
using range_type = view<Iterator>;
tokeniser (cruft::view<Iterator,Iterator> _range, value_type _separator):
m_range (_range),
m_separator (_separator)
@ -39,6 +46,7 @@ namespace cruft {
m_separator (_separator)
{ ; }
struct iterator : public std::iterator<
std::forward_iterator_tag,
range_type,
@ -73,15 +81,8 @@ namespace cruft {
return std::next (*this, count);
}
auto const& operator* (void) const&
{
return m_range;
}
auto const* operator-> (void) const&
{
return &m_range;
}
auto const& operator* (void) const& { return m_range; }
auto const* operator-> (void) const& { return &m_range; }
bool operator== (const iterator &rhs) const
{
@ -96,11 +97,10 @@ namespace cruft {
private:
iterator (range_type _range, value_type _separator):
m_separator (_separator),
m_range (_range.cbegin (),
std::find (_range.cbegin (),
_range.cend (),
_separator)
),
m_range {
_range.cbegin (),
std::find (_range.cbegin (), _range.cend (), _separator)
},
m_end (_range.cend ())
{ ; }
@ -111,6 +111,7 @@ namespace cruft {
friend tokeniser;
};
iterator cbegin (void) const { return { m_range, m_separator }; }
iterator begin (void) const { return { m_range, m_separator }; }
@ -129,7 +130,11 @@ namespace cruft {
bool contains (std::string_view key) const noexcept
{
return std::find (begin (), end (), key) != end ();
for (auto const &i: *this)
if (equal (i, key))
return true;
return false;
}
private:

View File

@ -75,11 +75,71 @@ test_contains (cruft::TAP::logger &tap)
{
struct {
char const *haystack;
char separator;
char const *needle;
bool expected;
char const *message;
} const TESTS[] = {
{
.haystack = "foo",
.separator = ',',
.needle = "foo",
.expected = true,
.message = "needle is haystack"
},
{
.haystack = "",
.separator = ',',
.needle = "foo",
.expected = false,
.message = "haystack is empty",
},
{
.haystack = "foo",
.separator = ',',
.needle = "",
.expected = false,
.message = "needle is empty",
},
{
.haystack = "a,b,foo",
.separator = ',',
.needle = "foo",
.expected = true,
.message = "needle is last",
},
{
.haystack = "foo,b,c",
.separator = ',',
.needle = "foo",
.expected = true,
.message = "needle is first",
},
{
.haystack = "foo,b,c",
.separator = ';',
.needle = "foo",
.expected = false,
.message = "separator is mismatched",
},
{
.haystack = "a;b;foo",
.separator = ';',
.needle = "foo",
.expected = true,
.message = "separator isn't default"
}
};
for (auto const &t: TESTS) {
cruft::tokeniser tok (t.haystack, t.separator);
auto const found = tok.contains (t.needle);
tap.expect_eq (found, t.expected, "%!", t.message);
}
}