Browse Source

Minor string tweaks.

tags/2021-05-28
Julian Storer 15 years ago
parent
commit
06fb01aab3
7 changed files with 182 additions and 186 deletions
  1. +74
    -81
      juce_amalgamated.cpp
  2. +14
    -12
      juce_amalgamated.h
  3. +3
    -12
      src/gui/components/mouse/juce_MouseInputSource.cpp
  4. +44
    -50
      src/text/juce_String.cpp
  5. +1
    -1
      src/text/juce_String.h
  6. +27
    -19
      src/text/juce_XmlElement.cpp
  7. +19
    -11
      src/text/juce_XmlElement.h

+ 74
- 81
juce_amalgamated.cpp View File

@@ -10433,7 +10433,7 @@ public:
return text; return text;


juce_wchar* const newText = create (b->allocatedNumChars); juce_wchar* const newText = create (b->allocatedNumChars);
memcpy (newText, text, (b->allocatedNumChars + 1) * sizeof (juce_wchar));
copyChars (newText, text, b->allocatedNumChars);
release (b); release (b);


return newText; return newText;
@@ -10447,7 +10447,7 @@ public:
return text; return text;


juce_wchar* const newText = create (jmax (b->allocatedNumChars, numChars)); juce_wchar* const newText = create (jmax (b->allocatedNumChars, numChars));
memcpy (newText, text, (b->allocatedNumChars + 1) * sizeof (juce_wchar));
copyChars (newText, text, b->allocatedNumChars);
release (b); release (b);


return newText; return newText;
@@ -10458,6 +10458,12 @@ public:
return bufferFromText (text)->allocatedNumChars; return bufferFromText (text)->allocatedNumChars;
} }


static void copyChars (juce_wchar* const dest, const juce_wchar* const src, const size_t numChars) throw()
{
memcpy (dest, src, numChars * sizeof (juce_wchar));
dest [numChars] = 0;
}

int refCount; int refCount;
size_t allocatedNumChars; size_t allocatedNumChars;
juce_wchar text[1]; juce_wchar text[1];
@@ -10479,7 +10485,7 @@ void String::createInternal (const juce_wchar* const t, const size_t numChars)
jassert (t[numChars] == 0); // must have a null terminator jassert (t[numChars] == 0); // must have a null terminator


text = StringHolder::create (numChars); text = StringHolder::create (numChars);
memcpy (text, t, (numChars + 1) * sizeof (juce_wchar));
StringHolder::copyChars (text, t, numChars);
} }


void String::appendInternal (const juce_wchar* const newText, const int numExtraChars) void String::appendInternal (const juce_wchar* const newText, const int numExtraChars)
@@ -10490,16 +10496,10 @@ void String::appendInternal (const juce_wchar* const newText, const int numExtra
const int newTotalLen = oldLen + numExtraChars; const int newTotalLen = oldLen + numExtraChars;


text = StringHolder::makeUniqueWithSize (text, newTotalLen); text = StringHolder::makeUniqueWithSize (text, newTotalLen);
memcpy (text + oldLen, newText, numExtraChars * sizeof (juce_wchar));
text [newTotalLen] = 0;
StringHolder::copyChars (text + oldLen, newText, numExtraChars);
} }
} }


void String::dupeInternalIfMultiplyReferenced()
{
text = StringHolder::makeUnique (text);
}

