Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:58

0001 //===---------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
0033 _LIBCPP_HIDE_FROM_ABI inline void
0034 __vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
0035   // [ostream.formatted.print]/3
0036   // Effects: Behaves as a formatted output function
0037   // ([ostream.formatted.reqmts]) of os, except that:
0038   // - failure to generate output is reported as specified below, and
0039   // - any exception thrown by the call to vformat is propagated without regard
0040   //   to the value of os.exceptions() and without turning on ios_base::badbit
0041   //   in the error state of os.
0042   // After constructing a sentry object, the function initializes an automatic
0043   // variable via
0044   //   string out = vformat(os.getloc(), fmt, args);
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 // _LIBCPP_HAS_EXCEPTIONS
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 // _LIBCPP_HAS_EXCEPTIONS
0074   }
0075 }
0076 
0077 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
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 // Returns the FILE* associated with the __os.
0083 // Returns a nullptr when no FILE* is associated with __os.
0084 // This function is in the dylib since the type of the buffer associated
0085 // with std::cout, std::cerr, and std::clog is only known in the dylib.
0086 //
0087 // This function implements part of the implementation-defined behavior
0088 // of [ostream.formatted.print]/3
0089 //   If the function is vprint_unicode and os is a stream that refers to
0090 //   a terminal capable of displaying Unicode which is determined in an
0091 //   implementation-defined manner, writes out to the terminal using the
0092 //   native Unicode API;
0093 // Whether the returned FILE* is "a terminal capable of displaying Unicode"
0094 // is determined in the same way as the print(FILE*, ...) overloads.
0095 _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);
0096 
0097 #    if _LIBCPP_HAS_UNICODE
0098 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
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   // [ostream.formatted.print]/3
0108   //    If the function is vprint_unicode and os is a stream that refers to a
0109   //    terminal capable of displaying Unicode which is determined in an
0110   //    implementation-defined manner, writes out to the terminal using the
0111   //    native Unicode API; if out contains invalid code units, the behavior is
0112   //    undefined and implementations are encouraged to diagnose it. If the
0113   //    native Unicode API is used, the function flushes os before writing out.
0114   //
0115   // This is the path for the native API, start with flushing.
0116   __os.flush();
0117 
0118 #        if _LIBCPP_HAS_EXCEPTIONS
0119   try {
0120 #        endif // _LIBCPP_HAS_EXCEPTIONS
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 // _LIBCPP_HAS_EXCEPTIONS
0137 #      endif   // _LIBCPP_AVAILABILITY_HAS_PRINT
0138 }
0139 
0140 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
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 // _LIBCPP_HAS_UNICODE
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  // _LIBCPP_HAS_UNICODE
0154   std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
0155 #    endif // _LIBCPP_HAS_UNICODE
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   // Note the wording in the Standard is inefficient. The output of
0162   // std::format is a std::string which is then copied. This solution
0163   // just appends a newline at the end of the output.
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  // _LIBCPP_HAS_UNICODE
0169   std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
0170 #    endif // _LIBCPP_HAS_UNICODE
0171 }
0172 
0173 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
0174 _LIBCPP_HIDE_FROM_ABI inline void println(ostream& __os) {
0175   std::print(__os, "\n");
0176 }
0177 
0178 #  endif // _LIBCPP_STD_VER >= 23
0179 
0180 _LIBCPP_END_NAMESPACE_STD
0181 
0182 #endif // _LIBCPP_HAS_LOCALIZATION
0183 
0184 #endif // _LIBCPP___OSTREAM_PRINT_H