-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
printf %ls support - C++ standard compatibility #572
Comments
Thanks for raising this, I agree that it should be supported. Would you be willing to contribute a fix by any chance? |
I haven't dived into the internals yet. Made a workaround for now... |
This sounds very useful. I started looking at how to do it. If narrow format strings can accept wide arguments (and vice versa), we lose compile-time safety checks like #606. But if we can always do the right thing with the input, this is not a problem. However, conversion errors can happen if the input is invalid UTF-8, or if it contains characters that aren't in the output code set (non-Unicode only). We could throw an exception, replace with some other character or truncate the string. Should this be configurable somehow? One more complication: setlocale() can't use UTF-8 on Windows/MSVC, so std::wcrtomb/mbrtowc() won't work here. I'm guessing most users want UTF-8, but console output uses the system code page by default so if they do not explicitly set up for UTF-8 they will get mojibake or worse when writing non-ASCII. This points to another new option: use the locale setting or force UTF-8. Or we could say fmtlib always uses UTF-8 and if you want some other char encoding you have to convert it yourself. I would be happy with this restriction. BasicWriter::write_str() currently uses std::uninitialized_copy() to convert from char to wchar_t. This only works if the input is ASCII. I think this would have to change. I'm assuming wchar_t* always means UTF-16 on Windows and UTF-32 elsewhere. Hope that's OK. @vitaut, are you ready to dive into Unicode madness? 😃 |
I'd go with an exception by default, but it would be nice to make this configurable. The
Yes, this is a pre-#606 artefact.
Why is it necessary? If we use
Yes, sounds exciting =). |
I have a very rough proof of concept using This work overlaps with #628, but if I tried to handle that at the same time I would never finish anything, so I'm ignoring it for now. This modern C++ stuff is still new to me. |
Just commenting that boost.locale and https://tzlaine.github.io/text/doc/html/index.html provide tools to work with unicode; maybe they are useful for fmt? |
Thanks, @aetchevarne. |
Is there a cheap workaround to use On Windows these ANSI<>UTF-16 differences are really an issue. |
You could use |
But does that mean that I need to link against Microsoft's complete C++ Rest SDK?
How does this returned string's content outlive the call to As a side note, sometimes I need to perform conversions between
|
In the above example, however, allocation will always happen, since the A more transparent way consists of partially specializing Or alternatively why does |
No, I was talking about fmt's |
Ah, ok that seems like a more appropriate choice. Seems like ATL's CA2W and CW2A, but with exceptions.
Correction: the size of the buffer is a template argument set to 128 by default. So I can increase this to 256 or 512 as well. So can I call these Without knowing much about the internals of Thanks for the support. |
Sure, if it compiles =). You can safely pass temporaries to |
Looks like there is not enough interest in this feature so closing, but PRs are still welcome. |
Could you please reconsider implementing this. |
Hi,
The current library version does not allow mixing wchar_t* arguments into char* format strings. The following code does not compile (at least not on MS VS2017):
fmt::printf("%ls", L"foo");
However, according to the C++11 standard, it should be valid for printf-like functions.
Looking into ISO/IEC 9899:TC2, it describes the %ls format specifier as following (page 279):
It would be nice to have this standard compatibility :-)
Best regards,
Vasili
The text was updated successfully, but these errors were encountered: