]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Format all contiguous ranges usign spoan.
authorTomasz Kamiński <tkaminsk@redhat.com>
Wed, 29 Apr 2026 14:06:16 +0000 (16:06 +0200)
committerTomasz Kamiński <tkaminsk@redhat.com>
Wed, 6 May 2026 13:07:32 +0000 (15:07 +0200)
We convert all contiguous ranges into span of the possibly-const qualified
_Tp (__format::__maybe_const<_Tp, _CharT>). The span object is const qualifed,
to trigger _M_format<const span<T>> specialization, that is already used by
formatter specialization for ranges.

This conversion is applied regardless if range is sized, and ranges::distance
to compute the size. Even for user-defined iterators, they will observe range
being iterated only once.

Finally, using raw pointers to iterate range during formatting guarantees,
that no stdio lock will be taken. In consequence it would be now possible
to enable_nonlocking_formatter_optimization for all contiguous ranges.

libstdc++-v3/ChangeLog:

* include/std/format (range_formatter::format): Format all
contiguous ranges as span<__format::__maybe_const<_Tp, _CharT>>.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
libstdc++-v3/include/std/format

index 4627381fa9e73d7ba92d37d7ff2080f48250ff47..60adca93b5d801d5d1708fd3d32370038517d82c 100644 (file)
@@ -6175,7 +6175,13 @@ namespace __format
        format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc) const
        {
          using _Range = remove_reference_t<_Rg>;
-         if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
+         if constexpr (ranges::contiguous_range<_Rg>)
+           {
+             const span<__format::__maybe_const<_Tp, _CharT>>
+               __spn(ranges::data(__rg), size_t(ranges::distance(__rg)));
+             return _M_format(__spn, __fc);
+           }
+         else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
            return _M_format<const _Range>(__rg, __fc);
          else
            return _M_format(__rg, __fc);