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

Improve std formatters #2902

Merged
merged 3 commits into from
May 28, 2022
Merged

Improve std formatters #2902

merged 3 commits into from
May 28, 2022

Conversation

phprus
Copy link
Contributor

@phprus phprus commented May 18, 2022

  1. Improve std::filesystem::path formatter (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1636r2.pdf).
  2. Improve xchar support for std formatters.

Discussion:
f0903ad

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

include/fmt/std.h Outdated Show resolved Hide resolved
namespace detail {

template <typename Char>
void write_escaped_path(basic_memory_buffer<Char>& quoted,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this function for? Avoid string construction if native path's code unit type is the same as Char?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

@vitaut vitaut merged commit 65dd2ea into fmtlib:master May 28, 2022
@vitaut
Copy link
Contributor

vitaut commented May 28, 2022

Thanks!

Wongboo referenced this pull request in Wongboo/pkgsrc Sep 23, 2022
9.1.0 - 2022-08-27
------------------

* ``fmt::formatted_size`` now works at compile time
  . For example
  (`godbolt <https://godbolt.org/z/1MW5rMdf8>`__):

  .. code:: c++

     #include <fmt/compile.h>

     int main() {
       using namespace fmt::literals;
       constexpr size_t n = fmt::formatted_size("{}"_cf, 42);
       fmt::print("{}\n", n); // prints 2
     }

* Fixed handling of invalid UTF-8.

* Improved Unicode support in ``ostream`` overloads of ``print``.

* Fixed handling of the sign specifier in localized formatting on systems with
  32-bit ``wchar_t`` .

* Added support for wide streams to ``fmt::streamed``.

* Added the ``n`` specifier that disables the output of delimiters when
  formatting ranges.
  For example (`godbolt <https://godbolt.org/z/roKqGdj8c>`__):

  .. code:: c++

     #include <fmt/ranges.h>
     #include <vector>

     int main() {
       auto v = std::vector{1, 2, 3};
       fmt::print("{:n}\n", v); // prints 1, 2, 3
     }

* Worked around problematic ``std::string_view`` constructors introduced in
  C++23

* Improve handling (exclusion) of recursive ranges

* Improved error reporting in format string compilation.

* Improved the implementation of
  `Dragonbox <https://github.com/jk-jeon/dragonbox>`_, the algorithm used for
  the default floating-point formatting.

* Fixed issues with floating-point formatting on exotic platforms.

* Improved the implementation of chrono formatting.

* Improved documentation.

* Improved build configuration.

* Fixed various warnings and compilation issues.

9.0.0 - 2022-07-04
------------------

* Switched to the internal floating point formatter for all decimal presentation
  formats. In particular this results in consistent rounding on all platforms
  and removing the ``s[n]printf`` fallback for decimal FP formatting.

* Compile-time floating point formatting no longer requires the header-only
  mode. For example (`godbolt <https://godbolt.org/z/G37PTeG3b>`__):

  .. code:: c++

     #include <array>
     #include <fmt/compile.h>

     consteval auto compile_time_dtoa(double value) -> std::array<char, 10> {
       auto result = std::array<char, 10>();
       fmt::format_to(result.data(), FMT_COMPILE("{}"), value);
       return result;
     }

     constexpr auto answer = compile_time_dtoa(0.42);

  works with the default settings.

* Improved the implementation of
  `Dragonbox <https://github.com/jk-jeon/dragonbox>`_, the algorithm used for
  the default floating-point formatting.

* Made ``fmt::to_string`` work with ``__float128``. This uses the internal
  FP formatter and works even on system without ``__float128`` support in
  ``[s]printf``.

* Disabled automatic ``std::ostream`` insertion operator (``operator<<``)
  discovery when ``fmt/ostream.h`` is included to prevent ODR violations.
  You can get the old behavior by defining ``FMT_DEPRECATED_OSTREAM`` but this
  will be removed in the next major release. Use ``fmt::streamed`` or
  ``fmt::ostream_formatter`` to enable formatting via ``std::ostream`` instead.

* Added ``fmt::ostream_formatter`` that can be used to write ``formatter``
  specializations that perform formatting via ``std::ostream``.
  For example (`godbolt <https://godbolt.org/z/5sEc5qMsf>`__):

  .. code:: c++

     #include <fmt/ostream.h>

     struct date {
       int year, month, day;

       friend std::ostream& operator<<(std::ostream& os, const date& d) {
         return os << d.year << '-' << d.month << '-' << d.day;
       }
     };

     template <> struct fmt::formatter<date> : ostream_formatter {};

     std::string s = fmt::format("The date is {}", date{2012, 12, 9});
     // s == "The date is 2012-12-9"

* Added the ``fmt::streamed`` function that takes an object and formats it
  via ``std::ostream``.
  For example (`godbolt <https://godbolt.org/z/5G3346G1f>`__):

  .. code:: c++

     #include <thread>
     #include <fmt/ostream.h>

     int main() {
       fmt::print("Current thread id: {}\n",
                  fmt::streamed(std::this_thread::get_id()));
     }

  Note that ``fmt/std.h`` provides a ``formatter`` specialization for
  ``std::thread::id`` so you don't need to format it via ``std::ostream``.

* Deprecated implicit conversions of unscoped enums to integers for consistency
  with scoped enums.

* Added an argument-dependent lookup based ``format_as`` extension API to
  simplify formatting of enums.

* Added experimental ``std::variant`` formatting support.
  For example (`godbolt <https://godbolt.org/z/KG9z6cq68>`__):

  .. code:: c++

     #include <variant>
     #include <fmt/std.h>

     int main() {
       auto v = std::variant<int, std::string>(42);
       fmt::print("{}\n", v);
     }

  prints::

     variant(42)

  Thanks `@jehelset <https://github.com/jehelset>`_.

* Added experimental ``std::filesystem::path`` formatting support
  (`#2865 <https://github.com/fmtlib/fmt/issues/2865>`_,
  `#2902 <https://github.com/fmtlib/fmt/pull/2902>`_,
  `#2917 <https://github.com/fmtlib/fmt/issues/2917>`_,
  `#2918 <https://github.com/fmtlib/fmt/pull/2918>`_).
  For example (`godbolt <https://godbolt.org/z/o44dMexEb>`__):

  .. code:: c++

     #include <filesystem>
     #include <fmt/std.h>

     int main() {
       fmt::print("There is no place like {}.", std::filesystem::path("/home"));
     }

  prints::

     There is no place like "/home".

* Added a ``std::thread::id`` formatter to ``fmt/std.h``.
  For example (`godbolt <https://godbolt.org/z/j1azbYf3E>`__):

  .. code:: c++

     #include <thread>
     #include <fmt/std.h>

     int main() {
       fmt::print("Current thread id: {}\n", std::this_thread::get_id());
     }

* Added ``fmt::styled`` that applies a text style to an individual argument.
  .
  For example (`godbolt <https://godbolt.org/z/vWGW7v5M6>`__):

  .. code:: c++

     #include <fmt/chrono.h>
     #include <fmt/color.h>

     int main() {
       auto now = std::chrono::system_clock::now();
       fmt::print(
         "[{}] {}: {}\n",
         fmt::styled(now, fmt::emphasis::bold),
         fmt::styled("error", fg(fmt::color::red)),
         "something went wrong");
     }

* Made ``fmt::print`` overload for text styles correctly handle UTF-8.

* Fixed Unicode handling when writing to an ostream.

* Added support for nested specifiers to range formatting:

  .. code:: c++

     #include <vector>
     #include <fmt/ranges.h>

     int main() {
       fmt::print("{::#x}\n", std::vector{10, 20, 30});
     }

  prints ``[0xa, 0x14, 0x1e]``.

* Implemented escaping of wide strings in ranges.

* Added support for ranges with ``begin`` / ``end`` found via the
  argument-dependent lookup.

* Fixed formatting of certain kinds of ranges of ranges.

* Fixed handling of maps with element types other than ``std::pair``.

* Made tuple formatter enabled only if elements are formattable.

* Made ``fmt::join`` compatible with format string compilation.

* Made compile-time checks work with named arguments of custom types and
  ``std::ostream`` ``print`` overloads.

* Removed ``make_args_checked`` because it is no longer needed for compile-time.

* Removed the following deprecated APIs: ``_format``, ``arg_join``,
  the ``format_to`` overload that takes a memory buffer,
  ``[v]fprintf`` that takes an ``ostream``.

* Removed the deprecated implicit conversion of ``[const] signed char*`` and
  ``[const] unsigned char*`` to C strings.

* Removed the deprecated ``fmt/locale.h``.

* Replaced the deprecated ``fileno()`` with ``descriptor()`` in
  ``buffered_file``.

* Moved ``to_string_view`` to the ``detail`` namespace since it's an
  implementation detail.

* Made access mode of a created file consistent with ``fopen`` by setting
  ``S_IWGRP`` and ``S_IWOTH``.

* Removed a redundant buffer resize when formatting to ``std::ostream``.

* Made precision computation for strings consistent with width.
  .

* Fixed handling of locale separators in floating point formatting.

* Made sign specifiers work with ``__int128_t``.

* Improved support for systems such as CHERI with extra data stored in pointers.

* Improved documentation.

* Improved build configuration.

* Fixed various warnings and compilation issues.
netbsd-srcmastr referenced this pull request in NetBSD/pkgsrc Oct 8, 2022
From Wongboo via Github pull request.

Closes #111.

9.1.0 - 2022-08-27
------------------

* ``fmt::formatted_size`` now works at compile time
  . For example
  (`godbolt <https://godbolt.org/z/1MW5rMdf8>`__):

  .. code:: c++

     #include <fmt/compile.h>

     int main() {
       using namespace fmt::literals;
       constexpr size_t n = fmt::formatted_size("{}"_cf, 42);
       fmt::print("{}\n", n); // prints 2
     }

* Fixed handling of invalid UTF-8.

* Improved Unicode support in ``ostream`` overloads of ``print``.

* Fixed handling of the sign specifier in localized formatting on systems with
  32-bit ``wchar_t`` .

* Added support for wide streams to ``fmt::streamed``.

* Added the ``n`` specifier that disables the output of delimiters when
  formatting ranges.
  For example (`godbolt <https://godbolt.org/z/roKqGdj8c>`__):

  .. code:: c++

     #include <fmt/ranges.h>
     #include <vector>

     int main() {
       auto v = std::vector{1, 2, 3};
       fmt::print("{:n}\n", v); // prints 1, 2, 3
     }

* Worked around problematic ``std::string_view`` constructors introduced in
  C++23

* Improve handling (exclusion) of recursive ranges

* Improved error reporting in format string compilation.

* Improved the implementation of
  `Dragonbox <https://github.com/jk-jeon/dragonbox>`_, the algorithm used for
  the default floating-point formatting.

* Fixed issues with floating-point formatting on exotic platforms.

* Improved the implementation of chrono formatting.

* Improved documentation.

* Improved build configuration.

* Fixed various warnings and compilation issues.

9.0.0 - 2022-07-04
------------------

* Switched to the internal floating point formatter for all decimal presentation
  formats. In particular this results in consistent rounding on all platforms
  and removing the ``s[n]printf`` fallback for decimal FP formatting.

* Compile-time floating point formatting no longer requires the header-only
  mode. For example (`godbolt <https://godbolt.org/z/G37PTeG3b>`__):

  .. code:: c++

     #include <array>
     #include <fmt/compile.h>

     consteval auto compile_time_dtoa(double value) -> std::array<char, 10> {
       auto result = std::array<char, 10>();
       fmt::format_to(result.data(), FMT_COMPILE("{}"), value);
       return result;
     }

     constexpr auto answer = compile_time_dtoa(0.42);

  works with the default settings.

* Improved the implementation of
  `Dragonbox <https://github.com/jk-jeon/dragonbox>`_, the algorithm used for
  the default floating-point formatting.

* Made ``fmt::to_string`` work with ``__float128``. This uses the internal
  FP formatter and works even on system without ``__float128`` support in
  ``[s]printf``.

* Disabled automatic ``std::ostream`` insertion operator (``operator<<``)
  discovery when ``fmt/ostream.h`` is included to prevent ODR violations.
  You can get the old behavior by defining ``FMT_DEPRECATED_OSTREAM`` but this
  will be removed in the next major release. Use ``fmt::streamed`` or
  ``fmt::ostream_formatter`` to enable formatting via ``std::ostream`` instead.

* Added ``fmt::ostream_formatter`` that can be used to write ``formatter``
  specializations that perform formatting via ``std::ostream``.
  For example (`godbolt <https://godbolt.org/z/5sEc5qMsf>`__):

  .. code:: c++

     #include <fmt/ostream.h>

     struct date {
       int year, month, day;

       friend std::ostream& operator<<(std::ostream& os, const date& d) {
         return os << d.year << '-' << d.month << '-' << d.day;
       }
     };

     template <> struct fmt::formatter<date> : ostream_formatter {};

     std::string s = fmt::format("The date is {}", date{2012, 12, 9});
     // s == "The date is 2012-12-9"

* Added the ``fmt::streamed`` function that takes an object and formats it
  via ``std::ostream``.
  For example (`godbolt <https://godbolt.org/z/5G3346G1f>`__):

  .. code:: c++

     #include <thread>
     #include <fmt/ostream.h>

     int main() {
       fmt::print("Current thread id: {}\n",
                  fmt::streamed(std::this_thread::get_id()));
     }

  Note that ``fmt/std.h`` provides a ``formatter`` specialization for
  ``std::thread::id`` so you don't need to format it via ``std::ostream``.

* Deprecated implicit conversions of unscoped enums to integers for consistency
  with scoped enums.

* Added an argument-dependent lookup based ``format_as`` extension API to
  simplify formatting of enums.

* Added experimental ``std::variant`` formatting support.
  For example (`godbolt <https://godbolt.org/z/KG9z6cq68>`__):

  .. code:: c++

     #include <variant>
     #include <fmt/std.h>

     int main() {
       auto v = std::variant<int, std::string>(42);
       fmt::print("{}\n", v);
     }

  prints::

     variant(42)

  Thanks `@jehelset <https://github.com/jehelset>`_.

* Added experimental ``std::filesystem::path`` formatting support
  (`#2865 <https://github.com/fmtlib/fmt/issues/2865>`_,
  `#2902 <https://github.com/fmtlib/fmt/pull/2902>`_,
  `#2917 <https://github.com/fmtlib/fmt/issues/2917>`_,
  `#2918 <https://github.com/fmtlib/fmt/pull/2918>`_).
  For example (`godbolt <https://godbolt.org/z/o44dMexEb>`__):

  .. code:: c++

     #include <filesystem>
     #include <fmt/std.h>

     int main() {
       fmt::print("There is no place like {}.", std::filesystem::path("/home"));
     }

  prints::

     There is no place like "/home".

* Added a ``std::thread::id`` formatter to ``fmt/std.h``.
  For example (`godbolt <https://godbolt.org/z/j1azbYf3E>`__):

  .. code:: c++

     #include <thread>
     #include <fmt/std.h>

     int main() {
       fmt::print("Current thread id: {}\n", std::this_thread::get_id());
     }

* Added ``fmt::styled`` that applies a text style to an individual argument.
  .
  For example (`godbolt <https://godbolt.org/z/vWGW7v5M6>`__):

  .. code:: c++

     #include <fmt/chrono.h>
     #include <fmt/color.h>

     int main() {
       auto now = std::chrono::system_clock::now();
       fmt::print(
         "[{}] {}: {}\n",
         fmt::styled(now, fmt::emphasis::bold),
         fmt::styled("error", fg(fmt::color::red)),
         "something went wrong");
     }

* Made ``fmt::print`` overload for text styles correctly handle UTF-8.

* Fixed Unicode handling when writing to an ostream.

* Added support for nested specifiers to range formatting:

  .. code:: c++

     #include <vector>
     #include <fmt/ranges.h>

     int main() {
       fmt::print("{::#x}\n", std::vector{10, 20, 30});
     }

  prints ``[0xa, 0x14, 0x1e]``.

* Implemented escaping of wide strings in ranges.

* Added support for ranges with ``begin`` / ``end`` found via the
  argument-dependent lookup.

* Fixed formatting of certain kinds of ranges of ranges.

* Fixed handling of maps with element types other than ``std::pair``.

* Made tuple formatter enabled only if elements are formattable.

* Made ``fmt::join`` compatible with format string compilation.

* Made compile-time checks work with named arguments of custom types and
  ``std::ostream`` ``print`` overloads.

* Removed ``make_args_checked`` because it is no longer needed for compile-time.

* Removed the following deprecated APIs: ``_format``, ``arg_join``,
  the ``format_to`` overload that takes a memory buffer,
  ``[v]fprintf`` that takes an ``ostream``.

* Removed the deprecated implicit conversion of ``[const] signed char*`` and
  ``[const] unsigned char*`` to C strings.

* Removed the deprecated ``fmt/locale.h``.

* Replaced the deprecated ``fileno()`` with ``descriptor()`` in
  ``buffered_file``.

* Moved ``to_string_view`` to the ``detail`` namespace since it's an
  implementation detail.

* Made access mode of a created file consistent with ``fopen`` by setting
  ``S_IWGRP`` and ``S_IWOTH``.

* Removed a redundant buffer resize when formatting to ``std::ostream``.

* Made precision computation for strings consistent with width.
  .

* Fixed handling of locale separators in floating point formatting.

* Made sign specifiers work with ``__int128_t``.

* Improved support for systems such as CHERI with extra data stored in pointers.

* Improved documentation.

* Improved build configuration.

* Fixed various warnings and compilation issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants