forked from fmtlib/fmt
Parameterize core functions on the type of the format string.
Take #2 of n Signed-off-by: Daniela Engert <dani@ngrt.de>
This commit is contained in:
committed by
Victor Zverovich
parent
0f98de3011
commit
deb901b9e4
@@ -1340,23 +1340,14 @@ template <typename Char>
|
|||||||
struct is_contiguous<internal::basic_buffer<Char> >: std::true_type {};
|
struct is_contiguous<internal::basic_buffer<Char> >: std::true_type {};
|
||||||
|
|
||||||
/** Formats a string and writes the output to ``out``. */
|
/** Formats a string and writes the output to ``out``. */
|
||||||
template <typename Container>
|
template <typename Container, typename S>
|
||||||
typename std::enable_if<
|
typename std::enable_if<
|
||||||
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
|
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
|
||||||
vformat_to(std::back_insert_iterator<Container> out,
|
vformat_to(std::back_insert_iterator<Container> out,
|
||||||
string_view format_str, format_args args) {
|
const S &format_str,
|
||||||
|
basic_format_args<typename buffer_context<FMT_CHAR(S)>::type> args) {
|
||||||
internal::container_buffer<Container> buf(internal::get_container(out));
|
internal::container_buffer<Container> buf(internal::get_container(out));
|
||||||
vformat_to(buf, format_str, args);
|
vformat_to(buf, internal::to_string_view(format_str), args);
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Container>
|
|
||||||
typename std::enable_if<
|
|
||||||
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
|
|
||||||
vformat_to(std::back_insert_iterator<Container> out,
|
|
||||||
wstring_view format_str, wformat_args args) {
|
|
||||||
internal::container_buffer<Container> buf(internal::get_container(out));
|
|
||||||
vformat_to(buf, format_str, args);
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1366,8 +1357,8 @@ inline typename std::enable_if<
|
|||||||
std::back_insert_iterator<Container>>::type
|
std::back_insert_iterator<Container>>::type
|
||||||
format_to(std::back_insert_iterator<Container> out, const S &format_str,
|
format_to(std::back_insert_iterator<Container> out, const S &format_str,
|
||||||
const Args &... args) {
|
const Args &... args) {
|
||||||
return vformat_to(out, internal::to_string_view(format_str),
|
internal::checked_args<S, Args...> ca(format_str, args...);
|
||||||
internal::checked_args<S, Args...>(format_str, args...));
|
return vformat_to(out, internal::to_string_view(format_str), *ca);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = FMT_CHAR(S)>
|
template <typename S, typename Char = FMT_CHAR(S)>
|
||||||
|
@@ -3465,12 +3465,6 @@ inline typename buffer_context<FMT_CHAR(String)>::type::iterator vformat_to(
|
|||||||
buf, basic_string_view<Char>(format_str), args);
|
buf, basic_string_view<Char>(format_str), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline wformat_context::iterator vformat_to(
|
|
||||||
internal::wbuffer &buf, wstring_view format_str, wformat_args args) {
|
|
||||||
typedef back_insert_range<internal::wbuffer> range;
|
|
||||||
return vformat_to<arg_formatter<range>>(buf, format_str, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename String, typename... Args,
|
typename String, typename... Args,
|
||||||
std::size_t SIZE = inline_buffer_size,
|
std::size_t SIZE = inline_buffer_size,
|
||||||
@@ -3534,23 +3528,23 @@ struct format_to_n_result {
|
|||||||
std::size_t size;
|
std::size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename OutputIt>
|
template <typename OutputIt, typename Char = typename OutputIt::value_type>
|
||||||
using format_to_n_context = typename fmt::format_context_t<
|
using format_to_n_context = typename fmt::format_context_t<
|
||||||
fmt::internal::truncating_iterator<OutputIt>>::type;
|
fmt::internal::truncating_iterator<OutputIt>, Char>::type;
|
||||||
|
|
||||||
template <typename OutputIt>
|
template <typename OutputIt, typename Char = typename OutputIt::value_type>
|
||||||
using format_to_n_args = fmt::basic_format_args<format_to_n_context<OutputIt>>;
|
using format_to_n_args = fmt::basic_format_args<format_to_n_context<OutputIt, Char>>;
|
||||||
|
|
||||||
template <typename OutputIt, typename ...Args>
|
template <typename OutputIt, typename Char, typename ...Args>
|
||||||
inline format_arg_store<format_to_n_context<OutputIt>, Args...>
|
inline format_arg_store<format_to_n_context<OutputIt, Char>, Args...>
|
||||||
make_format_to_n_args(const Args &... args) {
|
make_format_to_n_args(const Args &... args) {
|
||||||
return format_arg_store<format_to_n_context<OutputIt>, Args...>(args...);
|
return format_arg_store<format_to_n_context<OutputIt, Char>, Args...>(args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputIt, typename... Args>
|
template <typename OutputIt, typename Char, typename... Args>
|
||||||
inline format_to_n_result<OutputIt> vformat_to_n(
|
inline format_to_n_result<OutputIt> vformat_to_n(
|
||||||
OutputIt out, std::size_t n, string_view format_str,
|
OutputIt out, std::size_t n, basic_string_view<Char> format_str,
|
||||||
format_to_n_args<OutputIt> args) {
|
format_to_n_args<OutputIt, Char> args) {
|
||||||
typedef internal::truncating_iterator<OutputIt> It;
|
typedef internal::truncating_iterator<OutputIt> It;
|
||||||
auto it = vformat_to(It(out, n), format_str, args);
|
auto it = vformat_to(It(out, n), format_str, args);
|
||||||
return {it.base(), it.count()};
|
return {it.base(), it.count()};
|
||||||
@@ -3563,20 +3557,16 @@ inline format_to_n_result<OutputIt> vformat_to_n(
|
|||||||
end of the output range.
|
end of the output range.
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename OutputIt, typename... Args>
|
template <typename OutputIt, typename String, typename... Args>
|
||||||
inline format_to_n_result<OutputIt> format_to_n(
|
inline typename std::enable_if<
|
||||||
OutputIt out, std::size_t n, string_view format_str, const Args &... args) {
|
internal::is_format_string<String>::value,
|
||||||
return vformat_to_n<OutputIt>(
|
format_to_n_result<OutputIt>>::type format_to_n(
|
||||||
out, n, format_str, make_format_to_n_args<OutputIt>(args...));
|
OutputIt out, std::size_t n, const String &format_str, const Args &... args) {
|
||||||
}
|
internal::check_format_string<Args...>(format_str);
|
||||||
template <typename OutputIt, typename... Args>
|
typedef FMT_CHAR(String) Char;
|
||||||
inline format_to_n_result<OutputIt> format_to_n(
|
format_arg_store<format_to_n_context<OutputIt, Char>, Args...> as{ args... };
|
||||||
OutputIt out, std::size_t n, wstring_view format_str,
|
return vformat_to_n(out, n, internal::to_string_view(format_str),
|
||||||
const Args &... args) {
|
format_to_n_args<OutputIt, Char>(as));
|
||||||
typedef internal::truncating_iterator<OutputIt> It;
|
|
||||||
auto it = vformat_to(It(out, n), format_str,
|
|
||||||
make_format_args<typename format_context_t<It, wchar_t>::type>(args...));
|
|
||||||
return {it.base(), it.count()};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
|
@@ -2401,6 +2401,26 @@ TEST(FormatTest, FormatStringErrors) {
|
|||||||
"cannot switch from automatic to manual argument indexing",
|
"cannot switch from automatic to manual argument indexing",
|
||||||
int, int);
|
int, int);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(FormatTest, VFormatTo) {
|
||||||
|
typedef fmt::format_context context;
|
||||||
|
fmt::basic_format_arg<context> arg = fmt::internal::make_arg<context>(42);
|
||||||
|
fmt::basic_format_args<context> args(&arg, 1);
|
||||||
|
std::string s;
|
||||||
|
fmt::vformat_to(std::back_inserter(s), "{}", args);
|
||||||
|
EXPECT_EQ("42", s);
|
||||||
|
s.clear();
|
||||||
|
fmt::vformat_to(std::back_inserter(s), FMT_STRING("{}"), args);
|
||||||
|
EXPECT_EQ("42", s);
|
||||||
|
|
||||||
|
typedef fmt::wformat_context wcontext;
|
||||||
|
fmt::basic_format_arg<wcontext> warg = fmt::internal::make_arg<wcontext>(42);
|
||||||
|
fmt::basic_format_args<wcontext> wargs(&warg, 1);
|
||||||
|
std::wstring w;
|
||||||
|
fmt::vformat_to(std::back_inserter(w), L"{}", wargs);
|
||||||
|
EXPECT_EQ(L"42", w);
|
||||||
|
}
|
||||||
|
|
||||||
#endif // FMT_USE_CONSTEXPR
|
#endif // FMT_USE_CONSTEXPR
|
||||||
|
|
||||||
TEST(FormatTest, ConstructU8StringViewFromCString) {
|
TEST(FormatTest, ConstructU8StringViewFromCString) {
|
||||||
|
Reference in New Issue
Block a user