Cleanup ranges API

This commit is contained in:
Victor Zverovich 2021-06-02 17:06:02 -07:00
parent aa09e0f5dd
commit 290d3f8b61
2 changed files with 45 additions and 39 deletions

View File

@ -19,15 +19,7 @@
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
template <typename Char> struct formatting_base { template <typename Char, typename Enable = void> struct formatting_range {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
};
template <typename Char, typename Enable = void>
struct formatting_range : formatting_base<Char> {
#ifdef FMT_DEPRECATED_BRACED_RANGES #ifdef FMT_DEPRECATED_BRACED_RANGES
Char prefix = '{'; Char prefix = '{';
Char postfix = '}'; Char postfix = '}';
@ -35,12 +27,21 @@ struct formatting_range : formatting_base<Char> {
Char prefix = '['; Char prefix = '[';
Char postfix = ']'; Char postfix = ']';
#endif #endif
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
}; };
template <typename Char, typename Enable = void> template <typename Char, typename Enable = void> struct formatting_tuple {
struct formatting_tuple : formatting_base<Char> {
Char prefix = '('; Char prefix = '(';
Char postfix = ')'; Char postfix = ')';
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
}; };
namespace detail { namespace detail {
@ -362,50 +363,52 @@ struct formatter<
} }
}; };
template <typename Char, typename... T> struct tuple_arg_join : detail::view { template <typename Char, typename... T> struct tuple_join_view : detail::view {
const std::tuple<T...>& tuple; const std::tuple<T...>& tuple;
basic_string_view<Char> sep; basic_string_view<Char> sep;
tuple_arg_join(const std::tuple<T...>& t, basic_string_view<Char> s) tuple_join_view(const std::tuple<T...>& t, basic_string_view<Char> s)
: tuple(t), sep{s} {} : tuple(t), sep{s} {}
}; };
template <typename Char, typename... T> template <typename Char, typename... T>
struct formatter<tuple_arg_join<Char, T...>, Char> { using tuple_arg_join = tuple_join_view<Char, T...>;
template <typename Char, typename... T>
struct formatter<tuple_join_view<Char, T...>, Char> {
template <typename ParseContext> template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin(); return ctx.begin();
} }
template <typename FormatContext> template <typename FormatContext>
typename FormatContext::iterator format( auto format(const tuple_join_view<Char, T...>& value, FormatContext& ctx) ->
const tuple_arg_join<Char, T...>& value, FormatContext& ctx) { typename FormatContext::iterator {
return format(value, ctx, detail::make_index_sequence<sizeof...(T)>{}); return format(value, ctx, detail::make_index_sequence<sizeof...(T)>{});
} }
private: private:
template <typename FormatContext, size_t... N> template <typename FormatContext, size_t... N>
typename FormatContext::iterator format( auto format(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
const tuple_arg_join<Char, T...>& value, FormatContext& ctx, detail::index_sequence<N...>) ->
detail::index_sequence<N...>) { typename FormatContext::iterator {
return format_args(value, ctx, std::get<N>(value.tuple)...); return format_args(value, ctx, std::get<N>(value.tuple)...);
} }
template <typename FormatContext> template <typename FormatContext>
typename FormatContext::iterator format_args( auto format_args(const tuple_join_view<Char, T...>&, FormatContext& ctx) ->
const tuple_arg_join<Char, T...>&, FormatContext& ctx) { typename FormatContext::iterator {
// NOTE: for compilers that support C++17, this empty function instantiation // NOTE: for compilers that support C++17, this empty function instantiation
// can be replaced with a constexpr branch in the variadic overload. // can be replaced with a constexpr branch in the variadic overload.
return ctx.out(); return ctx.out();
} }
template <typename FormatContext, typename Arg, typename... Args> template <typename FormatContext, typename Arg, typename... Args>
typename FormatContext::iterator format_args( auto format_args(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
const tuple_arg_join<Char, T...>& value, FormatContext& ctx, const Arg& arg, const Args&... args) ->
const Arg& arg, const Args&... args) { typename FormatContext::iterator {
using base = formatter<typename std::decay<Arg>::type, Char>; using base = formatter<typename std::decay<Arg>::type, Char>;
auto out = ctx.out(); auto out = base().format(arg, ctx);
out = base{}.format(arg, ctx);
if (sizeof...(Args) > 0) { if (sizeof...(Args) > 0) {
out = std::copy(value.sep.begin(), value.sep.end(), out); out = std::copy(value.sep.begin(), value.sep.end(), out);
ctx.advance_to(out); ctx.advance_to(out);
@ -429,14 +432,15 @@ FMT_MODULE_EXPORT_BEGIN
\endrst \endrst
*/ */
template <typename... T> template <typename... T>
FMT_CONSTEXPR tuple_arg_join<char, T...> join(const std::tuple<T...>& tuple, FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple, string_view sep)
string_view sep) { -> tuple_join_view<char, T...> {
return {tuple, sep}; return {tuple, sep};
} }
template <typename... T> template <typename... T>
FMT_CONSTEXPR tuple_arg_join<wchar_t, T...> join( FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple,
const std::tuple<T...>& tuple, basic_string_view<wchar_t> sep) { basic_string_view<wchar_t> sep)
-> tuple_join_view<wchar_t, T...> {
return {tuple, sep}; return {tuple, sep};
} }
@ -452,8 +456,8 @@ FMT_CONSTEXPR tuple_arg_join<wchar_t, T...> join(
\endrst \endrst
*/ */
template <typename T> template <typename T>
join_view<const T*, const T*> join(std::initializer_list<T> list, auto join(std::initializer_list<T> list, string_view sep)
string_view sep) { -> join_view<const T*, const T*> {
return join(std::begin(list), std::end(list), sep); return join(std::begin(list), std::end(list), sep);
} }

View File

@ -9,6 +9,7 @@
#define FMT_WCHAR_H_ #define FMT_WCHAR_H_
#include <cwchar> #include <cwchar>
#include <tuple>
#include "format.h" #include "format.h"
@ -75,9 +76,10 @@ auto join(std::initializer_list<T> list, wstring_view sep)
template <typename Locale, typename S, typename Char = char_t<S>, template <typename Locale, typename S, typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_locale<Locale>::value&& FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)> detail::is_exotic_char<Char>::value)>
inline std::basic_string<Char> vformat( inline auto vformat(
const Locale& loc, const S& format_str, const Locale& loc, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) { basic_format_args<buffer_context<type_identity_t<Char>>> args)
-> std::basic_string<Char> {
return detail::vformat(loc, to_string_view(format_str), args); return detail::vformat(loc, to_string_view(format_str), args);
} }
@ -85,8 +87,8 @@ template <typename Locale, typename S, typename... Args,
typename Char = char_t<S>, typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_locale<Locale>::value&& FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)> detail::is_exotic_char<Char>::value)>
inline std::basic_string<Char> format(const Locale& loc, const S& format_str, inline auto format(const Locale& loc, const S& format_str, Args&&... args)
Args&&... args) { -> std::basic_string<Char> {
return detail::vformat(loc, to_string_view(format_str), return detail::vformat(loc, to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str, args...)); fmt::make_args_checked<Args...>(format_str, args...));
} }
@ -126,9 +128,9 @@ template <typename Locale, typename S, typename OutputIt, typename... Args,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_locale<Locale>::value&& detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)> detail::is_exotic_char<Char>::value)>
inline OutputIt vformat_to( inline auto vformat_to(
OutputIt out, const Locale& loc, const S& format_str, OutputIt out, const Locale& loc, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) { basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt {
auto&& buf = detail::get_buffer<Char>(out); auto&& buf = detail::get_buffer<Char>(out);
vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc)); vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc));
return detail::get_iterator(buf); return detail::get_iterator(buf);
@ -202,7 +204,7 @@ template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
/** /**
Converts *value* to ``std::wstring`` using the default format for type *T*. Converts *value* to ``std::wstring`` using the default format for type *T*.
*/ */
template <typename T> inline std::wstring to_wstring(const T& value) { template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
return format(FMT_STRING(L"{}"), value); return format(FMT_STRING(L"{}"), value);
} }
FMT_MODULE_EXPORT_END FMT_MODULE_EXPORT_END