FMT_CHAR -> char_t

This commit is contained in:
Victor Zverovich
2019-06-01 12:32:24 -07:00
parent 56d2b91108
commit 4d4b8c238d
7 changed files with 99 additions and 94 deletions

View File

@ -556,7 +556,7 @@ std::basic_string<Char> vformat(
}
} // namespace internal
template <typename S, typename Char = typename internal::char_t<S>::type>
template <typename S, typename Char = char_t<S> >
void vprint(std::FILE* f, const text_style& ts, const S& format,
basic_format_args<typename buffer_context<Char>::type> args) {
bool has_style = false;
@ -587,13 +587,12 @@ void vprint(std::FILE* f, const text_style& ts, const S& format,
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
"Elapsed time: {0:.2f} seconds", 1.23);
*/
template <typename String, typename... Args,
FMT_ENABLE_IF(internal::is_string<String>::value)>
void print(std::FILE* f, const text_style& ts, const String& format_str,
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
void print(std::FILE* f, const text_style& ts, const S& format_str,
const Args&... args) {
internal::check_format_string<Args...>(format_str);
typedef typename internal::char_t<String>::type char_t;
typedef typename buffer_context<char_t>::type context_t;
typedef typename buffer_context<char_t<S> >::type context_t;
format_arg_store<context_t, Args...> as{args...};
vprint(f, ts, format_str, basic_format_args<context_t>(as));
}
@ -605,14 +604,13 @@ void print(std::FILE* f, const text_style& ts, const String& format_str,
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
"Elapsed time: {0:.2f} seconds", 1.23);
*/
template <typename String, typename... Args,
FMT_ENABLE_IF(internal::is_string<String>::value)>
void print(const text_style& ts, const String& format_str,
const Args&... args) {
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
void print(const text_style& ts, const S& format_str, const Args&... args) {
return print(stdout, ts, format_str, args...);
}
template <typename S, typename Char = FMT_CHAR(S)>
template <typename S, typename Char = char_t<S> >
inline std::basic_string<Char> vformat(
const text_style& ts, const S& format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
@ -631,9 +629,8 @@ inline std::basic_string<Char> vformat(
"The answer is {}", 42);
\endrst
*/
template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(const text_style& ts,
const S& format_str,
template <typename S, typename... Args, typename Char = char_t<S> >
inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
const Args&... args) {
return internal::vformat(ts, to_string_view(format_str),
{internal::make_args_checked(format_str, args...)});

View File

@ -617,7 +617,7 @@ struct is_string
template <> struct is_string<std::FILE*>;
template <> struct is_string<const std::FILE*>;
template <typename S> struct char_t {
template <typename S> struct char_t_impl {
typedef decltype(to_string_view(std::declval<S>())) result;
typedef typename result::char_type type;
};
@ -883,7 +883,7 @@ FMT_CONSTEXPR11 init<C, basic_string_view<typename C::char_type>, string_type>
make_value(const S& val) {
// Handle adapted strings.
static_assert(std::is_same<typename C::char_type,
typename internal::char_t<S>::type>::value,
typename internal::char_t_impl<S>::type>::value,
"mismatch between char-types of context and argument");
return to_string_view(val);
}
@ -1311,8 +1311,7 @@ struct wformat_args : basic_format_args<wformat_context> {
};
/** String's character type. */
template <typename S> using char_t = typename internal::char_t<S>::type;
#define FMT_CHAR(S) fmt::char_t<S>
template <typename S> using char_t = typename internal::char_t_impl<S>::type;
namespace internal {
template <typename Context>
@ -1353,8 +1352,8 @@ template <typename... Args, typename S,
void check_format_string(S);
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline format_arg_store<typename buffer_context<FMT_CHAR(S)>::type, Args...>
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
inline format_arg_store<typename buffer_context<Char>::type, Args...>
make_args_checked(const S& format_str, const Args&... args) {
internal::check_format_string<Args...>(format_str);
return {args...};
@ -1386,8 +1385,8 @@ typename buffer_context<Char>::type::iterator vformat_to(
fmt::print("Elapsed time: {s:.2f} seconds", fmt::arg("s", 1.23));
\endrst
*/
template <typename S, typename T>
inline internal::named_arg<T, FMT_CHAR(S)> arg(const S& name, const T& arg) {
template <typename S, typename T, typename Char = char_t<S>>
inline internal::named_arg<T, Char> arg(const S& name, const T& arg) {
static_assert(internal::is_string<S>::value, "");
return {name, arg};
}
@ -1404,13 +1403,23 @@ struct is_contiguous<std::basic_string<Char>> : std::true_type {};
template <typename Char>
struct is_contiguous<internal::buffer<Char>> : std::true_type {};
template <typename OutputIt>
struct is_contiguous_back_insert_iterator : std::false_type {};
template <typename Container>
struct is_contiguous_back_insert_iterator<std::back_insert_iterator<Container>>
: is_contiguous<Container> {};
/** Formats a string and writes the output to ``out``. */
template <typename Container, typename S>
typename std::enable_if<is_contiguous<Container>::value,
std::back_insert_iterator<Container>>::type
vformat_to(std::back_insert_iterator<Container> out, const S& format_str,
basic_format_args<typename buffer_context<FMT_CHAR(S)>::type> args) {
internal::container_buffer<Container> buf(internal::get_container(out));
template <typename OutputIt, typename S,
typename Char = enable_if_t<
is_contiguous_back_insert_iterator<OutputIt>::value, char_t<S>>>
OutputIt vformat_to(
OutputIt out, const S& format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
using container = typename std::remove_reference<decltype(
internal::get_container(out))>::type;
internal::container_buffer<container> buf((internal::get_container(out)));
internal::vformat_to(buf, to_string_view(format_str), args);
return out;
}
@ -1425,8 +1434,8 @@ inline std::back_insert_iterator<Container> format_to(
{internal::make_args_checked(format_str, args...)});
}
template <typename S, typename Char = FMT_CHAR(S),
FMT_ENABLE_IF(internal::is_string<S>::value)>
template <typename S,
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
inline std::basic_string<Char> vformat(
const S& format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
@ -1443,11 +1452,12 @@ inline std::basic_string<Char> vformat(
std::string message = fmt::format("The answer is {}", 42);
\endrst
*/
// Pass fmt::char_t as a default template parameter instead of using
// std::basic_string<fmt::char_t<S>> to reduce the symbol size.
// Pass char_t as a default template parameter instead of using
// std::basic_string<char_t<S>> to reduce the symbol size.
template <typename S, typename... Args,
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
inline std::basic_string<Char> format(const S& format_str, const Args&... args) {
inline std::basic_string<Char> format(const S& format_str,
const Args&... args) {
return internal::vformat(to_string_view(format_str),
{internal::make_args_checked(format_str, args...)});
}

View File

@ -1805,10 +1805,11 @@ struct string_view_metadata {
: offset_(view.data() - primary_string.data()), size_(view.size()) {}
FMT_CONSTEXPR string_view_metadata(std::size_t offset, std::size_t size)
: offset_(offset), size_(size) {}
template <typename S, FMT_ENABLE_IF(internal::is_string<S>::value)>
FMT_CONSTEXPR basic_string_view<FMT_CHAR(S)> to_view(S&& str) const {
template <typename S, typename Char = enable_if_t<
internal::is_string<S>::value, char_t<S>>>
FMT_CONSTEXPR basic_string_view<Char> to_view(S&& str) const {
const auto view = to_string_view(str);
return basic_string_view<FMT_CHAR(S)>(view.data() + offset_, size_);
return basic_string_view<Char>(view.data() + offset_, size_);
}
std::size_t offset_;
@ -3384,7 +3385,7 @@ typename buffer_context<Char>::type::iterator internal::vformat_to(
args);
}
template <typename S, typename Char = FMT_CHAR(S),
template <typename S, typename Char = char_t<S>,
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline typename buffer_context<Char>::type::iterator vformat_to(
internal::buffer<Char>& buf, const S& format_str,
@ -3393,12 +3394,12 @@ inline typename buffer_context<Char>::type::iterator vformat_to(
}
template <typename S, typename... Args, std::size_t SIZE = inline_buffer_size,
typename Char = typename internal::char_t<S>::type>
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
inline typename buffer_context<Char>::type::iterator format_to(
basic_memory_buffer<Char, SIZE>& buf, const S& format_str,
const Args&... args) {
internal::check_format_string<Args...>(format_str);
typedef typename buffer_context<Char>::type context;
using context = typename buffer_context<Char>::type;
format_arg_store<context, Args...> as{args...};
return internal::vformat_to(buf, to_string_view(format_str),
basic_format_args<context>(as));
@ -3459,12 +3460,13 @@ struct format_args_t {
type;
};
template <typename String, typename OutputIt, typename... Args,
FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value)>
template <typename S, typename OutputIt, typename... Args,
FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value &&
!is_contiguous_back_insert_iterator<OutputIt>::value)>
inline OutputIt vformat_to(
OutputIt out, const String& format_str,
typename format_args_t<OutputIt, FMT_CHAR(String)>::type args) {
typedef output_range<OutputIt, FMT_CHAR(String)> range;
OutputIt out, const S& format_str,
typename format_args_t<OutputIt, char_t<S>>::type args) {
typedef output_range<OutputIt, char_t<S>> range;
return vformat_to<arg_formatter<range>>(range(out),
to_string_view(format_str), args);
}
@ -3481,13 +3483,13 @@ inline OutputIt vformat_to(
\endrst
*/
template <typename OutputIt, typename S, typename... Args>
inline
typename std::enable_if<internal::is_string<S>::value &&
internal::is_output_iterator<OutputIt>::value,
OutputIt>::type
format_to(OutputIt out, const S& format_str, const Args&... args) {
inline OutputIt format_to(OutputIt out, const S& format_str,
const Args&... args) {
static_assert(internal::is_output_iterator<OutputIt>::value &&
internal::is_string<S>::value,
"");
internal::check_format_string<Args...>(format_str);
typedef typename format_context_t<OutputIt, FMT_CHAR(S)>::type context;
typedef typename format_context_t<OutputIt, char_t<S>>::type context;
format_arg_store<context, Args...> as{args...};
return vformat_to(out, to_string_view(format_str),
basic_format_args<context>(as));
@ -3542,7 +3544,7 @@ inline format_to_n_result<OutputIt> format_to_n(OutputIt out, std::size_t n,
const S& format_str,
const Args&... args) {
internal::check_format_string<Args...>(format_str);
typedef FMT_CHAR(S) Char;
using Char = char_t<S>;
format_arg_store<typename format_to_n_context<OutputIt, Char>::type, Args...>
as(args...);
return vformat_to_n(out, n, to_string_view(format_str),

View File

@ -34,38 +34,39 @@ std::basic_string<Char> vformat(
}
} // namespace internal
template <typename S, typename Char = FMT_CHAR(S)>
template <typename S, typename Char = char_t<S>>
inline std::basic_string<Char> vformat(
const std::locale& loc, const S& format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
return internal::vformat(loc, to_string_view(format_str), args);
}
template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(const std::locale& loc,
template <typename S, typename... Args, typename Char = char_t<S>>
inline std::basic_string<Char> format(const std::locale& loc,
const S& format_str,
const Args&... args) {
return internal::vformat(loc, to_string_view(format_str),
{internal::make_args_checked(format_str, args...)});
}
template <typename String, typename OutputIt, typename... Args,
FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value)>
inline OutputIt vformat_to(
OutputIt out, const std::locale& loc, const String& format_str,
typename format_args_t<OutputIt, FMT_CHAR(String)>::type args) {
typedef output_range<OutputIt, FMT_CHAR(String)> range;
template <typename S, typename OutputIt, typename... Args,
typename Char = enable_if_t<
internal::is_output_iterator<OutputIt>::value, char_t<S>>>
inline OutputIt vformat_to(OutputIt out, const std::locale& loc,
const S& format_str,
typename format_args_t<OutputIt, Char>::type args) {
using range = output_range<OutputIt, Char>;
return vformat_to<arg_formatter<range>>(
range(out), to_string_view(format_str), args, internal::locale_ref(loc));
}
template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value&&
internal::is_output_iterator<OutputIt>::value)>
FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value&&
internal::is_string<S>::value)>
inline OutputIt format_to(OutputIt out, const std::locale& loc,
const S& format_str, const Args&... args) {
internal::check_format_string<Args...>(format_str);
typedef typename format_context_t<OutputIt, FMT_CHAR(S)>::type context;
using context = typename format_context_t<OutputIt, char_t<S>>::type;
format_arg_store<context, Args...> as{args...};
return vformat_to(out, loc, to_string_view(format_str),
basic_format_args<context>(as));

View File

@ -120,13 +120,13 @@ struct convert_to_int<
};
template <typename Char>
inline void vprint(
std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
basic_memory_buffer<Char> buffer;
internal::vformat_to(buffer, format_str, args);
internal::write(os, buffer);
}
/**
\rst
Prints formatted data to the stream *os*.
@ -137,8 +137,8 @@ inline void vprint(
\endrst
*/
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline void print(std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
inline void print(std::basic_ostream<Char>& os, const S& format_str,
const Args&... args) {
vprint(os, to_string_view(format_str),
{internal::make_args_checked(format_str, args...)});

View File

@ -185,8 +185,8 @@ class format_preparation_handler : public internal::error_handler {
template <typename Format, typename PreparedPartsProvider, typename... Args>
class prepared_format {
public:
typedef FMT_CHAR(Format) char_type;
typedef format_part<char_type> format_part_t;
using char_type = char_t<Format>;
using format_part_t = format_part<char_type>;
prepared_format(Format f)
: format_(std::move(f)), parts_provider_(to_string_view(format_)) {}
@ -351,7 +351,7 @@ class prepared_format {
template <typename Format> class compiletime_prepared_parts_type_provider {
private:
typedef FMT_CHAR(Format) char_type;
using char_type = char_t<Format>;
class count_handler {
private:
@ -649,7 +649,7 @@ class parts_container {
// Delegate preparing to preparator, to take advantage of a partial
// specialization.
template <typename Format, typename... Args> struct preparator {
typedef parts_container<FMT_CHAR(Format)> container;
typedef parts_container<char_t<Format>> container;
typedef typename basic_prepared_format<Format, container, Args...>::type
prepared_format_type;

View File

@ -608,7 +608,7 @@ inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
return {args...};
}
template <typename S, typename Char = FMT_CHAR(S)>
template <typename S, typename Char = char_t<S>>
inline std::basic_string<Char> vsprintf(
const S& format,
basic_format_args<
@ -629,16 +629,14 @@ inline std::basic_string<Char> vsprintf(
\endrst
*/
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline std::basic_string<FMT_CHAR(S)> sprintf(const S& format,
const Args&... args) {
typedef internal::buffer<FMT_CHAR(S)> buffer;
typedef typename basic_printf_context_t<buffer>::type context;
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
format_arg_store<context, Args...> as{args...};
return vsprintf(to_string_view(format), basic_format_args<context>(as));
}
template <typename S, typename Char = FMT_CHAR(S)>
template <typename S, typename Char = char_t<S>>
inline int vfprintf(
std::FILE* f, const S& format,
basic_format_args<
@ -662,15 +660,14 @@ inline int vfprintf(
\endrst
*/
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
typedef internal::buffer<FMT_CHAR(S)> buffer;
typedef typename basic_printf_context_t<buffer>::type context;
using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
format_arg_store<context, Args...> as{args...};
return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
}
template <typename S, typename Char = FMT_CHAR(S)>
template <typename S, typename Char = char_t<S>>
inline int vprintf(
const S& format,
basic_format_args<
@ -692,13 +689,13 @@ template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline int printf(const S& format_str, const Args&... args) {
internal::check_format_string<Args...>(format_str);
typedef internal::buffer<FMT_CHAR(S)> buffer;
typedef typename basic_printf_context_t<buffer>::type context;
using buffer = internal::buffer<char_t<S>>;
using context = typename basic_printf_context_t<buffer>::type;
format_arg_store<context, Args...> as{args...};
return vprintf(to_string_view(format_str), basic_format_args<context>(as));
}
template <typename S, typename Char = FMT_CHAR(S)>
template <typename S, typename Char = char_t<S>>
inline int vfprintf(
std::basic_ostream<Char>& os, const S& format,
basic_format_args<
@ -731,13 +728,11 @@ typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
fmt::fprintf(cerr, "Don't %s!", "panic");
\endrst
*/
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline int fprintf(std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
template <typename S, typename... Args, typename Char = char_t<S>>
inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
const Args&... args) {
internal::check_format_string<Args...>(format_str);
typedef internal::buffer<FMT_CHAR(S)> buffer;
typedef typename basic_printf_context_t<buffer>::type context;
using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
format_arg_store<context, Args...> as{args...};
return vfprintf(os, to_string_view(format_str),
basic_format_args<context>(as));