From c08518a25b57a7068e0be04d74acff6f1a195f98 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 19 Jul 2020 10:21:15 -0700 Subject: [PATCH] Move make_args_checked to the public API --- doc/api.rst | 2 ++ doc/build.py | 4 +-- include/fmt/color.h | 4 +-- include/fmt/core.h | 57 +++++++++++++++++++++++-------------------- include/fmt/format.h | 4 +-- include/fmt/locale.h | 4 +-- include/fmt/ostream.h | 2 +- 7 files changed, 41 insertions(+), 36 deletions(-) diff --git a/doc/api.rst b/doc/api.rst index f4ea0ec7..28270144 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -68,6 +68,8 @@ Argument Lists .. doxygenfunction:: fmt::make_format_args(const Args&...) +.. doxygenfunction:: fmt::make_args_checked(const S&, const remove_reference_t&...) + .. doxygenclass:: fmt::format_arg_store :members: diff --git a/doc/build.py b/doc/build.py index 385205d9..57b0c7ae 100755 --- a/doc/build.py +++ b/doc/build.py @@ -74,8 +74,8 @@ def build_docs(sphinx_executable='sphinx-build', version='dev', **kwargs): GENERATE_MAN = NO GENERATE_RTF = NO CASE_SENSE_NAMES = NO - INPUT = {0}/core.h {0}/compile.h {0}/format.h {0}/os.h \ - {0}/ostream.h {0}/printf.h {0}/time.h + INPUT = {0}/color.h {0}/core.h {0}/compile.h {0}/format.h \ + {0}/os.h {0}/ostream.h {0}/printf.h {0}/time.h QUIET = YES JAVADOC_AUTOBRIEF = YES AUTOLINK_SUPPORT = NO diff --git a/include/fmt/color.h b/include/fmt/color.h index 83ff914d..b30dc031 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -515,7 +515,7 @@ template (format_str, args...)); + fmt::make_args_checked(format_str, args...)); } /** @@ -556,7 +556,7 @@ template > inline std::basic_string format(const text_style& ts, const S& format_str, const Args&... args) { return vformat(ts, to_string_view(format_str), - detail::make_args_checked(format_str, args...)); + fmt::make_args_checked(format_str, args...)); } FMT_END_NAMESPACE diff --git a/include/fmt/core.h b/include/fmt/core.h index bf144a35..2792aef8 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -506,6 +506,18 @@ template struct char_t_impl::value>> { using type = typename result::value_type; }; +// Reports a compile-time error if S is not a valid format string. +template ::value)> +FMT_INLINE void check_format_string(const S&) { +#ifdef FMT_ENFORCE_COMPILE_STRING + static_assert(is_compile_string::value, + "FMT_ENFORCE_COMPILE_STRING requires all format strings to use " + "FMT_STRING."); +#endif +} +template ::value)> +void check_format_string(S); + struct error_handler { constexpr error_handler() = default; constexpr error_handler(const error_handler&) = default; @@ -1555,6 +1567,20 @@ inline format_arg_store make_format_args( return {args...}; } +/** Same as `make_format_args` but with compile-time format string checks. */ +template > +inline format_arg_store, remove_reference_t...> +make_args_checked(const S& format_str, + const remove_reference_t&... args) { + static_assert( + detail::count<( + std::is_base_of>::value && + std::is_reference::value)...>() == 0, + "passing views as lvalues is disallowed"); + detail::check_format_string(format_str); + return {args...}; +} + /** \rst Returns a named argument to be used in a formatting function. It should only @@ -1870,29 +1896,6 @@ struct wformat_args : basic_format_args { namespace detail { -// Reports a compile-time error if S is not a valid format string. -template ::value)> -FMT_INLINE void check_format_string(const S&) { -#ifdef FMT_ENFORCE_COMPILE_STRING - static_assert(is_compile_string::value, - "FMT_ENFORCE_COMPILE_STRING requires all format strings to use " - "FMT_STRING."); -#endif -} -template ::value)> -void check_format_string(S); - -template > -inline format_arg_store, remove_reference_t...> -make_args_checked(const S& format_str, - const remove_reference_t&... args) { - static_assert(count<(std::is_base_of>::value && - std::is_reference::value)...>() == 0, - "passing views as lvalues is disallowed"); - check_format_string(format_str); - return {args...}; -} - template ::value)> std::basic_string vformat( basic_string_view format_str, @@ -1943,7 +1946,7 @@ template ::value&& detail::is_string::value)> inline OutputIt format_to(OutputIt out, const S& format_str, Args&&... args) { - const auto& vargs = detail::make_args_checked(format_str, args...); + const auto& vargs = fmt::make_args_checked(format_str, args...); return vformat_to(out, to_string_view(format_str), vargs); } @@ -1968,7 +1971,7 @@ FMT_INLINE std::basic_string vformat( // std::basic_string> to reduce the symbol size. template > FMT_INLINE std::basic_string format(const S& format_str, Args&&... args) { - const auto& vargs = detail::make_args_checked(format_str, args...); + const auto& vargs = fmt::make_args_checked(format_str, args...); return detail::vformat(to_string_view(format_str), vargs); } @@ -1988,7 +1991,7 @@ FMT_API void vprint(std::FILE*, string_view, format_args); */ template > inline void print(std::FILE* f, const S& format_str, Args&&... args) { - const auto& vargs = detail::make_args_checked(format_str, args...); + const auto& vargs = fmt::make_args_checked(format_str, args...); return detail::is_unicode() ? vprint(f, to_string_view(format_str), vargs) : detail::vprint_mojibake(f, to_string_view(format_str), vargs); @@ -2007,7 +2010,7 @@ inline void print(std::FILE* f, const S& format_str, Args&&... args) { */ template > inline void print(const S& format_str, Args&&... args) { - const auto& vargs = detail::make_args_checked(format_str, args...); + const auto& vargs = fmt::make_args_checked(format_str, args...); return detail::is_unicode() ? vprint(to_string_view(format_str), vargs) : detail::vprint_mojibake(stdout, to_string_view(format_str), diff --git a/include/fmt/format.h b/include/fmt/format.h index c8d9bf1b..6548f716 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3503,7 +3503,7 @@ template ::value, char_t>> inline typename buffer_context::iterator format_to( basic_memory_buffer& buf, const S& format_str, Args&&... args) { - const auto& vargs = detail::make_args_checked(format_str, args...); + const auto& vargs = fmt::make_args_checked(format_str, args...); return detail::vformat_to(buf, to_string_view(format_str), vargs); } @@ -3556,7 +3556,7 @@ template format_to_n(OutputIt out, size_t n, const S& format_str, const Args&... args) { - const auto& vargs = detail::make_args_checked(format_str, args...); + const auto& vargs = fmt::make_args_checked(format_str, args...); return vformat_to_n(out, n, to_string_view(format_str), vargs); } diff --git a/include/fmt/locale.h b/include/fmt/locale.h index 3b3e04b7..84819238 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -47,7 +47,7 @@ inline std::basic_string format(const std::locale& loc, const S& format_str, Args&&... args) { return detail::vformat( loc, to_string_view(format_str), - detail::make_args_checked(format_str, args...)); + fmt::make_args_checked(format_str, args...)); } template ::value)> inline OutputIt format_to(OutputIt out, const std::locale& loc, const S& format_str, Args&&... args) { - const auto& vargs = detail::make_args_checked(format_str, args...); + const auto& vargs = fmt::make_args_checked(format_str, args...); return vformat_to(out, loc, to_string_view(format_str), vargs); } diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 1361ac29..29c58ec1 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -170,7 +170,7 @@ template ::value, char_t>> void print(std::basic_ostream& os, const S& format_str, Args&&... args) { vprint(os, to_string_view(format_str), - detail::make_args_checked(format_str, args...)); + fmt::make_args_checked(format_str, args...)); } FMT_END_NAMESPACE