From 239de5c072348b418feb20dbd3e32783fd670c59 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 11 Oct 2017 17:56:42 -0700 Subject: [PATCH] ircd: Add constexpr string_view constructors and literal _sv operator. --- include/ircd/string_view.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/include/ircd/string_view.h b/include/ircd/string_view.h index 66972a167..7902fe684 100644 --- a/include/ircd/string_view.h +++ b/include/ircd/string_view.h @@ -41,6 +41,8 @@ namespace ircd bool operator!(const string_view &); bool defined(const string_view &); bool null(const string_view &); + + constexpr string_view operator ""_sv(const char *const literal, const size_t size); } /// Customized std::string_view (experimental TS / C++17) @@ -136,7 +138,7 @@ struct ircd::string_view // (non-standard) our array based constructor template - string_view(const std::array &array) + constexpr string_view(const std::array &array) :string_view { array.data(), std::find(array.begin(), array.end(), '\0') @@ -144,7 +146,7 @@ struct ircd::string_view // (non-standard) our buffer based constructor template - string_view(const char (&buf)[SIZE]) + constexpr string_view(const char (&buf)[SIZE]) :string_view { buf, std::find(buf, buf + SIZE, '\0') @@ -156,19 +158,26 @@ struct ircd::string_view //{} // Required due to current instability in stdlib - string_view(const std::experimental::fundamentals_v1::basic_string_view &bsv) + constexpr string_view(const std::experimental::fundamentals_v1::basic_string_view &bsv) :std::string_view{bsv} {} /// Our default constructor sets the elements to 0 for best behavior by /// defined() and null() et al. - string_view() + constexpr string_view() :std::string_view{nullptr, 0} {} using std::string_view::string_view; }; +/// Compile-time conversion from a string literal into a string_view. +constexpr ircd::string_view +ircd::operator ""_sv(const char *const literal, const size_t size) +{ + return string_view{literal, size}; +} + template struct ircd::vector_view {