1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_PRINT
11#define _LIBCPP_PRINT
12
13/*
14namespace std {
15 // [print.fun], print functions
16 template<class... Args>
17 void print(format_string<Args...> fmt, Args&&... args);
18 void println(); // Since C++26
19 template<class... Args>
20 void print(FILE* stream, format_string<Args...> fmt, Args&&... args);
21 void println(FILE* stream); // Since C++26
22
23 template<class... Args>
24 void println(format_string<Args...> fmt, Args&&... args);
25 template<class... Args>
26 void println(FILE* stream, format_string<Args...> fmt, Args&&... args);
27
28 void vprint_unicode(string_view fmt, format_args args);
29 void vprint_unicode(FILE* stream, string_view fmt, format_args args);
30
31 void vprint_nonunicode(string_view fmt, format_args args);
32 void vprint_nonunicode(FILE* stream, string_view fmt, format_args args);
33}
34*/
35
36#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
37# include <__cxx03/__config>
38#else
39# include <__assert>
40# include <__concepts/same_as.h>
41# include <__config>
42# include <__system_error/throw_system_error.h>
43# include <__utility/forward.h>
44# include <cerrno>
45# include <cstdio>
46# include <format>
47# include <string>
48# include <string_view>
49# include <version>
50
51# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
52# pragma GCC system_header
53# endif
54
55_LIBCPP_BEGIN_NAMESPACE_STD
56
57# ifdef _LIBCPP_WIN32API
58_LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream);
59
60# if _LIBCPP_HAS_WIDE_CHARACTERS
61// A wrapper for WriteConsoleW which is used to write to the Windows
62// console. This function is in the dylib to avoid pulling in windows.h
63// in the library headers. The function itself uses some private parts
64// of the dylib too.
65//
66// The function does not depend on the language standard used. Guarding
67// it with C++23 would fail since the dylib is currently built using C++20.
68//
69// Note the function is only implemented on the Windows platform.
70_LIBCPP_EXPORTED_FROM_ABI void __write_to_windows_console(FILE* __stream, wstring_view __view);
71# endif // _LIBCPP_HAS_WIDE_CHARACTERS
72# endif // _LIBCPP_WIN32API
73
74# if _LIBCPP_STD_VER >= 23
75
76# if _LIBCPP_HAS_UNICODE
77// This is the code to transcode UTF-8 to UTF-16. This is used on
78// Windows for the native Unicode API. The code is modeled to make it
79// easier to extend to
80//
81// P2728R0 Unicode in the Library, Part 1: UTF Transcoding
82//
83// This paper is still under heavy development so it makes no sense yet
84// to strictly follow the paper.
85namespace __unicode {
86
87// The names of these concepts are modelled after P2728R0, but the
88// implementation is not. char16_t may contain 32-bits so depending on the
89// number of bits is an issue.
90# ifdef _LIBCPP_SHORT_WCHAR
91template <class _Tp>
92concept __utf16_code_unit =
93 same_as<_Tp, char16_t>
94# if _LIBCPP_HAS_WIDE_CHARACTERS
95 || same_as<_Tp, wchar_t>
96# endif
97 ;
98template <class _Tp>
99concept __utf32_code_unit = same_as<_Tp, char32_t>;
100# else // _LIBCPP_SHORT_WCHAR
101template <class _Tp>
102concept __utf16_code_unit = same_as<_Tp, char16_t>;
103template <class _Tp>
104concept __utf32_code_unit =
105 same_as<_Tp, char32_t>
106# if _LIBCPP_HAS_WIDE_CHARACTERS
107 || same_as<_Tp, wchar_t>
108# endif
109 ;
110# endif // _LIBCPP_SHORT_WCHAR
111
112// Pass by reference since an output_iterator may not be copyable.
113template <class _OutIt>
114_LIBCPP_HIDE_FROM_ABI constexpr void __encode(_OutIt&, char32_t) = delete;
115
116template <class _OutIt>
117 requires __utf16_code_unit<iter_value_t<_OutIt>>
118_LIBCPP_HIDE_FROM_ABI constexpr void __encode(_OutIt& __out_it, char32_t __value) {
119 // [print.fun]/7 : "if `out` contains invalid code units, the behavior is undefined and implementations are encouraged
120 // to diagnose it".
121 _LIBCPP_ASSERT_UNCATEGORIZED(__is_scalar_value(__value), "an invalid unicode scalar value results in invalid UTF-16");
122
123 if (__value < 0x10000) {
124 *__out_it++ = static_cast<iter_value_t<_OutIt>>(__value);
125 return;
126 }
127
128 __value -= 0x10000;
129 *__out_it++ = 0xd800 + (__value >> 10);
130 *__out_it++ = 0xdc00 + (__value & 0x3FF);
131}
132
133template <class _OutIt>
134 requires __utf32_code_unit<iter_value_t<_OutIt>>
135_LIBCPP_HIDE_FROM_ABI constexpr void __encode(_OutIt& __out_it, char32_t __value) {
136 // [print.fun]/7 : "if `out` contains invalid code units, the behavior is undefined and implementations are encouraged
137 // to diagnose it".
138 _LIBCPP_ASSERT_UNCATEGORIZED(__is_scalar_value(__value), "an invalid unicode scalar value results in invalid UTF-32");
139 *__out_it++ = __value;
140}
141
142template <class _OutIt, input_iterator _InIt>
143 requires output_iterator<_OutIt, const iter_value_t<_OutIt>&> && (!same_as<iter_value_t<_OutIt>, iter_value_t<_InIt>>)
144_LIBCPP_HIDE_FROM_ABI constexpr _OutIt __transcode(_InIt __first, _InIt __last, _OutIt __out_it) {
145 // The __code_point_view has a basic_string_view interface.
146 // When transcoding becomes part of the standard we probably want to
147 // look at smarter algorithms.
148 // For example, when processing a code point that is encoded in
149 // 1 to 3 code units in UTF-8, the result will always be encoded
150 // in 1 code unit in UTF-16 (code points that require 4 code
151 // units in UTF-8 will require 2 code units in UTF-16).
152 //
153 // Note if P2728 is accepted types like int may become valid. In that case
154 // the __code_point_view should use a span. Libc++ will remove support for
155 // char_traits<int>.
156
157 // TODO PRINT Validate with clang-tidy
158 // NOLINTNEXTLINE(bugprone-dangling-handle)
159 basic_string_view<iter_value_t<_InIt>> __data{__first, __last};
160 __code_point_view<iter_value_t<_InIt>> __view{__data.begin(), __data.end()};
161 while (!__view.__at_end())
162 __unicode::__encode(__out_it, __view.__consume().__code_point);
163 return __out_it;
164}
165
166} // namespace __unicode
167
168# endif // _LIBCPP_HAS_UNICODE
169
170namespace __print {
171
172// [print.fun]/2
173// Effects: If the ordinary literal encoding ([lex.charset]) is UTF-8, equivalent to:
174// vprint_unicode(stream, fmt.str, make_format_args(args...));
175// Otherwise, equivalent to:
176// vprint_nonunicode(stream, fmt.str, make_format_args(args...));
177//
178// Based on the compiler and its compilation flags this value is or is
179// not true. As mentioned in P2093R14 this only affects Windows. The
180// test below could also be done for
181// - GCC using __GNUC_EXECUTION_CHARSET_NAME
182// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
183// - Clang using __clang_literal_encoding__
184// https://clang.llvm.org/docs/LanguageExtensions.html#builtin-macros
185// (note at the time of writing Clang is hard-coded to UTF-8.)
186//
187
188# if !_LIBCPP_HAS_UNICODE
189inline constexpr bool __use_unicode_execution_charset = false;
190# elif defined(_MSVC_EXECUTION_CHARACTER_SET)
191// This is the same test MSVC STL uses in their implementation of <print>
192// See: https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
193inline constexpr bool __use_unicode_execution_charset = _MSVC_EXECUTION_CHARACTER_SET == 65001;
194# else
195inline constexpr bool __use_unicode_execution_charset = true;
196# endif
197
198# ifdef _LIBCPP_WIN32API
199_LIBCPP_HIDE_FROM_ABI inline bool __is_terminal([[maybe_unused]] FILE* __stream) {
200 // The macro _LIBCPP_TESTING_PRINT_IS_TERMINAL is used to change
201 // the behavior in the test. This is not part of the public API.
202# ifdef _LIBCPP_TESTING_PRINT_IS_TERMINAL
203 return _LIBCPP_TESTING_PRINT_IS_TERMINAL(__stream);
204# else
205 return std::__is_windows_terminal(__stream);
206# endif
207}
208# endif // _LIBCPP_WIN32API
209
210template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
211_LIBCPP_HIDE_FROM_ABI inline void
212__vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args, bool __write_nl) {
213 _LIBCPP_ASSERT_NON_NULL(__stream, "__stream must be a valid pointer to an output C stream");
214 string __str = std::vformat(__fmt, __args);
215 if (__write_nl)
216 __str.push_back(c: '\n');
217
218 size_t __size = fwrite(ptr: __str.data(), size: 1, n: __str.size(), s: __stream);
219 if (__size < __str.size()) {
220 if (std::feof(__stream))
221 std::__throw_system_error(EIO, what_arg: "EOF while writing the formatted output");
222 std::__throw_system_error(ev: std::ferror(__stream), what_arg: "failed to write formatted output");
223 }
224}
225
226# if _LIBCPP_HAS_UNICODE
227
228// Note these helper functions are mainly used to aid testing.
229// On POSIX systems and Windows the output is no longer considered a
230// terminal when the output is redirected. Typically during testing the
231// output is redirected to be able to capture it. This makes it hard to
232// test this code path.
233
234# if _LIBCPP_HAS_WIDE_CHARACTERS
235template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
236_LIBCPP_HIDE_FROM_ABI inline void
237__vprint_unicode_windows([[maybe_unused]] FILE* __stream, string_view __fmt, format_args __args, bool __write_nl) {
238 string __str = std::vformat(__fmt, __args);
239 // UTF-16 uses the same number or less code units than UTF-8.
240 // However the size of the code unit is 16 bits instead of 8 bits.
241 //
242 // The buffer uses the worst-case estimate and should never resize.
243 // However when the string is large this could lead to OOM. Using a
244 // smaller size might work, but since the buffer uses a grow factor
245 // the final size might be larger when the estimate is wrong.
246 //
247 // TODO PRINT profile and improve the speed of this code.
248 __format::__retarget_buffer<wchar_t> __buffer{__str.size()};
249 __unicode::__transcode(first: __str.begin(), last: __str.end(), out_it: __buffer.__make_output_iterator());
250 if (__write_nl)
251 __buffer.push_back(c: L'\n');
252
253 [[maybe_unused]] wstring_view __view = __buffer.__view();
254
255 // The macro _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION is used to change
256 // the behavior in the test. This is not part of the public API.
257# ifdef _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION
258 _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION(__stream, __view);
259# elif defined(_LIBCPP_WIN32API)
260 std::__write_to_windows_console(__stream, __view);
261# else
262 std::__throw_runtime_error("No defintion of _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION and "
263 "__write_to_windows_console is not available.");
264# endif
265}
266# endif // _LIBCPP_HAS_WIDE_CHARACTERS
267
268template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
269_LIBCPP_HIDE_FROM_ABI inline void
270__vprint_unicode([[maybe_unused]] FILE* __stream,
271 [[maybe_unused]] string_view __fmt,
272 [[maybe_unused]] format_args __args,
273 [[maybe_unused]] bool __write_nl) {
274 _LIBCPP_ASSERT_NON_NULL(__stream, "__stream must be a valid pointer to an output C stream");
275
276 // [print.fun]
277 // 7 - Effects: If stream refers to a terminal capable of displaying
278 // Unicode, writes out to the terminal using the native Unicode
279 // API; if out contains invalid code units, the behavior is
280 // undefined and implementations are encouraged to diagnose it.
281 // Otherwise writes out to stream unchanged. If the native
282 // Unicode API is used, the function flushes stream before
283 // writing out.
284 // 8 - Throws: Any exception thrown by the call to vformat
285 // ([format.err.report]). system_error if writing to the terminal
286 // or stream fails. May throw bad_alloc.
287 // 9 - Recommended practice: If invoking the native Unicode API
288 // requires transcoding, implementations should substitute
289 // invalid code units with U+FFFD replacement character per the
290 // Unicode Standard, Chapter 3.9 U+FFFD Substitution in
291 // Conversion.
292
293 // On non-Windows platforms the Unicode API is the normal file I/O API
294 // so there the call can be forwarded to the non_unicode API. On
295 // Windows there is a different API. This API requires transcoding.
296
297# ifndef _LIBCPP_WIN32API
298 __print::__vprint_nonunicode(__stream, __fmt, __args, __write_nl);
299# elif _LIBCPP_HAS_WIDE_CHARACTERS
300 if (__print::__is_terminal(__stream)) {
301 // TODO PRINT Should flush errors throw too?
302 std::fflush(__stream);
303 __print::__vprint_unicode_windows(__stream, __fmt, __args, __write_nl);
304 } else {
305 __print::__vprint_nonunicode(__stream, __fmt, __args, __write_nl);
306 }
307# else
308# error "Windows builds with wchar_t disabled are not supported."
309# endif
310}
311
312# endif // _LIBCPP_HAS_UNICODE
313
314} // namespace __print
315
316template <class... _Args>
317_LIBCPP_HIDE_FROM_ABI void
318print(FILE* _LIBCPP_DIAGNOSE_NULLPTR __stream, format_string<_Args...> __fmt, _Args&&... __args) {
319# if _LIBCPP_HAS_UNICODE
320 if constexpr (__print::__use_unicode_execution_charset)
321 __print::__vprint_unicode(__stream, __fmt.get(), std::make_format_args(__args...), false);
322 else
323 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), false);
324# else // _LIBCPP_HAS_UNICODE
325 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), false);
326# endif // _LIBCPP_HAS_UNICODE
327}
328
329template <class... _Args>
330_LIBCPP_HIDE_FROM_ABI void print(format_string<_Args...> __fmt, _Args&&... __args) {
331 std::print(stdout, __fmt, std::forward<_Args>(__args)...);
332}
333
334template <class... _Args>
335_LIBCPP_HIDE_FROM_ABI void
336println(FILE* _LIBCPP_DIAGNOSE_NULLPTR __stream, format_string<_Args...> __fmt, _Args&&... __args) {
337# if _LIBCPP_HAS_UNICODE
338 // Note the wording in the Standard is inefficient. The output of
339 // std::format is a std::string which is then copied. This solution
340 // just appends a newline at the end of the output.
341 if constexpr (__print::__use_unicode_execution_charset)
342 __print::__vprint_unicode(__stream, __fmt.get(), std::make_format_args(__args...), true);
343 else
344 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), true);
345# else // _LIBCPP_HAS_UNICODE
346 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), true);
347# endif // _LIBCPP_HAS_UNICODE
348}
349
350template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
351_LIBCPP_HIDE_FROM_ABI inline void println(FILE* _LIBCPP_DIAGNOSE_NULLPTR __stream) {
352 std::print(__stream, fmt: "\n");
353}
354
355template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
356_LIBCPP_HIDE_FROM_ABI inline void println() {
357 println(stdout);
358}
359
360template <class... _Args>
361_LIBCPP_HIDE_FROM_ABI void println(format_string<_Args...> __fmt, _Args&&... __args) {
362 std::println(stdout, __fmt, std::forward<_Args>(__args)...);
363}
364
365# if _LIBCPP_HAS_UNICODE
366template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
367_LIBCPP_HIDE_FROM_ABI inline void
368vprint_unicode(FILE* _LIBCPP_DIAGNOSE_NULLPTR __stream, string_view __fmt, format_args __args) {
369 __print::__vprint_unicode(__stream, __fmt, __args, write_nl: false);
370}
371
372template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
373_LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(string_view __fmt, format_args __args) {
374 std::vprint_unicode(stdout, __fmt, __args);
375}
376
377# endif // _LIBCPP_HAS_UNICODE
378
379template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
380_LIBCPP_HIDE_FROM_ABI inline void
381vprint_nonunicode(FILE* _LIBCPP_DIAGNOSE_NULLPTR __stream, string_view __fmt, format_args __args) {
382 __print::__vprint_nonunicode(__stream, __fmt, __args, write_nl: false);
383}
384
385template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
386_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(string_view __fmt, format_args __args) {
387 std::vprint_nonunicode(stdout, __fmt, __args);
388}
389
390# endif // _LIBCPP_STD_VER >= 23
391
392_LIBCPP_END_NAMESPACE_STD
393
394#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
395
396#endif // _LIBCPP_PRINT
397