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

105 lines
2.4 KiB
C++
Raw Permalink Normal View History

// Copyright Johannes Kapfhammer 2019-2022.
2019-10-06 16:45:29 +02:00
// 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)
//
2019-10-06 18:37:37 +02:00
// pretty pretty_printing with c++
2019-10-06 16:45:29 +02:00
//
#ifndef SOI_PRETTY
#define SOI_PRETTY
2019-10-06 16:45:29 +02:00
#include "prettyprint.hpp"
2019-10-06 18:37:37 +02:00
namespace soi {
2019-10-06 16:45:29 +02:00
namespace prettyprint {
2019-10-06 18:38:23 +02:00
template <typename TChar, typename TCharTraits>
2019-10-06 16:45:29 +02:00
std::basic_ostream<TChar, TCharTraits> &
2019-10-06 18:38:23 +02:00
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, const bool &x) {
2019-10-06 16:45:29 +02:00
return stream << (x ? "true" : "false");
}
namespace detail {
2019-10-06 18:38:23 +02:00
template <typename TChar, typename TCharTraits>
void escape_char(std::basic_ostream<TChar, TCharTraits> &stream, char c) {
2019-10-06 16:45:29 +02:00
switch (c) {
2019-10-06 18:38:23 +02:00
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;
2019-10-06 16:45:29 +02:00
}
}
2019-10-06 18:38:23 +02:00
} // namespace detail
2019-10-06 16:45:29 +02:00
2019-10-06 18:38:23 +02:00
template <typename TChar, typename TCharTraits>
2019-10-06 16:45:29 +02:00
std::basic_ostream<TChar, TCharTraits> &
2019-10-06 18:38:23 +02:00
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream,
const std::string &x) {
2019-10-06 16:45:29 +02:00
stream << "\"";
for (char c : x)
detail::escape_char(stream, c);
return stream << "\"";
}
2019-10-06 18:38:23 +02:00
template <typename TChar, typename TCharTraits>
2019-10-06 16:45:29 +02:00
std::basic_ostream<TChar, TCharTraits> &
2019-10-06 18:38:23 +02:00
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, char c) {
2019-10-06 16:45:29 +02:00
stream << "'";
detail::escape_char(stream, c);
return stream << "'";
}
2019-10-06 18:38:23 +02:00
template <typename T, typename TChar, typename TCharTraits>
inline typename std::enable_if<!::soi::prettyprint::is_container<T>::value,
2019-10-06 18:38:23 +02:00
std::basic_ostream<TChar, TCharTraits> &>::type
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
2019-10-06 16:45:29 +02:00
return stream << x;
}
2019-10-06 18:38:23 +02:00
template <typename T, typename TChar, typename TCharTraits>
2019-10-06 18:37:37 +02:00
std::basic_ostream<TChar, TCharTraits> &
2019-10-06 18:38:23 +02:00
print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
return ::soi::prettyprint::pretty_print(stream, x);
2019-10-06 18:37:37 +02:00
}
} // namespace prettyprint
2019-10-06 18:38:23 +02:00
} // namespace soi
2019-10-06 16:45:29 +02:00
#endif // SOI_PRETTY