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_BOOL_H 11 #define _LIBCPP___FORMAT_FORMATTER_BOOL_H 12 13 #include <__algorithm/copy.h> 14 #include <__availability> 15 #include <__config> 16 #include <__debug> 17 #include <__format/format_error.h> 18 #include <__format/format_fwd.h> 19 #include <__format/format_parse_context.h> 20 #include <__format/formatter.h> 21 #include <__format/formatter_integral.h> 22 #include <__format/parser_std_format_spec.h> 23 #include <__utility/unreachable.h> 24 #include <string_view> 25 26 #ifndef _LIBCPP_HAS_NO_LOCALIZATION 27 # include <locale> 28 #endif 29 30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 31 # pragma GCC system_header 32 #endif 33 34 _LIBCPP_BEGIN_NAMESPACE_STD 35 36 #if _LIBCPP_STD_VER > 17 37 38 template <__formatter::__char_type _CharT> 39 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<bool, _CharT> { 40 public: 41 _LIBCPP_HIDE_FROM_ABI constexpr auto 42 parse(basic_format_parse_context<_CharT>& __parse_ctx) -> decltype(__parse_ctx.begin()) { 43 auto __result = __parser_.__parse(__parse_ctx, __format_spec::__fields_integral); 44 __format_spec::__process_parsed_bool(__parser_); 45 return __result; 46 } 47 48 _LIBCPP_HIDE_FROM_ABI auto format(bool __value, auto& __ctx) const -> decltype(__ctx.out()) { 49 switch (__parser_.__type_) { 50 case __format_spec::__type::__default: 51 case __format_spec::__type::__string: 52 return __formatter::__format_bool(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx)); 53 54 case __format_spec::__type::__binary_lower_case: 55 case __format_spec::__type::__binary_upper_case: 56 case __format_spec::__type::__octal: 57 case __format_spec::__type::__decimal: 58 case __format_spec::__type::__hexadecimal_lower_case: 59 case __format_spec::__type::__hexadecimal_upper_case: 60 // Promotes bool to an integral type. This reduces the number of 61 // instantiations of __format_integer reducing code size. 62 return __formatter::__format_integer( 63 static_cast<unsigned>(__value), __ctx, __parser_.__get_parsed_std_specifications(__ctx)); 64 65 default: 66 _LIBCPP_ASSERT(false, "The parse function should have validated the type"); 67 __libcpp_unreachable(); 68 } 69 } 70 71 __format_spec::__parser<_CharT> __parser_; 72 }; 73 74 #endif //_LIBCPP_STD_VER > 17 75 76 _LIBCPP_END_NAMESPACE_STD 77 78 #endif // _LIBCPP___FORMAT_FORMATTER_BOOL_H 79