Bug 1324430 - Implement nsTSubstring_CharT::Assign with length for a better string buffer sharing, r=froydnj

This commit is contained in:
Andrea Marchesini 2016-12-23 17:22:21 +01:00
parent 69164b6a1e
commit 2dba3e889e
3 changed files with 39 additions and 6 deletions

View File

@ -65,7 +65,7 @@ public:
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(aLength <= mData.Length());
return aString.Assign(mData.BeginReading(), aLength, mozilla::fallible);
return aString.Assign(mData, aLength, mozilla::fallible);
}
void

View File

@ -417,13 +417,28 @@ nsTSubstring_CharT::AssignLiteral(const char_type* aData, size_type aLength)
void
nsTSubstring_CharT::Assign(const self_type& aStr)
{
if (!Assign(aStr, mozilla::fallible)) {
if (!Assign(aStr, aStr.Length(), mozilla::fallible)) {
AllocFailed(aStr.Length());
}
}
bool
nsTSubstring_CharT::Assign(const self_type& aStr, const fallible_t& aFallible)
{
return Assign(aStr, aStr.Length(), aFallible);
}
void
nsTSubstring_CharT::Assign(const self_type& aStr, size_type aLength)
{
if (!Assign(aStr, aLength, mozilla::fallible)) {
AllocFailed(aLength);
}
}
bool
nsTSubstring_CharT::Assign(const self_type& aStr, size_type aLength,
const fallible_t& aFallible)
{
// |aStr| could be sharable. We need to check its flags to know how to
// deal with it.
@ -432,7 +447,11 @@ nsTSubstring_CharT::Assign(const self_type& aStr, const fallible_t& aFallible)
return true;
}
if (!aStr.mLength) {
if (aStr.Length() < aLength) {
aLength = aStr.Length();
}
if (!aLength) {
Truncate();
mFlags |= aStr.mFlags & F_VOIDED;
return true;
@ -447,7 +466,7 @@ nsTSubstring_CharT::Assign(const self_type& aStr, const fallible_t& aFallible)
::ReleaseData(mData, mFlags);
mData = aStr.mData;
mLength = aStr.mLength;
mLength = aLength;
SetDataFlags(F_TERMINATED | F_SHARED);
// get an owning reference to the mData
@ -456,12 +475,12 @@ nsTSubstring_CharT::Assign(const self_type& aStr, const fallible_t& aFallible)
} else if (aStr.mFlags & F_LITERAL) {
MOZ_ASSERT(aStr.mFlags & F_TERMINATED, "Unterminated literal");
AssignLiteral(aStr.mData, aStr.mLength);
AssignLiteral(aStr.mData, aLength);
return true;
}
// else, treat this like an ordinary assignment.
return Assign(aStr.Data(), aStr.Length(), aFallible);
return Assign(aStr.Data(), aLength, aFallible);
}
void

View File

@ -378,6 +378,20 @@ public:
MOZ_MUST_USE bool NS_FASTCALL Assign(const substring_tuple_type&,
const fallible_t&);
// Assign at most aLength characters of aStr to this, fallibly.
// If aLength is more than the number of characters in aStr.Length(),
// only assign aStr.Length() characters.
//
// Using this method is functionally equivalent to:
//
// str.Assign(other.BeginReading, other.Length(), fallible);
//
// but may enable string buffer sharing.
void NS_FASTCALL Assign(const self_type& aStr, size_type aLength);
MOZ_MUST_USE bool NS_FASTCALL Assign(const self_type& aStr,
size_type aLength,
const fallible_t&);
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
void Assign(char16ptr_t aData)
{