#ifndef _LIBCPP___OSTREAM_PRINT_H
#define _LIBCPP___OSTREAM_PRINT_H
#include <__config>
#include <__fwd/ostream.h>
#include <__iterator/ostreambuf_iterator.h>
#include <__ostream/basic_ostream.h>
#include <format>
#include <ios>
#include <locale>
#include <print>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 23
template <class = void>
_LIBCPP_HIDE_FROM_ABI inline void
__vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
ostream::sentry __s(__os);
if (__s) {
string __o = std::vformat(__os.getloc(), __fmt, __args);
if (__write_nl)
__o += '\n';
const char* __str = __o.data();
size_t __len = __o.size();
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try {
# endif
typedef ostreambuf_iterator<char> _Ip;
if (std::__pad_and_output(
_Ip(__os),
__str,
(__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
__str + __len,
__os,
__os.fill())
.failed())
__os.setstate(ios_base::badbit | ios_base::failbit);
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
} catch (...) {
__os.__set_badbit_and_consider_rethrow();
}
# endif
}
}
template <class = void>
_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) {
std::__vprint_nonunicode(__os, __fmt, __args, false);
}
_LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);
# ifndef _LIBCPP_HAS_NO_UNICODE
template <class = void>
_LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
# if _LIBCPP_AVAILABILITY_HAS_PRINT == 0
return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
# else
FILE* __file = std::__get_ostream_file(__os);
if (!__file || !__print::__is_terminal(__file))
return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
__os.flush();
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try {
# endif
ostream::sentry __s(__os);
if (__s) {
# ifndef _LIBCPP_WIN32API
__print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);
# elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
__print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);
# else
# error "Windows builds with wchar_t disabled are not supported."
# endif
}
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
} catch (...) {
__os.__set_badbit_and_consider_rethrow();
}
# endif
# endif
}
template <class = void>
_LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) {
std::__vprint_unicode(__os, __fmt, __args, false);
}
# endif
template <class... _Args>
_LIBCPP_HIDE_FROM_ABI void print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
# ifndef _LIBCPP_HAS_NO_UNICODE
if constexpr (__print::__use_unicode_execution_charset)
std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false);
else
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
# else
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
# endif
}
template <class... _Args>
_LIBCPP_HIDE_FROM_ABI void println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
# ifndef _LIBCPP_HAS_NO_UNICODE
if constexpr (__print::__use_unicode_execution_charset)
std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true);
else
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
# else
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
# endif
}
template <class = void>
_LIBCPP_HIDE_FROM_ABI inline void println(ostream& __os) {
std::print(__os, "\n");
}
#endif
_LIBCPP_END_NAMESPACE_STD
#endif