From 436df2952f38613a06a30e1278c908ee5ccab3bd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 17 Mar 2017 17:59:30 +1100 Subject: [PATCH] view: add make_view specialisations for std::string --- test/view.cpp | 8 ++++---- view.hpp | 22 ++++++++++++++++++++++ view.ipp | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/test/view.cpp b/test/view.cpp index a3322cec..0f8cf2ec 100644 --- a/test/view.cpp +++ b/test/view.cpp @@ -9,10 +9,10 @@ main (int, char**) const std::string s = "this is a test string"; const std::string t = "not the same string"; - tap.expect_eq (s, util::view (s.cbegin (), s.cend ()), "string-const_iterator inequality comparison"); - tap.expect_eq (s, util::view (&*s.cbegin (), &*s.cend ()), "string-pointer equality comparison"); - - tap.expect_neq (t, util::make_view (s), "string-view inequality"); + 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"); return tap.status (); } diff --git a/view.hpp b/view.hpp index 4bfb1fd7..78db55ee 100644 --- a/view.hpp +++ b/view.hpp @@ -22,6 +22,7 @@ #include #include +#include namespace util { template @@ -86,6 +87,27 @@ namespace util { return view {first, last}; } + // string conversions + view make_view (const char *str); + view make_view (char *str); + + template + view + make_view (const std::basic_string&); + + template + view + make_view (std::basic_string&); + + template + view + make_view (const std::basic_string&&) = delete; + + template + view + make_view (std::basic_string&&) = delete; + + // boolean operationrs template bool operator!= (view, view); diff --git a/view.ipp b/view.ipp index a14691df..dc42b71b 100644 --- a/view.ipp +++ b/view.ipp @@ -213,3 +213,44 @@ util::make_cview (const T &t) { return util::view { std::cbegin (t), std::cend (t) }; } + + +/////////////////////////////////////////////////////////////////////////////// +inline +util::view +util::make_view (const char *str) +{ + return { str, str + strlen (str) }; +} + +//----------------------------------------------------------------------------- +inline +util::view +util::make_view (char *str) +{ + return { str, str + strlen (str) }; +} + + +//----------------------------------------------------------------------------- +template +util::view +util::make_view (const std::basic_string &str) +{ + return { + std::data (str), + std::data (str) + std::size (str) + }; +} + + +//----------------------------------------------------------------------------- +template +util::view +util::make_view (std::basic_string &str) +{ + return { + std::data (str), + std::data (str) + std::size (str) + }; +}