xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/formatter_bool.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1349cc55cSDimitry Andric // -*- C++ -*-
2349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
3349cc55cSDimitry Andric //
4349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7349cc55cSDimitry Andric //
8349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
9349cc55cSDimitry Andric 
10349cc55cSDimitry Andric #ifndef _LIBCPP___FORMAT_FORMATTER_BOOL_H
11349cc55cSDimitry Andric #define _LIBCPP___FORMAT_FORMATTER_BOOL_H
12349cc55cSDimitry Andric 
1381ad6265SDimitry Andric #include <__algorithm/copy.h>
1406c3fb27SDimitry Andric #include <__assert>
15349cc55cSDimitry Andric #include <__config>
16bdd1243dSDimitry Andric #include <__format/concepts.h>
1781ad6265SDimitry Andric #include <__format/format_parse_context.h>
18349cc55cSDimitry Andric #include <__format/formatter.h>
19349cc55cSDimitry Andric #include <__format/formatter_integral.h>
20349cc55cSDimitry Andric #include <__format/parser_std_format_spec.h>
2181ad6265SDimitry Andric #include <__utility/unreachable.h>
22349cc55cSDimitry Andric 
23349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_LOCALIZATION
24*0fca6ea1SDimitry Andric #  include <__locale>
25349cc55cSDimitry Andric #endif
26349cc55cSDimitry Andric 
27349cc55cSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28349cc55cSDimitry Andric #  pragma GCC system_header
29349cc55cSDimitry Andric #endif
30349cc55cSDimitry Andric 
31349cc55cSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
32349cc55cSDimitry Andric 
3306c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
34349cc55cSDimitry Andric 
35bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
3606c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<bool, _CharT> {
37349cc55cSDimitry Andric public:
3806c3fb27SDimitry Andric   template <class _ParseContext>
3906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
4006c3fb27SDimitry Andric     typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_integral);
4106c3fb27SDimitry Andric     __format_spec::__process_parsed_bool(__parser_, "a bool");
4281ad6265SDimitry Andric     return __result;
4381ad6265SDimitry Andric   }
44349cc55cSDimitry Andric 
4506c3fb27SDimitry Andric   template <class _FormatContext>
4606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(bool __value, _FormatContext& __ctx) const {
4781ad6265SDimitry Andric     switch (__parser_.__type_) {
48753f127fSDimitry Andric     case __format_spec::__type::__default:
4981ad6265SDimitry Andric     case __format_spec::__type::__string:
5081ad6265SDimitry Andric       return __formatter::__format_bool(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx));
51349cc55cSDimitry Andric 
5281ad6265SDimitry Andric     case __format_spec::__type::__binary_lower_case:
5381ad6265SDimitry Andric     case __format_spec::__type::__binary_upper_case:
5481ad6265SDimitry Andric     case __format_spec::__type::__octal:
5581ad6265SDimitry Andric     case __format_spec::__type::__decimal:
5681ad6265SDimitry Andric     case __format_spec::__type::__hexadecimal_lower_case:
5781ad6265SDimitry Andric     case __format_spec::__type::__hexadecimal_upper_case:
5881ad6265SDimitry Andric       // Promotes bool to an integral type. This reduces the number of
5981ad6265SDimitry Andric       // instantiations of __format_integer reducing code size.
6081ad6265SDimitry Andric       return __formatter::__format_integer(
6181ad6265SDimitry Andric           static_cast<unsigned>(__value), __ctx, __parser_.__get_parsed_std_specifications(__ctx));
62349cc55cSDimitry Andric 
63349cc55cSDimitry Andric     default:
641db9f3b2SDimitry Andric       _LIBCPP_ASSERT_INTERNAL(false, "The parse function should have validated the type");
6581ad6265SDimitry Andric       __libcpp_unreachable();
6681ad6265SDimitry Andric     }
67349cc55cSDimitry Andric   }
68349cc55cSDimitry Andric 
6981ad6265SDimitry Andric   __format_spec::__parser<_CharT> __parser_;
70349cc55cSDimitry Andric };
71349cc55cSDimitry Andric 
7206c3fb27SDimitry Andric #endif //_LIBCPP_STD_VER >= 20
73349cc55cSDimitry Andric 
74349cc55cSDimitry Andric _LIBCPP_END_NAMESPACE_STD
75349cc55cSDimitry Andric 
76349cc55cSDimitry Andric #endif // _LIBCPP___FORMAT_FORMATTER_BOOL_H
77