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