0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-18 16:00:57 +01:00

ircd: Add a c_str() to string_view which asserts for null termination.

This commit is contained in:
Jason Volk 2017-12-12 13:06:47 -07:00
parent 49b32de864
commit dff9be2df3

View file

@ -80,6 +80,21 @@ struct ircd::string_view
return !undefined(); return !undefined();
} }
/// (non-standard) string_view's have no guarantee to be null terminated
/// and most likely aren't. The std::string_view does not offer the
/// c_str() function because using it is overwhelmingly likely to be wrong.
/// Nevertheless if our developer is certain their view is of a null
/// terminated string where the terminator is one past the end they can
/// invoke this function rather than data() to assert their intent. Note
/// that this assertion is still not foolproof because reading beyond
/// size() might still be incorrect whether or not a null is found there
/// and there is nothing else we can do. The developer must be sure.
auto c_str() const
{
assert(!data() || data()[size()] == '\0');
return data();
}
/// (non-standard) After using data() == nullptr for undefined, we're fresh /// (non-standard) After using data() == nullptr for undefined, we're fresh
/// out of legitimate bits here to represent the null type string. In this /// 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 /// case we expect a hack pointer of 0x1 which will mean JS null