Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize core functions on the type of the format string. #885

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 32 additions & 42 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1290,8 +1290,9 @@ template <typename S>
struct is_format_string : std::is_empty<format_string_traits<S>> {};

template <typename S>
struct is_compile_string :
std::integral_constant<bool, std::is_base_of<compile_string, S>::value> {};
struct is_compile_string : std::is_base_of<compile_string, S> {};

#define FMT_CHAR(Str) typename internal::format_string_traits<Str>::char_type

template <typename... Args, typename S>
inline typename std::enable_if<!is_compile_string<S>::value>::type
Expand Down Expand Up @@ -1341,26 +1342,19 @@ typename std::enable_if<
return out;
}

template <typename Container, typename... Args>
template <typename Container, typename String, typename... Args>
inline typename std::enable_if<
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
is_contiguous<Container>::value && internal::is_format_string<String>::value,
std::back_insert_iterator<Container>>::type
format_to(std::back_insert_iterator<Container> out,
string_view format_str, const Args &... args) {
format_arg_store<format_context, Args...> as{args...};
return vformat_to(out, format_str, as);
}

template <typename Container, typename... Args>
inline typename std::enable_if<
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
format_to(std::back_insert_iterator<Container> out,
wstring_view format_str, const Args &... args) {
return vformat_to(out, format_str,
make_format_args<wformat_context>(args...));
const String &format_str, const Args &... args) {
internal::check_format_string<Args...>(format_str);
typedef typename buffer_context< FMT_CHAR(String) >::type context_t;
format_arg_store<context_t, Args...> as{args...};
return vformat_to(out, basic_string_view< FMT_CHAR(String) >(format_str),
basic_format_args<context_t>(as));
}

#define FMT_CHAR(Str) typename internal::format_string_traits<Str>::char_type

template <typename String, typename Char = FMT_CHAR(String) >
inline std::basic_string<Char> vformat(
const String &format_str,
Expand Down Expand Up @@ -1397,26 +1391,24 @@ FMT_API void vprint(std::FILE *f, wstring_view format_str, wformat_args args);

/**
\rst
Prints formatted data to the file *f*.
Prints formatted data to the file *f*. For wide format strings,
*f* should be in wide-oriented mode set via ``fwide(f, 1)`` or
``_setmode(_fileno(f), _O_U8TEXT)`` on Windows.

**Example**::

fmt::print(stderr, "Don't {}!", "panic");

\endrst
*/
template <typename... Args>
inline void print(std::FILE *f, string_view format_str, const Args &... args) {
format_arg_store<format_context, Args...> as(args...);
vprint(f, format_str, as);
}
/**
Prints formatted data to the file *f* which should be in wide-oriented mode
set via ``fwide(f, 1)`` or ``_setmode(_fileno(f), _O_U8TEXT)`` on Windows.
*/
template <typename... Args>
inline void print(std::FILE *f, wstring_view format_str, const Args &... args) {
format_arg_store<wformat_context, Args...> as(args...);
vprint(f, format_str, as);
template <typename String, typename... Args>
inline typename std::enable_if<internal::is_format_string<String>::value>::type
print(std::FILE *f, const String &format_str, const Args &... args) {
internal::check_format_string<Args...>(format_str);
typedef typename buffer_context< FMT_CHAR(String) >::type context_t;
format_arg_store<context_t, Args...> as{ args... };
vprint(f, basic_string_view< FMT_CHAR(String) >(format_str),
basic_format_args<context_t>(as));
}

FMT_API void vprint(string_view format_str, format_args args);
Expand All @@ -1431,16 +1423,14 @@ FMT_API void vprint(wstring_view format_str, wformat_args args);
fmt::print("Elapsed time: {0:.2f} seconds", 1.23);
\endrst
*/
template <typename... Args>
inline void print(string_view format_str, const Args &... args) {
format_arg_store<format_context, Args...> as{args...};
vprint(format_str, as);
}

template <typename... Args>
inline void print(wstring_view format_str, const Args &... args) {
format_arg_store<wformat_context, Args...> as(args...);
vprint(format_str, as);
template <typename String, typename... Args>
inline typename std::enable_if<internal::is_format_string<String>::value>::type
print(const String &format_str, const Args &... args) {
internal::check_format_string<Args...>(format_str);
typedef typename buffer_context< FMT_CHAR(String) >::type context_t;
format_arg_store<context_t, Args...> as{ args... };
vprint(basic_string_view< FMT_CHAR(String) >(format_str),
basic_format_args<context_t>(as));
}
FMT_END_NAMESPACE

Expand Down