Browse Source

Fixes for builds using utf-32 string storage.

tags/2021-05-28
jules 12 years ago
parent
commit
c54ca1037d
4 changed files with 31 additions and 8 deletions
  1. +1
    -1
      modules/juce_core/text/juce_Identifier.h
  2. +20
    -5
      modules/juce_core/text/juce_String.cpp
  3. +3
    -0
      modules/juce_core/text/juce_String.h
  4. +7
    -2
      modules/juce_core/text/juce_StringRef.h

+ 1
- 1
modules/juce_core/text/juce_Identifier.h View File

@@ -83,7 +83,7 @@ public:
const String::CharPointerType getCharPointer() const noexcept { return name; } const String::CharPointerType getCharPointer() const noexcept { return name; }
/** Returns this identifier as a StringRef. */ /** 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 */ /** Returns true if this Identifier is not null */
bool isValid() const noexcept { return name.getAddress() != nullptr; } bool isValid() const noexcept { return name.getAddress() != nullptr; }


+ 20
- 5
modules/juce_core/text/juce_String.cpp View File

@@ -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) 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) #if (JUCE_STRING_UTF_TYPE == 8)
stream.write (text.getCharPointer().getAddress(), numBytes);
stream.write (text.text.getAddress(), numBytes);
#else #else
// (This avoids using toUTF8() to prevent the memory bloat that it would leave behind // (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). // if lots of large, persistent strings were to be written to streams).
HeapBlock<char> temp (numBytes + 1); HeapBlock<char> temp (numBytes + 1);
CharPointer_UTF8 (temp).writeAll (text.getCharPointer());
CharPointer_UTF8 (temp).writeAll (text.text);
stream.write (temp, numBytes); stream.write (temp, numBytes);
#endif #endif
@@ -2047,12 +2052,21 @@ String String::fromUTF8 (const char* const buffer, int bufferSizeBytes)
#endif #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!! jassert (stringLiteral != nullptr); // This must be a valid string literal, not a null pointer!!
#if JUCE_NATIVE_WCHAR_IS_UTF8 #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()) {} StringRef::StringRef (const String& string) noexcept : text (string.getCharPointer()) {}
//============================================================================== //==============================================================================
//============================================================================== //==============================================================================
#if JUCE_UNIT_TESTS #if JUCE_UNIT_TESTS


+ 3
- 0
modules/juce_core/text/juce_String.h View File

@@ -1337,5 +1337,8 @@ std::basic_ostream <wchar_t, traits>& JUCE_CALLTYPE operator<< (std::basic_ostre
/** Writes a string to an OutputStream as UTF8. */ /** Writes a string to an OutputStream as UTF8. */
JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& stringToWrite); 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 #endif // JUCE_STRING_H_INCLUDED

+ 7
- 2
modules/juce_core/text/juce_StringRef.h View File

@@ -29,7 +29,6 @@
#ifndef JUCE_STRINGREF_H_INCLUDED #ifndef JUCE_STRINGREF_H_INCLUDED
#define JUCE_STRINGREF_H_INCLUDED #define JUCE_STRINGREF_H_INCLUDED
//============================================================================== //==============================================================================
/** /**
A simple class for holding temporary references to a string literal or String. 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. ensure that the data does not change during the lifetime of the StringRef.
Note that this pointer not be null! 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. /** Creates a StringRef from a raw char pointer.
The StringRef object does NOT take ownership or copy this data, so you must The StringRef object does NOT take ownership or copy this data, so you must
@@ -115,6 +114,12 @@ public:
//============================================================================== //==============================================================================
/** The text that is referenced. */ /** The text that is referenced. */
String::CharPointerType text; 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
}; };
//============================================================================== //==============================================================================


Loading…
Cancel
Save