io/BufferedOutputStream: add libfmt support

This commit is contained in:
Max Kellermann 2022-07-13 12:49:16 +02:00
parent 20310437d0
commit 9a30286289
3 changed files with 43 additions and 0 deletions

View File

@ -118,6 +118,20 @@ BufferedOutputStream::Format(const char *fmt, ...)
buffer.Append(size);
}
void
BufferedOutputStream::VFmt(fmt::string_view format_str, fmt::format_args args)
{
/* TODO format into this object's buffer instead of allocating
a new one */
fmt::memory_buffer b;
#if FMT_VERSION >= 80000
fmt::vformat_to(std::back_inserter(b), format_str, args);
#else
fmt::vformat_to(b, format_str, args);
#endif
return Write(b.data(), b.size());
}
#ifdef _UNICODE
void

View File

@ -33,6 +33,11 @@
#include "util/Compiler.h"
#include "util/DynamicFifoBuffer.hxx"
#include <fmt/core.h>
#if FMT_VERSION < 70000 || FMT_VERSION >= 80000
#include <fmt/format.h>
#endif
#include <cstddef>
#ifdef _UNICODE
@ -92,6 +97,24 @@ public:
gcc_printf(2,3)
void Format(const char *fmt, ...);
void VFmt(fmt::string_view format_str, fmt::format_args args);
template<typename S, typename... Args>
void Fmt(const S &format_str, Args&&... args) {
#if FMT_VERSION >= 90000
VFmt(format_str,
fmt::make_format_args(args...));
#elif FMT_VERSION >= 70000
VFmt(fmt::to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str,
args...));
#else
/* expensive fallback for older libfmt versions */
const auto result = fmt::format(format_str, args...);
Write(result.data(), result.size());
#endif
}
#ifdef _UNICODE
/**
* Write one narrow character.

View File

@ -8,8 +8,14 @@ io = static_library(
'FileOutputStream.cxx',
'BufferedOutputStream.cxx',
include_directories: inc,
dependencies: [
fmt_dep,
],
)
io_dep = declare_dependency(
link_with: io,
dependencies: [
fmt_dep,
],
)