libstdc++: Simplify std::vprint_unicode for non-Windows targets

Since we don't need to do anything special to print Unicode on
non-Windows targets, we might as well just use std::vprint_nonunicode to
implement std::vprint_unicode. Removing the duplicated code should
reduce code size in cases where those calls aren't inlined.

Also use an RAII type for the unused case where a non-Windows target
calls __open_terminal(streambuf*) and needs to fclose the result. This
makes the code futureproof in case we ever start using the
__write_terminal function for non-Windows targets.

libstdc++-v3/ChangeLog:

	* include/std/ostream (vprint_unicode) [_WIN32]: Use RAII guard.
	(vprint_unicode) [!_WIN32]: Just call vprint_nonunicode.
	* include/std/print (vprint_unicode) [!_WIN32]: Likewise.
This commit is contained in:
Jonathan Wakely 2023-12-15 12:24:26 +00:00
parent 8a5cac92e7
commit 7d2e100058
2 changed files with 30 additions and 11 deletions

View file

@ -906,6 +906,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
inline void
vprint_unicode(ostream& __os, string_view __fmt, format_args __args)
{
#ifndef _WIN32
// For most targets we don't need to do anything special to write
// Unicode to a terminal.
std::vprint_nonunicode(__os, __fmt, __args);
#else
ostream::sentry __cerb(__os);
if (__cerb)
{
@ -913,12 +918,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args);
auto __out = __buf.view();
#ifdef _WIN32
void* __open_terminal(streambuf*);
error_code __write_to_terminal(void*, span<char>);
// If stream refers to a terminal, write a Unicode string to it.
if (auto __term = __open_terminal(__os.rdbuf()))
{
#ifndef _WIN32
// For POSIX, __open_terminal(streambuf*) uses fdopen to open a
// new file, so we would need to close it here. This code is not
// actually compiled because it's inside an #ifdef _WIN32 group,
// but just in case that changes in future ...
struct _Guard
{
_Guard(void* __p) : _M_f((FILE*)__p) { }
~_Guard() { std::fclose(_M_f); }
_Guard(_Guard&&) = delete;
_Guard& operator=(_Guard&&) = delete;
FILE* _M_f;
};
_Guard __g(__term);
#endif
ios_base::iostate __err = ios_base::goodbit;
__try
{
@ -927,11 +947,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
else if (auto __e = __write_to_terminal(__term, __out))
if (__e != std::make_error_code(errc::illegal_byte_sequence))
__err = ios::badbit;
#ifndef _WIN32
// __open_terminal(streambuf*) opens a new FILE with fdopen,
// so we need to close it here.
std::fclose((FILE*)__term);
#endif
}
__catch(const __cxxabiv1::__forced_unwind&)
{
@ -945,9 +960,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__os.setstate(__err);
return;
}
#endif
// Otherwise just insert the string as normal.
// Otherwise just insert the string as vprint_nonunicode does.
__try
{
std::__ostream_write(__os, __out.data(), __out.size());
@ -960,6 +974,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__catch(...)
{ __os._M_setstate(ios_base::badbit); }
}
#endif // _WIN32
}
template<typename... _Args>

View file

@ -64,11 +64,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
inline void
vprint_unicode(FILE* __stream, string_view __fmt, format_args __args)
{
#ifndef _WIN32
// For most targets we don't need to do anything special to write
// Unicode to a terminal.
std::vprint_nonunicode(__stream, __fmt, __args);
#else
__format::_Str_sink<char> __buf;
std::vformat_to(__buf.out(), __fmt, __args);
auto __out = __buf.view();
#ifdef _WIN32
void* __open_terminal(FILE*);
error_code __write_to_terminal(void*, span<char>);
// If stream refers to a terminal, write a native Unicode string to it.
@ -88,11 +92,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__e = error_code(errno, generic_category());
_GLIBCXX_THROW_OR_ABORT(system_error(__e, "std::vprint_unicode"));
}
#endif
// Otherwise just write the string to the file.
// Otherwise just write the string to the file as vprint_nonunicode does.
if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size())
__throw_system_error(EIO);
#endif
}
template<typename... _Args>