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