diff --git a/modules/juce_core/text/juce_String.cpp b/modules/juce_core/text/juce_String.cpp index bcdd12dfd0..16909d8d74 100644 --- a/modules/juce_core/text/juce_String.cpp +++ b/modules/juce_core/text/juce_String.cpp @@ -605,6 +605,30 @@ void String::append (const String& textToAppend, size_t maxCharsToTake) appendCharPointer (textToAppend.text, maxCharsToTake); } +void String::appendCharPointer (const CharPointerType textToAppend) +{ + appendCharPointer (textToAppend, textToAppend.findTerminatingNull()); +} + +void String::appendCharPointer (const CharPointerType startOfTextToAppend, + const CharPointerType endOfTextToAppend) +{ + jassert (startOfTextToAppend.getAddress() != nullptr && endOfTextToAppend.getAddress() != nullptr); + jassert (startOfTextToAppend.getAddress() <= endOfTextToAppend.getAddress()); + + const size_t extraBytesNeeded = endOfTextToAppend.getAddress() - startOfTextToAppend.getAddress(); + + if (extraBytesNeeded > 0) + { + const size_t byteOffsetOfNull = getByteOffsetOfEnd(); + preallocateBytes (byteOffsetOfNull + extraBytesNeeded); + + char* const newStringStart = addBytesToPointer (text.getAddress(), (int) byteOffsetOfNull); + memcpy (newStringStart, startOfTextToAppend.getAddress(), extraBytesNeeded); + CharPointerType (newStringStart + extraBytesNeeded).writeNull(); + } +} + String& String::operator+= (const wchar_t* const t) { appendCharPointer (castToCharPointer_wchar_t (t)); @@ -628,7 +652,7 @@ String& String::operator+= (const char* const t) */ jassert (t == nullptr || CharPointer_ASCII::isValidString (t, std::numeric_limits::max())); - appendCharPointer (CharPointer_ASCII (t)); + appendCharPointer (CharPointer_UTF8 (t)); // (using UTF8 here triggers a faster code-path than ascii) return *this; } diff --git a/modules/juce_core/text/juce_String.h b/modules/juce_core/text/juce_String.h index 79018a46cf..a25f479eca 100644 --- a/modules/juce_core/text/juce_String.h +++ b/modules/juce_core/text/juce_String.h @@ -217,6 +217,17 @@ public: */ void append (const String& textToAppend, size_t maxCharsToTake); + /** Appends a string to the end of this one. + + @param startOfTextToAppend the start of the string to add. This must not be a nullptr + @param endOfTextToAppend the end of the string to add. This must not be a nullptr + */ + void appendCharPointer (const CharPointerType startOfTextToAppend, + const CharPointerType endOfTextToAppend); + + /** Appends a string to the end of this one. */ + void appendCharPointer (const CharPointerType textToAppend); + /** Appends a string to the end of this one. @param textToAppend the string to add diff --git a/modules/juce_core/xml/juce_XmlDocument.cpp b/modules/juce_core/xml/juce_XmlDocument.cpp index da4cc4e8de..8ba0463f47 100644 --- a/modules/juce_core/xml/juce_XmlDocument.cpp +++ b/modules/juce_core/xml/juce_XmlDocument.cpp @@ -326,7 +326,6 @@ void XmlDocument::readQuotedString (String& result) else { const String::CharPointerType start (input); - size_t numChars = 0; for (;;) { @@ -334,13 +333,13 @@ void XmlDocument::readQuotedString (String& result) if (character == quote) { - result.appendCharPointer (start, numChars); + result.appendCharPointer (start, input); ++input; return; } else if (character == '&') { - result.appendCharPointer (start, numChars); + result.appendCharPointer (start, input); break; } else if (character == 0) @@ -351,7 +350,6 @@ void XmlDocument::readQuotedString (String& result) } ++input; - ++numChars; } } } @@ -582,17 +580,15 @@ void XmlDocument::readChildElements (XmlElement* parent) else { const String::CharPointerType start (input); - size_t len = 0; for (;;) { const juce_wchar nextChar = *input; if (nextChar == '<' || nextChar == '&') - { break; - } - else if (nextChar == 0) + + if (nextChar == 0) { setLastError ("unmatched tags", false); outOfData = true; @@ -600,17 +596,14 @@ void XmlDocument::readChildElements (XmlElement* parent) } ++input; - ++len; } - textElementContent.appendCharPointer (start, len); + textElementContent.appendCharPointer (start, input); } } if ((! ignoreEmptyTextElements) || textElementContent.containsNonWhitespaceChars()) - { childAppender.append (XmlElement::createTextElement (textElementContent)); - } } } }