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 <__availability> 14 #include <__config> 15 #include <__format/concepts.h> 16 #include <__format/format_parse_context.h> 17 #include <__format/formatter.h> 18 #include <__format/formatter_output.h> 19 #include <__format/parser_std_format_spec.h> 20 #include <__format/write_escaped.h> 21 #include <string> 22 #include <string_view> 23 24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 25 # pragma GCC system_header 26 #endif 27 28 _LIBCPP_BEGIN_NAMESPACE_STD 29 30 #if _LIBCPP_STD_VER >= 20 31 32 template <__fmt_char_type _CharT> 33 struct _LIBCPP_TEMPLATE_VIS __formatter_string { 34 public: 35 template <class _ParseContext> 36 _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { 37 typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_string); 38 __format_spec::__process_display_type_string(__parser_.__type_); 39 return __result; 40 } 41 42 template <class _FormatContext> 43 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 44 format(basic_string_view<_CharT> __str, _FormatContext& __ctx) const { 45 # if _LIBCPP_STD_VER >= 23 46 if (__parser_.__type_ == __format_spec::__type::__debug) 47 return __formatter::__format_escaped_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 48 # endif 49 50 return __formatter::__write_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 51 } 52 53 # if _LIBCPP_STD_VER >= 23 54 _LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; } 55 # endif 56 57 __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left}; 58 }; 59 60 // Formatter const char*. 61 template <__fmt_char_type _CharT> 62 struct _LIBCPP_TEMPLATE_VIS formatter<const _CharT*, _CharT> : public __formatter_string<_CharT> { 63 using _Base = __formatter_string<_CharT>; 64 65 template <class _FormatContext> 66 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _CharT* __str, _FormatContext& __ctx) const { 67 _LIBCPP_ASSERT_INTERNAL(__str, "The basic_format_arg constructor should have prevented an invalid pointer."); 68 69 __format_spec::__parsed_specifications<_CharT> __specs = _Base::__parser_.__get_parsed_std_specifications(__ctx); 70 # if _LIBCPP_STD_VER >= 23 71 if (_Base::__parser_.__type_ == __format_spec::__type::__debug) 72 return __formatter::__format_escaped_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); 73 # endif 74 75 // When using a center or right alignment and the width option the length 76 // of __str must be known to add the padding upfront. This case is handled 77 // by the base class by converting the argument to a basic_string_view. 78 // 79 // When using left alignment and the width option the padding is added 80 // after outputting __str so the length can be determined while outputting 81 // __str. The same holds true for the precision, during outputting __str it 82 // can be validated whether the precision threshold has been reached. For 83 // now these optimizations aren't implemented. Instead the base class 84 // handles these options. 85 // TODO FMT Implement these improvements. 86 if (__specs.__has_width() || __specs.__has_precision()) 87 return __formatter::__write_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); 88 89 // No formatting required, copy the string to the output. 90 auto __out_it = __ctx.out(); 91 while (*__str) 92 *__out_it++ = *__str++; 93 return __out_it; 94 } 95 }; 96 97 // Formatter char*. 98 template <__fmt_char_type _CharT> 99 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> { 100 using _Base = formatter<const _CharT*, _CharT>; 101 102 template <class _FormatContext> 103 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const { 104 return _Base::format(__str, __ctx); 105 } 106 }; 107 108 // Formatter char[]. 109 template <__fmt_char_type _CharT, size_t _Size> 110 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> { 111 using _Base = __formatter_string<_CharT>; 112 113 template <class _FormatContext> 114 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 115 format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const { 116 return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx); 117 } 118 }; 119 120 // Formatter std::string. 121 template <__fmt_char_type _CharT, class _Traits, class _Allocator> 122 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT> 123 : public __formatter_string<_CharT> { 124 using _Base = __formatter_string<_CharT>; 125 126 template <class _FormatContext> 127 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 128 format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const { 129 // Drop _Traits and _Allocator to have one std::basic_string formatter. 130 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 131 } 132 }; 133 134 // Formatter std::string_view. 135 template <__fmt_char_type _CharT, class _Traits> 136 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> { 137 using _Base = __formatter_string<_CharT>; 138 139 template <class _FormatContext> 140 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 141 format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const { 142 // Drop _Traits to have one std::basic_string_view formatter. 143 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 144 } 145 }; 146 147 #endif //_LIBCPP_STD_VER >= 20 148 149 _LIBCPP_END_NAMESPACE_STD 150 151 #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H 152