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_CHAR_H 11 #define _LIBCPP___FORMAT_FORMATTER_CHAR_H 12 13 #include <__availability> 14 #include <__concepts/same_as.h> 15 #include <__config> 16 #include <__format/concepts.h> 17 #include <__format/format_parse_context.h> 18 #include <__format/formatter.h> 19 #include <__format/formatter_integral.h> 20 #include <__format/formatter_output.h> 21 #include <__format/parser_std_format_spec.h> 22 #include <__format/write_escaped.h> 23 #include <__type_traits/conditional.h> 24 #include <__type_traits/is_signed.h> 25 26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27 # pragma GCC system_header 28 #endif 29 30 _LIBCPP_BEGIN_NAMESPACE_STD 31 32 #if _LIBCPP_STD_VER >= 20 33 34 template <__fmt_char_type _CharT> 35 struct _LIBCPP_TEMPLATE_VIS __formatter_char { 36 public: 37 template <class _ParseContext> 38 _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { 39 typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_integral); 40 __format_spec::__process_parsed_char(__parser_, "a character"); 41 return __result; 42 } 43 44 template <class _FormatContext> 45 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT __value, _FormatContext& __ctx) const { 46 if (__parser_.__type_ == __format_spec::__type::__default || __parser_.__type_ == __format_spec::__type::__char) 47 return __formatter::__format_char(__value, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 48 49 # if _LIBCPP_STD_VER >= 23 50 if (__parser_.__type_ == __format_spec::__type::__debug) 51 return __formatter::__format_escaped_char(__value, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 52 # endif 53 54 if constexpr (sizeof(_CharT) <= sizeof(int)) 55 // Promotes _CharT to an integral type. This reduces the number of 56 // instantiations of __format_integer reducing code size. 57 return __formatter::__format_integer( 58 static_cast<conditional_t<is_signed_v<_CharT>, int, unsigned>>(__value), 59 __ctx, 60 __parser_.__get_parsed_std_specifications(__ctx)); 61 else 62 return __formatter::__format_integer(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx)); 63 } 64 65 template <class _FormatContext> 66 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(char __value, _FormatContext& __ctx) const 67 requires(same_as<_CharT, wchar_t>) 68 { 69 return format(static_cast<wchar_t>(__value), __ctx); 70 } 71 72 # if _LIBCPP_STD_VER >= 23 73 _LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; } 74 # endif 75 76 __format_spec::__parser<_CharT> __parser_; 77 }; 78 79 template <> 80 struct _LIBCPP_TEMPLATE_VIS formatter<char, char> : public __formatter_char<char> {}; 81 82 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 83 template <> 84 struct _LIBCPP_TEMPLATE_VIS formatter<char, wchar_t> : public __formatter_char<wchar_t> {}; 85 86 template <> 87 struct _LIBCPP_TEMPLATE_VIS formatter<wchar_t, wchar_t> : public __formatter_char<wchar_t> { 88 }; 89 90 # endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 91 92 #endif //_LIBCPP_STD_VER >= 20 93 94 _LIBCPP_END_NAMESPACE_STD 95 96 #endif // _LIBCPP___FORMAT_FORMATTER_CHAR_H 97