2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-09 19:38:47 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_STRING_VIEW_H
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
struct string_view;
|
2017-09-29 23:02:22 +02:00
|
|
|
|
2019-08-15 08:16:38 +02:00
|
|
|
constexpr size_t _constexpr_strlen(const char *) noexcept;
|
|
|
|
constexpr const char *data(const string_view &) noexcept;
|
|
|
|
constexpr size_t size(const string_view &) noexcept;
|
2017-11-06 21:09:18 +01:00
|
|
|
bool empty(const string_view &);
|
2017-09-09 19:38:47 +02:00
|
|
|
bool operator!(const string_view &);
|
2017-09-28 03:26:43 +02:00
|
|
|
bool defined(const string_view &);
|
|
|
|
bool null(const string_view &);
|
2017-10-12 02:56:42 +02:00
|
|
|
|
|
|
|
constexpr string_view operator ""_sv(const char *const literal, const size_t size);
|
2017-09-09 19:38:47 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 02:21:26 +01:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template<> struct std::hash<ircd::string_view>;
|
|
|
|
template<> struct std::less<ircd::string_view>;
|
|
|
|
template<> struct std::equal_to<ircd::string_view>;
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Customized std::string_view (experimental TS / C++17)
|
|
|
|
///
|
|
|
|
/// This class adds iterator-based (char*, char*) construction to std::string_view which otherwise
|
|
|
|
/// takes traditional (char*, size_t) arguments. This allows boost::spirit grammars to create
|
|
|
|
/// string_view's using the raw[] directive achieving zero-copy/zero-allocation parsing.
|
|
|
|
///
|
2017-09-09 19:38:47 +02:00
|
|
|
struct ircd::string_view
|
|
|
|
:std::string_view
|
|
|
|
{
|
2017-09-25 01:58:58 +02:00
|
|
|
// (non-standard)
|
|
|
|
explicit operator bool() const
|
|
|
|
{
|
|
|
|
return !empty();
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:43:30 +02:00
|
|
|
/// CAREFUL. THIS IS ON PURPOSE. By relaxing this conversion we're reducing
|
|
|
|
/// the amount of explicit std::string() pollution when calling out to code
|
|
|
|
/// which doesn't support string_view *yet* (keyword: yet). When it does
|
|
|
|
/// support string_view then this conversion won't happen, and we don't
|
|
|
|
/// have to change anything in our code. The price here is that an
|
|
|
|
/// occasional regression analysis on where these conversions are occurring
|
|
|
|
/// should be periodically performed to make sure there are no unwanted
|
|
|
|
/// silent accidents.
|
|
|
|
operator std::string() const
|
|
|
|
{
|
|
|
|
return std::string(cbegin(), cend());
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:26:43 +02:00
|
|
|
/// (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();
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:06:47 +01:00
|
|
|
/// (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();
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:26:43 +02:00
|
|
|
/// (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);
|
|
|
|
}
|
|
|
|
|
2017-09-09 19:38:47 +02:00
|
|
|
// (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.
|
|
|
|
// __attribute__((error("string_view is not insertable (hint: use raw[] directive)")))
|
|
|
|
void insert(const iterator &, const char &)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// (non-standard) our iterator-based assign
|
|
|
|
string_view &assign(const char *const &begin, const char *const &end)
|
|
|
|
{
|
2017-11-01 23:54:20 +01:00
|
|
|
this->~string_view();
|
2018-09-17 05:01:55 +02:00
|
|
|
new (this) string_view{begin, end};
|
2017-09-09 19:38:47 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (non-standard) intuitive wrapper for remove_suffix.
|
|
|
|
// Unlike std::string, we can cheaply involve a reference to the removed character
|
|
|
|
// which still exists.
|
|
|
|
const char &pop_back()
|
|
|
|
{
|
|
|
|
const char &ret(back());
|
|
|
|
remove_suffix(1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (non-standard) intuitive wrapper for remove_prefix.
|
|
|
|
// Unlike std::string, we can cheaply involve a reference to the removed character
|
|
|
|
// which still exists.
|
|
|
|
const char &pop_front()
|
|
|
|
{
|
|
|
|
const char &ret(front());
|
|
|
|
remove_prefix(1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-15 21:05:43 +02:00
|
|
|
/// (non-standard) resize viewer
|
|
|
|
void resize(const size_t &count)
|
|
|
|
{
|
|
|
|
*this = string_view{data(), data() + count};
|
|
|
|
}
|
|
|
|
|
2017-09-09 19:38:47 +02:00
|
|
|
// (non-standard) our iterator-based constructor
|
2019-08-15 08:16:38 +02:00
|
|
|
constexpr string_view(const char *const &begin, const char *const &end) noexcept
|
2018-09-17 05:01:55 +02:00
|
|
|
:string_view{begin, size_t(end - begin)}
|
|
|
|
{
|
|
|
|
assert(begin <= end);
|
|
|
|
}
|
2017-09-09 19:38:47 +02:00
|
|
|
|
2017-09-15 21:05:43 +02:00
|
|
|
// (non-standard) our iterator-based constructor
|
|
|
|
string_view(const std::string::const_iterator &begin, const std::string::const_iterator &end)
|
|
|
|
:string_view{&*begin, &*end}
|
|
|
|
{}
|
|
|
|
|
2017-09-09 19:38:47 +02:00
|
|
|
// (non-standard) our array based constructor
|
2019-05-08 08:51:15 +02:00
|
|
|
template<size_t SIZE> constexpr
|
|
|
|
__attribute__((always_inline))
|
2019-08-15 08:16:38 +02:00
|
|
|
string_view(const std::array<char, SIZE> &array) noexcept
|
2017-09-09 19:38:47 +02:00
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
array.data(), std::find(array.begin(), array.end(), '\0')
|
|
|
|
}{}
|
|
|
|
|
|
|
|
// (non-standard) our buffer based constructor
|
2019-05-08 08:51:15 +02:00
|
|
|
template<size_t SIZE> constexpr
|
|
|
|
__attribute__((always_inline))
|
2019-08-15 08:16:38 +02:00
|
|
|
string_view(const char (&buf)[SIZE]) noexcept
|
2017-09-09 19:38:47 +02:00
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
buf, std::find(buf, buf + SIZE, '\0')
|
|
|
|
}{}
|
|
|
|
|
2019-08-15 08:16:38 +02:00
|
|
|
constexpr string_view(const char *const &start, const size_t &size) noexcept
|
2018-09-17 05:01:55 +02:00
|
|
|
:std::string_view{start, size}
|
|
|
|
{}
|
2017-11-01 23:54:20 +01:00
|
|
|
|
2019-08-15 08:16:38 +02:00
|
|
|
constexpr string_view(const char *const &start) noexcept
|
2018-09-17 05:01:55 +02:00
|
|
|
:std::string_view{start, _constexpr_strlen(start)}
|
|
|
|
{}
|
2017-11-01 23:54:20 +01:00
|
|
|
|
2019-08-15 08:16:38 +02:00
|
|
|
string_view(const std::string &string) noexcept
|
2018-09-17 05:01:55 +02:00
|
|
|
:string_view{string.data(), string.size()}
|
2017-11-01 23:54:20 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
constexpr string_view(const std::string_view &sv)
|
|
|
|
:std::string_view{sv}
|
2017-09-09 19:38:47 +02:00
|
|
|
{}
|
|
|
|
|
2017-09-28 03:26:43 +02:00
|
|
|
/// Our default constructor sets the elements to 0 for best behavior by
|
|
|
|
/// defined() and null() et al.
|
2017-10-12 02:56:42 +02:00
|
|
|
constexpr string_view()
|
2018-03-06 12:51:51 +01:00
|
|
|
:std::string_view{}
|
2017-09-09 19:38:47 +02:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-11-16 02:21:26 +01:00
|
|
|
/// Specialization for std::hash<> participation
|
|
|
|
template<>
|
|
|
|
struct std::hash<ircd::string_view>
|
|
|
|
:std::hash<std::string_view>
|
2017-09-09 19:38:47 +02:00
|
|
|
{
|
2017-11-16 02:21:26 +01:00
|
|
|
using std::hash<std::string_view>::operator();
|
|
|
|
using std::hash<std::string_view>::hash;
|
2017-09-09 19:38:47 +02:00
|
|
|
};
|
|
|
|
|
2017-11-16 02:21:26 +01:00
|
|
|
/// Specialization for std::less<> participation
|
|
|
|
template<>
|
|
|
|
struct std::less<ircd::string_view>
|
|
|
|
:std::less<std::string_view>
|
2017-09-09 19:38:47 +02:00
|
|
|
{
|
2017-11-16 02:21:26 +01:00
|
|
|
using std::less<std::string_view>::operator();
|
|
|
|
using std::less<std::string_view>::less;
|
2017-09-09 19:38:47 +02:00
|
|
|
};
|
|
|
|
|
2017-11-16 02:21:26 +01:00
|
|
|
/// Specialization for std::equal_to<> participation
|
2017-09-09 19:38:47 +02:00
|
|
|
template<>
|
2017-11-16 02:21:26 +01:00
|
|
|
struct std::equal_to<ircd::string_view>
|
|
|
|
:std::equal_to<std::string_view>
|
2017-09-09 19:38:47 +02:00
|
|
|
{
|
2017-11-16 02:21:26 +01:00
|
|
|
using std::equal_to<std::string_view>::operator();
|
|
|
|
using std::equal_to<std::string_view>::equal_to;
|
2017-10-16 06:20:02 +02:00
|
|
|
};
|
2017-09-09 19:38:47 +02:00
|
|
|
|
2017-11-16 02:21:26 +01:00
|
|
|
/// 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};
|
|
|
|
}
|
|
|
|
|
2017-09-09 19:38:47 +02:00
|
|
|
inline bool
|
|
|
|
ircd::operator!(const string_view &str)
|
2017-11-06 21:09:18 +01:00
|
|
|
{
|
|
|
|
return empty(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::empty(const string_view &str)
|
2017-09-09 19:38:47 +02:00
|
|
|
{
|
|
|
|
return str.empty();
|
|
|
|
}
|
2017-09-14 22:01:06 +02:00
|
|
|
|
2017-09-28 03:26:43 +02:00
|
|
|
inline bool
|
|
|
|
ircd::null(const string_view &str)
|
|
|
|
{
|
|
|
|
return str.null();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::defined(const string_view &str)
|
|
|
|
{
|
|
|
|
return str.defined();
|
|
|
|
}
|
|
|
|
|
2018-06-20 05:30:29 +02:00
|
|
|
constexpr size_t
|
2017-09-30 07:58:04 +02:00
|
|
|
ircd::size(const string_view &str)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2017-09-30 07:58:04 +02:00
|
|
|
{
|
|
|
|
return str.size();
|
|
|
|
}
|
|
|
|
|
2018-06-20 05:30:29 +02:00
|
|
|
constexpr const char *
|
2017-11-16 02:21:26 +01:00
|
|
|
ircd::data(const string_view &str)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2017-11-16 02:21:26 +01:00
|
|
|
{
|
|
|
|
return str.data();
|
|
|
|
}
|
2018-09-17 05:01:55 +02:00
|
|
|
|
2019-08-06 01:19:55 +02:00
|
|
|
[[gnu::pure]]
|
2018-09-17 05:01:55 +02:00
|
|
|
constexpr size_t
|
|
|
|
ircd::_constexpr_strlen(const char *const s)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-09-17 05:01:55 +02:00
|
|
|
{
|
|
|
|
const char *e(s);
|
|
|
|
if(e) for(; *e; ++e);
|
|
|
|
return e - s;
|
|
|
|
}
|