Back to home page

EIC code displayed by LXR

 
 

    


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

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___FORMAT_FORMATTER_STRING_H
0011 #define _LIBCPP___FORMAT_FORMATTER_STRING_H
0012 
0013 #include <__config>
0014 #include <__format/concepts.h>
0015 #include <__format/format_parse_context.h>
0016 #include <__format/formatter.h>
0017 #include <__format/formatter_output.h>
0018 #include <__format/parser_std_format_spec.h>
0019 #include <__format/write_escaped.h>
0020 #include <string>
0021 #include <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 _LIBCPP_NODEBUG = __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     // Converting the input to a basic_string_view means the data is looped over twice;
0068     // - once to determine the length, and
0069     // - once to process the data.
0070     //
0071     // This sounds slower than writing the output directly. However internally
0072     // the output algorithms have optimizations for "bulk" operations, which
0073     // makes this faster than a single-pass character-by-character output.
0074     return _Base::format(basic_string_view<_CharT>(__str), __ctx);
0075   }
0076 };
0077 
0078 // Formatter char*.
0079 template <__fmt_char_type _CharT>
0080 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> {
0081   using _Base _LIBCPP_NODEBUG = formatter<const _CharT*, _CharT>;
0082 
0083   template <class _FormatContext>
0084   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const {
0085     return _Base::format(__str, __ctx);
0086   }
0087 };
0088 
0089 // Formatter char[].
0090 template <__fmt_char_type _CharT, size_t _Size>
0091 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> {
0092   using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>;
0093 
0094   template <class _FormatContext>
0095   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0096   format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const {
0097     return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx);
0098   }
0099 };
0100 
0101 // Formatter std::string.
0102 template <__fmt_char_type _CharT, class _Traits, class _Allocator>
0103 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT>
0104     : public __formatter_string<_CharT> {
0105   using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>;
0106 
0107   template <class _FormatContext>
0108   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0109   format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const {
0110     // Drop _Traits and _Allocator to have one std::basic_string formatter.
0111     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
0112   }
0113 };
0114 
0115 // Formatter std::string_view.
0116 template <__fmt_char_type _CharT, class _Traits>
0117 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> {
0118   using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>;
0119 
0120   template <class _FormatContext>
0121   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0122   format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const {
0123     // Drop _Traits to have one std::basic_string_view formatter.
0124     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
0125   }
0126 };
0127 
0128 #  if _LIBCPP_STD_VER >= 23
0129 template <>
0130 inline constexpr bool enable_nonlocking_formatter_optimization<char*> = true;
0131 template <>
0132 inline constexpr bool enable_nonlocking_formatter_optimization<const char*> = true;
0133 template <size_t _Size>
0134 inline constexpr bool enable_nonlocking_formatter_optimization<char[_Size]> = true;
0135 template <class _Traits, class _Allocator>
0136 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<char, _Traits, _Allocator>> = true;
0137 template <class _Traits>
0138 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<char, _Traits>> = true;
0139 
0140 #    if _LIBCPP_HAS_WIDE_CHARACTERS
0141 template <>
0142 inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t*> = true;
0143 template <>
0144 inline constexpr bool enable_nonlocking_formatter_optimization<const wchar_t*> = true;
0145 template <size_t _Size>
0146 inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t[_Size]> = true;
0147 template <class _Traits, class _Allocator>
0148 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Traits, _Allocator>> = true;
0149 template <class _Traits>
0150 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Traits>> = true;
0151 #    endif // _LIBCPP_HAS_WIDE_CHARACTERS
0152 #  endif   // _LIBCPP_STD_VER >= 23
0153 #endif     // _LIBCPP_STD_VER >= 20
0154 
0155 _LIBCPP_END_NAMESPACE_STD
0156 
0157 #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H