view: add slicing operation, stolen from python
This commit is contained in:
parent
f9a202877a
commit
32a4aa23e5
@ -15,19 +15,21 @@ main (int, char**)
|
||||
"character array view does not include trailing null"
|
||||
);
|
||||
|
||||
const std::string s = "this is a test string";
|
||||
const std::string t = "not the same string";
|
||||
|
||||
tap.expect_eq (s, util::make_view(s), "string/view equality");
|
||||
tap.expect_neq (s, util::make_view(t), "string/view inequality");
|
||||
tap.expect_eq (s.data (), util::make_view (s), "c-str/view equality");
|
||||
tap.expect_eq (
|
||||
s,
|
||||
util::view<const char*> (&*s.cbegin (), &*s.cend ()),
|
||||
"string/pointer-view equality");
|
||||
|
||||
{
|
||||
// comparator tests
|
||||
const std::string s = "this is a test string";
|
||||
const std::string t = "not the same string";
|
||||
|
||||
tap.expect_eq (s, util::make_view(s), "string/view equality");
|
||||
tap.expect_neq (s, util::make_view(t), "string/view inequality");
|
||||
tap.expect_eq (s.data (), util::make_view (s), "c-str/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;
|
||||
@ -48,5 +50,25 @@ main (int, char**)
|
||||
}), "comparator less-than");
|
||||
};
|
||||
|
||||
// slicing
|
||||
{
|
||||
static const struct {
|
||||
const char *init;
|
||||
const char *result;
|
||||
int a;
|
||||
int b;
|
||||
const char *message;
|
||||
} TESTS[] = {
|
||||
{ "01234567", "01234567", 0, 8, "identity, +ve +ve" },
|
||||
{ "01234567", "01234567", 0, -1, "identity, +ve -ve" },
|
||||
{ "01234567", "", -1, -1, "empty, -ve -ve" },
|
||||
{ "01234567", "45", 4, -3, "centre, +ve -ve" },
|
||||
};
|
||||
|
||||
for (const auto &t: TESTS) {
|
||||
tap.expect_eq (util::view { t.init }.slice (t.a, t.b), t.result, "slice; %s", t.message);
|
||||
}
|
||||
}
|
||||
|
||||
return tap.status ();
|
||||
}
|
||||
|
18
view.hpp
18
view.hpp
@ -308,6 +308,24 @@ namespace util {
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// slices a view using python indexing semantics. ie,
|
||||
// "abc".slice(0, 3) == "abc"
|
||||
// "abc".slice(0, -1) == "abc"
|
||||
// "abc".slice(0, -2) == "ab"
|
||||
constexpr auto
|
||||
slice (int a, int b)
|
||||
{
|
||||
auto first = m_begin;
|
||||
auto last = m_begin;
|
||||
|
||||
std::advance (first, a < 0 ? size () + a + 1 : a);
|
||||
std::advance (last, b < 0 ? size () + b + 1 : b);
|
||||
|
||||
return util::view { first, last };
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
constexpr auto
|
||||
consume (int count) const
|
||||
|
Loading…
Reference in New Issue
Block a user