xref: /llvm-project/libcxx/include/__ostream/print.h (revision c6f3b7bcd0596d30f8dabecdfb9e44f9a07b6e4c)
1 //===---------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===---------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___OSTREAM_PRINT_H
10 #define _LIBCPP___OSTREAM_PRINT_H
11 
12 #include <__config>
13 
14 #if _LIBCPP_HAS_LOCALIZATION
15 
16 #  include <__fwd/ostream.h>
17 #  include <__iterator/ostreambuf_iterator.h>
18 #  include <__ostream/basic_ostream.h>
19 #  include <format>
20 #  include <ios>
21 #  include <locale>
22 #  include <print>
23 
24 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #    pragma GCC system_header
26 #  endif
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 #  if _LIBCPP_STD_VER >= 23
31 
32 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
33 _LIBCPP_HIDE_FROM_ABI inline void
34 __vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
35   // [ostream.formatted.print]/3
36   // Effects: Behaves as a formatted output function
37   // ([ostream.formatted.reqmts]) of os, except that:
38   // - failure to generate output is reported as specified below, and
39   // - any exception thrown by the call to vformat is propagated without regard
40   //   to the value of os.exceptions() and without turning on ios_base::badbit
41   //   in the error state of os.
42   // After constructing a sentry object, the function initializes an automatic
43   // variable via
44   //   string out = vformat(os.getloc(), fmt, args);
45 
46   ostream::sentry __s(__os);
47   if (__s) {
48     string __o = std::vformat(__os.getloc(), __fmt, __args);
49     if (__write_nl)
50       __o += '\n';
51 
52     const char* __str = __o.data();
53     size_t __len      = __o.size();
54 
55 #    if _LIBCPP_HAS_EXCEPTIONS
56     try {
57 #    endif // _LIBCPP_HAS_EXCEPTIONS
58       typedef ostreambuf_iterator<char> _Ip;
59       if (std::__pad_and_output(
60               _Ip(__os),
61               __str,
62               (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
63               __str + __len,
64               __os,
65               __os.fill())
66               .failed())
67         __os.setstate(ios_base::badbit | ios_base::failbit);
68 
69 #    if _LIBCPP_HAS_EXCEPTIONS
70     } catch (...) {
71       __os.__set_badbit_and_consider_rethrow();
72     }
73 #    endif // _LIBCPP_HAS_EXCEPTIONS
74   }
75 }
76 
77 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
78 _LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) {
79   std::__vprint_nonunicode(__os, __fmt, __args, false);
80 }
81 
82 // Returns the FILE* associated with the __os.
83 // Returns a nullptr when no FILE* is associated with __os.
84 // This function is in the dylib since the type of the buffer associated
85 // with std::cout, std::cerr, and std::clog is only known in the dylib.
86 //
87 // This function implements part of the implementation-defined behavior
88 // of [ostream.formatted.print]/3
89 //   If the function is vprint_unicode and os is a stream that refers to
90 //   a terminal capable of displaying Unicode which is determined in an
91 //   implementation-defined manner, writes out to the terminal using the
92 //   native Unicode API;
93 // Whether the returned FILE* is "a terminal capable of displaying Unicode"
94 // is determined in the same way as the print(FILE*, ...) overloads.
95 _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);
96 
97 #    if _LIBCPP_HAS_UNICODE
98 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
99 _LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
100 #      if _LIBCPP_AVAILABILITY_HAS_PRINT == 0
101   return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
102 #      else
103   FILE* __file = std::__get_ostream_file(__os);
104   if (!__file || !__print::__is_terminal(__file))
105     return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
106 
107   // [ostream.formatted.print]/3
108   //    If the function is vprint_unicode and os is a stream that refers to a
109   //    terminal capable of displaying Unicode which is determined in an
110   //    implementation-defined manner, writes out to the terminal using the
111   //    native Unicode API; if out contains invalid code units, the behavior is
112   //    undefined and implementations are encouraged to diagnose it. If the
113   //    native Unicode API is used, the function flushes os before writing out.
114   //
115   // This is the path for the native API, start with flushing.
116   __os.flush();
117 
118 #        if _LIBCPP_HAS_EXCEPTIONS
119   try {
120 #        endif // _LIBCPP_HAS_EXCEPTIONS
121     ostream::sentry __s(__os);
122     if (__s) {
123 #        ifndef _LIBCPP_WIN32API
124       __print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);
125 #        elif _LIBCPP_HAS_WIDE_CHARACTERS
126     __print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);
127 #        else
128 #          error "Windows builds with wchar_t disabled are not supported."
129 #        endif
130     }
131 
132 #        if _LIBCPP_HAS_EXCEPTIONS
133   } catch (...) {
134     __os.__set_badbit_and_consider_rethrow();
135   }
136 #        endif // _LIBCPP_HAS_EXCEPTIONS
137 #      endif   // _LIBCPP_AVAILABILITY_HAS_PRINT
138 }
139 
140 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
141 _LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) {
142   std::__vprint_unicode(__os, __fmt, __args, false);
143 }
144 #    endif // _LIBCPP_HAS_UNICODE
145 
146 template <class... _Args>
147 _LIBCPP_HIDE_FROM_ABI void print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
148 #    if _LIBCPP_HAS_UNICODE
149   if constexpr (__print::__use_unicode_execution_charset)
150     std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false);
151   else
152     std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
153 #    else  // _LIBCPP_HAS_UNICODE
154   std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
155 #    endif // _LIBCPP_HAS_UNICODE
156 }
157 
158 template <class... _Args>
159 _LIBCPP_HIDE_FROM_ABI void println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
160 #    if _LIBCPP_HAS_UNICODE
161   // Note the wording in the Standard is inefficient. The output of
162   // std::format is a std::string which is then copied. This solution
163   // just appends a newline at the end of the output.
164   if constexpr (__print::__use_unicode_execution_charset)
165     std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true);
166   else
167     std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
168 #    else  // _LIBCPP_HAS_UNICODE
169   std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
170 #    endif // _LIBCPP_HAS_UNICODE
171 }
172 
173 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
174 _LIBCPP_HIDE_FROM_ABI inline void println(ostream& __os) {
175   std::print(__os, "\n");
176 }
177 
178 #  endif // _LIBCPP_STD_VER >= 23
179 
180 _LIBCPP_END_NAMESPACE_STD
181 
182 #endif // _LIBCPP_HAS_LOCALIZATION
183 
184 #endif // _LIBCPP___OSTREAM_PRINT_H
185