From 9e2910aa1f2b037ccc306fc5c0a9acecd51b26c3 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 27 Sep 2017 18:26:43 -0700 Subject: [PATCH] ircd: Attempt to add these hacks to string_view. --- include/ircd/string_view.h | 42 +++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/include/ircd/string_view.h b/include/ircd/string_view.h index dbe38aec6..1170379b9 100644 --- a/include/ircd/string_view.h +++ b/include/ircd/string_view.h @@ -35,6 +35,8 @@ namespace ircd template auto ctype(const string_view &s); bool operator!(const string_view &); + bool defined(const string_view &); + bool null(const string_view &); } /// Customized std::string_view (experimental TS / C++17) @@ -52,6 +54,30 @@ struct ircd::string_view return !empty(); } + /// (non-standard) When data() != nullptr we consider the string defined + /// downstream in this project wrt JS/JSON. This is the bit of information + /// we're deciding on for defined|undefined. If this string_view is + /// constructed from a literal "" we must assert that inputs a valid pointer + /// in the std::string_view with length 0; stdlib can't optimize that with + /// a nullptr replacement. + bool undefined() const + { + return data() == nullptr; + } + + bool defined() const + { + return !undefined(); + } + + /// (non-standard) After using data() == nullptr for undefined, we're fresh + /// out of legitimate bits here to represent the null type string. In this + /// case we expect a hack pointer of 0x1 which will mean JS null + bool null() const + { + return data() == reinterpret_cast(0x1); + } + // (non-standard) our faux insert stub // Tricks boost::spirit into thinking this is mutable string (hint: it's not). // Instead, the raw[] directive in Qi grammar will use the iterator constructor only. @@ -130,8 +156,10 @@ struct ircd::string_view :std::string_view{bsv} {} + /// Our default constructor sets the elements to 0 for best behavior by + /// defined() and null() et al. string_view() - :std::string_view{} + :std::string_view{nullptr, 0} {} using std::string_view::string_view; @@ -271,6 +299,18 @@ ircd::operator!(const string_view &str) return str.empty(); } +inline bool +ircd::null(const string_view &str) +{ + return str.null(); +} + +inline bool +ircd::defined(const string_view &str) +{ + return str.defined(); +} + template auto ircd::ctype(const string_view &s)