[libcxx-commits] [libcxx] granularize locale (PR #83735)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sun Mar 3 10:25:27 PST 2024


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/83735

- [libc++][NFC] Replace _ALIGNAS_TYPE with alignas in iostream.cpp
- [libc++] Granularize <__locale>


>From d5140f91bdab634af5f7b630b8305b91277899fe Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 3 Mar 2024 18:15:00 +0100
Subject: [PATCH 1/2] [libc++][NFC] Replace _ALIGNAS_TYPE with alignas in
 iostream.cpp

---
 libcxx/src/iostream.cpp | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)

diff --git a/libcxx/src/iostream.cpp b/libcxx/src/iostream.cpp
index bb49e265ba0ef5..c5ad77a0191608 100644
--- a/libcxx/src/iostream.cpp
+++ b/libcxx/src/iostream.cpp
@@ -21,74 +21,67 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-_ALIGNAS_TYPE(istream)
-_LIBCPP_EXPORTED_FROM_ABI char cin[sizeof(istream)]
+alignas(istream) _LIBCPP_EXPORTED_FROM_ABI char cin[sizeof(istream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream at DU?$char_traits at D@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")
 #endif
         ;
-_ALIGNAS_TYPE(__stdinbuf<char>) static char __cin[sizeof(__stdinbuf<char>)];
+alignas(__stdinbuf<char>) static char __cin[sizeof(__stdinbuf<char>)];
 static mbstate_t mb_cin;
 
 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-_ALIGNAS_TYPE(wistream)
-_LIBCPP_EXPORTED_FROM_ABI char wcin[sizeof(wistream)]
+alignas(wistream) _LIBCPP_EXPORTED_FROM_ABI char wcin[sizeof(wistream)]
 #  if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream at _WU?$char_traits at _W@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")
 #  endif
         ;
-_ALIGNAS_TYPE(__stdinbuf<wchar_t>) static char __wcin[sizeof(__stdinbuf<wchar_t>)];
+alignas(__stdinbuf<wchar_t>) static char __wcin[sizeof(__stdinbuf<wchar_t>)];
 static mbstate_t mb_wcin;
 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
-_ALIGNAS_TYPE(ostream)
-_LIBCPP_EXPORTED_FROM_ABI char cout[sizeof(ostream)]
+alignas(ostream) _LIBCPP_EXPORTED_FROM_ABI char cout[sizeof(ostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream at DU?$char_traits at D@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")
 #endif
         ;
-_ALIGNAS_TYPE(__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
+alignas(__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
 static mbstate_t mb_cout;
 
 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-_ALIGNAS_TYPE(wostream)
-_LIBCPP_EXPORTED_FROM_ABI char wcout[sizeof(wostream)]
+alignas(wostream) _LIBCPP_EXPORTED_FROM_ABI char wcout[sizeof(wostream)]
 #  if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream at _WU?$char_traits at _W@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")
 #  endif
         ;
-_ALIGNAS_TYPE(__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
+alignas(__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
 static mbstate_t mb_wcout;
 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
-_ALIGNAS_TYPE(ostream)
-_LIBCPP_EXPORTED_FROM_ABI char cerr[sizeof(ostream)]
+alignas(ostream) _LIBCPP_EXPORTED_FROM_ABI char cerr[sizeof(ostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream at DU?$char_traits at D@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")
 #endif
         ;
-_ALIGNAS_TYPE(__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
+alignas(__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
 static mbstate_t mb_cerr;
 
 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-_ALIGNAS_TYPE(wostream)
-_LIBCPP_EXPORTED_FROM_ABI char wcerr[sizeof(wostream)]
+alignas(wostream) _LIBCPP_EXPORTED_FROM_ABI char wcerr[sizeof(wostream)]
 #  if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream at _WU?$char_traits at _W@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")
 #  endif
         ;
-_ALIGNAS_TYPE(__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
+alignas(__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
 static mbstate_t mb_wcerr;
 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
-_ALIGNAS_TYPE(ostream)
-_LIBCPP_EXPORTED_FROM_ABI char clog[sizeof(ostream)]
+alignas(ostream) _LIBCPP_EXPORTED_FROM_ABI char clog[sizeof(ostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream at DU?$char_traits at D@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")
@@ -96,8 +89,7 @@ _LIBCPP_EXPORTED_FROM_ABI char clog[sizeof(ostream)]
         ;
 
 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-_ALIGNAS_TYPE(wostream)
-_LIBCPP_EXPORTED_FROM_ABI char wclog[sizeof(wostream)]
+alignas(wostream) _LIBCPP_EXPORTED_FROM_ABI char wclog[sizeof(wostream)]
 #  if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
     __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream at _WU?$char_traits at _W@" _LIBCPP_ABI_NAMESPACE_STR
             "@std@@@12 at A")

>From 2e622cffbaa075f92304091bbab7c24eee1f7d37 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 4 Feb 2024 13:29:53 +0100
Subject: [PATCH 2/2] [libc++] Granularize <__locale>

---
 libcxx/include/CMakeLists.txt                 |   15 +-
 libcxx/include/__config                       |    4 -
 libcxx/include/__format/formatter_bool.h      |    4 -
 libcxx/include/__format/formatter_integral.h  |    3 +-
 libcxx/include/__locale                       | 1511 -----------------
 libcxx/include/__locale_dir/codecvt.h         |  518 ++++++
 libcxx/include/__locale_dir/codecvt_base.h    |   28 +
 libcxx/include/__locale_dir/codecvt_byname.h  |   55 +
 libcxx/include/__locale_dir/collate.h         |   94 +
 libcxx/include/__locale_dir/collate_byname.h  |   67 +
 libcxx/include/__locale_dir/ctype.h           |  189 +++
 libcxx/include/__locale_dir/ctype_base.h      |  177 ++
 libcxx/include/__locale_dir/ctype_byname.h    |   73 +
 libcxx/include/__locale_dir/locale.h          |  163 ++
 libcxx/include/__locale_dir/locale_base_api.h |    6 +
 .../locale_base_api/bsd_locale_fallbacks.h    |    1 +
 .../locale_base_api/locale_guard.h            |    3 +-
 .../__locale_dir/locale_base_api/win32.h      |    1 +
 libcxx/include/__locale_dir/narrow_to_utf8.h  |   99 ++
 libcxx/include/__locale_dir/numpunct.h        |   87 +
 libcxx/include/__locale_dir/numpunct_byname.h |   62 +
 libcxx/include/__locale_dir/traits.h          |   95 ++
 libcxx/include/__locale_dir/widen_from_utf8.h |   99 ++
 libcxx/include/__utility/swap.h               |    1 +
 libcxx/include/codecvt                        |    5 +-
 libcxx/include/fstream                        |    1 -
 libcxx/include/ios                            |    5 +-
 libcxx/include/locale                         |   18 +-
 libcxx/include/module.modulemap.in            |   14 +
 libcxx/include/regex                          |    6 +-
 libcxx/src/ios.cpp                            |    2 +-
 libcxx/src/iostream.cpp                       |    2 +-
 libcxx/src/std_stream.h                       |    2 +-
 .../tools/clang_tidy_checks/CMakeLists.txt    |    2 +-
 34 files changed, 1880 insertions(+), 1532 deletions(-)
 delete mode 100644 libcxx/include/__locale
 create mode 100644 libcxx/include/__locale_dir/codecvt.h
 create mode 100644 libcxx/include/__locale_dir/codecvt_base.h
 create mode 100644 libcxx/include/__locale_dir/codecvt_byname.h
 create mode 100644 libcxx/include/__locale_dir/collate.h
 create mode 100644 libcxx/include/__locale_dir/collate_byname.h
 create mode 100644 libcxx/include/__locale_dir/ctype.h
 create mode 100644 libcxx/include/__locale_dir/ctype_base.h
 create mode 100644 libcxx/include/__locale_dir/ctype_byname.h
 create mode 100644 libcxx/include/__locale_dir/locale.h
 create mode 100644 libcxx/include/__locale_dir/narrow_to_utf8.h
 create mode 100644 libcxx/include/__locale_dir/numpunct.h
 create mode 100644 libcxx/include/__locale_dir/numpunct_byname.h
 create mode 100644 libcxx/include/__locale_dir/traits.h
 create mode 100644 libcxx/include/__locale_dir/widen_from_utf8.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index cafd8c6e00d968..227482b7a20e39 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -489,7 +489,15 @@ set(files
   __iterator/sortable.h
   __iterator/unreachable_sentinel.h
   __iterator/wrap_iter.h
-  __locale
+  __locale_dir/codecvt.h
+  __locale_dir/codecvt_base.h
+  __locale_dir/codecvt_byname.h
+  __locale_dir/collate.h
+  __locale_dir/collate_byname.h
+  __locale_dir/ctype.h
+  __locale_dir/ctype_base.h
+  __locale_dir/ctype_byname.h
+  __locale_dir/locale.h
   __locale_dir/locale_base_api.h
   __locale_dir/locale_base_api/android.h
   __locale_dir/locale_base_api/bsd_locale_defaults.h
@@ -501,6 +509,11 @@ set(files
   __locale_dir/locale_base_api/newlib.h
   __locale_dir/locale_base_api/openbsd.h
   __locale_dir/locale_base_api/win32.h
+  __locale_dir/narrow_to_utf8.h
+  __locale_dir/numpunct.h
+  __locale_dir/numpunct_byname.h
+  __locale_dir/traits.h
+  __locale_dir/widen_from_utf8.h
   __math/abs.h
   __math/copysign.h
   __math/error_functions.h
diff --git a/libcxx/include/__config b/libcxx/include/__config
index 8d4d17378b2973..63bfe3a25294e9 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -842,10 +842,6 @@ typedef __char32_t char32_t;
 #    define _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(x)
 #  endif // _LIBCPP_CXX03_LANG
 
-#  if defined(__APPLE__) || defined(__FreeBSD__) || defined(_LIBCPP_MSVCRT_LIKE) || defined(__NetBSD__)
-#    define _LIBCPP_LOCALE__L_EXTENSIONS 1
-#  endif
-
 #  ifdef __FreeBSD__
 #    define _DECLARE_C99_LDBL_MATH 1
 #  endif
diff --git a/libcxx/include/__format/formatter_bool.h b/libcxx/include/__format/formatter_bool.h
index 5e3daff7b3dba6..2abe2550dcfd16 100644
--- a/libcxx/include/__format/formatter_bool.h
+++ b/libcxx/include/__format/formatter_bool.h
@@ -21,10 +21,6 @@
 #include <__format/parser_std_format_spec.h>
 #include <__utility/unreachable.h>
 
-#ifndef _LIBCPP_HAS_NO_LOCALIZATION
-#  include <__locale>
-#endif
-
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
diff --git a/libcxx/include/__format/formatter_integral.h b/libcxx/include/__format/formatter_integral.h
index eca966f8886f84..79a424015b74c0 100644
--- a/libcxx/include/__format/formatter_integral.h
+++ b/libcxx/include/__format/formatter_integral.h
@@ -32,7 +32,8 @@
 #include <string_view>
 
 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
-#  include <__locale>
+#  include <__locale_dir/locale.h>
+#  include <__locale_dir/numpunct.h>
 #endif
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
diff --git a/libcxx/include/__locale b/libcxx/include/__locale
deleted file mode 100644
index 2186db84933103..00000000000000
--- a/libcxx/include/__locale
+++ /dev/null
@@ -1,1511 +0,0 @@
-// -*- 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___LOCALE
-#define _LIBCPP___LOCALE
-
-#include <__availability>
-#include <__config>
-#include <__locale_dir/locale_base_api.h>
-#include <__memory/shared_ptr.h> // __shared_count
-#include <__mutex/once_flag.h>
-#include <__type_traits/make_unsigned.h>
-#include <__utility/no_destroy.h>
-#include <cctype>
-#include <clocale>
-#include <cstdint>
-#include <cstdlib>
-#include <string>
-
-// Some platforms require more includes than others. Keep the includes on all plaforms for now.
-#include <cstddef>
-#include <cstring>
-
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-#  include <cwchar>
-#else
-#  include <__std_mbstate_t.h>
-#endif
-
-#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-#  pragma GCC system_header
-#endif
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-class _LIBCPP_EXPORTED_FROM_ABI locale;
-
-template <class _Facet>
-_LIBCPP_HIDE_FROM_ABI bool has_facet(const locale&) _NOEXCEPT;
-
-template <class _Facet>
-_LIBCPP_HIDE_FROM_ABI const _Facet& use_facet(const locale&);
-
-class _LIBCPP_EXPORTED_FROM_ABI locale {
-public:
-  // types:
-  class _LIBCPP_EXPORTED_FROM_ABI facet;
-  class _LIBCPP_EXPORTED_FROM_ABI id;
-
-  typedef int category;
-
-  static const category // values assigned here are for exposition only
-      none    = 0,
-      collate = LC_COLLATE_MASK, ctype = LC_CTYPE_MASK, monetary = LC_MONETARY_MASK, numeric = LC_NUMERIC_MASK,
-      time = LC_TIME_MASK, messages = LC_MESSAGES_MASK, all = collate | ctype | monetary | numeric | time | messages;
-
-  // construct/copy/destroy:
-  locale() _NOEXCEPT;
-  locale(const locale&) _NOEXCEPT;
-  explicit locale(const char*);
-  explicit locale(const string&);
-  locale(const locale&, const char*, category);
-  locale(const locale&, const string&, category);
-  template <class _Facet>
-  _LIBCPP_HIDE_FROM_ABI locale(const locale&, _Facet*);
-  locale(const locale&, const locale&, category);
-
-  ~locale();
-
-  const locale& operator=(const locale&) _NOEXCEPT;
-
-  template <class _Facet>
-  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS locale combine(const locale&) const;
-
-  // locale operations:
-  string name() const;
-  bool operator==(const locale&) const;
-#if _LIBCPP_STD_VER <= 17
-  _LIBCPP_HIDE_FROM_ABI bool operator!=(const locale& __y) const { return !(*this == __y); }
-#endif
-  template <class _CharT, class _Traits, class _Allocator>
-  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
-  operator()(const basic_string<_CharT, _Traits, _Allocator>&, const basic_string<_CharT, _Traits, _Allocator>&) const;
-
-  // global locale objects:
-  static locale global(const locale&);
-  static const locale& classic();
-
-private:
-  class __imp;
-  __imp* __locale_;
-
-  template <class>
-  friend struct __no_destroy;
-  struct __private_tag {};
-  _LIBCPP_HIDE_FROM_ABI explicit locale(__private_tag, __imp* __loc) : __locale_(__loc) {}
-
-  void __install_ctor(const locale&, facet*, long);
-  static locale& __global();
-  bool has_facet(id&) const;
-  const facet* use_facet(id&) const;
-
-  template <class _Facet>
-  friend bool has_facet(const locale&) _NOEXCEPT;
-  template <class _Facet>
-  friend const _Facet& use_facet(const locale&);
-};
-
-class _LIBCPP_EXPORTED_FROM_ABI locale::facet : public __shared_count {
-protected:
-  _LIBCPP_HIDE_FROM_ABI explicit facet(size_t __refs = 0) : __shared_count(static_cast<long>(__refs) - 1) {}
-
-  ~facet() override;
-
-  //    facet(const facet&) = delete;     // effectively done in __shared_count
-  //    void operator=(const facet&) = delete;
-
-private:
-  void __on_zero_shared() _NOEXCEPT override;
-};
-
-class _LIBCPP_EXPORTED_FROM_ABI locale::id {
-  once_flag __flag_;
-  int32_t __id_;
-
-  static int32_t __next_id;
-
-public:
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR id() : __id_(0) {}
-  void operator=(const id&) = delete;
-  id(const id&)             = delete;
-
-public: // only needed for tests
-  long __get();
-
-  friend class locale;
-  friend class locale::__imp;
-};
-
-template <class _Facet>
-inline _LIBCPP_HIDE_FROM_ABI locale::locale(const locale& __other, _Facet* __f) {
-  __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
-}
-
-template <class _Facet>
-locale locale::combine(const locale& __other) const {
-  if (!std::has_facet<_Facet>(__other))
-    __throw_runtime_error("locale::combine: locale missing facet");
-
-  return locale(*this, &const_cast<_Facet&>(std::use_facet<_Facet>(__other)));
-}
-
-template <class _Facet>
-inline _LIBCPP_HIDE_FROM_ABI bool has_facet(const locale& __l) _NOEXCEPT {
-  return __l.has_facet(_Facet::id);
-}
-
-template <class _Facet>
-inline _LIBCPP_HIDE_FROM_ABI const _Facet& use_facet(const locale& __l) {
-  return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
-}
-
-// template <class _CharT> class collate;
-
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS collate : public locale::facet {
-public:
-  typedef _CharT char_type;
-  typedef basic_string<char_type> string_type;
-
-  _LIBCPP_HIDE_FROM_ABI explicit collate(size_t __refs = 0) : locale::facet(__refs) {}
-
-  _LIBCPP_HIDE_FROM_ABI int
-  compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
-    return do_compare(__lo1, __hi1, __lo2, __hi2);
-  }
-
-  // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
-  // around a dllimport bug that expects an external instantiation.
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE string_type
-  transform(const char_type* __lo, const char_type* __hi) const {
-    return do_transform(__lo, __hi);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI long hash(const char_type* __lo, const char_type* __hi) const { return do_hash(__lo, __hi); }
-
-  static locale::id id;
-
-protected:
-  ~collate() override;
-  virtual int
-  do_compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const;
-  virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const {
-    return string_type(__lo, __hi);
-  }
-  virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
-};
-
-template <class _CharT>
-locale::id collate<_CharT>::id;
-
-template <class _CharT>
-collate<_CharT>::~collate() {}
-
-template <class _CharT>
-int collate<_CharT>::do_compare(
-    const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
-  for (; __lo2 != __hi2; ++__lo1, ++__lo2) {
-    if (__lo1 == __hi1 || *__lo1 < *__lo2)
-      return -1;
-    if (*__lo2 < *__lo1)
-      return 1;
-  }
-  return __lo1 != __hi1;
-}
-
-template <class _CharT>
-long collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const {
-  size_t __h          = 0;
-  const size_t __sr   = __CHAR_BIT__ * sizeof(size_t) - 8;
-  const size_t __mask = size_t(0xF) << (__sr + 4);
-  for (const char_type* __p = __lo; __p != __hi; ++__p) {
-    __h        = (__h << 4) + static_cast<size_t>(*__p);
-    size_t __g = __h & __mask;
-    __h ^= __g | (__g >> __sr);
-  }
-  return static_cast<long>(__h);
-}
-
-extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>;
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>;
-#endif
-
-// template <class CharT> class collate_byname;
-
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS collate_byname;
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI collate_byname<char> : public collate<char> {
-  locale_t __l_;
-
-public:
-  typedef char char_type;
-  typedef basic_string<char_type> string_type;
-
-  explicit collate_byname(const char* __n, size_t __refs = 0);
-  explicit collate_byname(const string& __n, size_t __refs = 0);
-
-protected:
-  ~collate_byname() override;
-  int do_compare(
-      const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
-  string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
-};
-
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI collate_byname<wchar_t> : public collate<wchar_t> {
-  locale_t __l_;
-
-public:
-  typedef wchar_t char_type;
-  typedef basic_string<char_type> string_type;
-
-  explicit collate_byname(const char* __n, size_t __refs = 0);
-  explicit collate_byname(const string& __n, size_t __refs = 0);
-
-protected:
-  ~collate_byname() override;
-
-  int do_compare(
-      const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
-  string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
-};
-#endif
-
-template <class _CharT, class _Traits, class _Allocator>
-bool locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
-                        const basic_string<_CharT, _Traits, _Allocator>& __y) const {
-  return std::use_facet<std::collate<_CharT> >(*this).compare(
-             __x.data(), __x.data() + __x.size(), __y.data(), __y.data() + __y.size()) < 0;
-}
-
-// template <class charT> class ctype
-
-class _LIBCPP_EXPORTED_FROM_ABI ctype_base {
-public:
-#if defined(_LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE)
-  typedef unsigned long mask;
-  static const mask space  = 1 << 0;
-  static const mask print  = 1 << 1;
-  static const mask cntrl  = 1 << 2;
-  static const mask upper  = 1 << 3;
-  static const mask lower  = 1 << 4;
-  static const mask alpha  = 1 << 5;
-  static const mask digit  = 1 << 6;
-  static const mask punct  = 1 << 7;
-  static const mask xdigit = 1 << 8;
-  static const mask blank  = 1 << 9;
-#  if defined(__BIONIC__)
-  // Historically this was a part of regex_traits rather than ctype_base. The
-  // historical value of the constant is preserved for ABI compatibility.
-  static const mask __regex_word = 0x8000;
-#  else
-  static const mask __regex_word = 1 << 10;
-#  endif // defined(__BIONIC__)
-#elif defined(__GLIBC__)
-  typedef unsigned short mask;
-  static const mask space  = _ISspace;
-  static const mask print  = _ISprint;
-  static const mask cntrl  = _IScntrl;
-  static const mask upper  = _ISupper;
-  static const mask lower  = _ISlower;
-  static const mask alpha  = _ISalpha;
-  static const mask digit  = _ISdigit;
-  static const mask punct  = _ISpunct;
-  static const mask xdigit = _ISxdigit;
-  static const mask blank  = _ISblank;
-#  if defined(__mips__) || (BYTE_ORDER == BIG_ENDIAN)
-  static const mask __regex_word = static_cast<mask>(_ISbit(15));
-#  else
-  static const mask __regex_word = 0x80;
-#  endif
-#elif defined(_LIBCPP_MSVCRT_LIKE)
-  typedef unsigned short mask;
-  static const mask space        = _SPACE;
-  static const mask print        = _BLANK | _PUNCT | _ALPHA | _DIGIT;
-  static const mask cntrl        = _CONTROL;
-  static const mask upper        = _UPPER;
-  static const mask lower        = _LOWER;
-  static const mask alpha        = _ALPHA;
-  static const mask digit        = _DIGIT;
-  static const mask punct        = _PUNCT;
-  static const mask xdigit       = _HEX;
-  static const mask blank        = _BLANK;
-  static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used
-#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
-#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
-#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
-#  ifdef __APPLE__
-  typedef __uint32_t mask;
-#  elif defined(__FreeBSD__)
-  typedef unsigned long mask;
-#  elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
-  typedef unsigned short mask;
-#  endif
-  static const mask space  = _CTYPE_S;
-  static const mask print  = _CTYPE_R;
-  static const mask cntrl  = _CTYPE_C;
-  static const mask upper  = _CTYPE_U;
-  static const mask lower  = _CTYPE_L;
-  static const mask alpha  = _CTYPE_A;
-  static const mask digit  = _CTYPE_D;
-  static const mask punct  = _CTYPE_P;
-  static const mask xdigit = _CTYPE_X;
-
-#  if defined(__NetBSD__)
-  static const mask blank = _CTYPE_BL;
-  // NetBSD defines classes up to 0x2000
-  // see sys/ctype_bits.h, _CTYPE_Q
-  static const mask __regex_word = 0x8000;
-#  else
-  static const mask blank        = _CTYPE_B;
-  static const mask __regex_word = 0x80;
-#  endif
-#elif defined(_AIX)
-  typedef unsigned int mask;
-  static const mask space        = _ISSPACE;
-  static const mask print        = _ISPRINT;
-  static const mask cntrl        = _ISCNTRL;
-  static const mask upper        = _ISUPPER;
-  static const mask lower        = _ISLOWER;
-  static const mask alpha        = _ISALPHA;
-  static const mask digit        = _ISDIGIT;
-  static const mask punct        = _ISPUNCT;
-  static const mask xdigit       = _ISXDIGIT;
-  static const mask blank        = _ISBLANK;
-  static const mask __regex_word = 0x8000;
-#elif defined(_NEWLIB_VERSION)
-  // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
-  typedef char mask;
-  // In case char is signed, static_cast is needed to avoid warning on
-  // positive value becomming negative.
-  static const mask space  = static_cast<mask>(_S);
-  static const mask print  = static_cast<mask>(_P | _U | _L | _N | _B);
-  static const mask cntrl  = static_cast<mask>(_C);
-  static const mask upper  = static_cast<mask>(_U);
-  static const mask lower  = static_cast<mask>(_L);
-  static const mask alpha  = static_cast<mask>(_U | _L);
-  static const mask digit  = static_cast<mask>(_N);
-  static const mask punct  = static_cast<mask>(_P);
-  static const mask xdigit = static_cast<mask>(_X | _N);
-  static const mask blank  = static_cast<mask>(_B);
-  // mask is already fully saturated, use a different type in regex_type_traits.
-  static const unsigned short __regex_word = 0x100;
-#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
-#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
-#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
-#elif defined(__MVS__)
-#  if defined(__NATIVE_ASCII_F)
-  typedef unsigned int mask;
-  static const mask space  = _ISSPACE_A;
-  static const mask print  = _ISPRINT_A;
-  static const mask cntrl  = _ISCNTRL_A;
-  static const mask upper  = _ISUPPER_A;
-  static const mask lower  = _ISLOWER_A;
-  static const mask alpha  = _ISALPHA_A;
-  static const mask digit  = _ISDIGIT_A;
-  static const mask punct  = _ISPUNCT_A;
-  static const mask xdigit = _ISXDIGIT_A;
-  static const mask blank  = _ISBLANK_A;
-#  else
-  typedef unsigned short mask;
-  static const mask space  = __ISSPACE;
-  static const mask print  = __ISPRINT;
-  static const mask cntrl  = __ISCNTRL;
-  static const mask upper  = __ISUPPER;
-  static const mask lower  = __ISLOWER;
-  static const mask alpha  = __ISALPHA;
-  static const mask digit  = __ISDIGIT;
-  static const mask punct  = __ISPUNCT;
-  static const mask xdigit = __ISXDIGIT;
-  static const mask blank  = __ISBLANK;
-#  endif
-  static const mask __regex_word = 0x8000;
-#else
-#  error unknown rune table for this platform -- do you mean to define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE?
-#endif
-  static const mask alnum = alpha | digit;
-  static const mask graph = alnum | punct;
-
-  _LIBCPP_HIDE_FROM_ABI ctype_base() {}
-
-  static_assert((__regex_word & ~(std::make_unsigned<mask>::type)(space | print | cntrl | upper | lower | alpha |
-                                                                  digit | punct | xdigit | blank)) == __regex_word,
-                "__regex_word can't overlap other bits");
-};
-
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS ctype;
-
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI ctype<wchar_t> : public locale::facet, public ctype_base {
-public:
-  typedef wchar_t char_type;
-
-  _LIBCPP_HIDE_FROM_ABI explicit ctype(size_t __refs = 0) : locale::facet(__refs) {}
-
-  _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const { return do_is(__m, __c); }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
-    return do_is(__low, __high, __vec);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const {
-    return do_scan_is(__m, __low, __high);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const {
-    return do_scan_not(__m, __low, __high);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
-    return do_toupper(__low, __high);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
-    return do_tolower(__low, __high);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
-
-  _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
-    return do_widen(__low, __high, __to);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const { return do_narrow(__c, __dfault); }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type*
-  narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
-    return do_narrow(__low, __high, __dfault, __to);
-  }
-
-  static locale::id id;
-
-protected:
-  ~ctype() override;
-  virtual bool do_is(mask __m, char_type __c) const;
-  virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
-  virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
-  virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
-  virtual char_type do_toupper(char_type) const;
-  virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
-  virtual char_type do_tolower(char_type) const;
-  virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
-  virtual char_type do_widen(char) const;
-  virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
-  virtual char do_narrow(char_type, char __dfault) const;
-  virtual const char_type*
-  do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
-};
-#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI ctype<char> : public locale::facet, public ctype_base {
-  const mask* __tab_;
-  bool __del_;
-
-public:
-  typedef char char_type;
-
-  explicit ctype(const mask* __tab = nullptr, bool __del = false, size_t __refs = 0);
-
-  _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const {
-    return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) != 0 : false;
-  }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
-    for (; __low != __high; ++__low, ++__vec)
-      *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
-    return __low;
-  }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const {
-    for (; __low != __high; ++__low)
-      if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
-        break;
-    return __low;
-  }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const {
-    for (; __low != __high; ++__low)
-      if (!isascii(*__low) || !(__tab_[static_cast<int>(*__low)] & __m))
-        break;
-    return __low;
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
-    return do_toupper(__low, __high);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
-
-  _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
-    return do_tolower(__low, __high);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
-
-  _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
-    return do_widen(__low, __high, __to);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const { return do_narrow(__c, __dfault); }
-
-  _LIBCPP_HIDE_FROM_ABI const char*
-  narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
-    return do_narrow(__low, __high, __dfault, __to);
-  }
-
-  static locale::id id;
-
-#ifdef _CACHED_RUNES
-  static const size_t table_size = _CACHED_RUNES;
-#else
-  static const size_t table_size = 256; // FIXME: Don't hardcode this.
-#endif
-  _LIBCPP_HIDE_FROM_ABI const mask* table() const _NOEXCEPT { return __tab_; }
-  static const mask* classic_table() _NOEXCEPT;
-#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
-  static const int* __classic_upper_table() _NOEXCEPT;
-  static const int* __classic_lower_table() _NOEXCEPT;
-#endif
-#if defined(__NetBSD__)
-  static const short* __classic_upper_table() _NOEXCEPT;
-  static const short* __classic_lower_table() _NOEXCEPT;
-#endif
-#if defined(__MVS__)
-  static const unsigned short* __classic_upper_table() _NOEXCEPT;
-  static const unsigned short* __classic_lower_table() _NOEXCEPT;
-#endif
-
-protected:
-  ~ctype() override;
-  virtual char_type do_toupper(char_type __c) const;
-  virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
-  virtual char_type do_tolower(char_type __c) const;
-  virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
-  virtual char_type do_widen(char __c) const;
-  virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
-  virtual char do_narrow(char_type __c, char __dfault) const;
-  virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
-};
-
-// template <class CharT> class ctype_byname;
-
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS ctype_byname;
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<char> : public ctype<char> {
-  locale_t __l_;
-
-public:
-  explicit ctype_byname(const char*, size_t = 0);
-  explicit ctype_byname(const string&, size_t = 0);
-
-protected:
-  ~ctype_byname() override;
-  char_type do_toupper(char_type) const override;
-  const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
-  char_type do_tolower(char_type) const override;
-  const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
-};
-
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<wchar_t> : public ctype<wchar_t> {
-  locale_t __l_;
-
-public:
-  explicit ctype_byname(const char*, size_t = 0);
-  explicit ctype_byname(const string&, size_t = 0);
-
-protected:
-  ~ctype_byname() override;
-  bool do_is(mask __m, char_type __c) const override;
-  const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const override;
-  const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const override;
-  const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const override;
-  char_type do_toupper(char_type) const override;
-  const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
-  char_type do_tolower(char_type) const override;
-  const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
-  char_type do_widen(char) const override;
-  const char* do_widen(const char* __low, const char* __high, char_type* __dest) const override;
-  char do_narrow(char_type, char __dfault) const override;
-  const char_type*
-  do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const override;
-};
-#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isspace(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isprint(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool iscntrl(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isupper(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool islower(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isalpha(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isdigit(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool ispunct(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isxdigit(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isalnum(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI bool isgraph(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
-}
-
-template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI bool isblank(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::blank, __c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI _CharT toupper(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).toupper(__c);
-}
-
-template <class _CharT>
-inline _LIBCPP_HIDE_FROM_ABI _CharT tolower(_CharT __c, const locale& __loc) {
-  return std::use_facet<ctype<_CharT> >(__loc).tolower(__c);
-}
-
-// codecvt_base
-
-class _LIBCPP_EXPORTED_FROM_ABI codecvt_base {
-public:
-  _LIBCPP_HIDE_FROM_ABI codecvt_base() {}
-  enum result { ok, partial, error, noconv };
-};
-
-// template <class internT, class externT, class stateT> class codecvt;
-
-template <class _InternT, class _ExternT, class _StateT>
-class _LIBCPP_TEMPLATE_VIS codecvt;
-
-// template <> class codecvt<char, char, mbstate_t>
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI codecvt<char, char, mbstate_t> : public locale::facet, public codecvt_base {
-public:
-  typedef char intern_type;
-  typedef char extern_type;
-  typedef mbstate_t state_type;
-
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
-
-  _LIBCPP_HIDE_FROM_ABI result
-  out(state_type& __st,
-      const intern_type* __frm,
-      const intern_type* __frm_end,
-      const intern_type*& __frm_nxt,
-      extern_type* __to,
-      extern_type* __to_end,
-      extern_type*& __to_nxt) const {
-    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
-    return do_unshift(__st, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  in(state_type& __st,
-     const extern_type* __frm,
-     const extern_type* __frm_end,
-     const extern_type*& __frm_nxt,
-     intern_type* __to,
-     intern_type* __to_end,
-     intern_type*& __to_nxt) const {
-    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
-
-  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
-
-  _LIBCPP_HIDE_FROM_ABI int
-  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
-    return do_length(__st, __frm, __end, __mx);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
-
-  static locale::id id;
-
-protected:
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
-
-  ~codecvt() override;
-
-  virtual result
-  do_out(state_type& __st,
-         const intern_type* __frm,
-         const intern_type* __frm_end,
-         const intern_type*& __frm_nxt,
-         extern_type* __to,
-         extern_type* __to_end,
-         extern_type*& __to_nxt) const;
-  virtual result
-  do_in(state_type& __st,
-        const extern_type* __frm,
-        const extern_type* __frm_end,
-        const extern_type*& __frm_nxt,
-        intern_type* __to,
-        intern_type* __to_end,
-        intern_type*& __to_nxt) const;
-  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
-  virtual int do_encoding() const _NOEXCEPT;
-  virtual bool do_always_noconv() const _NOEXCEPT;
-  virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
-  virtual int do_max_length() const _NOEXCEPT;
-};
-
-// template <> class codecvt<wchar_t, char, mbstate_t>
-
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI codecvt<wchar_t, char, mbstate_t> : public locale::facet, public codecvt_base {
-  locale_t __l_;
-
-public:
-  typedef wchar_t intern_type;
-  typedef char extern_type;
-  typedef mbstate_t state_type;
-
-  explicit codecvt(size_t __refs = 0);
-
-  _LIBCPP_HIDE_FROM_ABI result
-  out(state_type& __st,
-      const intern_type* __frm,
-      const intern_type* __frm_end,
-      const intern_type*& __frm_nxt,
-      extern_type* __to,
-      extern_type* __to_end,
-      extern_type*& __to_nxt) const {
-    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
-    return do_unshift(__st, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  in(state_type& __st,
-     const extern_type* __frm,
-     const extern_type* __frm_end,
-     const extern_type*& __frm_nxt,
-     intern_type* __to,
-     intern_type* __to_end,
-     intern_type*& __to_nxt) const {
-    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
-
-  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
-
-  _LIBCPP_HIDE_FROM_ABI int
-  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
-    return do_length(__st, __frm, __end, __mx);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
-
-  static locale::id id;
-
-protected:
-  explicit codecvt(const char*, size_t __refs = 0);
-
-  ~codecvt() override;
-
-  virtual result
-  do_out(state_type& __st,
-         const intern_type* __frm,
-         const intern_type* __frm_end,
-         const intern_type*& __frm_nxt,
-         extern_type* __to,
-         extern_type* __to_end,
-         extern_type*& __to_nxt) const;
-  virtual result
-  do_in(state_type& __st,
-        const extern_type* __frm,
-        const extern_type* __frm_end,
-        const extern_type*& __frm_nxt,
-        intern_type* __to,
-        intern_type* __to_end,
-        intern_type*& __to_nxt) const;
-  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
-  virtual int do_encoding() const _NOEXCEPT;
-  virtual bool do_always_noconv() const _NOEXCEPT;
-  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
-  virtual int do_max_length() const _NOEXCEPT;
-};
-#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
-
-// template <> class codecvt<char16_t, char, mbstate_t> // deprecated in C++20
-
-template <>
-class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char, mbstate_t>
-    : public locale::facet, public codecvt_base {
-public:
-  typedef char16_t intern_type;
-  typedef char extern_type;
-  typedef mbstate_t state_type;
-
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
-
-  _LIBCPP_HIDE_FROM_ABI result
-  out(state_type& __st,
-      const intern_type* __frm,
-      const intern_type* __frm_end,
-      const intern_type*& __frm_nxt,
-      extern_type* __to,
-      extern_type* __to_end,
-      extern_type*& __to_nxt) const {
-    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
-    return do_unshift(__st, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  in(state_type& __st,
-     const extern_type* __frm,
-     const extern_type* __frm_end,
-     const extern_type*& __frm_nxt,
-     intern_type* __to,
-     intern_type* __to_end,
-     intern_type*& __to_nxt) const {
-    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
-
-  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
-
-  _LIBCPP_HIDE_FROM_ABI int
-  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
-    return do_length(__st, __frm, __end, __mx);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
-
-  static locale::id id;
-
-protected:
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
-
-  ~codecvt() override;
-
-  virtual result
-  do_out(state_type& __st,
-         const intern_type* __frm,
-         const intern_type* __frm_end,
-         const intern_type*& __frm_nxt,
-         extern_type* __to,
-         extern_type* __to_end,
-         extern_type*& __to_nxt) const;
-  virtual result
-  do_in(state_type& __st,
-        const extern_type* __frm,
-        const extern_type* __frm_end,
-        const extern_type*& __frm_nxt,
-        intern_type* __to,
-        intern_type* __to_end,
-        intern_type*& __to_nxt) const;
-  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
-  virtual int do_encoding() const _NOEXCEPT;
-  virtual bool do_always_noconv() const _NOEXCEPT;
-  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
-  virtual int do_max_length() const _NOEXCEPT;
-};
-
-#ifndef _LIBCPP_HAS_NO_CHAR8_T
-
-// template <> class codecvt<char16_t, char8_t, mbstate_t> // C++20
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char8_t, mbstate_t> : public locale::facet, public codecvt_base {
-public:
-  typedef char16_t intern_type;
-  typedef char8_t extern_type;
-  typedef mbstate_t state_type;
-
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
-
-  _LIBCPP_HIDE_FROM_ABI result
-  out(state_type& __st,
-      const intern_type* __frm,
-      const intern_type* __frm_end,
-      const intern_type*& __frm_nxt,
-      extern_type* __to,
-      extern_type* __to_end,
-      extern_type*& __to_nxt) const {
-    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
-    return do_unshift(__st, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  in(state_type& __st,
-     const extern_type* __frm,
-     const extern_type* __frm_end,
-     const extern_type*& __frm_nxt,
-     intern_type* __to,
-     intern_type* __to_end,
-     intern_type*& __to_nxt) const {
-    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
-
-  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
-
-  _LIBCPP_HIDE_FROM_ABI int
-  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
-    return do_length(__st, __frm, __end, __mx);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
-
-  static locale::id id;
-
-protected:
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
-
-  ~codecvt() override;
-
-  virtual result
-  do_out(state_type& __st,
-         const intern_type* __frm,
-         const intern_type* __frm_end,
-         const intern_type*& __frm_nxt,
-         extern_type* __to,
-         extern_type* __to_end,
-         extern_type*& __to_nxt) const;
-  virtual result
-  do_in(state_type& __st,
-        const extern_type* __frm,
-        const extern_type* __frm_end,
-        const extern_type*& __frm_nxt,
-        intern_type* __to,
-        intern_type* __to_end,
-        intern_type*& __to_nxt) const;
-  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
-  virtual int do_encoding() const _NOEXCEPT;
-  virtual bool do_always_noconv() const _NOEXCEPT;
-  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
-  virtual int do_max_length() const _NOEXCEPT;
-};
-
-#endif
-
-// template <> class codecvt<char32_t, char, mbstate_t> // deprecated in C++20
-
-template <>
-class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char, mbstate_t>
-    : public locale::facet, public codecvt_base {
-public:
-  typedef char32_t intern_type;
-  typedef char extern_type;
-  typedef mbstate_t state_type;
-
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
-
-  _LIBCPP_HIDE_FROM_ABI result
-  out(state_type& __st,
-      const intern_type* __frm,
-      const intern_type* __frm_end,
-      const intern_type*& __frm_nxt,
-      extern_type* __to,
-      extern_type* __to_end,
-      extern_type*& __to_nxt) const {
-    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
-    return do_unshift(__st, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  in(state_type& __st,
-     const extern_type* __frm,
-     const extern_type* __frm_end,
-     const extern_type*& __frm_nxt,
-     intern_type* __to,
-     intern_type* __to_end,
-     intern_type*& __to_nxt) const {
-    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
-
-  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
-
-  _LIBCPP_HIDE_FROM_ABI int
-  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
-    return do_length(__st, __frm, __end, __mx);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
-
-  static locale::id id;
-
-protected:
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
-
-  ~codecvt() override;
-
-  virtual result
-  do_out(state_type& __st,
-         const intern_type* __frm,
-         const intern_type* __frm_end,
-         const intern_type*& __frm_nxt,
-         extern_type* __to,
-         extern_type* __to_end,
-         extern_type*& __to_nxt) const;
-  virtual result
-  do_in(state_type& __st,
-        const extern_type* __frm,
-        const extern_type* __frm_end,
-        const extern_type*& __frm_nxt,
-        intern_type* __to,
-        intern_type* __to_end,
-        intern_type*& __to_nxt) const;
-  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
-  virtual int do_encoding() const _NOEXCEPT;
-  virtual bool do_always_noconv() const _NOEXCEPT;
-  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
-  virtual int do_max_length() const _NOEXCEPT;
-};
-
-#ifndef _LIBCPP_HAS_NO_CHAR8_T
-
-// template <> class codecvt<char32_t, char8_t, mbstate_t> // C++20
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char8_t, mbstate_t> : public locale::facet, public codecvt_base {
-public:
-  typedef char32_t intern_type;
-  typedef char8_t extern_type;
-  typedef mbstate_t state_type;
-
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
-
-  _LIBCPP_HIDE_FROM_ABI result
-  out(state_type& __st,
-      const intern_type* __frm,
-      const intern_type* __frm_end,
-      const intern_type*& __frm_nxt,
-      extern_type* __to,
-      extern_type* __to_end,
-      extern_type*& __to_nxt) const {
-    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
-    return do_unshift(__st, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI result
-  in(state_type& __st,
-     const extern_type* __frm,
-     const extern_type* __frm_end,
-     const extern_type*& __frm_nxt,
-     intern_type* __to,
-     intern_type* __to_end,
-     intern_type*& __to_nxt) const {
-    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
-
-  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
-
-  _LIBCPP_HIDE_FROM_ABI int
-  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
-    return do_length(__st, __frm, __end, __mx);
-  }
-
-  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
-
-  static locale::id id;
-
-protected:
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
-
-  ~codecvt() override;
-
-  virtual result
-  do_out(state_type& __st,
-         const intern_type* __frm,
-         const intern_type* __frm_end,
-         const intern_type*& __frm_nxt,
-         extern_type* __to,
-         extern_type* __to_end,
-         extern_type*& __to_nxt) const;
-  virtual result
-  do_in(state_type& __st,
-        const extern_type* __frm,
-        const extern_type* __frm_end,
-        const extern_type*& __frm_nxt,
-        intern_type* __to,
-        intern_type* __to_end,
-        intern_type*& __to_nxt) const;
-  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
-  virtual int do_encoding() const _NOEXCEPT;
-  virtual bool do_always_noconv() const _NOEXCEPT;
-  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
-  virtual int do_max_length() const _NOEXCEPT;
-};
-
-#endif
-
-// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
-
-template <class _InternT, class _ExternT, class _StateT>
-class _LIBCPP_TEMPLATE_VIS codecvt_byname : public codecvt<_InternT, _ExternT, _StateT> {
-public:
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const char* __nm, size_t __refs = 0)
-      : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
-  _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const string& __nm, size_t __refs = 0)
-      : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
-
-protected:
-  ~codecvt_byname() override;
-};
-
-_LIBCPP_SUPPRESS_DEPRECATED_PUSH
-template <class _InternT, class _ExternT, class _StateT>
-codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname() {}
-_LIBCPP_SUPPRESS_DEPRECATED_POP
-
-extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>;
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>;
-#endif
-extern template class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
-    codecvt_byname<char16_t, char, mbstate_t>; // deprecated in C++20
-extern template class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
-    codecvt_byname<char32_t, char, mbstate_t>; // deprecated in C++20
-#ifndef _LIBCPP_HAS_NO_CHAR8_T
-extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char8_t, mbstate_t>; // C++20
-extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char8_t, mbstate_t>; // C++20
-#endif
-
-template <size_t _Np>
-struct __narrow_to_utf8 {
-  template <class _OutputIterator, class _CharT>
-  _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
-};
-
-template <>
-struct __narrow_to_utf8<8> {
-  template <class _OutputIterator, class _CharT>
-  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
-    for (; __wb < __we; ++__wb, ++__s)
-      *__s = *__wb;
-    return __s;
-  }
-};
-
-_LIBCPP_SUPPRESS_DEPRECATED_PUSH
-template <>
-struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
-  _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
-  _LIBCPP_SUPPRESS_DEPRECATED_POP
-
-  ~__narrow_to_utf8() override;
-
-  template <class _OutputIterator, class _CharT>
-  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
-    result __r = ok;
-    mbstate_t __mb;
-    while (__wb < __we && __r != error) {
-      const int __sz = 32;
-      char __buf[__sz];
-      char* __bn;
-      const char16_t* __wn = (const char16_t*)__wb;
-      __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn, __buf, __buf + __sz, __bn);
-      if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
-        __throw_runtime_error("locale not supported");
-      for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
-        *__s = *__p;
-      __wb = (const _CharT*)__wn;
-    }
-    return __s;
-  }
-};
-
-_LIBCPP_SUPPRESS_DEPRECATED_PUSH
-template <>
-struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
-  _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
-  _LIBCPP_SUPPRESS_DEPRECATED_POP
-
-  ~__narrow_to_utf8() override;
-
-  template <class _OutputIterator, class _CharT>
-  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
-    result __r = ok;
-    mbstate_t __mb;
-    while (__wb < __we && __r != error) {
-      const int __sz = 32;
-      char __buf[__sz];
-      char* __bn;
-      const char32_t* __wn = (const char32_t*)__wb;
-      __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn, __buf, __buf + __sz, __bn);
-      if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
-        __throw_runtime_error("locale not supported");
-      for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
-        *__s = *__p;
-      __wb = (const _CharT*)__wn;
-    }
-    return __s;
-  }
-};
-
-template <size_t _Np>
-struct __widen_from_utf8 {
-  template <class _OutputIterator>
-  _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
-};
-
-template <>
-struct __widen_from_utf8<8> {
-  template <class _OutputIterator>
-  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
-    for (; __nb < __ne; ++__nb, ++__s)
-      *__s = *__nb;
-    return __s;
-  }
-};
-
-_LIBCPP_SUPPRESS_DEPRECATED_PUSH
-template <>
-struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
-  _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
-  _LIBCPP_SUPPRESS_DEPRECATED_POP
-
-  ~__widen_from_utf8() override;
-
-  template <class _OutputIterator>
-  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
-    result __r = ok;
-    mbstate_t __mb;
-    while (__nb < __ne && __r != error) {
-      const int __sz = 32;
-      char16_t __buf[__sz];
-      char16_t* __bn;
-      const char* __nn = __nb;
-      __r              = do_in(__mb, __nb, __ne - __nb > __sz ? __nb + __sz : __ne, __nn, __buf, __buf + __sz, __bn);
-      if (__r == codecvt_base::error || __nn == __nb)
-        __throw_runtime_error("locale not supported");
-      for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
-        *__s = *__p;
-      __nb = __nn;
-    }
-    return __s;
-  }
-};
-
-_LIBCPP_SUPPRESS_DEPRECATED_PUSH
-template <>
-struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
-  _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
-  _LIBCPP_SUPPRESS_DEPRECATED_POP
-
-  ~__widen_from_utf8() override;
-
-  template <class _OutputIterator>
-  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
-    result __r = ok;
-    mbstate_t __mb;
-    while (__nb < __ne && __r != error) {
-      const int __sz = 32;
-      char32_t __buf[__sz];
-      char32_t* __bn;
-      const char* __nn = __nb;
-      __r              = do_in(__mb, __nb, __ne - __nb > __sz ? __nb + __sz : __ne, __nn, __buf, __buf + __sz, __bn);
-      if (__r == codecvt_base::error || __nn == __nb)
-        __throw_runtime_error("locale not supported");
-      for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
-        *__s = *__p;
-      __nb = __nn;
-    }
-    return __s;
-  }
-};
-
-// template <class charT> class numpunct
-
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS numpunct;
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI numpunct<char> : public locale::facet {
-public:
-  typedef char char_type;
-  typedef basic_string<char_type> string_type;
-
-  explicit numpunct(size_t __refs = 0);
-
-  _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
-  _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
-  _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
-  _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
-  _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
-
-  static locale::id id;
-
-protected:
-  ~numpunct() override;
-  virtual char_type do_decimal_point() const;
-  virtual char_type do_thousands_sep() const;
-  virtual string do_grouping() const;
-  virtual string_type do_truename() const;
-  virtual string_type do_falsename() const;
-
-  char_type __decimal_point_;
-  char_type __thousands_sep_;
-  string __grouping_;
-};
-
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI numpunct<wchar_t> : public locale::facet {
-public:
-  typedef wchar_t char_type;
-  typedef basic_string<char_type> string_type;
-
-  explicit numpunct(size_t __refs = 0);
-
-  _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
-  _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
-  _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
-  _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
-  _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
-
-  static locale::id id;
-
-protected:
-  ~numpunct() override;
-  virtual char_type do_decimal_point() const;
-  virtual char_type do_thousands_sep() const;
-  virtual string do_grouping() const;
-  virtual string_type do_truename() const;
-  virtual string_type do_falsename() const;
-
-  char_type __decimal_point_;
-  char_type __thousands_sep_;
-  string __grouping_;
-};
-#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
-
-// template <class charT> class numpunct_byname
-
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS numpunct_byname;
-
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<char> : public numpunct<char> {
-public:
-  typedef char char_type;
-  typedef basic_string<char_type> string_type;
-
-  explicit numpunct_byname(const char* __nm, size_t __refs = 0);
-  explicit numpunct_byname(const string& __nm, size_t __refs = 0);
-
-protected:
-  ~numpunct_byname() override;
-
-private:
-  void __init(const char*);
-};
-
-#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
-template <>
-class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<wchar_t> : public numpunct<wchar_t> {
-public:
-  typedef wchar_t char_type;
-  typedef basic_string<char_type> string_type;
-
-  explicit numpunct_byname(const char* __nm, size_t __refs = 0);
-  explicit numpunct_byname(const string& __nm, size_t __refs = 0);
-
-protected:
-  ~numpunct_byname() override;
-
-private:
-  void __init(const char*);
-};
-#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
-
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // _LIBCPP___LOCALE
diff --git a/libcxx/include/__locale_dir/codecvt.h b/libcxx/include/__locale_dir/codecvt.h
new file mode 100644
index 00000000000000..b0e89ebe10335f
--- /dev/null
+++ b/libcxx/include/__locale_dir/codecvt.h
@@ -0,0 +1,518 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_CODECVT_H
+#define _LIBCPP___LOCALE_CODECVT_H
+
+#include <__config>
+#include <__locale_dir/codecvt_base.h>
+#include <__locale_dir/locale.h>
+#include <__locale_dir/locale_base_api.h>
+#include <__std_mbstate_t.h>
+#include <cstddef>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _InternT, class _ExternT, class _StateT>
+class _LIBCPP_TEMPLATE_VIS codecvt;
+
+// template <> class codecvt<char, char, mbstate_t>
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI codecvt<char, char, mbstate_t> : public locale::facet, public codecvt_base {
+public:
+  typedef char intern_type;
+  typedef char extern_type;
+  typedef mbstate_t state_type;
+
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
+
+  _LIBCPP_HIDE_FROM_ABI result
+  out(state_type& __st,
+      const intern_type* __frm,
+      const intern_type* __frm_end,
+      const intern_type*& __frm_nxt,
+      extern_type* __to,
+      extern_type* __to_end,
+      extern_type*& __to_nxt) const {
+    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
+    return do_unshift(__st, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  in(state_type& __st,
+     const extern_type* __frm,
+     const extern_type* __frm_end,
+     const extern_type*& __frm_nxt,
+     intern_type* __to,
+     intern_type* __to_end,
+     intern_type*& __to_nxt) const {
+    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
+
+  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
+
+  _LIBCPP_HIDE_FROM_ABI int
+  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
+    return do_length(__st, __frm, __end, __mx);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
+
+  static locale::id id;
+
+protected:
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
+
+  ~codecvt() override;
+
+  virtual result
+  do_out(state_type& __st,
+         const intern_type* __frm,
+         const intern_type* __frm_end,
+         const intern_type*& __frm_nxt,
+         extern_type* __to,
+         extern_type* __to_end,
+         extern_type*& __to_nxt) const;
+  virtual result
+  do_in(state_type& __st,
+        const extern_type* __frm,
+        const extern_type* __frm_end,
+        const extern_type*& __frm_nxt,
+        intern_type* __to,
+        intern_type* __to_end,
+        intern_type*& __to_nxt) const;
+  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
+  virtual int do_encoding() const _NOEXCEPT;
+  virtual bool do_always_noconv() const _NOEXCEPT;
+  virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
+  virtual int do_max_length() const _NOEXCEPT;
+};
+
+// template <> class codecvt<wchar_t, char, mbstate_t>
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI codecvt<wchar_t, char, mbstate_t> : public locale::facet, public codecvt_base {
+  locale_t __l_;
+
+public:
+  typedef wchar_t intern_type;
+  typedef char extern_type;
+  typedef mbstate_t state_type;
+
+  explicit codecvt(size_t __refs = 0);
+
+  _LIBCPP_HIDE_FROM_ABI result
+  out(state_type& __st,
+      const intern_type* __frm,
+      const intern_type* __frm_end,
+      const intern_type*& __frm_nxt,
+      extern_type* __to,
+      extern_type* __to_end,
+      extern_type*& __to_nxt) const {
+    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
+    return do_unshift(__st, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  in(state_type& __st,
+     const extern_type* __frm,
+     const extern_type* __frm_end,
+     const extern_type*& __frm_nxt,
+     intern_type* __to,
+     intern_type* __to_end,
+     intern_type*& __to_nxt) const {
+    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
+
+  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
+
+  _LIBCPP_HIDE_FROM_ABI int
+  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
+    return do_length(__st, __frm, __end, __mx);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
+
+  static locale::id id;
+
+protected:
+  explicit codecvt(const char*, size_t __refs = 0);
+
+  ~codecvt() override;
+
+  virtual result
+  do_out(state_type& __st,
+         const intern_type* __frm,
+         const intern_type* __frm_end,
+         const intern_type*& __frm_nxt,
+         extern_type* __to,
+         extern_type* __to_end,
+         extern_type*& __to_nxt) const;
+  virtual result
+  do_in(state_type& __st,
+        const extern_type* __frm,
+        const extern_type* __frm_end,
+        const extern_type*& __frm_nxt,
+        intern_type* __to,
+        intern_type* __to_end,
+        intern_type*& __to_nxt) const;
+  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
+  virtual int do_encoding() const _NOEXCEPT;
+  virtual bool do_always_noconv() const _NOEXCEPT;
+  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
+  virtual int do_max_length() const _NOEXCEPT;
+};
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
+
+// template <> class codecvt<char16_t, char, mbstate_t> // deprecated in C++20
+
+template <>
+class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char, mbstate_t>
+    : public locale::facet, public codecvt_base {
+public:
+  typedef char16_t intern_type;
+  typedef char extern_type;
+  typedef mbstate_t state_type;
+
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
+
+  _LIBCPP_HIDE_FROM_ABI result
+  out(state_type& __st,
+      const intern_type* __frm,
+      const intern_type* __frm_end,
+      const intern_type*& __frm_nxt,
+      extern_type* __to,
+      extern_type* __to_end,
+      extern_type*& __to_nxt) const {
+    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
+    return do_unshift(__st, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  in(state_type& __st,
+     const extern_type* __frm,
+     const extern_type* __frm_end,
+     const extern_type*& __frm_nxt,
+     intern_type* __to,
+     intern_type* __to_end,
+     intern_type*& __to_nxt) const {
+    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
+
+  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
+
+  _LIBCPP_HIDE_FROM_ABI int
+  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
+    return do_length(__st, __frm, __end, __mx);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
+
+  static locale::id id;
+
+protected:
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
+
+  ~codecvt() override;
+
+  virtual result
+  do_out(state_type& __st,
+         const intern_type* __frm,
+         const intern_type* __frm_end,
+         const intern_type*& __frm_nxt,
+         extern_type* __to,
+         extern_type* __to_end,
+         extern_type*& __to_nxt) const;
+  virtual result
+  do_in(state_type& __st,
+        const extern_type* __frm,
+        const extern_type* __frm_end,
+        const extern_type*& __frm_nxt,
+        intern_type* __to,
+        intern_type* __to_end,
+        intern_type*& __to_nxt) const;
+  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
+  virtual int do_encoding() const _NOEXCEPT;
+  virtual bool do_always_noconv() const _NOEXCEPT;
+  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
+  virtual int do_max_length() const _NOEXCEPT;
+};
+
+#ifndef _LIBCPP_HAS_NO_CHAR8_T
+
+// template <> class codecvt<char16_t, char8_t, mbstate_t> // C++20
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI codecvt<char16_t, char8_t, mbstate_t> : public locale::facet, public codecvt_base {
+public:
+  typedef char16_t intern_type;
+  typedef char8_t extern_type;
+  typedef mbstate_t state_type;
+
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
+
+  _LIBCPP_HIDE_FROM_ABI result
+  out(state_type& __st,
+      const intern_type* __frm,
+      const intern_type* __frm_end,
+      const intern_type*& __frm_nxt,
+      extern_type* __to,
+      extern_type* __to_end,
+      extern_type*& __to_nxt) const {
+    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
+    return do_unshift(__st, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  in(state_type& __st,
+     const extern_type* __frm,
+     const extern_type* __frm_end,
+     const extern_type*& __frm_nxt,
+     intern_type* __to,
+     intern_type* __to_end,
+     intern_type*& __to_nxt) const {
+    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
+
+  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
+
+  _LIBCPP_HIDE_FROM_ABI int
+  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
+    return do_length(__st, __frm, __end, __mx);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
+
+  static locale::id id;
+
+protected:
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
+
+  ~codecvt() override;
+
+  virtual result
+  do_out(state_type& __st,
+         const intern_type* __frm,
+         const intern_type* __frm_end,
+         const intern_type*& __frm_nxt,
+         extern_type* __to,
+         extern_type* __to_end,
+         extern_type*& __to_nxt) const;
+  virtual result
+  do_in(state_type& __st,
+        const extern_type* __frm,
+        const extern_type* __frm_end,
+        const extern_type*& __frm_nxt,
+        intern_type* __to,
+        intern_type* __to_end,
+        intern_type*& __to_nxt) const;
+  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
+  virtual int do_encoding() const _NOEXCEPT;
+  virtual bool do_always_noconv() const _NOEXCEPT;
+  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
+  virtual int do_max_length() const _NOEXCEPT;
+};
+
+#endif
+
+// template <> class codecvt<char32_t, char, mbstate_t> // deprecated in C++20
+
+template <>
+class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char, mbstate_t>
+    : public locale::facet, public codecvt_base {
+public:
+  typedef char32_t intern_type;
+  typedef char extern_type;
+  typedef mbstate_t state_type;
+
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
+
+  _LIBCPP_HIDE_FROM_ABI result
+  out(state_type& __st,
+      const intern_type* __frm,
+      const intern_type* __frm_end,
+      const intern_type*& __frm_nxt,
+      extern_type* __to,
+      extern_type* __to_end,
+      extern_type*& __to_nxt) const {
+    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
+    return do_unshift(__st, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  in(state_type& __st,
+     const extern_type* __frm,
+     const extern_type* __frm_end,
+     const extern_type*& __frm_nxt,
+     intern_type* __to,
+     intern_type* __to_end,
+     intern_type*& __to_nxt) const {
+    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
+
+  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
+
+  _LIBCPP_HIDE_FROM_ABI int
+  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
+    return do_length(__st, __frm, __end, __mx);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
+
+  static locale::id id;
+
+protected:
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
+
+  ~codecvt() override;
+
+  virtual result
+  do_out(state_type& __st,
+         const intern_type* __frm,
+         const intern_type* __frm_end,
+         const intern_type*& __frm_nxt,
+         extern_type* __to,
+         extern_type* __to_end,
+         extern_type*& __to_nxt) const;
+  virtual result
+  do_in(state_type& __st,
+        const extern_type* __frm,
+        const extern_type* __frm_end,
+        const extern_type*& __frm_nxt,
+        intern_type* __to,
+        intern_type* __to_end,
+        intern_type*& __to_nxt) const;
+  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
+  virtual int do_encoding() const _NOEXCEPT;
+  virtual bool do_always_noconv() const _NOEXCEPT;
+  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
+  virtual int do_max_length() const _NOEXCEPT;
+};
+
+#ifndef _LIBCPP_HAS_NO_CHAR8_T
+
+// template <> class codecvt<char32_t, char8_t, mbstate_t> // C++20
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI codecvt<char32_t, char8_t, mbstate_t> : public locale::facet, public codecvt_base {
+public:
+  typedef char32_t intern_type;
+  typedef char8_t extern_type;
+  typedef mbstate_t state_type;
+
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
+
+  _LIBCPP_HIDE_FROM_ABI result
+  out(state_type& __st,
+      const intern_type* __frm,
+      const intern_type* __frm_end,
+      const intern_type*& __frm_nxt,
+      extern_type* __to,
+      extern_type* __to_end,
+      extern_type*& __to_nxt) const {
+    return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const {
+    return do_unshift(__st, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI result
+  in(state_type& __st,
+     const extern_type* __frm,
+     const extern_type* __frm_end,
+     const extern_type*& __frm_nxt,
+     intern_type* __to,
+     intern_type* __to_end,
+     intern_type*& __to_nxt) const {
+    return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int encoding() const _NOEXCEPT { return do_encoding(); }
+
+  _LIBCPP_HIDE_FROM_ABI bool always_noconv() const _NOEXCEPT { return do_always_noconv(); }
+
+  _LIBCPP_HIDE_FROM_ABI int
+  length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const {
+    return do_length(__st, __frm, __end, __mx);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI int max_length() const _NOEXCEPT { return do_max_length(); }
+
+  static locale::id id;
+
+protected:
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt(const char*, size_t __refs = 0) : locale::facet(__refs) {}
+
+  ~codecvt() override;
+
+  virtual result
+  do_out(state_type& __st,
+         const intern_type* __frm,
+         const intern_type* __frm_end,
+         const intern_type*& __frm_nxt,
+         extern_type* __to,
+         extern_type* __to_end,
+         extern_type*& __to_nxt) const;
+  virtual result
+  do_in(state_type& __st,
+        const extern_type* __frm,
+        const extern_type* __frm_end,
+        const extern_type*& __frm_nxt,
+        intern_type* __to,
+        intern_type* __to_end,
+        intern_type*& __to_nxt) const;
+  virtual result do_unshift(state_type& __st, extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
+  virtual int do_encoding() const _NOEXCEPT;
+  virtual bool do_always_noconv() const _NOEXCEPT;
+  virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
+  virtual int do_max_length() const _NOEXCEPT;
+};
+
+#endif
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_CODECVT_H
diff --git a/libcxx/include/__locale_dir/codecvt_base.h b/libcxx/include/__locale_dir/codecvt_base.h
new file mode 100644
index 00000000000000..96708e30f67dee
--- /dev/null
+++ b/libcxx/include/__locale_dir/codecvt_base.h
@@ -0,0 +1,28 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_CODECVT_BASE_H
+#define _LIBCPP___LOCALE_CODECVT_BASE_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+class _LIBCPP_EXPORTED_FROM_ABI codecvt_base {
+public:
+  _LIBCPP_HIDE_FROM_ABI codecvt_base() {}
+  enum result { ok, partial, error, noconv };
+};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_CODECVT_BASE_H
diff --git a/libcxx/include/__locale_dir/codecvt_byname.h b/libcxx/include/__locale_dir/codecvt_byname.h
new file mode 100644
index 00000000000000..7a3835b581edb5
--- /dev/null
+++ b/libcxx/include/__locale_dir/codecvt_byname.h
@@ -0,0 +1,55 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_CODECVT_BYNAME_H
+#define _LIBCPP___LOCALE_CODECVT_BYNAME_H
+
+#include <__config>
+#include <__locale_dir/codecvt.h>
+#include <cstddef>
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _InternT, class _ExternT, class _StateT>
+class _LIBCPP_TEMPLATE_VIS codecvt_byname : public codecvt<_InternT, _ExternT, _StateT> {
+public:
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const char* __nm, size_t __refs = 0)
+      : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
+  _LIBCPP_HIDE_FROM_ABI explicit codecvt_byname(const string& __nm, size_t __refs = 0)
+      : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
+
+protected:
+  ~codecvt_byname() override;
+};
+
+_LIBCPP_SUPPRESS_DEPRECATED_PUSH
+template <class _InternT, class _ExternT, class _StateT>
+codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname() {}
+_LIBCPP_SUPPRESS_DEPRECATED_POP
+
+extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>;
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>;
+#endif
+extern template class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
+    codecvt_byname<char16_t, char, mbstate_t>; // deprecated in C++20
+extern template class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
+    codecvt_byname<char32_t, char, mbstate_t>; // deprecated in C++20
+#ifndef _LIBCPP_HAS_NO_CHAR8_T
+extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char8_t, mbstate_t>; // C++20
+extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char8_t, mbstate_t>; // C++20
+#endif
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_CODECVT_BYNAME_H
diff --git a/libcxx/include/__locale_dir/collate.h b/libcxx/include/__locale_dir/collate.h
new file mode 100644
index 00000000000000..43cabd5abac7cb
--- /dev/null
+++ b/libcxx/include/__locale_dir/collate.h
@@ -0,0 +1,94 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_COLLATE_H
+#define _LIBCPP___LOCALE_COLLATE_H
+
+#include <__config>
+#include <__locale_dir/locale.h>
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS collate : public locale::facet {
+public:
+  typedef _CharT char_type;
+  typedef basic_string<char_type> string_type;
+
+  _LIBCPP_HIDE_FROM_ABI explicit collate(size_t __refs = 0) : locale::facet(__refs) {}
+
+  _LIBCPP_HIDE_FROM_ABI int
+  compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
+    return do_compare(__lo1, __hi1, __lo2, __hi2);
+  }
+
+  // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
+  // around a dllimport bug that expects an external instantiation.
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE string_type
+  transform(const char_type* __lo, const char_type* __hi) const {
+    return do_transform(__lo, __hi);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI long hash(const char_type* __lo, const char_type* __hi) const { return do_hash(__lo, __hi); }
+
+  static locale::id id;
+
+protected:
+  ~collate() override;
+  virtual int
+  do_compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const;
+  virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const {
+    return string_type(__lo, __hi);
+  }
+  virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
+};
+
+template <class _CharT>
+locale::id collate<_CharT>::id;
+
+template <class _CharT>
+collate<_CharT>::~collate() {}
+
+template <class _CharT>
+int collate<_CharT>::do_compare(
+    const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
+  for (; __lo2 != __hi2; ++__lo1, ++__lo2) {
+    if (__lo1 == __hi1 || *__lo1 < *__lo2)
+      return -1;
+    if (*__lo2 < *__lo1)
+      return 1;
+  }
+  return __lo1 != __hi1;
+}
+
+template <class _CharT>
+long collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const {
+  size_t __h          = 0;
+  const size_t __sr   = __CHAR_BIT__ * sizeof(size_t) - 8;
+  const size_t __mask = size_t(0xF) << (__sr + 4);
+  for (const char_type* __p = __lo; __p != __hi; ++__p) {
+    __h        = (__h << 4) + static_cast<size_t>(*__p);
+    size_t __g = __h & __mask;
+    __h ^= __g | (__g >> __sr);
+  }
+  return static_cast<long>(__h);
+}
+
+extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>;
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>;
+#endif
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_COLLATE_H
diff --git a/libcxx/include/__locale_dir/collate_byname.h b/libcxx/include/__locale_dir/collate_byname.h
new file mode 100644
index 00000000000000..81d21a330fdef8
--- /dev/null
+++ b/libcxx/include/__locale_dir/collate_byname.h
@@ -0,0 +1,67 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_COLLATE_BYNAME_H
+#define _LIBCPP___LOCALE_COLLATE_BYNAME_H
+
+#include <__config>
+#include <__locale_dir/collate.h>
+#include <__locale_dir/locale_base_api.h>
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS collate_byname;
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI collate_byname<char> : public collate<char> {
+  locale_t __l_;
+
+public:
+  typedef char char_type;
+  typedef basic_string<char_type> string_type;
+
+  explicit collate_byname(const char* __n, size_t __refs = 0);
+  explicit collate_byname(const string& __n, size_t __refs = 0);
+
+protected:
+  ~collate_byname() override;
+  int do_compare(
+      const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
+  string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
+};
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI collate_byname<wchar_t> : public collate<wchar_t> {
+  locale_t __l_;
+
+public:
+  typedef wchar_t char_type;
+  typedef basic_string<char_type> string_type;
+
+  explicit collate_byname(const char* __n, size_t __refs = 0);
+  explicit collate_byname(const string& __n, size_t __refs = 0);
+
+protected:
+  ~collate_byname() override;
+
+  int do_compare(
+      const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
+  string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
+};
+#endif
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_COLLATE_BYNAME_H
diff --git a/libcxx/include/__locale_dir/ctype.h b/libcxx/include/__locale_dir/ctype.h
new file mode 100644
index 00000000000000..60de4c2877fed9
--- /dev/null
+++ b/libcxx/include/__locale_dir/ctype.h
@@ -0,0 +1,189 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_CTYPE_H
+#define _LIBCPP___LOCALE_CTYPE_H
+
+#include <__config>
+#include <__locale_dir/ctype_base.h>
+#include <__locale_dir/locale.h>
+#include <__locale_dir/locale_base_api.h>
+#include <cstddef>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS ctype;
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI ctype<wchar_t> : public locale::facet, public ctype_base {
+public:
+  typedef wchar_t char_type;
+
+  _LIBCPP_HIDE_FROM_ABI explicit ctype(size_t __refs = 0) : locale::facet(__refs) {}
+
+  _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const { return do_is(__m, __c); }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
+    return do_is(__low, __high, __vec);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const {
+    return do_scan_is(__m, __low, __high);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const {
+    return do_scan_not(__m, __low, __high);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
+    return do_toupper(__low, __high);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
+    return do_tolower(__low, __high);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
+
+  _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
+    return do_widen(__low, __high, __to);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const { return do_narrow(__c, __dfault); }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type*
+  narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
+    return do_narrow(__low, __high, __dfault, __to);
+  }
+
+  static locale::id id;
+
+protected:
+  ~ctype() override;
+  virtual bool do_is(mask __m, char_type __c) const;
+  virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
+  virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
+  virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
+  virtual char_type do_toupper(char_type) const;
+  virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
+  virtual char_type do_tolower(char_type) const;
+  virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
+  virtual char_type do_widen(char) const;
+  virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
+  virtual char do_narrow(char_type, char __dfault) const;
+  virtual const char_type*
+  do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
+};
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI ctype<char> : public locale::facet, public ctype_base {
+  const mask* __tab_;
+  bool __del_;
+
+public:
+  typedef char char_type;
+
+  explicit ctype(const mask* __tab = nullptr, bool __del = false, size_t __refs = 0);
+
+  _LIBCPP_HIDE_FROM_ABI bool is(mask __m, char_type __c) const {
+    return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) != 0 : false;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const {
+    for (; __low != __high; ++__low, ++__vec)
+      *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
+    return __low;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const {
+    for (; __low != __high; ++__low)
+      if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
+        break;
+    return __low;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const {
+    for (; __low != __high; ++__low)
+      if (!isascii(*__low) || !(__tab_[static_cast<int>(*__low)] & __m))
+        break;
+    return __low;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char_type toupper(char_type __c) const { return do_toupper(__c); }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* toupper(char_type* __low, const char_type* __high) const {
+    return do_toupper(__low, __high);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char_type tolower(char_type __c) const { return do_tolower(__c); }
+
+  _LIBCPP_HIDE_FROM_ABI const char_type* tolower(char_type* __low, const char_type* __high) const {
+    return do_tolower(__low, __high);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char_type widen(char __c) const { return do_widen(__c); }
+
+  _LIBCPP_HIDE_FROM_ABI const char* widen(const char* __low, const char* __high, char_type* __to) const {
+    return do_widen(__low, __high, __to);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI char narrow(char_type __c, char __dfault) const { return do_narrow(__c, __dfault); }
+
+  _LIBCPP_HIDE_FROM_ABI const char*
+  narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const {
+    return do_narrow(__low, __high, __dfault, __to);
+  }
+
+  static locale::id id;
+
+#ifdef _CACHED_RUNES
+  static const size_t table_size = _CACHED_RUNES;
+#else
+  static const size_t table_size = 256; // FIXME: Don't hardcode this.
+#endif
+  _LIBCPP_HIDE_FROM_ABI const mask* table() const _NOEXCEPT { return __tab_; }
+  static const mask* classic_table() _NOEXCEPT;
+#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
+  static const int* __classic_upper_table() _NOEXCEPT;
+  static const int* __classic_lower_table() _NOEXCEPT;
+#endif
+#if defined(__NetBSD__)
+  static const short* __classic_upper_table() _NOEXCEPT;
+  static const short* __classic_lower_table() _NOEXCEPT;
+#endif
+#if defined(__MVS__)
+  static const unsigned short* __classic_upper_table() _NOEXCEPT;
+  static const unsigned short* __classic_lower_table() _NOEXCEPT;
+#endif
+
+protected:
+  ~ctype() override;
+  virtual char_type do_toupper(char_type __c) const;
+  virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
+  virtual char_type do_tolower(char_type __c) const;
+  virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
+  virtual char_type do_widen(char __c) const;
+  virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
+  virtual char do_narrow(char_type __c, char __dfault) const;
+  virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
+};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_CTYPE_H
diff --git a/libcxx/include/__locale_dir/ctype_base.h b/libcxx/include/__locale_dir/ctype_base.h
new file mode 100644
index 00000000000000..3b49212bd58d8b
--- /dev/null
+++ b/libcxx/include/__locale_dir/ctype_base.h
@@ -0,0 +1,177 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_CTYPE_BASE_H
+#define _LIBCPP___LOCALE_CTYPE_BASE_H
+
+#include <__config>
+#include <__type_traits/make_unsigned.h>
+#include <cctype>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+class _LIBCPP_EXPORTED_FROM_ABI ctype_base {
+public:
+#if defined(_LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE)
+  typedef unsigned long mask;
+  static const mask space  = 1 << 0;
+  static const mask print  = 1 << 1;
+  static const mask cntrl  = 1 << 2;
+  static const mask upper  = 1 << 3;
+  static const mask lower  = 1 << 4;
+  static const mask alpha  = 1 << 5;
+  static const mask digit  = 1 << 6;
+  static const mask punct  = 1 << 7;
+  static const mask xdigit = 1 << 8;
+  static const mask blank  = 1 << 9;
+#  if defined(__BIONIC__)
+  // Historically this was a part of regex_traits rather than ctype_base. The
+  // historical value of the constant is preserved for ABI compatibility.
+  static const mask __regex_word = 0x8000;
+#  else
+  static const mask __regex_word = 1 << 10;
+#  endif // defined(__BIONIC__)
+#elif defined(__GLIBC__)
+  typedef unsigned short mask;
+  static const mask space  = _ISspace;
+  static const mask print  = _ISprint;
+  static const mask cntrl  = _IScntrl;
+  static const mask upper  = _ISupper;
+  static const mask lower  = _ISlower;
+  static const mask alpha  = _ISalpha;
+  static const mask digit  = _ISdigit;
+  static const mask punct  = _ISpunct;
+  static const mask xdigit = _ISxdigit;
+  static const mask blank  = _ISblank;
+#  if defined(__mips__) || defined(_LIBCPP_BIG_ENDIAN)
+  static const mask __regex_word = static_cast<mask>(_ISbit(15));
+#  else
+  static const mask __regex_word = 0x80;
+#  endif
+#elif defined(_LIBCPP_MSVCRT_LIKE)
+  typedef unsigned short mask;
+  static const mask space        = _SPACE;
+  static const mask print        = _BLANK | _PUNCT | _ALPHA | _DIGIT;
+  static const mask cntrl        = _CONTROL;
+  static const mask upper        = _UPPER;
+  static const mask lower        = _LOWER;
+  static const mask alpha        = _ALPHA;
+  static const mask digit        = _DIGIT;
+  static const mask punct        = _PUNCT;
+  static const mask xdigit       = _HEX;
+  static const mask blank        = _BLANK;
+  static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used
+#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
+#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
+#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
+#  ifdef __APPLE__
+  typedef __uint32_t mask;
+#  elif defined(__FreeBSD__)
+  typedef unsigned long mask;
+#  elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
+  typedef unsigned short mask;
+#  endif
+  static const mask space  = _CTYPE_S;
+  static const mask print  = _CTYPE_R;
+  static const mask cntrl  = _CTYPE_C;
+  static const mask upper  = _CTYPE_U;
+  static const mask lower  = _CTYPE_L;
+  static const mask alpha  = _CTYPE_A;
+  static const mask digit  = _CTYPE_D;
+  static const mask punct  = _CTYPE_P;
+  static const mask xdigit = _CTYPE_X;
+
+#  if defined(__NetBSD__)
+  static const mask blank = _CTYPE_BL;
+  // NetBSD defines classes up to 0x2000
+  // see sys/ctype_bits.h, _CTYPE_Q
+  static const mask __regex_word = 0x8000;
+#  else
+  static const mask blank        = _CTYPE_B;
+  static const mask __regex_word = 0x80;
+#  endif
+#elif defined(_AIX)
+  typedef unsigned int mask;
+  static const mask space        = _ISSPACE;
+  static const mask print        = _ISPRINT;
+  static const mask cntrl        = _ISCNTRL;
+  static const mask upper        = _ISUPPER;
+  static const mask lower        = _ISLOWER;
+  static const mask alpha        = _ISALPHA;
+  static const mask digit        = _ISDIGIT;
+  static const mask punct        = _ISPUNCT;
+  static const mask xdigit       = _ISXDIGIT;
+  static const mask blank        = _ISBLANK;
+  static const mask __regex_word = 0x8000;
+#elif defined(_NEWLIB_VERSION)
+  // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
+  typedef char mask;
+  // In case char is signed, static_cast is needed to avoid warning on
+  // positive value becomming negative.
+  static const mask space  = static_cast<mask>(_S);
+  static const mask print  = static_cast<mask>(_P | _U | _L | _N | _B);
+  static const mask cntrl  = static_cast<mask>(_C);
+  static const mask upper  = static_cast<mask>(_U);
+  static const mask lower  = static_cast<mask>(_L);
+  static const mask alpha  = static_cast<mask>(_U | _L);
+  static const mask digit  = static_cast<mask>(_N);
+  static const mask punct  = static_cast<mask>(_P);
+  static const mask xdigit = static_cast<mask>(_X | _N);
+  static const mask blank  = static_cast<mask>(_B);
+  // mask is already fully saturated, use a different type in regex_type_traits.
+  static const unsigned short __regex_word = 0x100;
+#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
+#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
+#  define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
+#elif defined(__MVS__)
+#  if defined(__NATIVE_ASCII_F)
+  typedef unsigned int mask;
+  static const mask space  = _ISSPACE_A;
+  static const mask print  = _ISPRINT_A;
+  static const mask cntrl  = _ISCNTRL_A;
+  static const mask upper  = _ISUPPER_A;
+  static const mask lower  = _ISLOWER_A;
+  static const mask alpha  = _ISALPHA_A;
+  static const mask digit  = _ISDIGIT_A;
+  static const mask punct  = _ISPUNCT_A;
+  static const mask xdigit = _ISXDIGIT_A;
+  static const mask blank  = _ISBLANK_A;
+#  else
+  typedef unsigned short mask;
+  static const mask space  = __ISSPACE;
+  static const mask print  = __ISPRINT;
+  static const mask cntrl  = __ISCNTRL;
+  static const mask upper  = __ISUPPER;
+  static const mask lower  = __ISLOWER;
+  static const mask alpha  = __ISALPHA;
+  static const mask digit  = __ISDIGIT;
+  static const mask punct  = __ISPUNCT;
+  static const mask xdigit = __ISXDIGIT;
+  static const mask blank  = __ISBLANK;
+#  endif
+  static const mask __regex_word = 0x8000;
+#else
+#  error unknown rune table for this platform -- do you mean to define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE?
+#endif
+  static const mask alnum = alpha | digit;
+  static const mask graph = alnum | punct;
+
+  _LIBCPP_HIDE_FROM_ABI ctype_base() {}
+
+  static_assert((__regex_word & ~(std::make_unsigned<mask>::type)(space | print | cntrl | upper | lower | alpha |
+                                                                  digit | punct | xdigit | blank)) == __regex_word,
+                "__regex_word can't overlap other bits");
+};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_CTYPE_BASE_H
diff --git a/libcxx/include/__locale_dir/ctype_byname.h b/libcxx/include/__locale_dir/ctype_byname.h
new file mode 100644
index 00000000000000..0c7d2a09f5b936
--- /dev/null
+++ b/libcxx/include/__locale_dir/ctype_byname.h
@@ -0,0 +1,73 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_CTYPE_BYNAME_H
+#define _LIBCPP___LOCALE_CTYPE_BYNAME_H
+
+#include <__config>
+#include <__locale_dir/ctype.h>
+#include <__locale_dir/ctype_base.h>
+#include <__locale_dir/locale_base_api.h>
+#include <cstddef>
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS ctype_byname;
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<char> : public ctype<char> {
+  locale_t __l_;
+
+public:
+  explicit ctype_byname(const char*, size_t = 0);
+  explicit ctype_byname(const string&, size_t = 0);
+
+protected:
+  ~ctype_byname() override;
+  char_type do_toupper(char_type) const override;
+  const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
+  char_type do_tolower(char_type) const override;
+  const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
+};
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<wchar_t> : public ctype<wchar_t> {
+  locale_t __l_;
+
+public:
+  explicit ctype_byname(const char*, size_t = 0);
+  explicit ctype_byname(const string&, size_t = 0);
+
+protected:
+  ~ctype_byname() override;
+  bool do_is(mask __m, char_type __c) const override;
+  const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const override;
+  const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const override;
+  const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const override;
+  char_type do_toupper(char_type) const override;
+  const char_type* do_toupper(char_type* __low, const char_type* __high) const override;
+  char_type do_tolower(char_type) const override;
+  const char_type* do_tolower(char_type* __low, const char_type* __high) const override;
+  char_type do_widen(char) const override;
+  const char* do_widen(const char* __low, const char* __high, char_type* __dest) const override;
+  char do_narrow(char_type, char __dfault) const override;
+  const char_type*
+  do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const override;
+};
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_CTYPE_BYNAME_H
diff --git a/libcxx/include/__locale_dir/locale.h b/libcxx/include/__locale_dir/locale.h
new file mode 100644
index 00000000000000..e26f6f49b673c8
--- /dev/null
+++ b/libcxx/include/__locale_dir/locale.h
@@ -0,0 +1,163 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_LOCALE_H
+#define _LIBCPP___LOCALE_LOCALE_H
+
+#include <__config>
+#include <__memory/shared_ptr.h>
+#include <__mutex/once_flag.h>
+#include <clocale>
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+class _LIBCPP_EXPORTED_FROM_ABI locale;
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS collate;
+
+template <class _Facet>
+_LIBCPP_HIDE_FROM_ABI bool has_facet(const locale&) _NOEXCEPT;
+
+template <class _Facet>
+_LIBCPP_HIDE_FROM_ABI const _Facet& use_facet(const locale&);
+
+class _LIBCPP_EXPORTED_FROM_ABI locale {
+public:
+  // types:
+  class _LIBCPP_EXPORTED_FROM_ABI facet;
+  class _LIBCPP_EXPORTED_FROM_ABI id;
+
+  typedef int category;
+
+  static const category // values assigned here are for exposition only
+      none    = 0,
+      collate = LC_COLLATE_MASK, ctype = LC_CTYPE_MASK, monetary = LC_MONETARY_MASK, numeric = LC_NUMERIC_MASK,
+      time = LC_TIME_MASK, messages = LC_MESSAGES_MASK, all = collate | ctype | monetary | numeric | time | messages;
+
+  // construct/copy/destroy:
+  locale() _NOEXCEPT;
+  locale(const locale&) _NOEXCEPT;
+  explicit locale(const char*);
+  explicit locale(const string&);
+  locale(const locale&, const char*, category);
+  locale(const locale&, const string&, category);
+  template <class _Facet>
+  _LIBCPP_HIDE_FROM_ABI locale(const locale&, _Facet*);
+  locale(const locale&, const locale&, category);
+
+  ~locale();
+
+  const locale& operator=(const locale&) _NOEXCEPT;
+
+  template <class _Facet>
+  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS locale combine(const locale&) const;
+
+  // locale operations:
+  string name() const;
+  bool operator==(const locale&) const;
+#if _LIBCPP_STD_VER <= 17
+  _LIBCPP_HIDE_FROM_ABI bool operator!=(const locale& __y) const { return !(*this == __y); }
+#endif
+  template <class _CharT, class _Traits, class _Allocator>
+  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
+  operator()(const basic_string<_CharT, _Traits, _Allocator>&, const basic_string<_CharT, _Traits, _Allocator>&) const;
+
+  // global locale objects:
+  static locale global(const locale&);
+  static const locale& classic();
+
+private:
+  class __imp;
+  __imp* __locale_;
+
+  template <class>
+  friend struct __no_destroy;
+  struct __private_tag {};
+  _LIBCPP_HIDE_FROM_ABI explicit locale(__private_tag, __imp* __loc) : __locale_(__loc) {}
+
+  void __install_ctor(const locale&, facet*, long);
+  static locale& __global();
+  bool has_facet(id&) const;
+  const facet* use_facet(id&) const;
+
+  template <class _Facet>
+  friend bool has_facet(const locale&) _NOEXCEPT;
+  template <class _Facet>
+  friend const _Facet& use_facet(const locale&);
+};
+
+class _LIBCPP_EXPORTED_FROM_ABI locale::facet : public __shared_count {
+protected:
+  _LIBCPP_HIDE_FROM_ABI explicit facet(size_t __refs = 0) : __shared_count(static_cast<long>(__refs) - 1) {}
+
+  ~facet() override;
+
+  //    facet(const facet&) = delete;     // effectively done in __shared_count
+  //    void operator=(const facet&) = delete;
+
+private:
+  void __on_zero_shared() _NOEXCEPT override;
+};
+
+class _LIBCPP_EXPORTED_FROM_ABI locale::id {
+  once_flag __flag_;
+  int32_t __id_;
+
+  static int32_t __next_id;
+
+public:
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR id() : __id_(0) {}
+  void operator=(const id&) = delete;
+  id(const id&)             = delete;
+
+public: // only needed for tests
+  long __get();
+
+  friend class locale;
+  friend class locale::__imp;
+};
+
+template <class _Facet>
+inline _LIBCPP_HIDE_FROM_ABI locale::locale(const locale& __other, _Facet* __f) {
+  __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
+}
+
+template <class _Facet>
+locale locale::combine(const locale& __other) const {
+  if (!std::has_facet<_Facet>(__other))
+    __throw_runtime_error("locale::combine: locale missing facet");
+
+  return locale(*this, &const_cast<_Facet&>(std::use_facet<_Facet>(__other)));
+}
+
+template <class _Facet>
+inline _LIBCPP_HIDE_FROM_ABI bool has_facet(const locale& __l) _NOEXCEPT {
+  return __l.has_facet(_Facet::id);
+}
+
+template <class _Facet>
+inline _LIBCPP_HIDE_FROM_ABI const _Facet& use_facet(const locale& __l) {
+  return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
+}
+
+template <class _CharT, class _Traits, class _Allocator>
+bool locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
+                        const basic_string<_CharT, _Traits, _Allocator>& __y) const {
+  return std::use_facet<std::collate<_CharT> >(*this).compare(
+             __x.data(), __x.data() + __x.size(), __y.data(), __y.data() + __y.size()) < 0;
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_LOCALE_H
diff --git a/libcxx/include/__locale_dir/locale_base_api.h b/libcxx/include/__locale_dir/locale_base_api.h
index 2355b4a840d734..5b7fb20ede42d8 100644
--- a/libcxx/include/__locale_dir/locale_base_api.h
+++ b/libcxx/include/__locale_dir/locale_base_api.h
@@ -9,6 +9,8 @@
 #ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_H
 #define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_H
 
+#include <__config>
+
 #if defined(_LIBCPP_MSVCRT_LIKE)
 #  include <__locale_dir/locale_base_api/win32.h>
 #elif defined(_AIX) || defined(__MVS__)
@@ -26,7 +28,11 @@
 #elif defined(__wasi__) || defined(_LIBCPP_HAS_MUSL_LIBC)
 #  include <__locale_dir/locale_base_api/musl.h>
 #elif defined(__APPLE__) || defined(__FreeBSD__)
+#  include <__locale_dir/locale_base_api/bsd_locale_defaults.h>
 #  include <xlocale.h>
+#elif defined(__GLIBC__)
+#  include <ctype.h>
+#  include <locale.h>
 #endif
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
diff --git a/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h b/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
index 76b94287cd6cc8..2b8933e6943011 100644
--- a/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
+++ b/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
@@ -13,6 +13,7 @@
 #ifndef _LIBCPP___LOCALE_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
 #define _LIBCPP___LOCALE_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
 
+#include <__locale_dir/locale_base_api.h>
 #include <__locale_dir/locale_base_api/locale_guard.h>
 #include <cstdio>
 #include <stdarg.h>
diff --git a/libcxx/include/__locale_dir/locale_base_api/locale_guard.h b/libcxx/include/__locale_dir/locale_base_api/locale_guard.h
index b7e3be756e711e..1450e3b6a7897f 100644
--- a/libcxx/include/__locale_dir/locale_base_api/locale_guard.h
+++ b/libcxx/include/__locale_dir/locale_base_api/locale_guard.h
@@ -10,8 +10,7 @@
 #define _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H
 
 #include <__config>
-#include <__locale> // for locale_t
-#include <clocale>
+#include <__locale_dir/locale_base_api.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
diff --git a/libcxx/include/__locale_dir/locale_base_api/win32.h b/libcxx/include/__locale_dir/locale_base_api/win32.h
index 5dfacfb9c1ee9d..eccb43c082c09f 100644
--- a/libcxx/include/__locale_dir/locale_base_api/win32.h
+++ b/libcxx/include/__locale_dir/locale_base_api/win32.h
@@ -11,6 +11,7 @@
 #define _LIBCPP___LOCALE_LOCALE_BASE_API_WIN32_H
 
 #include <__config>
+#include <__locale_dir/locale_base_api/bsd_locale_defaults.h>
 #include <cstddef>
 #include <locale.h> // _locale_t
 #include <stdio.h>
diff --git a/libcxx/include/__locale_dir/narrow_to_utf8.h b/libcxx/include/__locale_dir/narrow_to_utf8.h
new file mode 100644
index 00000000000000..b1d935b7c90dd1
--- /dev/null
+++ b/libcxx/include/__locale_dir/narrow_to_utf8.h
@@ -0,0 +1,99 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_NARROW_TO_UTF8_H
+#define _LIBCPP___LOCALE_NARROW_TO_UTF8_H
+
+#include <__config>
+#include <__locale_dir/codecvt.h>
+#include <__locale_dir/codecvt_base.h>
+#include <__std_mbstate_t.h>
+#include <cstddef>
+#include <stdexcept>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <size_t _Np>
+struct __narrow_to_utf8 {
+  template <class _OutputIterator, class _CharT>
+  _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
+};
+
+template <>
+struct __narrow_to_utf8<8> {
+  template <class _OutputIterator, class _CharT>
+  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
+    for (; __wb < __we; ++__wb, ++__s)
+      *__s = *__wb;
+    return __s;
+  }
+};
+
+_LIBCPP_SUPPRESS_DEPRECATED_PUSH
+template <>
+struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
+  _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
+  _LIBCPP_SUPPRESS_DEPRECATED_POP
+
+  ~__narrow_to_utf8() override;
+
+  template <class _OutputIterator, class _CharT>
+  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
+    result __r = ok;
+    mbstate_t __mb;
+    while (__wb < __we && __r != error) {
+      const int __sz = 32;
+      char __buf[__sz];
+      char* __bn;
+      const char16_t* __wn = (const char16_t*)__wb;
+      __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn, __buf, __buf + __sz, __bn);
+      if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
+        __throw_runtime_error("locale not supported");
+      for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
+        *__s = *__p;
+      __wb = (const _CharT*)__wn;
+    }
+    return __s;
+  }
+};
+
+_LIBCPP_SUPPRESS_DEPRECATED_PUSH
+template <>
+struct _LIBCPP_EXPORTED_FROM_ABI __narrow_to_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
+  _LIBCPP_HIDE_FROM_ABI __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
+  _LIBCPP_SUPPRESS_DEPRECATED_POP
+
+  ~__narrow_to_utf8() override;
+
+  template <class _OutputIterator, class _CharT>
+  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const {
+    result __r = ok;
+    mbstate_t __mb;
+    while (__wb < __we && __r != error) {
+      const int __sz = 32;
+      char __buf[__sz];
+      char* __bn;
+      const char32_t* __wn = (const char32_t*)__wb;
+      __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn, __buf, __buf + __sz, __bn);
+      if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
+        __throw_runtime_error("locale not supported");
+      for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
+        *__s = *__p;
+      __wb = (const _CharT*)__wn;
+    }
+    return __s;
+  }
+};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_NARROW_TO_UTF8_H
diff --git a/libcxx/include/__locale_dir/numpunct.h b/libcxx/include/__locale_dir/numpunct.h
new file mode 100644
index 00000000000000..ca10c9f6eb4476
--- /dev/null
+++ b/libcxx/include/__locale_dir/numpunct.h
@@ -0,0 +1,87 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_NUMPUNCT_H
+#define _LIBCPP___LOCALE_NUMPUNCT_H
+
+#include <__config>
+#include <__locale_dir/locale.h>
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS numpunct;
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI numpunct<char> : public locale::facet {
+public:
+  typedef char char_type;
+  typedef basic_string<char_type> string_type;
+
+  explicit numpunct(size_t __refs = 0);
+
+  _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
+  _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
+  _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
+  _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
+  _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
+
+  static locale::id id;
+
+protected:
+  ~numpunct() override;
+  virtual char_type do_decimal_point() const;
+  virtual char_type do_thousands_sep() const;
+  virtual string do_grouping() const;
+  virtual string_type do_truename() const;
+  virtual string_type do_falsename() const;
+
+  char_type __decimal_point_;
+  char_type __thousands_sep_;
+  string __grouping_;
+};
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI numpunct<wchar_t> : public locale::facet {
+public:
+  typedef wchar_t char_type;
+  typedef basic_string<char_type> string_type;
+
+  explicit numpunct(size_t __refs = 0);
+
+  _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
+  _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
+  _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
+  _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
+  _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
+
+  static locale::id id;
+
+protected:
+  ~numpunct() override;
+  virtual char_type do_decimal_point() const;
+  virtual char_type do_thousands_sep() const;
+  virtual string do_grouping() const;
+  virtual string_type do_truename() const;
+  virtual string_type do_falsename() const;
+
+  char_type __decimal_point_;
+  char_type __thousands_sep_;
+  string __grouping_;
+};
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_NUMPUNCT_H
diff --git a/libcxx/include/__locale_dir/numpunct_byname.h b/libcxx/include/__locale_dir/numpunct_byname.h
new file mode 100644
index 00000000000000..eac7e7df38d48f
--- /dev/null
+++ b/libcxx/include/__locale_dir/numpunct_byname.h
@@ -0,0 +1,62 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_NUMPUNCT_BYNAME_H
+#define _LIBCPP___LOCALE_NUMPUNCT_BYNAME_H
+
+#include <__config>
+#include <__locale_dir/locale.h>
+#include <__locale_dir/numpunct.h>
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS numpunct_byname;
+
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<char> : public numpunct<char> {
+public:
+  typedef char char_type;
+  typedef basic_string<char_type> string_type;
+
+  explicit numpunct_byname(const char* __nm, size_t __refs = 0);
+  explicit numpunct_byname(const string& __nm, size_t __refs = 0);
+
+protected:
+  ~numpunct_byname() override;
+
+private:
+  void __init(const char*);
+};
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+template <>
+class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<wchar_t> : public numpunct<wchar_t> {
+public:
+  typedef wchar_t char_type;
+  typedef basic_string<char_type> string_type;
+
+  explicit numpunct_byname(const char* __nm, size_t __refs = 0);
+  explicit numpunct_byname(const string& __nm, size_t __refs = 0);
+
+protected:
+  ~numpunct_byname() override;
+
+private:
+  void __init(const char*);
+};
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_NUMPUNCT_BYNAME_H
diff --git a/libcxx/include/__locale_dir/traits.h b/libcxx/include/__locale_dir/traits.h
new file mode 100644
index 00000000000000..fce1475f478237
--- /dev/null
+++ b/libcxx/include/__locale_dir/traits.h
@@ -0,0 +1,95 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_CTYPE_BYNAME_H
+#define _LIBCPP___LOCALE_CTYPE_BYNAME_H
+
+#include <__config>
+#include <__locale_dir/ctype.h>
+#include <__locale_dir/ctype_base.h>
+#include <__locale_dir/locale.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isspace(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isprint(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool iscntrl(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isupper(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool islower(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isalpha(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isdigit(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool ispunct(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isxdigit(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isalnum(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI bool isgraph(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
+}
+
+template <class _CharT>
+_LIBCPP_HIDE_FROM_ABI bool isblank(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).is(ctype_base::blank, __c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI _CharT toupper(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).toupper(__c);
+}
+
+template <class _CharT>
+inline _LIBCPP_HIDE_FROM_ABI _CharT tolower(_CharT __c, const locale& __loc) {
+  return std::use_facet<ctype<_CharT> >(__loc).tolower(__c);
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_CTYPE_BYNAME_H
diff --git a/libcxx/include/__locale_dir/widen_from_utf8.h b/libcxx/include/__locale_dir/widen_from_utf8.h
new file mode 100644
index 00000000000000..9f072f652181b7
--- /dev/null
+++ b/libcxx/include/__locale_dir/widen_from_utf8.h
@@ -0,0 +1,99 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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___LOCALE_WIDEN_FROM_UTF8_H
+#define _LIBCPP___LOCALE_WIDEN_FROM_UTF8_H
+
+#include <__config>
+#include <__locale_dir/codecvt.h>
+#include <__locale_dir/codecvt_base.h>
+#include <__std_mbstate_t.h>
+#include <cstddef>
+#include <stdexcept>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <size_t _Np>
+struct __widen_from_utf8 {
+  template <class _OutputIterator>
+  _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
+};
+
+template <>
+struct __widen_from_utf8<8> {
+  template <class _OutputIterator>
+  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
+    for (; __nb < __ne; ++__nb, ++__s)
+      *__s = *__nb;
+    return __s;
+  }
+};
+
+_LIBCPP_SUPPRESS_DEPRECATED_PUSH
+template <>
+struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<16> : public codecvt<char16_t, char, mbstate_t> {
+  _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
+  _LIBCPP_SUPPRESS_DEPRECATED_POP
+
+  ~__widen_from_utf8() override;
+
+  template <class _OutputIterator>
+  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
+    result __r = ok;
+    mbstate_t __mb;
+    while (__nb < __ne && __r != error) {
+      const int __sz = 32;
+      char16_t __buf[__sz];
+      char16_t* __bn;
+      const char* __nn = __nb;
+      __r              = do_in(__mb, __nb, __ne - __nb > __sz ? __nb + __sz : __ne, __nn, __buf, __buf + __sz, __bn);
+      if (__r == codecvt_base::error || __nn == __nb)
+        __throw_runtime_error("locale not supported");
+      for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
+        *__s = *__p;
+      __nb = __nn;
+    }
+    return __s;
+  }
+};
+
+_LIBCPP_SUPPRESS_DEPRECATED_PUSH
+template <>
+struct _LIBCPP_EXPORTED_FROM_ABI __widen_from_utf8<32> : public codecvt<char32_t, char, mbstate_t> {
+  _LIBCPP_HIDE_FROM_ABI __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
+  _LIBCPP_SUPPRESS_DEPRECATED_POP
+
+  ~__widen_from_utf8() override;
+
+  template <class _OutputIterator>
+  _LIBCPP_HIDE_FROM_ABI _OutputIterator operator()(_OutputIterator __s, const char* __nb, const char* __ne) const {
+    result __r = ok;
+    mbstate_t __mb;
+    while (__nb < __ne && __r != error) {
+      const int __sz = 32;
+      char32_t __buf[__sz];
+      char32_t* __bn;
+      const char* __nn = __nb;
+      __r              = do_in(__mb, __nb, __ne - __nb > __sz ? __nb + __sz : __ne, __nn, __buf, __buf + __sz, __bn);
+      if (__r == codecvt_base::error || __nn == __nb)
+        __throw_runtime_error("locale not supported");
+      for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
+        *__s = *__p;
+      __nb = __nn;
+    }
+    return __s;
+  }
+};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_WIDEN_FROM_UTF8_H
diff --git a/libcxx/include/__utility/swap.h b/libcxx/include/__utility/swap.h
index ca8280c729df1d..10808a79c1c544 100644
--- a/libcxx/include/__utility/swap.h
+++ b/libcxx/include/__utility/swap.h
@@ -10,6 +10,7 @@
 #define _LIBCPP___UTILITY_SWAP_H
 
 #include <__config>
+#include <__type_traits/enable_if.h>
 #include <__type_traits/is_move_assignable.h>
 #include <__type_traits/is_move_constructible.h>
 #include <__type_traits/is_nothrow_move_assignable.h>
diff --git a/libcxx/include/codecvt b/libcxx/include/codecvt
index b7182ff471559d..14aec48dc14e1d 100644
--- a/libcxx/include/codecvt
+++ b/libcxx/include/codecvt
@@ -55,7 +55,10 @@ class codecvt_utf8_utf16
 */
 
 #include <__config>
-#include <__locale>
+#include <__locale_dir/codecvt.h>
+#include <__locale_dir/codecvt_base.h>
+#include <__std_mbstate_t.h>
+#include <cstddef>
 #include <version>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
diff --git a/libcxx/include/fstream b/libcxx/include/fstream
index 776641b347e6c1..3e986c134715f8 100644
--- a/libcxx/include/fstream
+++ b/libcxx/include/fstream
@@ -191,7 +191,6 @@ typedef basic_fstream<wchar_t> wfstream;
 #include <__availability>
 #include <__config>
 #include <__fwd/fstream.h>
-#include <__locale>
 #include <__utility/move.h>
 #include <__utility/swap.h>
 #include <__utility/unreachable.h>
diff --git a/libcxx/include/ios b/libcxx/include/ios
index 4b1306fc2ad830..da0e2dfad32bf4 100644
--- a/libcxx/include/ios
+++ b/libcxx/include/ios
@@ -219,11 +219,14 @@ storage-class-specifier const error_category& iostream_category() noexcept;
 
 #include <__fwd/ios.h>
 #include <__ios/fpos.h>
-#include <__locale>
+#include <__locale_dir/ctype.h>
+#include <__locale_dir/locale.h>
+#include <__string/char_traits.h>
 #include <__system_error/error_category.h>
 #include <__system_error/error_code.h>
 #include <__system_error/error_condition.h>
 #include <__system_error/system_error.h>
+#include <__type_traits/is_same.h>
 #include <__utility/swap.h>
 #include <__verbose_abort>
 #include <version>
diff --git a/libcxx/include/locale b/libcxx/include/locale
index e3c63e3abe130e..db6a055a89956e 100644
--- a/libcxx/include/locale
+++ b/libcxx/include/locale
@@ -197,11 +197,26 @@ template <class charT> class messages_byname;
 #include <__config>
 #include <__iterator/access.h>
 #include <__iterator/back_insert_iterator.h>
+#include <__iterator/distance.h>
 #include <__iterator/istreambuf_iterator.h>
 #include <__iterator/ostreambuf_iterator.h>
-#include <__locale>
+#include <__locale_dir/codecvt.h>
+#include <__locale_dir/codecvt_base.h>
+#include <__locale_dir/codecvt_byname.h>
+#include <__locale_dir/collate.h>
+#include <__locale_dir/collate_byname.h>
+#include <__locale_dir/ctype.h>
+#include <__locale_dir/ctype_base.h>
+#include <__locale_dir/ctype_byname.h>
+#include <__locale_dir/locale.h>
+#include <__locale_dir/narrow_to_utf8.h>
+#include <__locale_dir/numpunct.h>
+#include <__locale_dir/numpunct_byname.h>
+#include <__locale_dir/traits.h>
+#include <__locale_dir/widen_from_utf8.h>
 #include <__memory/unique_ptr.h>
 #include <__type_traits/make_unsigned.h>
+#include <cctype>
 #include <cerrno>
 #include <cstdio>
 #include <cstdlib>
@@ -210,6 +225,7 @@ template <class charT> class messages_byname;
 #include <limits>
 #include <new>
 #include <streambuf>
+#include <string>
 #include <version>
 
 // TODO: Fix __bsd_locale_defaults.h
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 219906aa9a5668..7788da032952f6 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1477,6 +1477,20 @@ module std_private_locale_locale_base_api                      [system] {
   header "__locale_dir/locale_base_api.h"
   export *
 }
+module std_private_locale_locale_codecvt                       [system] { header "__locale_dir/codecvt.h" }
+module std_private_locale_locale_codecvt_base                  [system] { header "__locale_dir/codecvt_base.h" }
+module std_private_locale_locale_codecvt_byname                [system] { header "__locale_dir/codecvt_byname.h" }
+module std_private_locale_locale_collate                       [system] { header "__locale_dir/collate.h" }
+module std_private_locale_locale_collate_byname                [system] { header "__locale_dir/collate_byname.h" }
+module std_private_locale_locale_ctype                         [system] { header "__locale_dir/ctype.h" }
+module std_private_locale_locale_ctype_base                    [system] { header "__locale_dir/ctype_base.h" }
+module std_private_locale_locale_ctype_byname                  [system] { header "__locale_dir/ctype_byname.h" }
+module std_private_locale_locale_locale                        [system] { header "__locale_dir/locale.h" }
+module std_private_locale_locale_narrow_to_utf8                [system] { header "__locale_dir/narrow_to_utf8.h" }
+module std_private_locale_locale_numpunct                      [system] { header "__locale_dir/numpunct.h" }
+module std_private_locale_locale_numpunct_byname               [system] { header "__locale_dir/numpunct_byname.h" }
+module std_private_locale_locale_traits                        [system] { header "__locale_dir/traits.h" }
+module std_private_locale_locale_widen_from_utf8               [system] { header "__locale_dir/widen_from_utf8.h" }
 
 module std_private_math_abs                             [system] { header "__math/abs.h" }
 module std_private_math_copysign                        [system] { header "__math/copysign.h" }
diff --git a/libcxx/include/regex b/libcxx/include/regex
index dc3db93744b489..7aecde40cb60bd 100644
--- a/libcxx/include/regex
+++ b/libcxx/include/regex
@@ -797,7 +797,11 @@ typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
 #include <__iterator/back_insert_iterator.h>
 #include <__iterator/default_sentinel.h>
 #include <__iterator/wrap_iter.h>
-#include <__locale>
+#include <__locale_dir/collate.h>
+#include <__locale_dir/collate_byname.h>
+#include <__locale_dir/ctype.h>
+#include <__locale_dir/ctype_base.h>
+#include <__locale_dir/locale.h>
 #include <__memory/shared_ptr.h>
 #include <__memory_resource/polymorphic_allocator.h>
 #include <__type_traits/is_swappable.h>
diff --git a/libcxx/src/ios.cpp b/libcxx/src/ios.cpp
index d58827fa1255cf..79af7b36b34133 100644
--- a/libcxx/src/ios.cpp
+++ b/libcxx/src/ios.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include <__config>
-#include <__locale>
+#include <locale>
 #include <algorithm>
 #include <ios>
 #include <limits>
diff --git a/libcxx/src/iostream.cpp b/libcxx/src/iostream.cpp
index c5ad77a0191608..6e79bdf13f8cf7 100644
--- a/libcxx/src/iostream.cpp
+++ b/libcxx/src/iostream.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "std_stream.h"
-#include <__locale>
+#include <locale>
 #include <new>
 #include <string>
 
diff --git a/libcxx/src/std_stream.h b/libcxx/src/std_stream.h
index e55cd0b6b77af9..c6dcd95916a357 100644
--- a/libcxx/src/std_stream.h
+++ b/libcxx/src/std_stream.h
@@ -11,9 +11,9 @@
 #define _LIBCPP_STD_STREAM_H
 
 #include <__config>
-#include <__locale>
 #include <cstdio>
 #include <istream>
+#include <locale>
 #include <ostream>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
diff --git a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
index 978e7095216522..ab448061f52dcc 100644
--- a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
+++ b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
@@ -5,7 +5,7 @@
 set(LLVM_DIR_SAVE ${LLVM_DIR})
 set(Clang_DIR_SAVE ${Clang_DIR})
 
-find_package(Clang 18)
+find_package(Clang 19)
 if (NOT Clang_FOUND)
   find_package(Clang 17)
 endif()



More information about the libcxx-commits mailing list