soi-header/include/bits/soi-pretty.hpp

105 lines
2.4 KiB
C++

// Copyright Johannes Kapfhammer 2019-2022.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// pretty pretty_printing with c++
//
#ifndef SOI_PRETTY
#define SOI_PRETTY
#include "prettyprint.hpp"
namespace soi {
namespace prettyprint {
template <typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, const bool &x) {
return stream << (x ? "true" : "false");
}
namespace detail {
template <typename TChar, typename TCharTraits>
void escape_char(std::basic_ostream<TChar, TCharTraits> &stream, char c) {
switch (c) {
case '\a':
stream << "\\a";
break;
case '\b':
stream << "\\b";
break;
case '\t':
stream << "\\t";
break;
case '\n':
stream << "\\n";
break;
case '\v':
stream << "\\v";
break;
case '\f':
stream << "\\f";
break;
case '\r':
stream << "\\r";
break;
case '\e':
stream << "\\e";
break;
case '\"':
stream << "\\\"";
break;
case '\'':
stream << "\\'";
break;
case '\?':
stream << "\\?";
break;
case '\\':
stream << "\\\\";
break;
default:
stream << c;
}
}
} // namespace detail
template <typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream,
const std::string &x) {
stream << "\"";
for (char c : x)
detail::escape_char(stream, c);
return stream << "\"";
}
template <typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, char c) {
stream << "'";
detail::escape_char(stream, c);
return stream << "'";
}
template <typename T, typename TChar, typename TCharTraits>
inline typename std::enable_if<!::soi::prettyprint::is_container<T>::value,
std::basic_ostream<TChar, TCharTraits> &>::type
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
return stream << x;
}
template <typename T, typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
return ::soi::prettyprint::pretty_print(stream, x);
}
} // namespace prettyprint
} // namespace soi
#endif // SOI_PRETTY