Parameterize vformat to support custom char types

This commit is contained in:
Victor Zverovich 2018-08-05 08:30:27 -07:00
parent c68bab7014
commit 4061a0d35d
3 changed files with 32 additions and 23 deletions

View File

@ -1281,9 +1281,11 @@ template <typename Char>
struct format_string_traits<std::basic_string<Char>> :
format_string_traits_base<Char> {};
template <typename Char>
struct format_string_traits<basic_string_view<Char>> :
format_string_traits_base<Char> {};
template <typename S>
struct format_string_traits<
S, typename std::enable_if<std::is_base_of<
basic_string_view<typename S::char_type>, S>::value>::type> :
format_string_traits_base<typename S::char_type> {};
template <typename S>
struct is_format_string :
@ -1300,6 +1302,11 @@ typename std::enable_if<!is_compile_string<S>::value>::type
template <typename... Args, typename S>
typename std::enable_if<is_compile_string<S>::value>::type
check_format_string(S);
template <typename Char>
std::basic_string<Char> vformat(
basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args);
} // namespace internal
format_context::iterator vformat_to(
@ -1354,8 +1361,15 @@ inline typename std::enable_if<
make_format_args<wformat_context>(args...));
}
std::string vformat(string_view format_str, format_args args);
std::wstring vformat(wstring_view format_str, wformat_args args);
template <
typename String,
typename Char = typename internal::format_string_traits<String>::char_type>
inline std::basic_string<Char> vformat(
const String &format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
// Convert format string to string_view to reduce the number of overloads.
return internal::vformat(basic_string_view<Char>(format_str), args);
}
/**
\rst
@ -1376,8 +1390,11 @@ inline std::basic_string<
// return vformat(format_str, make_format_args(args...));
// but gcc has trouble optimizing the latter, so break it down.
typedef typename internal::format_string_traits<String>::char_type char_t;
format_arg_store<typename buffer_context<char_t>::type, Args...> as{args...};
return vformat(basic_string_view<char_t>(format_str), as);
typedef typename buffer_context<char_t>::type context_t;
format_arg_store<context_t, Args...> as{args...};
return internal::vformat(
basic_string_view<char_t>(format_str),
basic_format_args<context_t>(as));
}
FMT_API void vprint(std::FILE *f, string_view format_str, format_args args);

View File

@ -490,13 +490,14 @@ struct char8_t {
}
};
// A UTF-8 string.
// A UTF-8 string view.
class u8string_view : public basic_string_view<char8_t> {
private:
typedef basic_string_view<char8_t> base;
typedef basic_string_view<char8_t> base;
public:
using basic_string_view::basic_string_view;
using basic_string_view::char_type;
u8string_view(const char *s)
: base(reinterpret_cast<const char8_t*>(s)) {}
@ -3617,18 +3618,15 @@ inline format_to_n_result<OutputIt> format_to_n(
return {it.base(), it.count()};
}
inline std::string vformat(string_view format_str, format_args args) {
memory_buffer buffer;
template <typename Char>
inline std::basic_string<Char> internal::vformat(
basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
basic_memory_buffer<Char> buffer;
vformat_to(buffer, format_str, args);
return fmt::to_string(buffer);
}
inline std::wstring vformat(wstring_view format_str, wformat_args args) {
wmemory_buffer buffer;
vformat_to(buffer, format_str, args);
return to_string(buffer);
}
template <typename String, typename... Args>
inline typename std::enable_if<internal::is_compile_string<String>::value>::type
print(String format_str, const Args & ... args) {

View File

@ -8,12 +8,6 @@
#include "fmt/format-inl.h"
FMT_BEGIN_NAMESPACE
namespace internal {
// Force linking of inline functions into the library.
std::string (*vformat_ref)(string_view, format_args) = vformat;
std::wstring (*vformat_wref)(wstring_view, wformat_args) = vformat;
}
template struct internal::basic_data<void>;
// Explicit instantiations for char.