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