Browse Source

Optimisation for String and XML parsing.

tags/2021-05-28
jules 12 years ago
parent
commit
e336dd1c1c
3 changed files with 41 additions and 13 deletions
  1. +25
    -1
      modules/juce_core/text/juce_String.cpp
  2. +11
    -0
      modules/juce_core/text/juce_String.h
  3. +5
    -12
      modules/juce_core/xml/juce_XmlDocument.cpp

+ 25
- 1
modules/juce_core/text/juce_String.cpp View File

@@ -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<int>::max()));
appendCharPointer (CharPointer_ASCII (t));
appendCharPointer (CharPointer_UTF8 (t)); // (using UTF8 here triggers a faster code-path than ascii)
return *this;
}


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

@@ -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


+ 5
- 12
modules/juce_core/xml/juce_XmlDocument.cpp View File

@@ -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));
}
}
}
}


Loading…
Cancel
Save