I know that Jules doesn’t like String::formatted - but I have an internationalized(*) application which has a lot of strings like: “Unable to open file %s with error code %s” and there really isn’t another way to do it.
First, there’s a buffer overflow. The buffer used is 256 bytes long - if the results of String::formatted are greater than 256 bytes, it simply writes into “random memory”. This 256 character limitation isn’t documented, and as you know, buffer overflows are very dangerous…
So far, I haven’t hit this limitation - I found it while debugging the next problem - but since I use String::formatted to make fairly long error messages that include full file paths it is a matter of certainty that if enough people use my program, someone will get an error with a file with a very long path and I’ll have something completely unpredictable happen.
My second issue is that String::formatted seemed to work fine on the Mac to print strings, but I got non UTF-8 (“garbage”) characters generated on the PC.
After some debugging, I narrowed it down to the fact that Juce’s String::formatted only seems to accept wide characters on the PC, and only narrow characters on the Mac!
The following code works fine, but requires different actual calls on Mac and on PC…[code]String MAIL_SUBJECT(“Support Request: %s”);
String title = “Some title here”;
const char* narrow = s.toUTF8().getAddress();
const wchar_t* wide = s.toWideCharPointer();
String res =
#if JUCE_WINDOWS
String::formatted(MAIL_SUBJECT, wide);
#else
String::formatted(MAIL_SUBJECT, narrow);
#endif
[/code]The code sample works fine on both Mac and PC. If I change the condition to !JUCE_WINDOWS, it works incorrectly on both Mac and PC, so I can’t use just one of the two alternatives…
This doesn’t seem right. Is there a better way to do this, or am I missing something obvious?
(* - admittedly, we haven’t prepared a translation yet, but it’s all set up to do so…)