[libc++][format] Adds bool formatter.

Implements the formatter for Boolean types.
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
  template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.

Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format

Completes:
- P1868 width: clarifying units of width and precision in std::format

Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D103670

NOKEYCHECK=True
GitOrigin-RevId: 7fb9f99f3bb645337b4f4e6a2a3515219be82011
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 568f06b..3460f72 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -140,6 +140,7 @@
   __format/format_parse_context.h
   __format/format_string.h
   __format/formatter.h
+  __format/formatter_bool.h
   __format/formatter_char.h
   __format/formatter_integer.h
   __format/formatter_integral.h
diff --git a/include/__format/formatter_bool.h b/include/__format/formatter_bool.h
new file mode 100644
index 0000000..8d9a1d2
--- /dev/null
+++ b/include/__format/formatter_bool.h
@@ -0,0 +1,145 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FORMAT_FORMATTER_BOOL_H
+#define _LIBCPP___FORMAT_FORMATTER_BOOL_H
+
+#include <__availability>
+#include <__config>
+#include <__format/format_error.h>
+#include <__format/format_fwd.h>
+#include <__format/formatter.h>
+#include <__format/formatter_integral.h>
+#include <__format/parser_std_format_spec.h>
+#include <string_view>
+
+#ifndef _LIBCPP_HAS_NO_LOCALIZATION
+#include <locale>
+#endif
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17
+
+// TODO FMT Remove this once we require compilers with proper C++20 support.
+// If the compiler has no concepts support, the format header will be disabled.
+// Without concepts support enable_if needs to be used and that too much effort
+// to support compilers with partial C++20 support.
+#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+namespace __format_spec {
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS __parser_bool : public __parser_integral<_CharT> {
+public:
+  _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx)
+      -> decltype(__parse_ctx.begin()) {
+    auto __it = __parser_integral<_CharT>::__parse(__parse_ctx);
+
+    switch (this->__type) {
+    case _Flags::_Type::__default:
+      this->__type = _Flags::_Type::__string;
+      [[fallthrough]];
+    case _Flags::_Type::__string:
+      this->__handle_bool();
+      break;
+
+    case _Flags::_Type::__char:
+      this->__handle_char();
+      break;
+
+    case _Flags::_Type::__binary_lower_case:
+    case _Flags::_Type::__binary_upper_case:
+    case _Flags::_Type::__octal:
+    case _Flags::_Type::__decimal:
+    case _Flags::_Type::__hexadecimal_lower_case:
+    case _Flags::_Type::__hexadecimal_upper_case:
+      this->__handle_integer();
+      break;
+
+    default:
+      __throw_format_error(
+          "The format-spec type has a type not supported for a bool argument");
+    }
+
+    return __it;
+  }
+};
+
+template <class _CharT>
+struct _LIBCPP_TEMPLATE_VIS __bool_strings;
+
+template <>
+struct _LIBCPP_TEMPLATE_VIS __bool_strings<char> {
+  static constexpr string_view __true{"true"};
+  static constexpr string_view __false{"false"};
+};
+
+template <>
+struct _LIBCPP_TEMPLATE_VIS __bool_strings<wchar_t> {
+  static constexpr wstring_view __true{L"true"};
+  static constexpr wstring_view __false{L"false"};
+};
+
+template <class _CharT>
+using __formatter_bool = __formatter_integral<__parser_bool<_CharT>>;
+
+} //namespace __format_spec
+
+// [format.formatter.spec]/2.3
+// For each charT, for each cv-unqualified arithmetic type ArithmeticT other
+// than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
+
+template <class _CharT>
+struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<bool, _CharT>
+    : public __format_spec::__formatter_bool<_CharT> {
+  using _Base = __format_spec::__formatter_bool<_CharT>;
+
+  _LIBCPP_HIDE_FROM_ABI auto format(bool __value, auto& __ctx)
+      -> decltype(__ctx.out()) {
+    if (this->__type != __format_spec::_Flags::_Type::__string)
+      return _Base::format(static_cast<unsigned char>(__value), __ctx);
+
+    if (this->__width_needs_substitution())
+      this->__substitute_width_arg_id(__ctx.arg(this->__width));
+
+#ifndef _LIBCPP_HAS_NO_LOCALIZATION
+    if (this->__locale_specific_form) {
+      const auto& __np = use_facet<numpunct<_CharT>>(__ctx.locale());
+      basic_string<_CharT> __str = __value ? __np.truename() : __np.falsename();
+      return __formatter::__write_unicode(
+          __ctx.out(), basic_string_view<_CharT>{__str}, this->__width, -1,
+          this->__fill, this->__alignment);
+    }
+#endif
+    basic_string_view<_CharT> __str =
+        __value ? __format_spec::__bool_strings<_CharT>::__true
+                : __format_spec::__bool_strings<_CharT>::__false;
+
+    // The output only uses ASCII so every character is one column.
+    unsigned __size = __str.size();
+    if (__size >= this->__width)
+      return _VSTD::copy(__str.begin(), __str.end(), __ctx.out());
+
+    return __formatter::__write(__ctx.out(), __str.begin(), __str.end(), __size,
+                                this->__width, this->__fill, this->__alignment);
+  }
+};
+
+#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+#endif //_LIBCPP_STD_VER > 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___FORMAT_FORMATTER_BOOL_H
diff --git a/include/format b/include/format
index d4e24b6..9116015 100644
--- a/include/format
+++ b/include/format
@@ -279,6 +279,7 @@
 #include <__format/format_parse_context.h>
 #include <__format/format_string.h>
 #include <__format/formatter.h>
+#include <__format/formatter_bool.h>
 #include <__format/formatter_char.h>
 #include <__format/formatter_integer.h>
 #include <__format/formatter_string.h>
@@ -389,28 +390,6 @@
 // These specializations are helper stubs and not proper formatters.
 // TODO FMT Implement the proper formatter specializations.
 
-// [format.formatter.spec]/2.3
-// For each charT, for each cv-unqualified arithmetic type ArithmeticT other
-// than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
-
-// Boolean.
-template <class _CharT>
-struct _LIBCPP_TEMPLATE_VIS formatter<bool, _CharT> {
-  _LIBCPP_HIDE_FROM_ABI
-  auto parse(auto& __parse_ctx) -> decltype(__parse_ctx.begin()) {
-    // TODO FMT Implement
-    return __parse_ctx.begin();
-  }
-
-  _LIBCPP_HIDE_FROM_ABI
-  auto format(bool __b, auto& __ctx) -> decltype(__ctx.out()) {
-    // TODO FMT Implement using formatting arguments
-    auto __out_it = __ctx.out();
-    *__out_it++ = _CharT('0') + __b;
-    return __out_it;
-  }
-};
-
 // Floating point types.
 // TODO FMT There are no replacements for the floating point stubs due to not
 // having floating point support in std::to_chars yet. These stubs aren't
diff --git a/include/module.modulemap b/include/module.modulemap
index 4f10773..5abe243 100644
--- a/include/module.modulemap
+++ b/include/module.modulemap
@@ -450,6 +450,7 @@
       module format_parse_context   { private header "__format/format_parse_context.h"   }
       module format_string          { private header "__format/format_string.h"          }
       module formatter              { private header "__format/formatter.h"              }
+      module formatter_bool         { private header "__format/formatter_bool.h"         }
       module formatter_char         { private header "__format/formatter_char.h"         }
       module formatter_integer      { private header "__format/formatter_integer.h"      }
       module formatter_integral     { private header "__format/formatter_integral.h"     }