add fmt::print() overload to support compiled format (#2304)

This commit is contained in:
Alexey Ochapov 2021-05-26 00:54:56 +03:00 committed by GitHub
parent 82607efb57
commit ee52a6dc40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 5 deletions

View File

@ -606,6 +606,20 @@ size_t formatted_size(const S& format_str, const Args&... args) {
return format_to(detail::counting_iterator(), format_str, args...).count(); return format_to(detail::counting_iterator(), format_str, args...).count();
} }
template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
void print(std::FILE* f, const S& format_str, const Args&... args) {
memory_buffer buffer;
format_to(std::back_inserter(buffer), format_str, args...);
detail::print(f, {buffer.data(), buffer.size()});
}
template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
void print(const S& format_str, const Args&... args) {
print(stdout, format_str, args...);
}
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS #if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
inline namespace literals { inline namespace literals {
template <detail::fixed_string Str> template <detail::fixed_string Str>

View File

@ -2595,13 +2595,12 @@ extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
} // namespace detail } // namespace detail
#endif #endif
FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) { namespace detail {
memory_buffer buffer; FMT_FUNC void print(std::FILE* f, string_view text) {
detail::vformat_to(buffer, format_str, args);
#ifdef _WIN32 #ifdef _WIN32
auto fd = _fileno(f); auto fd = _fileno(f);
if (_isatty(fd)) { if (_isatty(fd)) {
detail::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size())); detail::utf8_to_utf16 u16(string_view(text.data(), text.size()));
auto written = detail::dword(); auto written = detail::dword();
if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
u16.c_str(), static_cast<uint32_t>(u16.size()), u16.c_str(), static_cast<uint32_t>(u16.size()),
@ -2612,7 +2611,14 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
// redirected to NUL. // redirected to NUL.
} }
#endif #endif
detail::fwrite_fully(buffer.data(), 1, buffer.size(), f); detail::fwrite_fully(text.data(), 1, text.size(), f);
}
} // namespace detail
FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
memory_buffer buffer;
detail::vformat_to(buffer, format_str, args);
detail::print(f, {buffer.data(), buffer.size()});
} }
#ifdef _WIN32 #ifdef _WIN32

View File

@ -791,6 +791,10 @@ template <typename T, size_t SIZE, typename Allocator>
struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type { struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
}; };
namespace detail {
FMT_API void print(std::FILE*, string_view);
}
/** A formatting error such as invalid format string. */ /** A formatting error such as invalid format string. */
FMT_CLASS_API FMT_CLASS_API
class FMT_API format_error : public std::runtime_error { class FMT_API format_error : public std::runtime_error {

View File

@ -11,6 +11,7 @@
#include "fmt/chrono.h" #include "fmt/chrono.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest-extra.h"
TEST(iterator_test, counting_iterator) { TEST(iterator_test, counting_iterator) {
auto it = fmt::detail::counting_iterator(); auto it = fmt::detail::counting_iterator();
@ -241,6 +242,13 @@ FMT_END_NAMESPACE
TEST(compile_test, to_string_and_formatter) { TEST(compile_test, to_string_and_formatter) {
fmt::format(FMT_COMPILE("{}"), to_stringable()); fmt::format(FMT_COMPILE("{}"), to_stringable());
} }
TEST(compile_test, print) {
EXPECT_WRITE(stdout, fmt::print(FMT_COMPILE("Don't {}!"), "panic"),
"Don't panic!");
EXPECT_WRITE(stderr, fmt::print(stderr, FMT_COMPILE("Don't {}!"), "panic"),
"Don't panic!");
}
#endif #endif
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS #if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS