1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___FORMAT_FORMATTER_STRING_H 11 #define _LIBCPP___FORMAT_FORMATTER_STRING_H 12 13 #include <__config> 14 #include <__format/concepts.h> 15 #include <__format/format_parse_context.h> 16 #include <__format/formatter.h> 17 #include <__format/formatter_output.h> 18 #include <__format/parser_std_format_spec.h> 19 #include <__format/write_escaped.h> 20 #include <string> 21 #include <string_view> 22 23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 24 # pragma GCC system_header 25 #endif 26 27 _LIBCPP_BEGIN_NAMESPACE_STD 28 29 #if _LIBCPP_STD_VER >= 20 30 31 template <__fmt_char_type _CharT> 32 struct _LIBCPP_TEMPLATE_VIS __formatter_string { 33 public: 34 template <class _ParseContext> 35 _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { 36 typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_string); 37 __format_spec::__process_display_type_string(__parser_.__type_); 38 return __result; 39 } 40 41 template <class _FormatContext> 42 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 43 format(basic_string_view<_CharT> __str, _FormatContext& __ctx) const { 44 # if _LIBCPP_STD_VER >= 23 45 if (__parser_.__type_ == __format_spec::__type::__debug) 46 return __formatter::__format_escaped_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 47 # endif 48 49 return __formatter::__write_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 50 } 51 52 # if _LIBCPP_STD_VER >= 23 53 _LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; } 54 # endif 55 56 __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left}; 57 }; 58 59 // Formatter const char*. 60 template <__fmt_char_type _CharT> 61 struct _LIBCPP_TEMPLATE_VIS formatter<const _CharT*, _CharT> : public __formatter_string<_CharT> { 62 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; 63 64 template <class _FormatContext> 65 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _CharT* __str, _FormatContext& __ctx) const { 66 _LIBCPP_ASSERT_INTERNAL(__str, "The basic_format_arg constructor should have prevented an invalid pointer."); 67 // Converting the input to a basic_string_view means the data is looped over twice; 68 // - once to determine the length, and 69 // - once to process the data. 70 // 71 // This sounds slower than writing the output directly. However internally 72 // the output algorithms have optimizations for "bulk" operations, which 73 // makes this faster than a single-pass character-by-character output. 74 return _Base::format(basic_string_view<_CharT>(__str), __ctx); 75 } 76 }; 77 78 // Formatter char*. 79 template <__fmt_char_type _CharT> 80 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> { 81 using _Base _LIBCPP_NODEBUG = formatter<const _CharT*, _CharT>; 82 83 template <class _FormatContext> 84 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const { 85 return _Base::format(__str, __ctx); 86 } 87 }; 88 89 // Formatter char[]. 90 template <__fmt_char_type _CharT, size_t _Size> 91 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> { 92 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; 93 94 template <class _FormatContext> 95 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 96 format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const { 97 return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx); 98 } 99 }; 100 101 // Formatter std::string. 102 template <__fmt_char_type _CharT, class _Traits, class _Allocator> 103 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT> 104 : public __formatter_string<_CharT> { 105 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; 106 107 template <class _FormatContext> 108 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 109 format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const { 110 // Drop _Traits and _Allocator to have one std::basic_string formatter. 111 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 112 } 113 }; 114 115 // Formatter std::string_view. 116 template <__fmt_char_type _CharT, class _Traits> 117 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> { 118 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; 119 120 template <class _FormatContext> 121 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 122 format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const { 123 // Drop _Traits to have one std::basic_string_view formatter. 124 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 125 } 126 }; 127 128 # if _LIBCPP_STD_VER >= 23 129 template <> 130 inline constexpr bool enable_nonlocking_formatter_optimization<char*> = true; 131 template <> 132 inline constexpr bool enable_nonlocking_formatter_optimization<const char*> = true; 133 template <size_t _Size> 134 inline constexpr bool enable_nonlocking_formatter_optimization<char[_Size]> = true; 135 template <class _Traits, class _Allocator> 136 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<char, _Traits, _Allocator>> = true; 137 template <class _Traits> 138 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<char, _Traits>> = true; 139 140 # if _LIBCPP_HAS_WIDE_CHARACTERS 141 template <> 142 inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t*> = true; 143 template <> 144 inline constexpr bool enable_nonlocking_formatter_optimization<const wchar_t*> = true; 145 template <size_t _Size> 146 inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t[_Size]> = true; 147 template <class _Traits, class _Allocator> 148 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Traits, _Allocator>> = true; 149 template <class _Traits> 150 inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Traits>> = true; 151 # endif // _LIBCPP_HAS_WIDE_CHARACTERS 152 # endif // _LIBCPP_STD_VER >= 23 153 #endif // _LIBCPP_STD_VER >= 20 154 155 _LIBCPP_END_NAMESPACE_STD 156 157 #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H 158