Back to home page

EIC code displayed by LXR

 
 

    


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

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_FORMATTER_STRING_H
0011 #define _LIBCPP___CXX03___FORMAT_FORMATTER_STRING_H
0012 
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__format/concepts.h>
0015 #include <__cxx03/__format/format_parse_context.h>
0016 #include <__cxx03/__format/formatter.h>
0017 #include <__cxx03/__format/formatter_output.h>
0018 #include <__cxx03/__format/parser_std_format_spec.h>
0019 #include <__cxx03/__format/write_escaped.h>
0020 #include <__cxx03/string>
0021 #include <__cxx03/string_view>
0022 
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 #  pragma GCC system_header
0025 #endif
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028 
0029 #if _LIBCPP_STD_VER >= 20
0030 
0031 template <__fmt_char_type _CharT>
0032 struct _LIBCPP_TEMPLATE_VIS __formatter_string {
0033 public:
0034   template <class _ParseContext>
0035   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
0036     typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_string);
0037     __format_spec::__process_display_type_string(__parser_.__type_);
0038     return __result;
0039   }
0040 
0041   template <class _FormatContext>
0042   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0043   format(basic_string_view<_CharT> __str, _FormatContext& __ctx) const {
0044 #  if _LIBCPP_STD_VER >= 23
0045     if (__parser_.__type_ == __format_spec::__type::__debug)
0046       return __formatter::__format_escaped_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx));
0047 #  endif
0048 
0049     return __formatter::__write_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx));
0050   }
0051 
0052 #  if _LIBCPP_STD_VER >= 23
0053   _LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; }
0054 #  endif
0055 
0056   __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left};
0057 };
0058 
0059 // Formatter const char*.
0060 template <__fmt_char_type _CharT>
0061 struct _LIBCPP_TEMPLATE_VIS formatter<const _CharT*, _CharT> : public __formatter_string<_CharT> {
0062   using _Base = __formatter_string<_CharT>;
0063 
0064   template <class _FormatContext>
0065   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _CharT* __str, _FormatContext& __ctx) const {
0066     _LIBCPP_ASSERT_INTERNAL(__str, "The basic_format_arg constructor should have prevented an invalid pointer.");
0067 
0068     __format_spec::__parsed_specifications<_CharT> __specs = _Base::__parser_.__get_parsed_std_specifications(__ctx);
0069 #  if _LIBCPP_STD_VER >= 23
0070     if (_Base::__parser_.__type_ == __format_spec::__type::__debug)
0071       return __formatter::__format_escaped_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
0072 #  endif
0073 
0074     // When using a center or right alignment and the width option the length
0075     // of __str must be known to add the padding upfront. This case is handled
0076     // by the base class by converting the argument to a basic_string_view.
0077     //
0078     // When using left alignment and the width option the padding is added
0079     // after outputting __str so the length can be determined while outputting
0080     // __str. The same holds true for the precision, during outputting __str it
0081     // can be validated whether the precision threshold has been reached. For
0082     // now these optimizations aren't implemented. Instead the base class
0083     // handles these options.
0084     // TODO FMT Implement these improvements.
0085     if (__specs.__has_width() || __specs.__has_precision())
0086       return __formatter::__write_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
0087 
0088     // No formatting required, copy the string to the output.
0089     auto __out_it = __ctx.out();
0090     while (*__str)
0091       *__out_it++ = *__str++;
0092     return __out_it;
0093   }
0094 };
0095 
0096 // Formatter char*.
0097 template <__fmt_char_type _CharT>
0098 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> {
0099   using _Base = formatter<const _CharT*, _CharT>;
0100 
0101   template <class _FormatContext>
0102   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const {
0103     return _Base::format(__str, __ctx);
0104   }
0105 };
0106 
0107 // Formatter char[].
0108 template <__fmt_char_type _CharT, size_t _Size>
0109 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> {
0110   using _Base = __formatter_string<_CharT>;
0111 
0112   template <class _FormatContext>
0113   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0114   format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const {
0115     return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx);
0116   }
0117 };
0118 
0119 // Formatter std::string.
0120 template <__fmt_char_type _CharT, class _Traits, class _Allocator>
0121 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT>
0122     : public __formatter_string<_CharT> {
0123   using _Base = __formatter_string<_CharT>;
0124 
0125   template <class _FormatContext>
0126   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0127   format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const {
0128     // Drop _Traits and _Allocator to have one std::basic_string formatter.
0129     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
0130   }
0131 };
0132 
0133 // Formatter std::string_view.
0134 template <__fmt_char_type _CharT, class _Traits>
0135 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> {
0136   using _Base = __formatter_string<_CharT>;
0137 
0138   template <class _FormatContext>
0139   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0140   format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const {
0141     // Drop _Traits to have one std::basic_string_view formatter.
0142     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
0143   }
0144 };
0145 
0146 #endif //_LIBCPP_STD_VER >= 20
0147 
0148 _LIBCPP_END_NAMESPACE_STD
0149 
0150 #endif // _LIBCPP___CXX03___FORMAT_FORMATTER_STRING_H