2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FORMAT_FORMATTER_BOOL_H
11 #define _LIBCPP___FORMAT_FORMATTER_BOOL_H
13 #include <__algorithm/copy.h>
15 #include <__availability>
17 #include <__format/concepts.h>
18 #include <__format/format_parse_context.h>
19 #include <__format/formatter.h>
20 #include <__format/formatter_integral.h>
21 #include <__format/parser_std_format_spec.h>
22 #include <__utility/unreachable.h>
24 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 #if _LIBCPP_STD_VER >= 20
36 template <__fmt_char_type _CharT
>
37 struct _LIBCPP_TEMPLATE_VIS formatter
<bool, _CharT
> {
39 template <class _ParseContext
>
40 _LIBCPP_HIDE_FROM_ABI
constexpr typename
_ParseContext::iterator
parse(_ParseContext
& __ctx
) {
41 typename
_ParseContext::iterator __result
= __parser_
.__parse(__ctx
, __format_spec::__fields_integral
);
42 __format_spec::__process_parsed_bool(__parser_
, "a bool");
46 template <class _FormatContext
>
47 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
format(bool __value
, _FormatContext
& __ctx
) const {
48 switch (__parser_
.__type_
) {
49 case __format_spec::__type::__default
:
50 case __format_spec::__type::__string
:
51 return __formatter::__format_bool(__value
, __ctx
, __parser_
.__get_parsed_std_specifications(__ctx
));
53 case __format_spec::__type::__binary_lower_case
:
54 case __format_spec::__type::__binary_upper_case
:
55 case __format_spec::__type::__octal
:
56 case __format_spec::__type::__decimal
:
57 case __format_spec::__type::__hexadecimal_lower_case
:
58 case __format_spec::__type::__hexadecimal_upper_case
:
59 // Promotes bool to an integral type. This reduces the number of
60 // instantiations of __format_integer reducing code size.
61 return __formatter::__format_integer(
62 static_cast<unsigned>(__value
), __ctx
, __parser_
.__get_parsed_std_specifications(__ctx
));
65 _LIBCPP_ASSERT_UNCATEGORIZED(false, "The parse function should have validated the type");
66 __libcpp_unreachable();
70 __format_spec::__parser
<_CharT
> __parser_
;
73 #endif //_LIBCPP_STD_VER >= 20
75 _LIBCPP_END_NAMESPACE_STD
77 #endif // _LIBCPP___FORMAT_FORMATTER_BOOL_H