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>
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/parser_std_format_spec.h>
21 #include <__utility/unreachable.h>
23 #if _LIBCPP_HAS_LOCALIZATION
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
31 _LIBCPP_BEGIN_NAMESPACE_STD
33 #if _LIBCPP_STD_VER >= 20
35 template <__fmt_char_type _CharT
>
36 struct _LIBCPP_TEMPLATE_VIS formatter
<bool, _CharT
> {
38 template <class _ParseContext
>
39 _LIBCPP_HIDE_FROM_ABI
constexpr typename
_ParseContext::iterator
parse(_ParseContext
& __ctx
) {
40 typename
_ParseContext::iterator __result
= __parser_
.__parse(__ctx
, __format_spec::__fields_integral
);
41 __format_spec::__process_parsed_bool(__parser_
, "a bool");
45 template <class _FormatContext
>
46 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
format(bool __value
, _FormatContext
& __ctx
) const {
47 switch (__parser_
.__type_
) {
48 case __format_spec::__type::__default
:
49 case __format_spec::__type::__string
:
50 return __formatter::__format_bool(__value
, __ctx
, __parser_
.__get_parsed_std_specifications(__ctx
));
52 case __format_spec::__type::__binary_lower_case
:
53 case __format_spec::__type::__binary_upper_case
:
54 case __format_spec::__type::__octal
:
55 case __format_spec::__type::__decimal
:
56 case __format_spec::__type::__hexadecimal_lower_case
:
57 case __format_spec::__type::__hexadecimal_upper_case
:
58 // Promotes bool to an integral type. This reduces the number of
59 // instantiations of __format_integer reducing code size.
60 return __formatter::__format_integer(
61 static_cast<unsigned>(__value
), __ctx
, __parser_
.__get_parsed_std_specifications(__ctx
));
64 _LIBCPP_ASSERT_INTERNAL(false, "The parse function should have validated the type");
65 __libcpp_unreachable();
69 __format_spec::__parser
<_CharT
> __parser_
;
72 # if _LIBCPP_STD_VER >= 23
74 inline constexpr bool enable_nonlocking_formatter_optimization
<bool> = true;
75 # endif // _LIBCPP_STD_VER >= 23
76 #endif // _LIBCPP_STD_VER >= 20
78 _LIBCPP_END_NAMESPACE_STD
80 #endif // _LIBCPP___FORMAT_FORMATTER_BOOL_H