Bug 1487606 - Make AppendLiteral() not undo the effect of SetCapacity(). r=froydnj

MozReview-Commit-ID: I2QSXbQhOUH

Differential Revision: https://phabricator.services.mozilla.com/D4752

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henri Sivonen 2018-08-31 14:57:26 +00:00
parent 4498bbb089
commit c62a32ebf1
2 changed files with 20 additions and 3 deletions

View File

@ -765,7 +765,10 @@ nsTSubstring<T>::ReplaceLiteral(index_type aCutStart, size_type aCutLength,
{
aCutStart = XPCOM_MIN(aCutStart, this->Length());
if (!aCutStart && aCutLength == this->Length()) {
if (!aCutStart && aCutLength == this->Length() &&
!(this->mDataFlags & DataFlags::REFCOUNTED)) {
// Check for REFCOUNTED above to avoid undoing the effect of
// SetCapacity().
AssignLiteral(aData, aLength);
} else if (ReplacePrep(aCutStart, aCutLength, aLength) && aLength > 0) {
char_traits::copy(this->mData + aCutStart, aData, aLength);

View File

@ -1289,7 +1289,7 @@ TEST_F(Strings, append_with_capacity)
{
nsAutoString s;
const char16_t* origPtr = s.BeginReading();
s.SetCapacity(100);
s.SetCapacity(8000);
const char16_t* ptr = s.BeginReading();
EXPECT_NE(origPtr, ptr);
for (int i = 0; i < 100; i++) {
@ -1306,7 +1306,7 @@ TEST_F(Strings, append_string_with_capacity)
aa.Append(u'a');
nsAutoString s;
const char16_t* origPtr = s.BeginReading();
s.SetCapacity(200);
s.SetCapacity(8000);
const char16_t* ptr = s.BeginReading();
EXPECT_NE(origPtr, ptr);
for (int i = 0; i < 100; i++) {
@ -1316,6 +1316,20 @@ TEST_F(Strings, append_string_with_capacity)
}
}
TEST_F(Strings, append_literal_with_capacity)
{
nsAutoString s;
const char16_t* origPtr = s.BeginReading();
s.SetCapacity(8000);
const char16_t* ptr = s.BeginReading();
EXPECT_NE(origPtr, ptr);
for (int i = 0; i < 100; i++) {
s.AppendLiteral(u"aa");
EXPECT_EQ(s.BeginReading(), ptr);
EXPECT_EQ(s.Length(), uint32_t(2 * (i + 1)));
}
}
TEST_F(Strings, legacy_set_length_semantics)
{
const char* foobar = "foobar";