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