Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to avoid wchar APIs on Windows #3636

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# include <locale>
#endif

#ifdef _WIN32
#if defined(_WIN32) && !defined(FMT_WINDOWS_NO_WCHAR)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be detected automatically e.g. via some predefined macro or __has_include(<io.h>)?

Copy link
Contributor Author

@glebm glebm Sep 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

It can't always be detected automatically but I've added the 2 cases where it can (missing <io.h> on xbox, or defined WINVER on properly configured Windows projects). Even when it can't be detected automatically, one can always add_definitions(FMT_WINDOWS_NO_WCHAR) in the fmt subproject, so that's not a problem.

Copy link
Contributor

@vitaut vitaut Sep 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping that it will be less messy but thanks for the effort =). It is particularly weird to have this tested in fmt/core.h which explicitly avoids wchar_t. Let's go back to requiring the user to define FMT_WINDOWS_NO_WCHAR, just without the CMake config option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

image

# include <io.h> // _isatty
#endif

Expand Down Expand Up @@ -1426,7 +1426,7 @@ FMT_FUNC std::string vformat(string_view fmt, format_args args) {
}

namespace detail {
#ifndef _WIN32
#if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR)
FMT_FUNC bool write_console(std::FILE*, string_view) { return false; }
#else
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
Expand All @@ -1441,7 +1441,9 @@ FMT_FUNC bool write_console(std::FILE* f, string_view text) {
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
static_cast<uint32_t>(u16.size()), &written, nullptr) != 0;
}
#endif

#ifdef _WIN32
// Print assuming legacy (non-Unicode) encoding.
FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) {
auto buffer = memory_buffer();
Expand Down
2 changes: 1 addition & 1 deletion test/format-impl-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ TEST(format_impl_test, write_dragon_even) {
if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450");
}

#ifdef _WIN32
#if defined(_WIN32) && !defined(FMT_WINDOWS_NO_WCHAR)
# include <windows.h>

TEST(format_impl_test, write_console_signature) {
Expand Down