lib/fmt/ToBuffer: new library
This commit is contained in:
parent
dc8e0c9c5c
commit
2aa42d2647
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright 2022 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util/StringBuffer.hxx"
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
template<std::size_t size>
|
||||
[[nodiscard]] [[gnu::pure]]
|
||||
auto
|
||||
VFmtBuffer(fmt::string_view format_str, fmt::format_args args) noexcept
|
||||
{
|
||||
StringBuffer<size> buffer;
|
||||
auto [p, _] = fmt::vformat_to_n(buffer.data(), buffer.capacity() - 1,
|
||||
format_str, args);
|
||||
*p = 0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
template<std::size_t size, typename S, typename... Args>
|
||||
[[nodiscard]] [[gnu::pure]]
|
||||
auto
|
||||
FmtBuffer(const S &format_str, Args&&... args) noexcept
|
||||
{
|
||||
#if FMT_VERSION >= 90000
|
||||
return VFmtBuffer(format_str,
|
||||
fmt::make_format_args(args...));
|
||||
#else
|
||||
return VFmtBuffer(fmt::to_string_view(format_str),
|
||||
fmt::make_args_checked<Args...>(format_str,
|
||||
args...));
|
||||
#endif
|
||||
}
|
|
@ -28,8 +28,7 @@
|
|||
*/
|
||||
|
||||
#include "FmtError.hxx"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include "lib/fmt/ToBuffer.hxx"
|
||||
|
||||
#include <array>
|
||||
|
||||
|
@ -37,11 +36,8 @@ std::system_error
|
|||
VFmtSystemError(std::error_code code,
|
||||
fmt::string_view format_str, fmt::format_args args) noexcept
|
||||
{
|
||||
std::array<char, 512> buffer;
|
||||
auto [p, _] = fmt::vformat_to_n(buffer.begin(), buffer.size() - 1,
|
||||
format_str, args);
|
||||
*p = 0;
|
||||
return std::system_error{code, buffer.data()};
|
||||
const auto msg = VFmtBuffer<512>(format_str, args);
|
||||
return std::system_error{code, msg};
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
|
Loading…
Reference in New Issue