diff --git a/juce_amalgamated.cpp b/juce_amalgamated.cpp index 3f63b23fcd..2e2e1f74db 100644 --- a/juce_amalgamated.cpp +++ b/juce_amalgamated.cpp @@ -12007,6 +12007,44 @@ bool String::containsNonWhitespaceChars() const throw() return false; } +const String String::formatted (const juce_wchar* const pf, ... ) +{ + jassert (pf != 0); + + va_list args; + va_start (args, pf); + + size_t bufferSize = 256; + String result (bufferSize, (int) 0); + result.text[0] = 0; + + for (;;) + { +#if JUCE_LINUX && JUCE_64BIT + va_list tempArgs; + va_copy (tempArgs, args); + const int num = (int) vswprintf (result.text, bufferSize - 1, pf, tempArgs); + va_end (tempArgs); +#elif JUCE_WINDOWS + const int num = (int) _vsnwprintf (result.text, bufferSize - 1, pf, args); +#else + const int num = (int) vswprintf (result.text, bufferSize - 1, pf, args); +#endif + + if (num > 0) + return result; + + bufferSize += 256; + + if (num == 0 || bufferSize > 65536) // the upper limit is a sanity check to avoid situations where vprintf repeatedly + break; // returns -1 because of an error rather than because it needs more space. + + result.preallocateStorage (bufferSize); + } + + return empty; +} + int String::getIntValue() const throw() { return CharacterFunctions::getIntValue (text); diff --git a/juce_amalgamated.h b/juce_amalgamated.h index c4c9518c42..9cdd79ffba 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -1273,6 +1273,8 @@ public: static const String createStringFromData (const void* data, int size); + static const String formatted (const juce_wchar* formatString, ... ); + // Numeric conversions.. explicit String (int decimalInteger); diff --git a/src/text/juce_String.cpp b/src/text/juce_String.cpp index 356b039011..1c67c9a06c 100644 --- a/src/text/juce_String.cpp +++ b/src/text/juce_String.cpp @@ -1686,6 +1686,43 @@ bool String::containsNonWhitespaceChars() const throw() return false; } +const String String::formatted (const juce_wchar* const pf, ... ) +{ + jassert (pf != 0); + + va_list args; + va_start (args, pf); + + size_t bufferSize = 256; + String result (bufferSize, (int) 0); + result.text[0] = 0; + + for (;;) + { +#if JUCE_LINUX && JUCE_64BIT + va_list tempArgs; + va_copy (tempArgs, args); + const int num = (int) vswprintf (result.text, bufferSize - 1, pf, tempArgs); + va_end (tempArgs); +#elif JUCE_WINDOWS + const int num = (int) _vsnwprintf (result.text, bufferSize - 1, pf, args); +#else + const int num = (int) vswprintf (result.text, bufferSize - 1, pf, args); +#endif + + if (num > 0) + return result; + + bufferSize += 256; + + if (num == 0 || bufferSize > 65536) // the upper limit is a sanity check to avoid situations where vprintf repeatedly + break; // returns -1 because of an error rather than because it needs more space. + + result.preallocateStorage (bufferSize); + } + + return empty; +} //============================================================================== int String::getIntValue() const throw() diff --git a/src/text/juce_String.h b/src/text/juce_String.h index 7c1ce520be..1da08e0ad8 100644 --- a/src/text/juce_String.h +++ b/src/text/juce_String.h @@ -730,6 +730,17 @@ public: */ static const String createStringFromData (const void* data, int size); + /** Creates a String from a printf-style parameter list. + + I don't like this method. I don't use it myself, and I recommend avoiding it and + using the operator<< methods or pretty much anything else instead. It's only provided + here because of the popular unrest that was stirred-up when I tried to remove it... + + If you're really determined to use it, at least make sure that you never, ever, + pass any String objects to it as parameters. + */ + static const String formatted (const juce_wchar* formatString, ... ); + //============================================================================== // Numeric conversions..