24namespace CppSpec::Util {
38 static constexpr std::string_view keywords[] = {
"enum ",
"class ",
"struct ",
"union "};
39 const auto continues_identifier = [](
char c) {
40 return (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9') || c ==
'_';
43 stripped.reserve(name.size());
45 while (i < name.size()) {
46 const bool at_word_start = i == 0 || !continues_identifier(name[i - 1]);
47 const auto* keyword = std::ranges::find_if(
48 keywords, [&](std::string_view word) {
return at_word_start && name.substr(i).starts_with(word); });
49 if (keyword != std::ranges::end(keywords)) {
67inline std::string demangle(
const char* mangled) {
69 std::unique_ptr<
char[], void (*)(
void*)> result{
70 abi::__cxa_demangle(mangled,
nullptr,
nullptr, &status),
73 return (status == 0) ? result.get() : mangled;
87 static_assert(result,
"Assertion failed (see below for more information)");
88 static bool const value = result;
105 requires std::ranges::range<C>;
106 { c.max_size() } -> std::integral;
107 { c.empty() } -> std::convertible_to<bool>;
121 { os << obj } -> std::same_as<std::ostream&>;
128 std::is_function_v<C> ||
132 std::is_convertible_v<C, std::function<void()>> ||
137 requires std::is_object_v<C>;
138 { func() } -> std::invocable<>;
153 std::same_as<std::remove_const_t<std::remove_pointer_t<std::decay_t<T>>>,
char>;
162concept is_wide_character = std::same_as<std::remove_cv_t<T>,
wchar_t> || std::same_as<std::remove_cv_t<T>,
char8_t> ||
163 std::same_as<std::remove_cv_t<T>,
char16_t> || std::same_as<std::remove_cv_t<T>,
char32_t>;
176 std::is_pointer_v<T> && (std::same_as<std::remove_const_t<std::remove_pointer_t<T>>,
signed char> ||
177 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>,
unsigned char> ||
178 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>,
wchar_t> ||
179 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>,
char8_t> ||
180 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>,
char16_t> ||
181 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>,
char32_t>);
210template <
typename A,
typename E>
213 (std::ranges::range<A> &&
214 (std::ranges::range<E> ||
215 (
is_string_like<E> && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>,
char>)));
224concept is_cmp_integer = std::integral<std::remove_cv_t<T>> && !std::same_as<std::remove_cv_t<T>,
bool> &&
231template <
typename A,
typename B>
232constexpr bool less(
const A& a,
const B& b) {
234 return std::cmp_less(a, b);
243template <
typename A,
typename B>
246 return std::cmp_less_equal(a, b);
255template <
typename A,
typename B>
256constexpr bool greater(
const A& a,
const B& b) {
258 return std::cmp_greater(a, b);
267template <
typename A,
typename B>
270 return std::cmp_greater_equal(a, b);
277concept not_c_string = !std::is_same_v<T, const char*> && !std::is_same_v<T, char*> &&
278 !std::is_convertible_v<T, const char*> && !std::is_convertible_v<T, char*>;
290[[nodiscard]]
inline std::string
join(std::ranges::range
auto& iterable,
const std::string& separator =
"") {
291 std::ostringstream oss;
293 for (
auto& thing : iterable) {
303[[nodiscard]]
inline std::string join_endl(std::ranges::range
auto&& iterable) {
304 std::ostringstream oss;
306 for (
auto& thing : iterable) {
336template <Util::is_
string_like T>
337[[nodiscard]]
constexpr std::optional<std::string_view>
string_view_of(
const T& text) {
338 if constexpr (std::is_array_v<T>) {
339 return std::string_view(std::ranges::begin(text), std::ranges::find(text,
'\0'));
341 if (text ==
nullptr) {
344 return std::string_view(text);
346 return std::string_view(text);
350std::optional<std::string_view> string_view_of(
const std::string&& text) =
delete;
Checks whether to_start_with and to_end_with compare an actual value of type A with an expected one o...
Definition util.hpp:211
Checks whether T is a C string: a pointer to char, const or not, or a char array, which decays to one...
Definition util.hpp:152
Checks whether T is an integer type that std::cmp_less and its kin accept: every integral type but bo...
Definition util.hpp:224
Concept for checking whether T is a container type.
Definition util.hpp:104
Checks whether T is a pointer to a character type other than char: signed char, unsigned char,...
Definition util.hpp:175
Checks whether T can be streamed.
Definition util.hpp:120
Checks whether T is text that CppSpec::string_view_of() can view: a C string, or a type that converts...
Definition util.hpp:201
Checks whether T is a character type that std::ostream does not print as a character: wchar_t,...
Definition util.hpp:162
Helper class for static assertions that has a built-in error string.
Definition util.hpp:86
constexpr bool is_optional_v
Checks whether T is a std::optional.
Definition util.hpp:189
std::string demangle(const char *name)
Definition util.hpp:77
constexpr std::optional< std::string_view > string_view_of(const T &text)
View text as a std::string_view, the one form the string matchers and the printer compare and print.
Definition util.hpp:337
constexpr bool less(const A &a, const B &b)
a < b, by value when both are integers (std::cmp_less, so a negative signed value is below any unsign...
Definition util.hpp:232
std::string strip_type_keywords(std::string_view name)
Removes the enum , class , struct and union keywords MSVC writes before every type in a type name.
Definition util.hpp:37
constexpr bool greater_equal(const A &a, const B &b)
a >= b, by value when both are integers (std::cmp_greater_equal), and with >= otherwise.
Definition util.hpp:268
constexpr bool greater(const A &a, const B &b)
a > b, by value when both are integers (std::cmp_greater), and with > otherwise.
Definition util.hpp:256
constexpr bool less_equal(const A &a, const B &b)
a <= b, by value when both are integers (std::cmp_less_equal), and with <= otherwise.
Definition util.hpp:244
std::string join(std::ranges::range auto &iterable, const std::string &separator="")
Implode a string.
Definition util.hpp:290