String::toNative()


#1

Can you bring back this method so it produce a string that is platform native format, whatever the current String representation ?
I need this for the String::formatted method, where I’m passing %ls as a format rule, expecting a const wchar_t * parameter.

(Or am I missing something in the String class ?)


#2

There’s not really any such thing as a platform “native” string format. What you’re really asking for is String::toAPointerSuitableForUseAsAParameterInFormatted(), and that ain’t gonna happen. You know my feelings about the formatted() method, and I’d really strongly recommend only using it for numeric types, not strings.


#3

X-Ryl have you taken a look at boost::format? You can use wstring and wchar_t, and it is more type-safe.

Also, one solution I have used in the past is simply to write my own printf. With that technique you can have your own conversion. For example, you could have a conversion for a native juce String. And if you are clever with templates you can use a templatized version of printf() that, instead of outputting, measures the space needed for the conversion. This way you can call it once to measure, allocate perfect sized storage, and then call it again to actually write the result.

There’s an open source printf() floating around that is perfectly suited for this or you could rip it from an open sourced stdlib.


#4

I’ve done mine a few years back. Probably not the fastest one, but it used to work in a cross platform way by the time.
Anyway, I think having a method to convert to the native “const wchar_t *” type is required in any String class, especially in 2011.

While I still don’t understand your woes against sprintf like method, as soon as you want to speak to the exterior world, you need this, there isn’t only wsprint function, but all Windows’ API taking wchar_t as native string format (not speaking of the linux’s text processing functions).

Here’s the code for this, if you don’t want to write it, please copy and paste this:

/** Convert the string to the native wchar_t type supported by compiler. */
const wchar_t * toNative();

[...]
#ifdef JUCE_WINDOWS
   const wchar_t * String::toNative() { return toUTF16().getAddress(); }
#else
   const wchar_t * String::toNative() { return toUTF32().getAddress(); }
#endif

#5

But even wchar_t is ambiguous! On linux, it’s UTF32, on windows it’s UTF16. I guess I could have a String::getWideCharPointer(), but it’d behave differently on different platforms, and I don’t really want the String class to be too heavily platform-dependent.


#6

It’s already platform dependent, since you’ve abstracted the OLESTR/BSTR stuff already.
Anyway, when you use external libraries, they use wchar_t type, whatever the compiler’s storage size. If you want to call such functions, currently, it’s not possible. It’s a huge regression from the previous String implementation you’ve made.

You can already do L"string" to feed a String, why not the opposite ?
I swear I will not serialize the string on Linux and unserialize on Windows (most protocol use UTF-8 for this anyway) :o .


#7

Well, ok, I’ll have a bit of a think about it…