diff --git a/test/view.cpp b/test/view.cpp index e0f99ec8..0e640e21 100644 --- a/test/view.cpp +++ b/test/view.cpp @@ -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 (&*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 (&*s.cbegin (), &*s.cend ()), + "string/pointer-view equality"); + } + + // comparator tests + { static const struct { util::view a; util::view 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 (); } diff --git a/view.hpp b/view.hpp index b5bbbc0a..abc81b06 100644 --- a/view.hpp +++ b/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