mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
fix formatted_size
with "compiled format" as argument (#2161)
This commit is contained in:
@ -928,7 +928,10 @@ format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, const S&,
|
|||||||
return {it.base(), it.count()};
|
return {it.base(), it.count()};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename CompiledFormat, typename... Args>
|
template <typename CompiledFormat, typename... Args,
|
||||||
|
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
|
||||||
|
CompiledFormat>::value ||
|
||||||
|
detail::is_compiled_string<CompiledFormat>::value)>
|
||||||
size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
|
size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
|
||||||
return format_to(detail::counting_iterator(), cf, args...).count();
|
return format_to(detail::counting_iterator(), cf, args...).count();
|
||||||
}
|
}
|
||||||
|
@ -1865,11 +1865,11 @@ inline auto format_to_n(OutputIt out, size_t n, const S& format_str,
|
|||||||
Returns the number of characters in the output of
|
Returns the number of characters in the output of
|
||||||
``format(format_str, args...)``.
|
``format(format_str, args...)``.
|
||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename S, typename... Args, typename Char = char_t<S>>
|
||||||
inline size_t formatted_size(string_view format_str, Args&&... args) {
|
inline size_t formatted_size(const S& format_str, Args&&... args) {
|
||||||
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
|
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
|
||||||
detail::counting_buffer<> buf;
|
detail::counting_buffer<> buf;
|
||||||
detail::vformat_to(buf, format_str, vargs);
|
detail::vformat_to(buf, to_string_view(format_str), vargs);
|
||||||
return buf.count();
|
return buf.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,6 +261,11 @@ TEST(CompileTest, FormatToNWithCompileMacro) {
|
|||||||
EXPECT_STREQ("2a", buffer);
|
EXPECT_STREQ("2a", buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(CompileTest, FormattedSizeWithCompileMacro) {
|
||||||
|
EXPECT_EQ(2, fmt::formatted_size(FMT_COMPILE("{0}"), 42));
|
||||||
|
EXPECT_EQ(5, fmt::formatted_size(FMT_COMPILE("{0:<4.2f}"), 42.0));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(CompileTest, TextAndArg) {
|
TEST(CompileTest, TextAndArg) {
|
||||||
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
|
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
|
||||||
EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
|
EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
|
||||||
|
Reference in New Issue
Block a user