Update <format>

This commit is contained in:
Victor Zverovich 2019-04-10 08:29:07 -07:00
parent 09e2ac5e46
commit 52eb3fe274

View File

@ -716,34 +716,36 @@ string vformat(string_view fmt, format_args args) {
wstring vformat(wstring_view fmt, wformat_args args);
template<FMT_CONCEPT(OutputIterator<const char&>) O, class... Args>
O format_to(O out, string_view fmt, const Args&... args) {
return vformat_to(out, fmt, {make_format_args<basic_format_context<O, char>>(args...)});
template<class Out, class... Args>
Out format_to(Out out, string_view fmt, const Args&... args) {
using context = basic_format_context<Out, decltype(fmt)::value_type>;
return vformat_to(out, fmt, {make_format_args<context>(args...)});
}
template<FMT_CONCEPT(OutputIterator<const wchar_t&>) O, class... Args>
O format_to(O out, wstring_view fmt, const Args&... args) {
return vformat_to(out, fmt, {make_format_args<basic_format_context<O, wchar_t>>(args...)});
template<class Out, class... Args>
Out format_to(Out out, wstring_view fmt, const Args&... args) {
using context = basic_format_context<Out, decltype(fmt)::value_type>;
return vformat_to(out, fmt, {make_format_args<context>(args...)});
}
template<FMT_CONCEPT(OutputIterator<const char&>) O>
O vformat_to(O out, string_view fmt, format_args_t<O, char> args) {
typedef fmt::output_range<O, char> range;
detail::format_handler<detail::arg_formatter<range>, char, basic_format_context<O, char>>
template<class Out>
Out vformat_to(Out out, string_view fmt, format_args_t<Out, char> args) {
typedef fmt::output_range<Out, char> range;
detail::format_handler<detail::arg_formatter<range>, char, basic_format_context<Out, char>>
h(range(out), fmt, args, {});
fmt::internal::parse_format_string<false>(fmt::to_string_view(fmt), h);
return h.context.out();
}
template<FMT_CONCEPT(OutputIterator<const wchar_t&>) O>
O vformat_to(O out, wstring_view fmt, format_args_t<O, wchar_t> args);
template<class Out>
Out vformat_to(Out out, wstring_view fmt, format_args_t<Out, wchar_t> args);
template<FMT_CONCEPT(OutputIterator<const char&>) O, class... Args>
format_to_n_result<O> format_to_n(O out, iter_difference_t<O> n,
string_view fmt, const Args&... args);
template<FMT_CONCEPT(OutputIterator<const wchar_t&>) O, class... Args>
format_to_n_result<O> format_to_n(O out, iter_difference_t<O> n,
wstring_view fmt, const Args&... args);
template<class Out, class... Args>
format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
string_view fmt, const Args&... args);
template<class Out, class... Args>
format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
wstring_view fmt, const Args&... args);
template<class... Args>
size_t formatted_size(string_view fmt, const Args&... args);
@ -764,11 +766,11 @@ template<size_t N> struct formatter<const charT[N], charT>
: detail::formatter<std::basic_string_view<charT>, charT> {};
template<class traits, class Allocator>
struct formatter<std::basic_string<charT, traits, Allocator>, charT>
struct formatter<basic_string<charT, traits, Allocator>, charT>
: detail::formatter<std::basic_string_view<charT>, charT> {};
template<class traits>
struct formatter<std::basic_string_view<charT, traits>, charT>
struct formatter<basic_string_view<charT, traits>, charT>
: detail::formatter<std::basic_string_view<charT>, charT> {};
template <> struct formatter<nullptr_t, charT> : detail::formatter<const void*, charT> {};
@ -845,4 +847,50 @@ template <> struct formatter<long double, charT> : detail::formatter<long double
};
}
inline void test0() {
using namespace std;
string s = format("{0}-{{", 8); // s == "8-{"
}
inline void test1() {
using namespace std;
string s0 = format("{} to {}", "a", "b"); // OK: automatic indexing
string s1 = format("{1} to {0}", "a", "b"); // OK: manual indexing
string s2 = format("{0} to {}", "a", "b"); // Error: mixing automatic and manual indexing
string s3 = format("{} to {1}", "a", "b"); // Error: mixing automatic and manual indexing
}
inline void test2() {
using namespace std;
char c = 120;
string s0 = format("{:6}", 42); // s0 == " 42"
string s1 = format("{:6}", 'x'); // s1 == "x "
string s2 = format("{:*<6}", 'x'); // s2 == "x*****"
string s3 = format("{:*>6}", 'x'); // s3 == "*****x"
string s4 = format("{:*^6}", 'x'); // s4 == "**x***"
string s5 = format("{:=6}", 'x'); // Error: '=' with charT and no integer presentation type
string s6 = format("{:6d}", c); // s6 == " 120"
string s7 = format("{:=+06d}", c); // s7 == "+00120"
string s8 = format("{:0=#6x}", 0xa); // s8 == "0x000a"
string s9 = format("{:6}", true); // s9 == "true "
}
inline void test3() {
using namespace std;
double inf = numeric_limits<double>::infinity();
double nan = numeric_limits<double>::quiet_NaN();
string s0 = format("{0:} {0:+} {0:-} {0: }", 1); // s0 == "1 +1 1 1"
string s1 = format("{0:} {0:+} {0:-} {0: }", -1); // s1 == "-1 -1 -1 -1"
string s2 = format("{0:} {0:+} {0:-} {0: }", inf); // s2 == "inf +inf inf inf"
string s3 = format("{0:} {0:+} {0:-} {0: }", nan); // s3 == "nan +nan nan nan"
}
inline void test4() {
using namespace std;
string s0 = format("{}", 42); // s0 == "42"
string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // s1 == "101010 42 52 2a"
string s2 = format("{0:#x} {0:#X}", 42); // s2 == "0x2a 0X2A"
string s3 = format("{:n}", 1234); // s3 == "1,234" (depends on the locale)
}
#endif // FMT_FORMAT_