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