diff --git a/modules/juce_core/text/juce_Identifier.h b/modules/juce_core/text/juce_Identifier.h index cac9761b67..0f58dbc3d3 100644 --- a/modules/juce_core/text/juce_Identifier.h +++ b/modules/juce_core/text/juce_Identifier.h @@ -83,7 +83,7 @@ public: const String::CharPointerType getCharPointer() const noexcept { return name; } /** Returns this identifier as a StringRef. */ - operator StringRef() const noexcept { return name.getAddress(); } + operator StringRef() const noexcept { return name; } /** Returns true if this Identifier is not null */ bool isValid() const noexcept { return name.getAddress() != nullptr; } diff --git a/modules/juce_core/text/juce_String.cpp b/modules/juce_core/text/juce_String.cpp index cd24e3f085..7895571981 100644 --- a/modules/juce_core/text/juce_String.cpp +++ b/modules/juce_core/text/juce_String.cpp @@ -738,15 +738,20 @@ JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const double number) JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& text) { - const size_t numBytes = text.getNumBytesAsUTF8(); + return operator<< (stream, StringRef (text)); +} + +JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, StringRef text) +{ + const size_t numBytes = CharPointer_UTF8::getBytesRequiredFor (text.text); #if (JUCE_STRING_UTF_TYPE == 8) - stream.write (text.getCharPointer().getAddress(), numBytes); + stream.write (text.text.getAddress(), numBytes); #else // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind // if lots of large, persistent strings were to be written to streams). HeapBlock temp (numBytes + 1); - CharPointer_UTF8 (temp).writeAll (text.getCharPointer()); + CharPointer_UTF8 (temp).writeAll (text.text); stream.write (temp, numBytes); #endif @@ -2047,12 +2052,21 @@ String String::fromUTF8 (const char* const buffer, int bufferSizeBytes) #endif //============================================================================== -StringRef::StringRef() noexcept : text ("\0\0\0") +StringRef::StringRef() noexcept : text ((const String::CharPointerType::CharType*) "\0\0\0") { } -StringRef::StringRef (const String::CharPointerType::CharType* stringLiteral) noexcept : text (stringLiteral) +StringRef::StringRef (const char* stringLiteral) noexcept + #if JUCE_STRING_UTF_TYPE != 8 + : text (nullptr), stringCopy (stringLiteral) + #else + : text (stringLiteral) + #endif { + #if JUCE_STRING_UTF_TYPE != 8 + text = stringCopy.getCharPointer(); + #endif + jassert (stringLiteral != nullptr); // This must be a valid string literal, not a null pointer!! #if JUCE_NATIVE_WCHAR_IS_UTF8 @@ -2080,6 +2094,7 @@ StringRef::StringRef (String::CharPointerType stringLiteral) noexcept : text (s StringRef::StringRef (const String& string) noexcept : text (string.getCharPointer()) {} + //============================================================================== //============================================================================== #if JUCE_UNIT_TESTS diff --git a/modules/juce_core/text/juce_String.h b/modules/juce_core/text/juce_String.h index b368058094..0df7504b31 100644 --- a/modules/juce_core/text/juce_String.h +++ b/modules/juce_core/text/juce_String.h @@ -1337,5 +1337,8 @@ std::basic_ostream & JUCE_CALLTYPE operator<< (std::basic_ostre /** Writes a string to an OutputStream as UTF8. */ JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& stringToWrite); +/** Writes a string to an OutputStream as UTF8. */ +JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, StringRef stringToWrite); + #endif // JUCE_STRING_H_INCLUDED diff --git a/modules/juce_core/text/juce_StringRef.h b/modules/juce_core/text/juce_StringRef.h index c7579608f2..4b66a791d0 100644 --- a/modules/juce_core/text/juce_StringRef.h +++ b/modules/juce_core/text/juce_StringRef.h @@ -29,7 +29,6 @@ #ifndef JUCE_STRINGREF_H_INCLUDED #define JUCE_STRINGREF_H_INCLUDED - //============================================================================== /** A simple class for holding temporary references to a string literal or String. @@ -71,7 +70,7 @@ public: ensure that the data does not change during the lifetime of the StringRef. Note that this pointer not be null! */ - StringRef (const String::CharPointerType::CharType* stringLiteral) noexcept; + StringRef (const char* stringLiteral) noexcept; /** Creates a StringRef from a raw char pointer. The StringRef object does NOT take ownership or copy this data, so you must @@ -115,6 +114,12 @@ public: //============================================================================== /** The text that is referenced. */ String::CharPointerType text; + + #if JUCE_STRING_UTF_TYPE != 8 && ! defined (DOXYGEN) + // Sorry, non-UTF8 people, you're unable to take advantage of StringRef, because + // you've chosen a character encoding that doesn't match C++ string literals. + String stringCopy; + #endif }; //==============================================================================