Simplify format string checks

This commit is contained in:
Victor Zverovich 2019-02-10 16:59:58 -08:00
parent 4a9d676362
commit 9660ea1bff
5 changed files with 25 additions and 33 deletions

View File

@ -634,9 +634,8 @@ template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(const text_style& ts, inline std::basic_string<FMT_CHAR(S)> format(const text_style& ts,
const S& format_str, const S& format_str,
const Args&... args) { const Args&... args) {
return internal::vformat( return internal::vformat(ts, to_string_view(format_str),
ts, to_string_view(format_str), {internal::make_args_checked(format_str, args...)});
*internal::checked_args<S, Args...>(format_str, args...));
} }
#endif #endif

View File

@ -1362,18 +1362,12 @@ FMT_ENABLE_IF_T(is_compile_string<S>::value)
check_format_string(S); check_format_string(S);
template <typename S, typename... Args> template <typename S, typename... Args>
struct checked_args inline format_arg_store<typename buffer_context<FMT_CHAR(S)>::type, Args...>
: format_arg_store<typename buffer_context<FMT_CHAR(S)>::type, Args...> { make_args_checked(const S& format_str, const Args&... args) {
typedef typename buffer_context<FMT_CHAR(S)>::type context;
checked_args(const S& format_str, const Args&... args)
: format_arg_store<context, Args...>(args...) {
internal::check_format_string<Args...>(format_str); internal::check_format_string<Args...>(format_str);
return {args...};
} }
basic_format_args<context> operator*() const { return *this; }
};
template <typename Char> template <typename Char>
std::basic_string<Char> vformat( std::basic_string<Char> vformat(
basic_string_view<Char> format_str, basic_string_view<Char> format_str,
@ -1434,7 +1428,7 @@ inline FMT_ENABLE_IF_T(
format_to(std::back_insert_iterator<Container> out, const S& format_str, format_to(std::back_insert_iterator<Container> out, const S& format_str,
const Args&... args) { const Args&... args) {
return vformat_to(out, to_string_view(format_str), return vformat_to(out, to_string_view(format_str),
{internal::checked_args<S, Args...>(format_str, args...)}); {internal::make_args_checked(format_str, args...)});
} }
template <typename S, typename Char = FMT_CHAR(S)> template <typename S, typename Char = FMT_CHAR(S)>
@ -1457,9 +1451,8 @@ inline std::basic_string<Char> vformat(
template <typename S, typename... Args> template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(const S& format_str, inline std::basic_string<FMT_CHAR(S)> format(const S& format_str,
const Args&... args) { const Args&... args) {
return internal::vformat( return internal::vformat(to_string_view(format_str),
to_string_view(format_str), {internal::make_args_checked(format_str, args...)});
{internal::checked_args<S, Args...>(format_str, args...)});
} }
FMT_API void vprint(std::FILE* f, string_view format_str, format_args args); FMT_API void vprint(std::FILE* f, string_view format_str, format_args args);
@ -1480,7 +1473,7 @@ template <typename S, typename... Args>
inline FMT_ENABLE_IF_T(internal::is_string<S>::value, void) inline FMT_ENABLE_IF_T(internal::is_string<S>::value, void)
print(std::FILE* f, const S& format_str, const Args&... args) { print(std::FILE* f, const S& format_str, const Args&... args) {
vprint(f, to_string_view(format_str), vprint(f, to_string_view(format_str),
internal::checked_args<S, Args...>(format_str, args...)); internal::make_args_checked(format_str, args...));
} }
FMT_API void vprint(string_view format_str, format_args args); FMT_API void vprint(string_view format_str, format_args args);
@ -1499,7 +1492,7 @@ template <typename S, typename... Args>
inline FMT_ENABLE_IF_T(internal::is_string<S>::value, void) inline FMT_ENABLE_IF_T(internal::is_string<S>::value, void)
print(const S& format_str, const Args&... args) { print(const S& format_str, const Args&... args) {
vprint(to_string_view(format_str), vprint(to_string_view(format_str),
internal::checked_args<S, Args...>(format_str, args...)); internal::make_args_checked(format_str, args...));
} }
FMT_END_NAMESPACE FMT_END_NAMESPACE

View File

@ -45,9 +45,8 @@ template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(const std::locale& loc, inline std::basic_string<FMT_CHAR(S)> format(const std::locale& loc,
const S& format_str, const S& format_str,
const Args&... args) { const Args&... args) {
return internal::vformat( return internal::vformat(loc, to_string_view(format_str),
loc, to_string_view(format_str), {internal::make_args_checked(format_str, args...)});
*internal::checked_args<S, Args...>(format_str, args...));
} }
template <typename String, typename OutputIt, typename... Args> template <typename String, typename OutputIt, typename... Args>

View File

@ -138,8 +138,8 @@ template <typename S, typename... Args>
inline typename std::enable_if<internal::is_string<S>::value>::type print( inline typename std::enable_if<internal::is_string<S>::value>::type print(
std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str, std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
const Args&... args) { const Args&... args) {
internal::checked_args<S, Args...> ca(format_str, args...); vprint(os, to_string_view(format_str),
vprint(os, to_string_view(format_str), *ca); {internal::make_args_checked(format_str, args...)});
} }
FMT_END_NAMESPACE FMT_END_NAMESPACE

View File

@ -187,7 +187,6 @@ class prepared_format {
public: public:
typedef FMT_CHAR(Format) char_type; typedef FMT_CHAR(Format) char_type;
typedef format_part<char_type> format_part_t; typedef format_part<char_type> format_part_t;
typedef internal::checked_args<Format, Args...> checked_args;
prepared_format(Format f) prepared_format(Format f)
: format_(std::move(f)), parts_provider_(to_string_view(format_)) {} : format_(std::move(f)), parts_provider_(to_string_view(format_)) {}
@ -218,7 +217,7 @@ class prepared_format {
std::basic_string<char_type> format(const Args&... args) const { std::basic_string<char_type> format(const Args&... args) const {
basic_memory_buffer<char_type> buffer; basic_memory_buffer<char_type> buffer;
typedef back_insert_range<internal::basic_buffer<char_type>> range; typedef back_insert_range<internal::basic_buffer<char_type>> range;
this->vformat_to(range(buffer), *checked_args(format_, args...)); this->vformat_to(range(buffer), make_args_checked(format_, args...));
return to_string(buffer); return to_string(buffer);
} }
@ -229,7 +228,7 @@ class prepared_format {
const Args&... args) const { const Args&... args) const {
internal::container_buffer<Container> buffer(internal::get_container(out)); internal::container_buffer<Container> buffer(internal::get_container(out));
typedef back_insert_range<internal::basic_buffer<char_type>> range; typedef back_insert_range<internal::basic_buffer<char_type>> range;
this->vformat_to(range(buffer), *checked_args(format_, args...)); this->vformat_to(range(buffer), make_args_checked(format_, args...));
return out; return out;
} }
@ -245,16 +244,18 @@ class prepared_format {
inline typename buffer_context<char_type>::type::iterator format_to( inline typename buffer_context<char_type>::type::iterator format_to(
basic_memory_buffer<char_type, SIZE>& buf, const Args&... args) const { basic_memory_buffer<char_type, SIZE>& buf, const Args&... args) const {
typedef back_insert_range<internal::basic_buffer<char_type>> range; typedef back_insert_range<internal::basic_buffer<char_type>> range;
return this->vformat_to(range(buf), *checked_args(format_, args...)); return this->vformat_to(range(buf), make_args_checked(format_, args...));
} }
private: private:
template <typename Range, typename Context> typedef typename buffer_context<char_type>::type context;
typename Context::iterator vformat_to(Range out,
basic_format_args<Context> args) const { template <typename Range>
typename context::iterator vformat_to(Range out,
basic_format_args<context> args) const {
const auto format_view = internal::to_string_view(format_); const auto format_view = internal::to_string_view(format_);
basic_parse_context<char_type> parse_ctx(format_view); basic_parse_context<char_type> parse_ctx(format_view);
Context ctx(out.begin(), args); context ctx(out.begin(), args);
const auto& parts = parts_provider_.parts(); const auto& parts = parts_provider_.parts();
for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) { for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) {