view: add make_view specialisations for std::string

This commit is contained in:
Danny Robson 2017-03-17 17:59:30 +11:00
parent cf36b5e1f9
commit 436df2952f
3 changed files with 67 additions and 4 deletions

View File

@ -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<std::string::const_iterator> (s.cbegin (), s.cend ()), "string-const_iterator inequality comparison");
tap.expect_eq (s, util::view<const char*> (&*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<const char*> (&*s.cbegin (), &*s.cend ()), "string/pointer-view equality");
return tap.status ();
}

View File

@ -22,6 +22,7 @@
#include <cstdlib>
#include <ostream>
#include <string>
namespace util {
template <typename T>
@ -86,6 +87,27 @@ namespace util {
return view<T> {first, last};
}
// string conversions
view<const char*> make_view (const char *str);
view<char*> make_view (char *str);
template <typename CharT, typename TraitsT, typename AllocT>
view<const CharT*>
make_view (const std::basic_string<CharT,TraitsT,AllocT>&);
template <typename CharT, typename TraitsT, typename AllocT>
view<CharT*>
make_view (std::basic_string<CharT,TraitsT,AllocT>&);
template <typename CharT, typename TraitsT, typename AllocT>
view<const CharT*>
make_view (const std::basic_string<CharT,TraitsT,AllocT>&&) = delete;
template <typename CharT, typename TraitsT, typename AllocT>
view<CharT*>
make_view (std::basic_string<CharT,TraitsT,AllocT>&&) = delete;
// boolean operationrs
template <typename T>
bool operator!= (view<T>, view<T>);

View File

@ -213,3 +213,44 @@ util::make_cview (const T &t)
{
return util::view<decltype(std::cbegin (t))> { std::cbegin (t), std::cend (t) };
}
///////////////////////////////////////////////////////////////////////////////
inline
util::view<const char*>
util::make_view (const char *str)
{
return { str, str + strlen (str) };
}
//-----------------------------------------------------------------------------
inline
util::view<char*>
util::make_view (char *str)
{
return { str, str + strlen (str) };
}
//-----------------------------------------------------------------------------
template <typename CharT, typename TraitsT, typename AllocT>
util::view<const CharT*>
util::make_view (const std::basic_string<CharT,TraitsT,AllocT> &str)
{
return {
std::data (str),
std::data (str) + std::size (str)
};
}
//-----------------------------------------------------------------------------
template <typename CharT, typename TraitsT, typename AllocT>
util::view<CharT*>
util::make_view (std::basic_string<CharT,TraitsT,AllocT> &str)
{
return {
std::data (str),
std::data (str) + std::size (str)
};
}