0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-30 04:38:52 +02:00

ircd: Attempt to add these hacks to string_view.

This commit is contained in:
Jason Volk 2017-09-27 18:26:43 -07:00
parent dc1281a17c
commit 9e2910aa1f

View file

@ -35,6 +35,8 @@ namespace ircd
template<int (&test)(int) = std::isprint> 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<const char *>(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<int (&test)(int)>
auto
ircd::ctype(const string_view &s)