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

Disallow strings as input for wstring templates #606

Closed
wants to merge 1 commit into from
Closed

Disallow strings as input for wstring templates #606

wants to merge 1 commit into from

Conversation

smat
Copy link

@smat smat commented Nov 9, 2017

This change will prevent mixing character encodings by accident, by disallowing using strings as input for wstring templates.

The change will cause the following to be a compile error:

EXPECT_EQ(L"abc", format(L"{}", "abc"));

Use case: Using a wstring as a template with an UTF-8 string will mix UTF-32/UTF-16 and UTF-8 in the resulting string. When you try to convert the string to UTF-8, to_bytes will throw an exception.

fmt::WMemoryWriter out;
out.write(L"{}", "\u00F6");
const auto utf8_string = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(out.str());
std::cout << utf8_string << std::endl;

@vitaut
Copy link
Contributor

vitaut commented Nov 10, 2017

Thanks for the PR!

This is somewhat related to #572. Instead of giving a compile-time error it might be better to convert a multibyte string into a wide one as if by calling mbrtowc. What do you think?

@vitaut
Copy link
Contributor

vitaut commented Nov 19, 2017

Implemented a slightly different fix in ba01a5d. This gives a compiler error now:

fmt::format(L"{}", "\u00F6");

Thanks for catching the issue!

@hestad
Copy link

hestad commented Dec 6, 2017

When will the next release of fmt be? We are waiting for this commit to find its way into spdlog :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants