| 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_IMPL_H |
| 11 | #define _LIBCPP___FORMAT_FORMATTER_BOOL_IMPL_H |
| 12 | |
| 13 | #include <__assert> |
| 14 | #include <__config> |
| 15 | #include <__format/concepts.h> |
| 16 | #include <__format/formatter_bool.h> |
| 17 | #include <__format/formatter_integral.h> |
| 18 | #include <__format/parser_std_format_spec.h> |
| 19 | #include <__utility/unreachable.h> |
| 20 | |
| 21 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 22 | # pragma GCC system_header |
| 23 | #endif |
| 24 | |
| 25 | #if _LIBCPP_STD_VER >= 20 |
| 26 | |
| 27 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 28 | |
| 29 | // This function is separated from formatter<bool> to avoid pulling in a bunch of code from <format> that we aren't |
| 30 | // required to provide in other headers where we need formatter<bool> itself to be complete. |
| 31 | template <__fmt_char_type _CharT, class _FormatContext> |
| 32 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator |
| 33 | __formatter_bool_format(bool __value, __format_spec::__parser<_CharT> __parser, _FormatContext& __ctx) { |
| 34 | switch (__parser.__type_) { |
| 35 | case __format_spec::__type::__default: |
| 36 | case __format_spec::__type::__string: |
| 37 | return __formatter::__format_bool(__value, __ctx, __parser.__get_parsed_std_specifications(__ctx)); |
| 38 | |
| 39 | case __format_spec::__type::__binary_lower_case: |
| 40 | case __format_spec::__type::__binary_upper_case: |
| 41 | case __format_spec::__type::__octal: |
| 42 | case __format_spec::__type::__decimal: |
| 43 | case __format_spec::__type::__hexadecimal_lower_case: |
| 44 | case __format_spec::__type::__hexadecimal_upper_case: |
| 45 | // Promotes bool to an integral type. This reduces the number of |
| 46 | // instantiations of __format_integer reducing code size. |
| 47 | return __formatter::__format_integer( |
| 48 | static_cast<unsigned>(__value), __ctx, __parser.__get_parsed_std_specifications(__ctx)); |
| 49 | |
| 50 | default: |
| 51 | _LIBCPP_ASSERT_INTERNAL(false, "The parse function should have validated the type"); |
| 52 | __libcpp_unreachable(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | _LIBCPP_END_NAMESPACE_STD |
| 57 | |
| 58 | #endif // _LIBCPP_STD_VER >= 20 |
| 59 | |
| 60 | #endif // _LIBCPP___FORMAT_FORMATTER_BOOL_IMPL_H |