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