void String::preallocateStorage (const size_t numChars) void String::preallocateStorage (const size_t numChars)
{ {
text = StringHolder::makeUniqueWithSize (text, numChars); text = StringHolder::makeUniqueWithSize (text, numChars);
@@ -10539,6 +10539,13 @@ String::String (const size_t numChars, const int /*dummyVariable*/)
{ {
} }


String::String (const String& stringToCopy, const size_t charsToAllocate)
{
const size_t otherSize = StringHolder::getAllocatedNumChars (stringToCopy.text);
text = StringHolder::create (jmax (charsToAllocate, otherSize));
StringHolder::copyChars (text, stringToCopy.text, otherSize);
}

String::String (const char* const t) String::String (const char* const t)
{ {
if (t != 0 && *t != 0) if (t != 0 && *t != 0)
@@ -10559,7 +10566,7 @@ String::String (const juce_wchar* const t)
{ {
const int len = CharacterFunctions::length (t); const int len = CharacterFunctions::length (t);
text = StringHolder::create (len); text = StringHolder::create (len);
memcpy (text, t, (len + 1) * sizeof (juce_wchar));
StringHolder::copyChars (text, t, len);
} }
else else
{ {
@@ -10596,8 +10603,7 @@ String::String (const juce_wchar* const t, const size_t maxChars)
if (i > 0) if (i > 0)
{ {
text = StringHolder::create (i); text = StringHolder::create (i);
memcpy (text, t, i * sizeof (juce_wchar));
text[i] = 0;
StringHolder::copyChars (text, t, i);
} }
else else
{ {
@@ -11413,13 +11419,11 @@ const String String::repeatedString (const juce_wchar* const stringToRepeat, int
{ {
const int len = CharacterFunctions::length (stringToRepeat); const int len = CharacterFunctions::length (stringToRepeat);
String result ((size_t) (len * numberOfTimesToRepeat + 1), (int) 0); String result ((size_t) (len * numberOfTimesToRepeat + 1), (int) 0);

juce_wchar* n = result.text; juce_wchar* n = result.text;
n[0] = 0;


while (--numberOfTimesToRepeat >= 0) while (--numberOfTimesToRepeat >= 0)
{ {
CharacterFunctions::append (n, stringToRepeat);
StringHolder::copyChars (n, stringToRepeat, len);
n += len; n += len;
} }


@@ -11431,21 +11435,17 @@ const String String::paddedLeft (const juce_wchar padCharacter, int minimumLengt
jassert (padCharacter != 0); jassert (padCharacter != 0);


const int len = length(); const int len = length();

if (len >= minimumLength || padCharacter == 0) if (len >= minimumLength || padCharacter == 0)
return *this; return *this;


String result ((size_t) minimumLength + 1, (int) 0); String result ((size_t) minimumLength + 1, (int) 0);

juce_wchar* n = result.text; juce_wchar* n = result.text;


minimumLength -= len; minimumLength -= len;
while (--minimumLength >= 0) while (--minimumLength >= 0)
*n++ = padCharacter; *n++ = padCharacter;


*n = 0;
CharacterFunctions::append (n, text);

StringHolder::copyChars (n, text, len);
return result; return result;
} }


@@ -11453,11 +11453,19 @@ const String String::paddedRight (const juce_wchar padCharacter, int minimumLeng
{ {
jassert (padCharacter != 0); jassert (padCharacter != 0);


const int paddingNeeded = minimumLength - length();
if (paddingNeeded <= 0 || padCharacter == 0)
const int len = length();
if (len >= minimumLength || padCharacter == 0)
return *this; return *this;


return *this + String::empty.paddedLeft (padCharacter, paddingNeeded);
String result (*this, (size_t) minimumLength);
juce_wchar* n = result.text + len;

minimumLength -= len;
while (--minimumLength >= 0)
*n++ = padCharacter;

*n = 0;
return result;
} }


const String String::replaceSection (int index, int numCharsToReplace, const String String::replaceSection (int index, int numCharsToReplace,
@@ -11499,21 +11507,17 @@ const String String::replaceSection (int index, int numCharsToReplace,


String result ((size_t) newTotalLen, (int) 0); String result ((size_t) newTotalLen, (int) 0);


memcpy (result.text, text, index * sizeof (juce_wchar));
StringHolder::copyChars (result.text, text, index);


if (newStringLen > 0) if (newStringLen > 0)
memcpy (result.text + index,
stringToInsert,
newStringLen * sizeof (juce_wchar));
StringHolder::copyChars (result.text + index, stringToInsert, newStringLen);


const int endStringLen = newTotalLen - (index + newStringLen); const int endStringLen = newTotalLen - (index + newStringLen);


if (endStringLen > 0) if (endStringLen > 0)
memcpy (result.text + (index + newStringLen),
text + (index + numCharsToReplace),
endStringLen * sizeof (juce_wchar));

result.text [newTotalLen] = 0;
StringHolder::copyChars (result.text + (index + newStringLen),
text + (index + numCharsToReplace),
endStringLen);


return result; return result;
} }
@@ -11545,9 +11549,7 @@ const String String::replaceCharacter (const juce_wchar charToReplace, const juc
if (index < 0) if (index < 0)
return *this; return *this;


String result (*this);
result.dupeInternalIfMultiplyReferenced();

String result (*this, size_t());
juce_wchar* t = result.text + index; juce_wchar* t = result.text + index;


while (*t != 0) while (*t != 0)
@@ -11564,9 +11566,7 @@ const String String::replaceCharacter (const juce_wchar charToReplace, const juc
const String String::replaceCharacters (const String& charactersToReplace, const String String::replaceCharacters (const String& charactersToReplace,
const juce_wchar* const charactersToInsertInstead) const const juce_wchar* const charactersToInsertInstead) const
{ {
String result (*this);
result.dupeInternalIfMultiplyReferenced();

String result (*this, size_t());
juce_wchar* t = result.text; juce_wchar* t = result.text;
const int len2 = CharacterFunctions::length (charactersToInsertInstead); const int len2 = CharacterFunctions::length (charactersToInsertInstead);


@@ -11639,16 +11639,14 @@ bool String::endsWithIgnoreCase (const juce_wchar* const other) const throw()


const String String::toUpperCase() const const String String::toUpperCase() const
{ {
String result (*this);
result.dupeInternalIfMultiplyReferenced();
String result (*this, size_t());
CharacterFunctions::toUpperCase (result.text); CharacterFunctions::toUpperCase (result.text);
return result; return result;
} }


const String String::toLowerCase() const const String String::toLowerCase() const
{ {
String result (*this);
result.dupeInternalIfMultiplyReferenced();
String result (*this, size_t());
CharacterFunctions::toLowerCase (result.text); CharacterFunctions::toLowerCase (result.text);
return result; return result;
} }
@@ -11656,9 +11654,7 @@ const String String::toLowerCase() const
juce_wchar& String::operator[] (const int index) juce_wchar& String::operator[] (const int index)
{ {
jassert (((unsigned int) index) <= (unsigned int) length()); jassert (((unsigned int) index) <= (unsigned int) length());

dupeInternalIfMultiplyReferenced();

text = StringHolder::makeUnique (text);
return text [index]; return text [index];
} }


@@ -11777,7 +11773,7 @@ const String String::unquoted() const
{ {
String s (*this); String s (*this);


if (s[0] == T('"') || s[0] == T('\''))
if (s.text[0] == T('"') || s.text[0] == T('\''))
s = s.substring (1); s = s.substring (1);


const int lastCharIndex = s.length() - 1; const int lastCharIndex = s.length() - 1;
@@ -12440,9 +12436,7 @@ int String::copyToCString (char* destBuffer, const int maxBufferSizeBytes) const


void String::copyToUnicode (juce_wchar* const destBuffer, const int maxCharsToCopy) const throw() void String::copyToUnicode (juce_wchar* const destBuffer, const int maxCharsToCopy) const throw()
{ {
const int len = jmin (maxCharsToCopy, length());
memcpy (destBuffer, text, len * sizeof (juce_wchar));
destBuffer [len] = 0;
StringHolder::copyChars (destBuffer, text, jmin (maxCharsToCopy, length()));
} }


String::Concatenator::Concatenator (String& stringToAppendTo) String::Concatenator::Concatenator (String& stringToAppendTo)
@@ -14125,7 +14119,7 @@ namespace XmlOutputFunctions
} }
} }


static void writeSpaces (OutputStream& out, int numSpaces) throw()
static void writeSpaces (OutputStream& out, int numSpaces)
{ {
if (numSpaces > 0) if (numSpaces > 0)
{ {
@@ -14145,7 +14139,7 @@ namespace XmlOutputFunctions


void XmlElement::writeElementAsText (OutputStream& outputStream, void XmlElement::writeElementAsText (OutputStream& outputStream,
const int indentationLevel, const int indentationLevel,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
using namespace XmlOutputFunctions; using namespace XmlOutputFunctions;
writeSpaces (outputStream, indentationLevel); writeSpaces (outputStream, indentationLevel);
@@ -14265,7 +14259,7 @@ const String XmlElement::createDocument (const String& dtdToUse,
const bool allOnOneLine, const bool allOnOneLine,
const bool includeXmlHeader, const bool includeXmlHeader,
const String& encodingType, const String& encodingType,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
MemoryOutputStream mem (2048, 4096); MemoryOutputStream mem (2048, 4096);
writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength); writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength);
@@ -14278,7 +14272,7 @@ void XmlElement::writeToStream (OutputStream& output,
const bool allOnOneLine, const bool allOnOneLine,
const bool includeXmlHeader, const bool includeXmlHeader,
const String& encodingType, const String& encodingType,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
if (includeXmlHeader) if (includeXmlHeader)
{ {
@@ -14306,7 +14300,7 @@ void XmlElement::writeToStream (OutputStream& output,
bool XmlElement::writeToFile (const File& file, bool XmlElement::writeToFile (const File& file,
const String& dtdToUse, const String& dtdToUse,
const String& encodingType, const String& encodingType,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
if (file.hasWriteAccess()) if (file.hasWriteAccess())
{ {
@@ -14418,8 +14412,22 @@ bool XmlElement::hasAttribute (const String& attributeName) const throw()
return false; return false;
} }


const String XmlElement::getStringAttribute (const String& attributeName,
const String& defaultReturnValue) const throw()
const String& XmlElement::getStringAttribute (const String& attributeName) const throw()
{
const XmlAttributeNode* att = attributes;

while (att != 0)
{
if (att->name.equalsIgnoreCase (attributeName))
return att->value;

att = att->next;
}

return String::empty;
}

const String XmlElement::getStringAttribute (const String& attributeName, const String& defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;


@@ -14434,8 +14442,7 @@ const String XmlElement::getStringAttribute (const String& attributeName,
return defaultReturnValue; return defaultReturnValue;
} }


int XmlElement::getIntAttribute (const String& attributeName,
const int defaultReturnValue) const throw()
int XmlElement::getIntAttribute (const String& attributeName, const int defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;


@@ -14450,8 +14457,7 @@ int XmlElement::getIntAttribute (const String& attributeName,
return defaultReturnValue; return defaultReturnValue;
} }


double XmlElement::getDoubleAttribute (const String& attributeName,
const double defaultReturnValue) const throw()
double XmlElement::getDoubleAttribute (const String& attributeName, const double defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;


@@ -14466,8 +14472,7 @@ double XmlElement::getDoubleAttribute (const String& attributeName,
return defaultReturnValue; return defaultReturnValue;
} }


bool XmlElement::getBoolAttribute (const String& attributeName,
const bool defaultReturnValue) const throw()
bool XmlElement::getBoolAttribute (const String& attributeName, const bool defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;


@@ -14515,8 +14520,7 @@ bool XmlElement::compareAttribute (const String& attributeName,
return false; return false;
} }


void XmlElement::setAttribute (const String& attributeName,
const String& value) throw()
void XmlElement::setAttribute (const String& attributeName, const String& value)
{ {
#ifdef JUCE_DEBUG #ifdef JUCE_DEBUG
// check the identifier being passed in is legal.. // check the identifier being passed in is legal..
@@ -14557,14 +14561,12 @@ void XmlElement::setAttribute (const String& attributeName,
} }
} }


void XmlElement::setAttribute (const String& attributeName,
const int number) throw()
void XmlElement::setAttribute (const String& attributeName, const int number)
{ {
setAttribute (attributeName, String (number)); setAttribute (attributeName, String (number));
} }


void XmlElement::setAttribute (const String& attributeName,
const double number) throw()
void XmlElement::setAttribute (const String& attributeName, const double number)
{ {
setAttribute (attributeName, String (number)); setAttribute (attributeName, String (number));
} }
@@ -70202,18 +70204,9 @@ public:


if (! screenArea.contains (lastScreenPos)) if (! screenArea.contains (lastScreenPos))
{ {
const Point<int> compPos (current->getScreenPosition());
int deltaX = 0, deltaY = 0;

if (lastScreenPos.getX() <= screenArea.getX() || lastScreenPos.getX() >= screenArea.getRight())
deltaX = compPos.getX() + current->getWidth() / 2 - lastScreenPos.getX();

if (lastScreenPos.getY() <= screenArea.getY() || lastScreenPos.getY() >= screenArea.getBottom())
deltaY = compPos.getY() + current->getHeight() / 2 - lastScreenPos.getY();

const Point<int> delta (deltaX, deltaY);
unboundedMouseOffset -= delta;
Desktop::setMousePosition (lastScreenPos + delta);
const Point<int> componentCentre (current->getScreenBounds().getCentre());
unboundedMouseOffset += (lastScreenPos - componentCentre);
Desktop::setMousePosition (componentCentre);
} }
else if (isCursorVisibleUntilOffscreen else if (isCursorVisibleUntilOffscreen
&& (! unboundedMouseOffset.isOrigin()) && (! unboundedMouseOffset.isOrigin())


+ 14
- 12
juce_amalgamated.h View File

@@ -1365,10 +1365,10 @@ private:


// internal constructor that preallocates a certain amount of memory // internal constructor that preallocates a certain amount of memory
String (size_t numChars, int dummyVariable); String (size_t numChars, int dummyVariable);
String (const String& stringToCopy, size_t charsToAllocate);


void createInternal (const juce_wchar* text, size_t numChars); void createInternal (const juce_wchar* text, size_t numChars);
void appendInternal (const juce_wchar* text, int numExtraChars); void appendInternal (const juce_wchar* text, int numExtraChars);
void dupeInternalIfMultiplyReferenced();
}; };


const String JUCE_CALLTYPE operator+ (const char* string1, const String& string2); const String JUCE_CALLTYPE operator+ (const char* string1, const String& string2);
@@ -4456,19 +4456,19 @@ public:
const bool allOnOneLine = false, const bool allOnOneLine = false,
const bool includeXmlHeader = true, const bool includeXmlHeader = true,
const String& encodingType = JUCE_T("UTF-8"), const String& encodingType = JUCE_T("UTF-8"),
const int lineWrapLength = 60) const throw();
const int lineWrapLength = 60) const;


void writeToStream (OutputStream& output, void writeToStream (OutputStream& output,
const String& dtdToUse, const String& dtdToUse,
const bool allOnOneLine = false, const bool allOnOneLine = false,
const bool includeXmlHeader = true, const bool includeXmlHeader = true,
const String& encodingType = JUCE_T("UTF-8"), const String& encodingType = JUCE_T("UTF-8"),
const int lineWrapLength = 60) const throw();
const int lineWrapLength = 60) const;


bool writeToFile (const File& destinationFile, bool writeToFile (const File& destinationFile,
const String& dtdToUse, const String& dtdToUse,
const String& encodingType = JUCE_T("UTF-8"), const String& encodingType = JUCE_T("UTF-8"),
const int lineWrapLength = 60) const throw();
const int lineWrapLength = 60) const;


inline const String& getTagName() const throw() { return tagName; } inline const String& getTagName() const throw() { return tagName; }


@@ -4484,30 +4484,32 @@ public:


bool hasAttribute (const String& attributeName) const throw(); bool hasAttribute (const String& attributeName) const throw();


const String& getStringAttribute (const String& attributeName) const throw();

const String getStringAttribute (const String& attributeName, const String getStringAttribute (const String& attributeName,
const String& defaultReturnValue = String::empty) const throw();
const String& defaultReturnValue) const;


bool compareAttribute (const String& attributeName, bool compareAttribute (const String& attributeName,
const String& stringToCompareAgainst, const String& stringToCompareAgainst,
const bool ignoreCase = false) const throw(); const bool ignoreCase = false) const throw();


int getIntAttribute (const String& attributeName, int getIntAttribute (const String& attributeName,
const int defaultReturnValue = 0) const throw();
const int defaultReturnValue = 0) const;


double getDoubleAttribute (const String& attributeName, double getDoubleAttribute (const String& attributeName,
const double defaultReturnValue = 0.0) const throw();
const double defaultReturnValue = 0.0) const;


bool getBoolAttribute (const String& attributeName, bool getBoolAttribute (const String& attributeName,
const bool defaultReturnValue = false) const throw();
const bool defaultReturnValue = false) const;


void setAttribute (const String& attributeName, void setAttribute (const String& attributeName,
const String& newValue) throw();
const String& newValue);


void setAttribute (const String& attributeName, void setAttribute (const String& attributeName,
const int newValue) throw();
const int newValue);


void setAttribute (const String& attributeName, void setAttribute (const String& attributeName,
const double newValue) throw();
const double newValue);


void removeAttribute (const String& attributeName) throw(); void removeAttribute (const String& attributeName) throw();


@@ -4609,7 +4611,7 @@ private:


void writeElementAsText (OutputStream& out, void writeElementAsText (OutputStream& out,
const int indentationLevel, const int indentationLevel,
const int lineWrapLength) const throw();
const int lineWrapLength) const;


void getChildElementsAsArray (XmlElement**) const throw(); void getChildElementsAsArray (XmlElement**) const throw();
void reorderChildElements (XmlElement** const, const int) throw(); void reorderChildElements (XmlElement** const, const int) throw();


+ 3
- 12
src/gui/components/mouse/juce_MouseInputSource.cpp View File

@@ -382,18 +382,9 @@ public:
if (! screenArea.contains (lastScreenPos)) if (! screenArea.contains (lastScreenPos))
{ {
const Point<int> compPos (current->getScreenPosition());
int deltaX = 0, deltaY = 0;
if (lastScreenPos.getX() <= screenArea.getX() || lastScreenPos.getX() >= screenArea.getRight())
deltaX = compPos.getX() + current->getWidth() / 2 - lastScreenPos.getX();
if (lastScreenPos.getY() <= screenArea.getY() || lastScreenPos.getY() >= screenArea.getBottom())
deltaY = compPos.getY() + current->getHeight() / 2 - lastScreenPos.getY();
const Point<int> delta (deltaX, deltaY);
unboundedMouseOffset -= delta;
Desktop::setMousePosition (lastScreenPos + delta);
const Point<int> componentCentre (current->getScreenBounds().getCentre());
unboundedMouseOffset += (lastScreenPos - componentCentre);
Desktop::setMousePosition (componentCentre);
} }
else if (isCursorVisibleUntilOffscreen else if (isCursorVisibleUntilOffscreen
&& (! unboundedMouseOffset.isOrigin()) && (! unboundedMouseOffset.isOrigin())


+ 44
- 50
src/text/juce_String.cpp View File

@@ -93,7 +93,7 @@ public:
return text; return text;
juce_wchar* const newText = create (b->allocatedNumChars); juce_wchar* const newText = create (b->allocatedNumChars);
memcpy (newText, text, (b->allocatedNumChars + 1) * sizeof (juce_wchar));
copyChars (newText, text, b->allocatedNumChars);
release (b); release (b);
return newText; return newText;
@@ -107,7 +107,7 @@ public:
return text; return text;
juce_wchar* const newText = create (jmax (b->allocatedNumChars, numChars)); juce_wchar* const newText = create (jmax (b->allocatedNumChars, numChars));
memcpy (newText, text, (b->allocatedNumChars + 1) * sizeof (juce_wchar));
copyChars (newText, text, b->allocatedNumChars);
release (b); release (b);
return newText; return newText;
@@ -118,6 +118,12 @@ public:
return bufferFromText (text)->allocatedNumChars; return bufferFromText (text)->allocatedNumChars;
} }
static void copyChars (juce_wchar* const dest, const juce_wchar* const src, const size_t numChars) throw()
{
memcpy (dest, src, numChars * sizeof (juce_wchar));
dest [numChars] = 0;
}
//============================================================================== //==============================================================================
int refCount; int refCount;
size_t allocatedNumChars; size_t allocatedNumChars;
@@ -141,7 +147,7 @@ void String::createInternal (const juce_wchar* const t, const size_t numChars)
jassert (t[numChars] == 0); // must have a null terminator jassert (t[numChars] == 0); // must have a null terminator
text = StringHolder::create (numChars); text = StringHolder::create (numChars);
memcpy (text, t, (numChars + 1) * sizeof (juce_wchar));
StringHolder::copyChars (text, t, numChars);
} }
void String::appendInternal (const juce_wchar* const newText, const int numExtraChars) void String::appendInternal (const juce_wchar* const newText, const int numExtraChars)
@@ -152,16 +158,10 @@ void String::appendInternal (const juce_wchar* const newText, const int numExtra
const int newTotalLen = oldLen + numExtraChars; const int newTotalLen = oldLen + numExtraChars;
text = StringHolder::makeUniqueWithSize (text, newTotalLen); text = StringHolder::makeUniqueWithSize (text, newTotalLen);
memcpy (text + oldLen, newText, numExtraChars * sizeof (juce_wchar));
text [newTotalLen] = 0;
StringHolder::copyChars (text + oldLen, newText, numExtraChars);
} }
} }
void String::dupeInternalIfMultiplyReferenced()
{
text = StringHolder::makeUnique (text);
}
void String::preallocateStorage (const size_t numChars) void String::preallocateStorage (const size_t numChars)
{ {
text = StringHolder::makeUniqueWithSize (text, numChars); text = StringHolder::makeUniqueWithSize (text, numChars);
@@ -202,6 +202,13 @@ String::String (const size_t numChars, const int /*dummyVariable*/)
{ {
} }
String::String (const String& stringToCopy, const size_t charsToAllocate)
{
const size_t otherSize = StringHolder::getAllocatedNumChars (stringToCopy.text);
text = StringHolder::create (jmax (charsToAllocate, otherSize));
StringHolder::copyChars (text, stringToCopy.text, otherSize);
}
String::String (const char* const t) String::String (const char* const t)
{ {
if (t != 0 && *t != 0) if (t != 0 && *t != 0)
@@ -222,7 +229,7 @@ String::String (const juce_wchar* const t)
{ {
const int len = CharacterFunctions::length (t); const int len = CharacterFunctions::length (t);
text = StringHolder::create (len); text = StringHolder::create (len);
memcpy (text, t, (len + 1) * sizeof (juce_wchar));
StringHolder::copyChars (text, t, len);
} }
else else
{ {
@@ -259,8 +266,7 @@ String::String (const juce_wchar* const t, const size_t maxChars)
if (i > 0) if (i > 0)
{ {
text = StringHolder::create (i); text = StringHolder::create (i);
memcpy (text, t, i * sizeof (juce_wchar));
text[i] = 0;
StringHolder::copyChars (text, t, i);
} }
else else
{ {
@@ -1085,13 +1091,11 @@ const String String::repeatedString (const juce_wchar* const stringToRepeat, int
{ {
const int len = CharacterFunctions::length (stringToRepeat); const int len = CharacterFunctions::length (stringToRepeat);
String result ((size_t) (len * numberOfTimesToRepeat + 1), (int) 0); String result ((size_t) (len * numberOfTimesToRepeat + 1), (int) 0);
juce_wchar* n = result.text; juce_wchar* n = result.text;
n[0] = 0;
while (--numberOfTimesToRepeat >= 0) while (--numberOfTimesToRepeat >= 0)
{ {
CharacterFunctions::append (n, stringToRepeat);
StringHolder::copyChars (n, stringToRepeat, len);
n += len; n += len;
} }
@@ -1103,21 +1107,17 @@ const String String::paddedLeft (const juce_wchar padCharacter, int minimumLengt
jassert (padCharacter != 0); jassert (padCharacter != 0);
const int len = length(); const int len = length();
if (len >= minimumLength || padCharacter == 0) if (len >= minimumLength || padCharacter == 0)
return *this; return *this;
String result ((size_t) minimumLength + 1, (int) 0); String result ((size_t) minimumLength + 1, (int) 0);
juce_wchar* n = result.text; juce_wchar* n = result.text;
minimumLength -= len; minimumLength -= len;
while (--minimumLength >= 0) while (--minimumLength >= 0)
*n++ = padCharacter; *n++ = padCharacter;
*n = 0;
CharacterFunctions::append (n, text);
StringHolder::copyChars (n, text, len);
return result; return result;
} }
@@ -1125,11 +1125,19 @@ const String String::paddedRight (const juce_wchar padCharacter, int minimumLeng
{ {
jassert (padCharacter != 0); jassert (padCharacter != 0);
const int paddingNeeded = minimumLength - length();
if (paddingNeeded <= 0 || padCharacter == 0)
const int len = length();
if (len >= minimumLength || padCharacter == 0)
return *this; return *this;
return *this + String::empty.paddedLeft (padCharacter, paddingNeeded);
String result (*this, (size_t) minimumLength);
juce_wchar* n = result.text + len;
minimumLength -= len;
while (--minimumLength >= 0)
*n++ = padCharacter;
*n = 0;
return result;
} }
//============================================================================== //==============================================================================
@@ -1172,21 +1180,17 @@ const String String::replaceSection (int index, int numCharsToReplace,
String result ((size_t) newTotalLen, (int) 0); String result ((size_t) newTotalLen, (int) 0);
memcpy (result.text, text, index * sizeof (juce_wchar));
StringHolder::copyChars (result.text, text, index);
if (newStringLen > 0) if (newStringLen > 0)
memcpy (result.text + index,
stringToInsert,
newStringLen * sizeof (juce_wchar));
StringHolder::copyChars (result.text + index, stringToInsert, newStringLen);
const int endStringLen = newTotalLen - (index + newStringLen); const int endStringLen = newTotalLen - (index + newStringLen);
if (endStringLen > 0) if (endStringLen > 0)
memcpy (result.text + (index + newStringLen),
text + (index + numCharsToReplace),
endStringLen * sizeof (juce_wchar));
result.text [newTotalLen] = 0;
StringHolder::copyChars (result.text + (index + newStringLen),
text + (index + numCharsToReplace),
endStringLen);
return result; return result;
} }
@@ -1218,9 +1222,7 @@ const String String::replaceCharacter (const juce_wchar charToReplace, const juc
if (index < 0) if (index < 0)
return *this; return *this;
String result (*this);
result.dupeInternalIfMultiplyReferenced();
String result (*this, size_t());
juce_wchar* t = result.text + index; juce_wchar* t = result.text + index;
while (*t != 0) while (*t != 0)
@@ -1237,9 +1239,7 @@ const String String::replaceCharacter (const juce_wchar charToReplace, const juc
const String String::replaceCharacters (const String& charactersToReplace, const String String::replaceCharacters (const String& charactersToReplace,
const juce_wchar* const charactersToInsertInstead) const const juce_wchar* const charactersToInsertInstead) const
{ {
String result (*this);
result.dupeInternalIfMultiplyReferenced();
String result (*this, size_t());
juce_wchar* t = result.text; juce_wchar* t = result.text;
const int len2 = CharacterFunctions::length (charactersToInsertInstead); const int len2 = CharacterFunctions::length (charactersToInsertInstead);
@@ -1314,16 +1314,14 @@ bool String::endsWithIgnoreCase (const juce_wchar* const other) const throw()
//============================================================================== //==============================================================================
const String String::toUpperCase() const const String String::toUpperCase() const
{ {
String result (*this);
result.dupeInternalIfMultiplyReferenced();
String result (*this, size_t());
CharacterFunctions::toUpperCase (result.text); CharacterFunctions::toUpperCase (result.text);
return result; return result;
} }
const String String::toLowerCase() const const String String::toLowerCase() const
{ {
String result (*this);
result.dupeInternalIfMultiplyReferenced();
String result (*this, size_t());
CharacterFunctions::toLowerCase (result.text); CharacterFunctions::toLowerCase (result.text);
return result; return result;
} }
@@ -1332,9 +1330,7 @@ const String String::toLowerCase() const
juce_wchar& String::operator[] (const int index) juce_wchar& String::operator[] (const int index)
{ {
jassert (((unsigned int) index) <= (unsigned int) length()); jassert (((unsigned int) index) <= (unsigned int) length());
dupeInternalIfMultiplyReferenced();
text = StringHolder::makeUnique (text);
return text [index]; return text [index];
} }
@@ -1453,7 +1449,7 @@ const String String::unquoted() const
{ {
String s (*this); String s (*this);
if (s[0] == T('"') || s[0] == T('\''))
if (s.text[0] == T('"') || s.text[0] == T('\''))
s = s.substring (1); s = s.substring (1);
const int lastCharIndex = s.length() - 1; const int lastCharIndex = s.length() - 1;
@@ -2124,9 +2120,7 @@ int String::copyToCString (char* destBuffer, const int maxBufferSizeBytes) const
//============================================================================== //==============================================================================
void String::copyToUnicode (juce_wchar* const destBuffer, const int maxCharsToCopy) const throw() void String::copyToUnicode (juce_wchar* const destBuffer, const int maxCharsToCopy) const throw()
{ {
const int len = jmin (maxCharsToCopy, length());
memcpy (destBuffer, text, len * sizeof (juce_wchar));
destBuffer [len] = 0;
StringHolder::copyChars (destBuffer, text, jmin (maxCharsToCopy, length()));
} }


+ 1
- 1
src/text/juce_String.h View File

@@ -1026,10 +1026,10 @@ private:
//============================================================================== //==============================================================================
// internal constructor that preallocates a certain amount of memory // internal constructor that preallocates a certain amount of memory
String (size_t numChars, int dummyVariable); String (size_t numChars, int dummyVariable);
String (const String& stringToCopy, size_t charsToAllocate);
void createInternal (const juce_wchar* text, size_t numChars); void createInternal (const juce_wchar* text, size_t numChars);
void appendInternal (const juce_wchar* text, int numExtraChars); void appendInternal (const juce_wchar* text, int numExtraChars);
void dupeInternalIfMultiplyReferenced();
}; };
//============================================================================== //==============================================================================


+ 27
- 19
src/text/juce_XmlElement.cpp View File

@@ -246,7 +246,7 @@ namespace XmlOutputFunctions
} }
} }
static void writeSpaces (OutputStream& out, int numSpaces) throw()
static void writeSpaces (OutputStream& out, int numSpaces)
{ {
if (numSpaces > 0) if (numSpaces > 0)
{ {
@@ -266,7 +266,7 @@ namespace XmlOutputFunctions
void XmlElement::writeElementAsText (OutputStream& outputStream, void XmlElement::writeElementAsText (OutputStream& outputStream,
const int indentationLevel, const int indentationLevel,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
using namespace XmlOutputFunctions; using namespace XmlOutputFunctions;
writeSpaces (outputStream, indentationLevel); writeSpaces (outputStream, indentationLevel);
@@ -386,7 +386,7 @@ const String XmlElement::createDocument (const String& dtdToUse,
const bool allOnOneLine, const bool allOnOneLine,
const bool includeXmlHeader, const bool includeXmlHeader,
const String& encodingType, const String& encodingType,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
MemoryOutputStream mem (2048, 4096); MemoryOutputStream mem (2048, 4096);
writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength); writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength);
@@ -399,7 +399,7 @@ void XmlElement::writeToStream (OutputStream& output,
const bool allOnOneLine, const bool allOnOneLine,
const bool includeXmlHeader, const bool includeXmlHeader,
const String& encodingType, const String& encodingType,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
if (includeXmlHeader) if (includeXmlHeader)
{ {
@@ -427,7 +427,7 @@ void XmlElement::writeToStream (OutputStream& output,
bool XmlElement::writeToFile (const File& file, bool XmlElement::writeToFile (const File& file,
const String& dtdToUse, const String& dtdToUse,
const String& encodingType, const String& encodingType,
const int lineWrapLength) const throw()
const int lineWrapLength) const
{ {
if (file.hasWriteAccess()) if (file.hasWriteAccess())
{ {
@@ -542,8 +542,22 @@ bool XmlElement::hasAttribute (const String& attributeName) const throw()
} }
//============================================================================== //==============================================================================
const String XmlElement::getStringAttribute (const String& attributeName,
const String& defaultReturnValue) const throw()
const String& XmlElement::getStringAttribute (const String& attributeName) const throw()
{
const XmlAttributeNode* att = attributes;
while (att != 0)
{
if (att->name.equalsIgnoreCase (attributeName))
return att->value;
att = att->next;
}
return String::empty;
}
const String XmlElement::getStringAttribute (const String& attributeName, const String& defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;
@@ -558,8 +572,7 @@ const String XmlElement::getStringAttribute (const String& attributeName,
return defaultReturnValue; return defaultReturnValue;
} }
int XmlElement::getIntAttribute (const String& attributeName,
const int defaultReturnValue) const throw()
int XmlElement::getIntAttribute (const String& attributeName, const int defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;
@@ -574,8 +587,7 @@ int XmlElement::getIntAttribute (const String& attributeName,
return defaultReturnValue; return defaultReturnValue;
} }
double XmlElement::getDoubleAttribute (const String& attributeName,
const double defaultReturnValue) const throw()
double XmlElement::getDoubleAttribute (const String& attributeName, const double defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;
@@ -590,8 +602,7 @@ double XmlElement::getDoubleAttribute (const String& attributeName,
return defaultReturnValue; return defaultReturnValue;
} }
bool XmlElement::getBoolAttribute (const String& attributeName,
const bool defaultReturnValue) const throw()
bool XmlElement::getBoolAttribute (const String& attributeName, const bool defaultReturnValue) const
{ {
const XmlAttributeNode* att = attributes; const XmlAttributeNode* att = attributes;
@@ -640,8 +651,7 @@ bool XmlElement::compareAttribute (const String& attributeName,
} }
//============================================================================== //==============================================================================
void XmlElement::setAttribute (const String& attributeName,
const String& value) throw()
void XmlElement::setAttribute (const String& attributeName, const String& value)
{ {
#ifdef JUCE_DEBUG #ifdef JUCE_DEBUG
// check the identifier being passed in is legal.. // check the identifier being passed in is legal..
@@ -682,14 +692,12 @@ void XmlElement::setAttribute (const String& attributeName,
} }
} }
void XmlElement::setAttribute (const String& attributeName,
const int number) throw()
void XmlElement::setAttribute (const String& attributeName, const int number)
{ {
setAttribute (attributeName, String (number)); setAttribute (attributeName, String (number));
} }
void XmlElement::setAttribute (const String& attributeName,
const double number) throw()
void XmlElement::setAttribute (const String& attributeName, const double number)
{ {
setAttribute (attributeName, String (number)); setAttribute (attributeName, String (number));
} }


+ 19
- 11
src/text/juce_XmlElement.h View File

@@ -193,7 +193,7 @@ public:
const bool allOnOneLine = false, const bool allOnOneLine = false,
const bool includeXmlHeader = true, const bool includeXmlHeader = true,
const String& encodingType = JUCE_T("UTF-8"), const String& encodingType = JUCE_T("UTF-8"),
const int lineWrapLength = 60) const throw();
const int lineWrapLength = 60) const;
/** Writes the document to a stream as UTF-8. /** Writes the document to a stream as UTF-8.
@@ -215,7 +215,7 @@ public:
const bool allOnOneLine = false, const bool allOnOneLine = false,
const bool includeXmlHeader = true, const bool includeXmlHeader = true,
const String& encodingType = JUCE_T("UTF-8"), const String& encodingType = JUCE_T("UTF-8"),
const int lineWrapLength = 60) const throw();
const int lineWrapLength = 60) const;
/** Writes the element to a file as an XML document. /** Writes the element to a file as an XML document.
@@ -239,7 +239,7 @@ public:
bool writeToFile (const File& destinationFile, bool writeToFile (const File& destinationFile,
const String& dtdToUse, const String& dtdToUse,
const String& encodingType = JUCE_T("UTF-8"), const String& encodingType = JUCE_T("UTF-8"),
const int lineWrapLength = 60) const throw();
const int lineWrapLength = 60) const;
//============================================================================== //==============================================================================
/** Returns this element's tag type name. /** Returns this element's tag type name.
@@ -291,6 +291,14 @@ public:
/** Checks whether the element contains an attribute with a certain name. */ /** Checks whether the element contains an attribute with a certain name. */
bool hasAttribute (const String& attributeName) const throw(); bool hasAttribute (const String& attributeName) const throw();
/** Returns the value of a named attribute.
@param attributeName the name of the attribute to look up
@param defaultReturnValue a value to return if the element doesn't have an attribute
with this name
*/
const String& getStringAttribute (const String& attributeName) const throw();
/** Returns the value of a named attribute. /** Returns the value of a named attribute.
@param attributeName the name of the attribute to look up @param attributeName the name of the attribute to look up
@@ -298,7 +306,7 @@ public:
with this name with this name
*/ */
const String getStringAttribute (const String& attributeName, const String getStringAttribute (const String& attributeName,
const String& defaultReturnValue = String::empty) const throw();
const String& defaultReturnValue) const;
/** Compares the value of a named attribute with a value passed-in. /** Compares the value of a named attribute with a value passed-in.
@@ -323,7 +331,7 @@ public:
@see setAttribute @see setAttribute
*/ */
int getIntAttribute (const String& attributeName, int getIntAttribute (const String& attributeName,
const int defaultReturnValue = 0) const throw();
const int defaultReturnValue = 0) const;
/** Returns the value of a named attribute as floating-point. /** Returns the value of a named attribute as floating-point.
@@ -336,7 +344,7 @@ public:
@see setAttribute @see setAttribute
*/ */
double getDoubleAttribute (const String& attributeName, double getDoubleAttribute (const String& attributeName,
const double defaultReturnValue = 0.0) const throw();
const double defaultReturnValue = 0.0) const;
/** Returns the value of a named attribute as a boolean. /** Returns the value of a named attribute as a boolean.
@@ -349,7 +357,7 @@ public:
with this name with this name
*/ */
bool getBoolAttribute (const String& attributeName, bool getBoolAttribute (const String& attributeName,
const bool defaultReturnValue = false) const throw();
const bool defaultReturnValue = false) const;
/** Adds a named attribute to the element. /** Adds a named attribute to the element.
@@ -365,7 +373,7 @@ public:
@see removeAttribute @see removeAttribute
*/ */
void setAttribute (const String& attributeName, void setAttribute (const String& attributeName,
const String& newValue) throw();
const String& newValue);
/** Adds a named attribute to the element, setting it to an integer value. /** Adds a named attribute to the element, setting it to an integer value.
@@ -380,7 +388,7 @@ public:
@param newValue the value to set it to @param newValue the value to set it to
*/ */
void setAttribute (const String& attributeName, void setAttribute (const String& attributeName,
const int newValue) throw();
const int newValue);
/** Adds a named attribute to the element, setting it to a floating-point value. /** Adds a named attribute to the element, setting it to a floating-point value.
@@ -395,7 +403,7 @@ public:
@param newValue the value to set it to @param newValue the value to set it to
*/ */
void setAttribute (const String& attributeName, void setAttribute (const String& attributeName,
const double newValue) throw();
const double newValue);
/** Removes a named attribute from the element. /** Removes a named attribute from the element.
@@ -704,7 +712,7 @@ private:
void writeElementAsText (OutputStream& out, void writeElementAsText (OutputStream& out,
const int indentationLevel, const int indentationLevel,
const int lineWrapLength) const throw();
const int lineWrapLength) const;
void getChildElementsAsArray (XmlElement**) const throw(); void getChildElementsAsArray (XmlElement**) const throw();
void reorderChildElements (XmlElement** const, const int) throw(); void reorderChildElements (XmlElement** const, const int) throw();


Loading…
Cancel
Save