File indexing completed on 2026-05-03 08:13:58
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___OSTREAM_PRINT_H
0010 #define _LIBCPP___OSTREAM_PRINT_H
0011
0012 #include <__config>
0013
0014 #if _LIBCPP_HAS_LOCALIZATION
0015
0016 # include <__fwd/ostream.h>
0017 # include <__iterator/ostreambuf_iterator.h>
0018 # include <__ostream/basic_ostream.h>
0019 # include <format>
0020 # include <ios>
0021 # include <locale>
0022 # include <print>
0023
0024 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 # pragma GCC system_header
0026 # endif
0027
0028 _LIBCPP_BEGIN_NAMESPACE_STD
0029
0030 # if _LIBCPP_STD_VER >= 23
0031
0032 template <class = void>
0033 _LIBCPP_HIDE_FROM_ABI inline void
0034 __vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 ostream::sentry __s(__os);
0047 if (__s) {
0048 string __o = std::vformat(__os.getloc(), __fmt, __args);
0049 if (__write_nl)
0050 __o += '\n';
0051
0052 const char* __str = __o.data();
0053 size_t __len = __o.size();
0054
0055 # if _LIBCPP_HAS_EXCEPTIONS
0056 try {
0057 # endif
0058 typedef ostreambuf_iterator<char> _Ip;
0059 if (std::__pad_and_output(
0060 _Ip(__os),
0061 __str,
0062 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
0063 __str + __len,
0064 __os,
0065 __os.fill())
0066 .failed())
0067 __os.setstate(ios_base::badbit | ios_base::failbit);
0068
0069 # if _LIBCPP_HAS_EXCEPTIONS
0070 } catch (...) {
0071 __os.__set_badbit_and_consider_rethrow();
0072 }
0073 # endif
0074 }
0075 }
0076
0077 template <class = void>
0078 _LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) {
0079 std::__vprint_nonunicode(__os, __fmt, __args, false);
0080 }
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);
0096
0097 # if _LIBCPP_HAS_UNICODE
0098 template <class = void>
0099 _LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
0100 # if _LIBCPP_AVAILABILITY_HAS_PRINT == 0
0101 return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
0102 # else
0103 FILE* __file = std::__get_ostream_file(__os);
0104 if (!__file || !__print::__is_terminal(__file))
0105 return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 __os.flush();
0117
0118 # if _LIBCPP_HAS_EXCEPTIONS
0119 try {
0120 # endif
0121 ostream::sentry __s(__os);
0122 if (__s) {
0123 # ifndef _LIBCPP_WIN32API
0124 __print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);
0125 # elif _LIBCPP_HAS_WIDE_CHARACTERS
0126 __print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);
0127 # else
0128 # error "Windows builds with wchar_t disabled are not supported."
0129 # endif
0130 }
0131
0132 # if _LIBCPP_HAS_EXCEPTIONS
0133 } catch (...) {
0134 __os.__set_badbit_and_consider_rethrow();
0135 }
0136 # endif
0137 # endif
0138 }
0139
0140 template <class = void>
0141 _LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) {
0142 std::__vprint_unicode(__os, __fmt, __args, false);
0143 }
0144 # endif
0145
0146 template <class... _Args>
0147 _LIBCPP_HIDE_FROM_ABI void print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
0148 # if _LIBCPP_HAS_UNICODE
0149 if constexpr (__print::__use_unicode_execution_charset)
0150 std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false);
0151 else
0152 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
0153 # else
0154 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
0155 # endif
0156 }
0157
0158 template <class... _Args>
0159 _LIBCPP_HIDE_FROM_ABI void println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
0160 # if _LIBCPP_HAS_UNICODE
0161
0162
0163
0164 if constexpr (__print::__use_unicode_execution_charset)
0165 std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true);
0166 else
0167 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
0168 # else
0169 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
0170 # endif
0171 }
0172
0173 template <class = void>
0174 _LIBCPP_HIDE_FROM_ABI inline void println(ostream& __os) {
0175 std::print(__os, "\n");
0176 }
0177
0178 # endif
0179
0180 _LIBCPP_END_NAMESPACE_STD
0181
0182 #endif
0183
0184 #endif