10#include <forward_list>
23#include <unordered_map>
24#include <unordered_set>
28#include "prettyprint.hpp"
37namespace pretty_print {
39struct is_container<std::optional<T>> : std::false_type {};
47template <
typename T,
template <
typename...>
class Template>
48inline constexpr bool is_specialization_of =
false;
50template <
template <
typename...>
class Template,
typename... Args>
51inline constexpr bool is_specialization_of<Template<Args...>, Template> =
true;
54inline constexpr bool is_std_array =
false;
56template <
typename T, std::
size_t N>
57inline constexpr bool is_std_array<std::array<T, N>> =
true;
60inline constexpr bool is_std_span =
false;
62template <
typename T, std::
size_t Extent>
63inline constexpr bool is_std_span<std::span<T, Extent>> =
true;
73 std::is_array_v<T> || is_std_array<T> || is_std_span<T> || is_specialization_of<T, std::vector> ||
74 is_specialization_of<T, std::deque> || is_specialization_of<T, std::list> ||
75 is_specialization_of<T, std::forward_list> || is_specialization_of<T, std::set> ||
76 is_specialization_of<T, std::multiset> || is_specialization_of<T, std::unordered_set> ||
77 is_specialization_of<T, std::unordered_multiset> || is_specialization_of<T, std::map> ||
78 is_specialization_of<T, std::multimap> || is_specialization_of<T, std::unordered_map> ||
79 is_specialization_of<T, std::unordered_multimap> || is_specialization_of<T, std::pair> ||
80 is_specialization_of<T, std::tuple>;
95 : std::bool_constant<std::same_as<T, bool> || std::same_as<T, signed char> || std::same_as<T, unsigned char> ||
96 is_wide_character<T> || is_c_string<T> || is_non_text_character_pointer<T> ||
97 (std::is_enum_v<T> && !is_streamable<T>)> {};
102template <
typename A,
typename B>
103struct prints_through_pretty<std::pair<A, B>> : std::bool_constant<prints_through_pretty<std::remove_cv_t<A>>::value ||
104 prints_through_pretty<std::remove_cv_t<B>>::value> {
107template <
typename... Ts>
109 : std::bool_constant<(prints_through_pretty<std::remove_cv_t<Ts>>::value || ...)> {};
115 (!std::same_as<std::remove_cv_t<std::ranges::range_value_t<const R>>, R>) &&
133template <
typename A,
typename E>
146 [[nodiscard]] std::string name(
const std::string& name)
const;
147 [[nodiscard]] std::string name_to_sentence(
const std::string& name)
const;
148 static std::string split_words(
const std::string& sym);
149 static std::string underscore(
const std::string& camel_cased_word);
150 static std::string last(
const std::string& s,
char delim);
151 static std::string improve_hash_formatting(
const std::string& inspect_string);
153 template <
typename T>
156 static std::string
to_word(
const T& item);
158 template <
typename T>
159 static std::string
to_word(
const T& item);
161 template <
typename T>
163 static std::string
to_word(
const T& item);
165 template <
typename T>
167 static std::string
to_word(
const T& item);
169 template <
typename T>
171 static std::string
to_word(
const T& item);
173 template <
typename T>
175 static std::string
to_word(
const T& item);
177 template <
typename T>
178 static std::string
to_word(
const std::optional<T>& item);
180 template <
typename T>
181 static std::string to_word_type(
const T& item);
183 template <
typename A,
typename E>
190 static std::string
to_sentence(
const std::vector<T>& words);
192 template <
typename T>
193 static std::string inspect_object(
const T&
object);
204 static std::string
address_of(
const void* address) {
return std::format(
"{}", address); }
217 std::vector<std::string> words;
218 for (
const auto&
object : objects) {
219 words.push_back(
to_word(
object));
222 std::stringstream ss;
223 switch (words.size()) {
227 ss <<
" " << words[0];
230 ss <<
" " << words[0] <<
" and " << words[1];
234 for (
size_t i = 0; i < words.size() - 1; ++i) {
240 ss <<
", and " << words.back();
277 return text ? std::string(*text) : std::string(
"nullptr");
279 std::ostringstream oss;
287 return item ?
"true" :
"false";
293 return std::to_string(item);
298 return std::to_string(item);
320 std::stringstream ss;
322 ss <<
"#<" << Util::demangle(
typeid(item).name()) <<
":" <<
address_of(std::addressof(item)) <<
">";
338 return std::to_string(
static_cast<std::uint_least32_t
>(item));
354 if (item ==
nullptr) {
374 const auto& delimiters = pretty_print::delimiters<T, char>::values;
375 std::string word = delimiters.prefix !=
nullptr ? delimiters.prefix :
"";
377 auto append = [&](
const auto& element) {
378 if (!first && delimiters.delimiter !=
nullptr) {
379 word += delimiters.delimiter;
384 if constexpr (std::ranges::input_range<const T>) {
385 for (
const auto& element : item) {
387 if constexpr (std::same_as<std::remove_cv_t<std::ranges::range_value_t<const T>>,
bool>) {
388 append(
static_cast<bool>(element));
394 std::apply([&](
const auto&... elements) { (append(elements), ...); }, item);
396 if (delimiters.postfix !=
nullptr) {
397 word += delimiters.postfix;
415 using Widest = std::conditional_t<std::is_signed_v<std::underlying_type_t<T>>,
long long,
unsigned long long>;
416 return std::format(
"{}({})",
Util::demangle(
typeid(T).name()),
static_cast<Widest
>(std::to_underlying(item)));
431 if (!item.has_value()) {
432 return "std::nullopt";
435 return "std::optional(" +
to_word(*item) +
")";
452template <
typename A,
typename E>
455 if (description.empty()) {
456 return "[No description]";
462inline std::string Pretty::to_word_type(
const T& item) {
463 std::string word =
to_word(item);
470inline std::string Pretty::name_to_sentence(
const std::string& n)
const {
471 return split_words(name(n));
474inline std::string Pretty::name(
const std::string& name)
const {
476 return last(name,
':');
481inline std::string Pretty::split_words(
const std::string& sym) {
482 return std::regex_replace(sym, std::regex(
"_"),
" ");
485inline std::string Pretty::underscore(
const std::string& word) {
486 std::string str = std::regex_replace(word, std::regex(
"([A-Z]+)([A-Z][a-z])"),
"$1_$2");
487 str = std::regex_replace(str, std::regex(
"([a-z\\d])([A-Z])"),
"$1_$2");
488 str = std::regex_replace(str, std::regex(
"-"),
"_");
489 std::ranges::transform(str, str.begin(), ::tolower);
494inline std::string Pretty::last(
const std::string& s,
const char delim) {
495 std::vector<std::string> elems;
496 std::stringstream ss(s);
498 while (getline(ss, item, delim)) {
500 elems.push_back(item);
503 return elems.empty() ? std::string() : elems.back();
506inline std::string Pretty::improve_hash_formatting(
const std::string& inspect_string) {
507 return std::regex_replace(inspect_string, std::regex(
"(\\S)=>(\\S)"),
"$1 => $2");
518inline std::string Pretty::inspect_object(
const O& o) {
521 constexpr std::string_view constness = std::is_const_v<std::remove_pointer_t<std::decay_t<O>>> ?
"const " :
"";
522 const std::string type = std::is_array_v<O> ? std::format(
"{}char [{}]", constness, std::extent_v<O>)
523 : std::format(
"{}char *", constness);
525 return text ? std::format(
"({}) => \"{}\"", type, *text) : std::format(
"({}) => nullptr", type);
539inline std::string Pretty::inspect_object<std::string>(
const std::string& o) {
540 return std::format(
"(std::string) => \"{}\"", o);
544inline std::string Pretty::inspect_object<std::string_view>(
const std::string_view& o) {
545 return std::format(
"(std::string_view) => \"{}\"", o);
the base class for all Matcher classes and objects
Definition matcher_base.hpp:77
virtual std::string description()
Get the description of the Matcher.
Definition matcher_base.hpp:200
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 a pointer to a character type other than char: signed char, unsigned char,...
Definition util.hpp:175
Checks whether T is a standard container, a std::span, a std::pair, a std::tuple or a built-in array:...
Definition pretty_matchers.hpp:72
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
Checks whether T is a container, pair or tuple that Pretty prints element by element,...
Definition pretty_matchers.hpp:126
A helper base class that assists in pretty-printing various objects.
Definition pretty_matchers.hpp:144
static std::string to_word(const T &item)
Formats an object as a string when operator<< is available.
Definition pretty_matchers.hpp:273
static std::string address_of(const void *address)
Formats an address as 0x and lowercase hex digits, the same on every platform.
Definition pretty_matchers.hpp:204
static std::string to_sentence(const T &item)
Take a single object and format it as a sentance.
Definition pretty_matchers.hpp:255
Whether T prints differently through Pretty than through operator<<, so a container holding it is pri...
Definition pretty_matchers.hpp:97
Utility functions and classes.
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