From a68fd44eccba060df65dfa344a43af648106d704 Mon Sep 17 00:00:00 2001 From: sv1990 Date: Mon, 14 May 2018 14:16:30 +0200 Subject: [PATCH 1/4] Add ranges.h to FMT_HEADERS in CMakeLists.txt (#738) --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 045004a2..639dc7fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,7 +96,7 @@ endfunction() # Define the fmt library, its includes and the needed defines. add_headers(FMT_HEADERS core.h format.h format-inl.h locale.h ostream.h printf.h - time.h) + time.h ranges.h) set(FMT_SOURCES src/format.cc) if (HAVE_OPEN) add_headers(FMT_HEADERS posix.h) From 8c2557710d531fc04b17239ba1994661a7dd9092 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 16 May 2018 07:58:43 -0700 Subject: [PATCH 2/4] Update docs and changelog --- ChangeLog.rst | 24 ++++++++++++++++++++++++ doc/api.rst | 2 ++ include/fmt/format.h | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/ChangeLog.rst b/ChangeLog.rst index 4e1356a3..982c1117 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -52,6 +52,17 @@ :12:45: error: expression '' is not a constant expression throw format_error("invalid specifier"); +* Added `iterator support + `_: + + .. code:: c++ + + #include + #include + + std::vector out; + fmt::format_to(std::back_inserter(out), "{}", 42); + * Improved compile times by reducing dependencies on standard headers and providing a lightweight `core API `_: @@ -94,6 +105,19 @@ in the format API and provided ``fmt::string_view`` which implements a subset of ``std::string_view`` API for pre-C++17 systems. +* Added support for ``std::experimental::string_view`` + (`#607 `_): + + .. code:: c++ + + #include + #include + + fmt::print("{}", std::experimental::string_view("foo")); + + Thanks `@virgiliofornazin (Virgilio Alexandre Fornazin) + `_. + * Allowed mixing named and automatic arguments: .. code:: c++ diff --git a/doc/api.rst b/doc/api.rst index b65a8fc6..2a380a16 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -151,6 +151,8 @@ The following user-defined literals are defined in ``fmt/format.h``. Utilities --------- +.. dosygenfunction:: fmt::formatted_size(string_view, const Args&...) + .. doxygenfunction:: fmt::to_string(const T&) .. doxygenfunction:: fmt::to_wstring(const T&) diff --git a/include/fmt/format.h b/include/fmt/format.h index 07c644aa..6e273e84 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3450,6 +3450,17 @@ inline OutputIt vformat_to(OutputIt out, string_view format_str, return vformat_to>(range(out), format_str, args); } +/** + \rst + Formats arguments, writes the result to the output iterator ``out`` and returns + the iterator past the end of the output range. + + **Example**:: + + std::vector out; + fmt::format_to(std::back_inserter(out), "{}", 42); + \endrst + */ template inline OutputIt format_to(OutputIt out, string_view format_str, const Args & ... args) { From 728e4f5a8dea29b6b68c1a7519141e3e7128be25 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 16 May 2018 08:19:26 -0700 Subject: [PATCH 3/4] Fix docs --- ChangeLog.rst | 10 ++++++++++ doc/api.rst | 2 +- include/fmt/core.h | 22 +++------------------- include/fmt/format.h | 13 +++++++------ 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/ChangeLog.rst b/ChangeLog.rst index 982c1117..8cd24414 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -63,6 +63,16 @@ std::vector out; fmt::format_to(std::back_inserter(out), "{}", 42); +* Added the `formatted_size + `_ function for + computing output size: + + .. code:: c++ + + #include + + auto size = fmt::formatted_size("{}", 12345); // size == 5 + * Improved compile times by reducing dependencies on standard headers and providing a lightweight `core API `_: diff --git a/doc/api.rst b/doc/api.rst index 2a380a16..0eeab744 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -151,7 +151,7 @@ The following user-defined literals are defined in ``fmt/format.h``. Utilities --------- -.. dosygenfunction:: fmt::formatted_size(string_view, const Args&...) +.. doxygenfunction:: fmt::formatted_size(string_view, const Args&...) .. doxygenfunction:: fmt::to_string(const T&) diff --git a/include/fmt/core.h b/include/fmt/core.h index 66597eed..7a654e2b 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -193,13 +193,11 @@ typename std::add_rvalue_reference::type declval() FMT_NOEXCEPT; } /** - \rst An implementation of ``std::basic_string_view`` for pre-C++17. It provides a subset of the API. ``fmt::basic_string_view`` is used for format strings even if ``std::string_view`` is available to prevent issues when a library is compiled with a different ``-std`` option than the client code (which is not recommended). - \endrst */ template class basic_string_view { @@ -238,11 +236,7 @@ class basic_string_view { basic_string_view(const Char *s) : data_(s), size_(std::char_traits::length(s)) {} - /** - \rst - Constructs a string reference from a ``std::basic_string`` object. - \endrst - */ + /** Constructs a string reference from a ``std::basic_string`` object. */ template FMT_CONSTEXPR basic_string_view( const std::basic_string &s) FMT_NOEXCEPT @@ -329,11 +323,7 @@ class basic_buffer { capacity_ = capacity; } - /** - \rst - Increases the buffer capacity to hold at least *capacity* elements. - \endrst - */ + /** Increases the buffer capacity to hold at least *capacity* elements. */ virtual void grow(std::size_t capacity) = 0; public: @@ -365,11 +355,7 @@ class basic_buffer { size_ = new_size; } - /** - \rst - Reserves space to store at least *capacity* elements. - \endrst - */ + /** Reserves space to store at least *capacity* elements. */ void reserve(std::size_t capacity) { if (capacity > capacity_) grow(capacity); @@ -899,10 +885,8 @@ class basic_format_context : using typename base::iterator; /** - \rst Constructs a ``basic_format_context`` object. References to the arguments are stored in the object so make sure they have appropriate lifetimes. - \endrst */ basic_format_context(OutputIt out, basic_string_view format_str, basic_format_args args) diff --git a/include/fmt/format.h b/include/fmt/format.h index 6e273e84..e9c16b1f 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3391,9 +3391,7 @@ std::string to_string(const T &value) { } /** - \rst Converts *value* to ``std::wstring`` using the default format for type *T*. - \endrst */ template std::wstring to_wstring(const T &value) { @@ -3455,10 +3453,10 @@ inline OutputIt vformat_to(OutputIt out, string_view format_str, Formats arguments, writes the result to the output iterator ``out`` and returns the iterator past the end of the output range. - **Example**:: + **Example**:: - std::vector out; - fmt::format_to(std::back_inserter(out), "{}", 42); + std::vector out; + fmt::format_to(std::back_inserter(out), "{}", 42); \endrst */ template @@ -3535,7 +3533,10 @@ inline typename std::enable_if::value>::type return vprint(format_str.data(), make_format_args(args...)); } -// Counts the number of characters in the output of format(format_str, args...). +/** + Returns the number of characters in the output of + ``format(format_str, args...)``. + */ template inline std::size_t formatted_size(string_view format_str, const Args & ... args) { From 550ef1d29d68c761194c884e33f3db8fe2faf21c Mon Sep 17 00:00:00 2001 From: Michael Winterberg Date: Thu, 17 May 2018 18:03:43 -0700 Subject: [PATCH 4/4] MSVC improvements and data truncation cleanup. MSVC is timid about evaluating constexpr functions unless it has to, so the "TYPES" variables end up in read-write memory even though the optimizer removes the initializer. Making TYPES constexpr causes MSVC to try harder to initialize these variables at compile time, which also ends up completely removing the (named) variable from the final compiled binary. Fixed a data truncation warning being reported in ostream-test. --- include/fmt/core.h | 18 ++++++++++++++---- include/fmt/format.h | 12 +++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 7a654e2b..2625d86b 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -73,7 +73,8 @@ # endif #endif -#if FMT_HAS_FEATURE(cxx_explicit_conversions) +#if FMT_HAS_FEATURE(cxx_explicit_conversions) || \ + FMT_MSC_VER >= 1800 # define FMT_EXPLICIT explicit #else # define FMT_EXPLICIT @@ -970,8 +971,17 @@ class format_arg_store { friend class basic_format_args; + static FMT_CONSTEXPR uint64_t get_types() { + return IS_PACKED ? internal::get_types() + : -static_cast(NUM_ARGS); + } + public: +#if FMT_USE_CONSTEXPR + static constexpr uint64_t TYPES = get_types(); +#else static const uint64_t TYPES; +#endif #if FMT_GCC_VERSION && FMT_GCC_VERSION <= 405 // Workaround an array initialization bug in gcc 4.5 and earlier. @@ -984,10 +994,10 @@ class format_arg_store { #endif }; +#if !FMT_USE_CONSTEXPR template -const uint64_t format_arg_store::TYPES = IS_PACKED ? - internal::get_types() : - -static_cast(NUM_ARGS); +const uint64_t format_arg_store::TYPES = get_types(); +#endif /** \rst diff --git a/include/fmt/format.h b/include/fmt/format.h index e9c16b1f..3d35a18c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1381,13 +1381,13 @@ class float_type_checker : private ErrorHandler { } }; -template +template class char_specs_checker : public ErrorHandler { private: - char type_; + CharType type_; public: - FMT_CONSTEXPR char_specs_checker(char type, ErrorHandler eh) + FMT_CONSTEXPR char_specs_checker(CharType type, ErrorHandler eh) : ErrorHandler(eh), type_(type) {} FMT_CONSTEXPR void on_int() { @@ -3110,8 +3110,10 @@ struct formatter< type_spec, internal::int_type_checker(eh)); break; case internal::char_type: - handle_char_specs(specs_, internal::char_specs_checker( - type_spec, eh)); + handle_char_specs( + specs_, + internal::char_specs_checker( + type_spec, eh)); break; case internal::double_type: case internal::long_double_type: