Bug 1487606 - Make AppendLiteral() not undo the effect of SetCapacity(). r=froydnj
authorHenri Sivonen <hsivonen@hsivonen.fi>
Fri, 31 Aug 2018 14:57:26 +0000
changeset 434401 bf6f5ec86920a3f58d16ca6ea2c406b09197e976
parent 434400 54a320057b31208111ac08818b69c694ff26cb46
child 434402 13777f26498ef48a9418838cde0ebc9128705708
push id34554
push usernbeleuzu@mozilla.com
push dateSat, 01 Sep 2018 21:55:14 +0000
treeherdermozilla-central@d718fc7c16e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1487606
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1487606 - Make AppendLiteral() not undo the effect of SetCapacity(). r=froydnj MozReview-Commit-ID: I2QSXbQhOUH Differential Revision: https://phabricator.services.mozilla.com/D4752
xpcom/string/nsTSubstring.cpp
xpcom/tests/gtest/TestStrings.cpp
--- a/xpcom/string/nsTSubstring.cpp
+++ b/xpcom/string/nsTSubstring.cpp
@@ -760,17 +760,20 @@ nsTSubstring<T>::Replace(index_type aCut
 
 template <typename T>
 void
 nsTSubstring<T>::ReplaceLiteral(index_type aCutStart, size_type aCutLength,
                                 const char_type* aData, size_type aLength)
 {
   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);
   }
 }
 
 template <typename T>
 void
--- a/xpcom/tests/gtest/TestStrings.cpp
+++ b/xpcom/tests/gtest/TestStrings.cpp
@@ -1284,43 +1284,57 @@ TEST(String, strip_chars)
                           u" ",
                           NS_LITERAL_STRING("foo"));
 }
 
 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++) {
     s.Append(u'a');
     EXPECT_EQ(s.BeginReading(), ptr);
     EXPECT_EQ(s.Length(), uint32_t(i + 1));
   }
 }
 
 TEST_F(Strings, append_string_with_capacity)
 {
   nsAutoString aa;
   aa.Append(u'a');
   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++) {
     s.Append(aa);
     EXPECT_EQ(s.BeginReading(), ptr);
     EXPECT_EQ(s.Length(), uint32_t(2 * (i + 1)));
   }
 }
 
+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";
   nsCString s;
   s.SetCapacity(2048);
   memcpy(s.BeginWriting(), foobar, strlen(foobar));
   s.SetLength(strlen(foobar));
   EXPECT_TRUE(s.EqualsASCII(foobar));