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_UNCATEGORIZED( 68 __str, 69 "The basic_format_arg constructor should have " 70 "prevented an invalid pointer."); 71 72 __format_spec::__parsed_specifications<_CharT> __specs = _Base::__parser_.__get_parsed_std_specifications(__ctx); 73 # if _LIBCPP_STD_VER >= 23 74 if (_Base::__parser_.__type_ == __format_spec::__type::__debug) 75 return __formatter::__format_escaped_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); 76 # endif 77 78 // When using a center or right alignment and the width option the length 79 // of __str must be known to add the padding upfront. This case is handled 80 // by the base class by converting the argument to a basic_string_view. 81 // 82 // When using left alignment and the width option the padding is added 83 // after outputting __str so the length can be determined while outputting 84 // __str. The same holds true for the precision, during outputting __str it 85 // can be validated whether the precision threshold has been reached. For 86 // now these optimizations aren't implemented. Instead the base class 87 // handles these options. 88 // TODO FMT Implement these improvements. 89 if (__specs.__has_width() || __specs.__has_precision()) 90 return __formatter::__write_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); 91 92 // No formatting required, copy the string to the output. 93 auto __out_it = __ctx.out(); 94 while (*__str) 95 *__out_it++ = *__str++; 96 return __out_it; 97 } 98 }; 99 100 // Formatter char*. 101 template <__fmt_char_type _CharT> 102 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> { 103 using _Base = formatter<const _CharT*, _CharT>; 104 105 template <class _FormatContext> 106 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const { 107 return _Base::format(__str, __ctx); 108 } 109 }; 110 111 // Formatter char[]. 112 template <__fmt_char_type _CharT, size_t _Size> 113 struct _LIBCPP_TEMPLATE_VIS formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> { 114 using _Base = __formatter_string<_CharT>; 115 116 template <class _FormatContext> 117 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 118 format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const { 119 return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx); 120 } 121 }; 122 123 // Formatter std::string. 124 template <__fmt_char_type _CharT, class _Traits, class _Allocator> 125 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT> 126 : public __formatter_string<_CharT> { 127 using _Base = __formatter_string<_CharT>; 128 129 template <class _FormatContext> 130 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 131 format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const { 132 // Drop _Traits and _Allocator to have one std::basic_string formatter. 133 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 134 } 135 }; 136 137 // Formatter std::string_view. 138 template <__fmt_char_type _CharT, class _Traits> 139 struct _LIBCPP_TEMPLATE_VIS formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> { 140 using _Base = __formatter_string<_CharT>; 141 142 template <class _FormatContext> 143 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 144 format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const { 145 // Drop _Traits to have one std::basic_string_view formatter. 146 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 147 } 148 }; 149 150 #endif //_LIBCPP_STD_VER >= 20 151 152 _LIBCPP_END_NAMESPACE_STD 153 154 #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H 155