Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:30

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___FORMAT_RANGE_FORMATTER_H
0011 #define _LIBCPP___CXX03___FORMAT_RANGE_FORMATTER_H
0012 
0013 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0014 #  pragma GCC system_header
0015 #endif
0016 
0017 #include <__cxx03/__algorithm/ranges_copy.h>
0018 #include <__cxx03/__chrono/statically_widen.h>
0019 #include <__cxx03/__concepts/same_as.h>
0020 #include <__cxx03/__config>
0021 #include <__cxx03/__format/buffer.h>
0022 #include <__cxx03/__format/concepts.h>
0023 #include <__cxx03/__format/format_context.h>
0024 #include <__cxx03/__format/format_error.h>
0025 #include <__cxx03/__format/formatter.h>
0026 #include <__cxx03/__format/formatter_output.h>
0027 #include <__cxx03/__format/parser_std_format_spec.h>
0028 #include <__cxx03/__iterator/back_insert_iterator.h>
0029 #include <__cxx03/__ranges/concepts.h>
0030 #include <__cxx03/__ranges/data.h>
0031 #include <__cxx03/__ranges/from_range.h>
0032 #include <__cxx03/__ranges/size.h>
0033 #include <__cxx03/__type_traits/remove_cvref.h>
0034 #include <__cxx03/string_view>
0035 
0036 _LIBCPP_BEGIN_NAMESPACE_STD
0037 
0038 #if _LIBCPP_STD_VER >= 23
0039 
0040 template <class _Tp, class _CharT = char>
0041   requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
0042 struct _LIBCPP_TEMPLATE_VIS range_formatter {
0043   _LIBCPP_HIDE_FROM_ABI constexpr void set_separator(basic_string_view<_CharT> __separator) noexcept {
0044     __separator_ = __separator;
0045   }
0046   _LIBCPP_HIDE_FROM_ABI constexpr void
0047   set_brackets(basic_string_view<_CharT> __opening_bracket, basic_string_view<_CharT> __closing_bracket) noexcept {
0048     __opening_bracket_ = __opening_bracket;
0049     __closing_bracket_ = __closing_bracket;
0050   }
0051 
0052   _LIBCPP_HIDE_FROM_ABI constexpr formatter<_Tp, _CharT>& underlying() noexcept { return __underlying_; }
0053   _LIBCPP_HIDE_FROM_ABI constexpr const formatter<_Tp, _CharT>& underlying() const noexcept { return __underlying_; }
0054 
0055   template <class _ParseContext>
0056   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
0057     auto __begin = __parser_.__parse(__ctx, __format_spec::__fields_range);
0058     auto __end   = __ctx.end();
0059     // Note the cases where __begin == __end in this code only happens when the
0060     // replacement-field has no terminating }, or when the parse is manually
0061     // called with a format-spec. The former is an error and the latter means
0062     // using a formatter without the format functions or print.
0063     if (__begin == __end) [[unlikely]]
0064       return __parse_empty_range_underlying_spec(__ctx, __begin);
0065 
0066     // The n field overrides a possible m type, therefore delay applying the
0067     // effect of n until the type has been procesed.
0068     __parse_type(__begin, __end);
0069     if (__parser_.__clear_brackets_)
0070       set_brackets({}, {});
0071     if (__begin == __end) [[unlikely]]
0072       return __parse_empty_range_underlying_spec(__ctx, __begin);
0073 
0074     bool __has_range_underlying_spec = *__begin == _CharT(':');
0075     if (__has_range_underlying_spec) {
0076       // range-underlying-spec:
0077       //   :  format-spec
0078       ++__begin;
0079     } else if (__begin != __end && *__begin != _CharT('}'))
0080       // When there is no underlaying range the current parse should have
0081       // consumed the format-spec. If not, the not consumed input will be
0082       // processed by the underlying. For example {:-} for a range in invalid,
0083       // the sign field is not present. Without this check the underlying_ will
0084       // get -} as input which my be valid.
0085       std::__throw_format_error("The format specifier should consume the input or end with a '}'");
0086 
0087     __ctx.advance_to(__begin);
0088     __begin = __underlying_.parse(__ctx);
0089 
0090     // This test should not be required if __has_range_underlying_spec is false.
0091     // However this test makes sure the underlying formatter left the parser in
0092     // a valid state. (Note this is not a full protection against evil parsers.
0093     // For example
0094     //   } this is test for the next argument {}
0095     //   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
0096     // could consume more than it should.
0097     if (__begin != __end && *__begin != _CharT('}'))
0098       std::__throw_format_error("The format specifier should consume the input or end with a '}'");
0099 
0100     if (__parser_.__type_ != __format_spec::__type::__default) {
0101       // [format.range.formatter]/6
0102       //   If the range-type is s or ?s, then there shall be no n option and no
0103       //   range-underlying-spec.
0104       if (__parser_.__clear_brackets_) {
0105         if (__parser_.__type_ == __format_spec::__type::__string)
0106           std::__throw_format_error("The n option and type s can't be used together");
0107         std::__throw_format_error("The n option and type ?s can't be used together");
0108       }
0109       if (__has_range_underlying_spec) {
0110         if (__parser_.__type_ == __format_spec::__type::__string)
0111           std::__throw_format_error("Type s and an underlying format specification can't be used together");
0112         std::__throw_format_error("Type ?s and an underlying format specification can't be used together");
0113       }
0114     } else if (!__has_range_underlying_spec)
0115       std::__set_debug_format(__underlying_);
0116 
0117     return __begin;
0118   }
0119 
0120   template <ranges::input_range _Rp, class _FormatContext>
0121     requires formattable<ranges::range_reference_t<_Rp>, _CharT> &&
0122              same_as<remove_cvref_t<ranges::range_reference_t<_Rp>>, _Tp>
0123   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Rp&& __range, _FormatContext& __ctx) const {
0124     __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
0125 
0126     if (!__specs.__has_width())
0127       return __format_range(__range, __ctx, __specs);
0128 
0129     // The size of the buffer needed is:
0130     // - open bracket characters
0131     // - close bracket character
0132     // - n elements where every element may have a different size
0133     // - (n -1) separators
0134     // The size of the element is hard to predict, knowing the type helps but
0135     // it depends on the format-spec. As an initial estimate we guess 6
0136     // characters.
0137     // Typically both brackets are 1 character and the separator is 2
0138     // characters. Which means there will be
0139     //   (n - 1) * 2 + 1 + 1 = n * 2 character
0140     // So estimate 8 times the range size as buffer.
0141     std::size_t __capacity_hint = 0;
0142     if constexpr (std::ranges::sized_range<_Rp>)
0143       __capacity_hint = 8 * ranges::size(__range);
0144     __format::__retarget_buffer<_CharT> __buffer{__capacity_hint};
0145     basic_format_context<typename __format::__retarget_buffer<_CharT>::__iterator, _CharT> __c{
0146         __buffer.__make_output_iterator(), __ctx};
0147 
0148     __format_range(__range, __c, __specs);
0149 
0150     return __formatter::__write_string_no_precision(__buffer.__view(), __ctx.out(), __specs);
0151   }
0152 
0153   template <ranges::input_range _Rp, class _FormatContext>
0154   typename _FormatContext::iterator _LIBCPP_HIDE_FROM_ABI
0155   __format_range(_Rp&& __range, _FormatContext& __ctx, __format_spec::__parsed_specifications<_CharT> __specs) const {
0156     if constexpr (same_as<_Tp, _CharT>) {
0157       switch (__specs.__std_.__type_) {
0158       case __format_spec::__type::__string:
0159       case __format_spec::__type::__debug:
0160         return __format_as_string(__range, __ctx, __specs.__std_.__type_ == __format_spec::__type::__debug);
0161       default:
0162         return __format_as_sequence(__range, __ctx);
0163       }
0164     } else
0165       return __format_as_sequence(__range, __ctx);
0166   }
0167 
0168   template <ranges::input_range _Rp, class _FormatContext>
0169   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0170   __format_as_string(_Rp&& __range, _FormatContext& __ctx, bool __debug_format) const {
0171     // When the range is contiguous use a basic_string_view instead to avoid a
0172     // copy of the underlying data. The basic_string_view formatter
0173     // specialization is the "basic" string formatter in libc++.
0174     if constexpr (ranges::contiguous_range<_Rp> && std::ranges::sized_range<_Rp>) {
0175       std::formatter<basic_string_view<_CharT>, _CharT> __formatter;
0176       if (__debug_format)
0177         __formatter.set_debug_format();
0178       return __formatter.format(
0179           basic_string_view<_CharT>{
0180               ranges::data(__range),
0181               ranges::size(__range),
0182           },
0183           __ctx);
0184     } else {
0185       std::formatter<basic_string<_CharT>, _CharT> __formatter;
0186       if (__debug_format)
0187         __formatter.set_debug_format();
0188       return __formatter.format(basic_string<_CharT>{from_range, __range}, __ctx);
0189     }
0190   }
0191 
0192   template <ranges::input_range _Rp, class _FormatContext>
0193   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0194   __format_as_sequence(_Rp&& __range, _FormatContext& __ctx) const {
0195     __ctx.advance_to(ranges::copy(__opening_bracket_, __ctx.out()).out);
0196     bool __use_separator = false;
0197     for (auto&& __e : __range) {
0198       if (__use_separator)
0199         __ctx.advance_to(ranges::copy(__separator_, __ctx.out()).out);
0200       else
0201         __use_separator = true;
0202 
0203       __ctx.advance_to(__underlying_.format(__e, __ctx));
0204     }
0205 
0206     return ranges::copy(__closing_bracket_, __ctx.out()).out;
0207   }
0208 
0209   __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left};
0210 
0211 private:
0212   template <contiguous_iterator _Iterator>
0213   _LIBCPP_HIDE_FROM_ABI constexpr void __parse_type(_Iterator& __begin, _Iterator __end) {
0214     switch (*__begin) {
0215     case _CharT('m'):
0216       if constexpr (__fmt_pair_like<_Tp>) {
0217         set_brackets(_LIBCPP_STATICALLY_WIDEN(_CharT, "{"), _LIBCPP_STATICALLY_WIDEN(_CharT, "}"));
0218         set_separator(_LIBCPP_STATICALLY_WIDEN(_CharT, ", "));
0219         ++__begin;
0220       } else
0221         std::__throw_format_error("Type m requires a pair or a tuple with two elements");
0222       break;
0223 
0224     case _CharT('s'):
0225       if constexpr (same_as<_Tp, _CharT>) {
0226         __parser_.__type_ = __format_spec::__type::__string;
0227         ++__begin;
0228       } else
0229         std::__throw_format_error("Type s requires character type as formatting argument");
0230       break;
0231 
0232     case _CharT('?'):
0233       ++__begin;
0234       if (__begin == __end || *__begin != _CharT('s'))
0235         std::__throw_format_error("The format specifier should consume the input or end with a '}'");
0236       if constexpr (same_as<_Tp, _CharT>) {
0237         __parser_.__type_ = __format_spec::__type::__debug;
0238         ++__begin;
0239       } else
0240         std::__throw_format_error("Type ?s requires character type as formatting argument");
0241     }
0242   }
0243 
0244   template <class _ParseContext>
0245   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator
0246   __parse_empty_range_underlying_spec(_ParseContext& __ctx, typename _ParseContext::iterator __begin) {
0247     __ctx.advance_to(__begin);
0248     [[maybe_unused]] typename _ParseContext::iterator __result = __underlying_.parse(__ctx);
0249     _LIBCPP_ASSERT_INTERNAL(__result == __begin,
0250                             "the underlying's parse function should not advance the input beyond the end of the input");
0251     return __begin;
0252   }
0253 
0254   formatter<_Tp, _CharT> __underlying_;
0255   basic_string_view<_CharT> __separator_       = _LIBCPP_STATICALLY_WIDEN(_CharT, ", ");
0256   basic_string_view<_CharT> __opening_bracket_ = _LIBCPP_STATICALLY_WIDEN(_CharT, "[");
0257   basic_string_view<_CharT> __closing_bracket_ = _LIBCPP_STATICALLY_WIDEN(_CharT, "]");
0258 };
0259 
0260 #endif //_LIBCPP_STD_VER >= 23
0261 
0262 _LIBCPP_END_NAMESPACE_STD
0263 
0264 #endif // _LIBCPP___CXX03___FORMAT_RANGE_FORMATTER_H