Bug 1358297: Part 3. Create special case for stripping CR and LF on TSubstring, and use it in all the places that it is called. Use ASCIIMask to make it fast. r=froydnj
☠☠ backed out by b820c2281605 ☠ ☠
authorMilan Sreckovic <milan@mozilla.com>
Fri, 05 May 2017 13:37:13 -0400
changeset 356829 3c1b426a5cce74b34841fc608842518c8746f8f3
parent 356828 9201d345a1d5cef6b5ffb91ed27983403cc43865
child 356830 95211a49619145612d3e55e95b9f596bd649f0cb
push id31775
push userihsiao@mozilla.com
push dateMon, 08 May 2017 03:10:38 +0000
treeherdermozilla-central@22aaf8bad4df [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1358297
milestone55.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 1358297: Part 3. Create special case for stripping CR and LF on TSubstring, and use it in all the places that it is called. Use ASCIIMask to make it fast. r=froydnj MozReview-Commit-ID: 3p97cCGfy6i
docshell/base/nsDefaultURIFixup.cpp
docshell/base/nsDocShell.cpp
dom/base/nsContentUtils.cpp
dom/base/nsFrameMessageManager.cpp
dom/html/HTMLInputElement.cpp
editor/libeditor/TextEditRules.cpp
netwerk/base/nsURLHelper.cpp
netwerk/mime/nsMIMEHeaderParamImpl.cpp
xpcom/string/nsTSubstring.cpp
xpcom/string/nsTSubstring.h
xpcom/tests/gtest/TestStrings.cpp
xpfe/appshell/nsXULWindow.cpp
--- a/docshell/base/nsDefaultURIFixup.cpp
+++ b/docshell/base/nsDefaultURIFixup.cpp
@@ -162,17 +162,17 @@ nsDefaultURIFixup::GetFixupURIInfo(const
 {
   NS_ENSURE_ARG(!aStringURI.IsEmpty());
 
   nsresult rv;
 
   nsAutoCString uriString(aStringURI);
 
   // Eliminate embedded newlines, which single-line text fields now allow:
-  uriString.StripChars("\r\n");
+  uriString.StripCRLF();
   // Cleanup the empty spaces that might be on each end:
   uriString.Trim(" ");
 
   NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE);
 
   RefPtr<nsDefaultURIFixupInfo> info = new nsDefaultURIFixupInfo(uriString);
   NS_ADDREF(*aInfo = info);
 
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -4775,17 +4775,17 @@ nsDocShell::LoadURIWithOptions(const cha
   // Create a URI from our string; if that succeeds, we want to
   // change aLoadFlags to not include the ALLOW_THIRD_PARTY_FIXUP
   // flag.
 
   NS_ConvertUTF16toUTF8 uriString(aURI);
   // Cleanup the empty spaces that might be on each end.
   uriString.Trim(" ");
   // Eliminate embedded newlines, which single-line text fields now allow:
-  uriString.StripChars("\r\n");
+  uriString.StripCRLF();
   NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE);
 
   rv = NS_NewURI(getter_AddRefs(uri), uriString);
   if (uri) {
     aLoadFlags &= ~LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
   }
 
   nsCOMPtr<nsIURIFixupInfo> fixupInfo;
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -6679,19 +6679,17 @@ nsContentUtils::FlushLayoutForTree(nsPID
         FlushLayoutForTree(win);
       }
     }
   }
 }
 
 void nsContentUtils::RemoveNewlines(nsString &aString)
 {
-  // strip CR/LF and null
-  static const char badChars[] = {'\r', '\n', 0};
-  aString.StripChars(badChars);
+  aString.StripCRLF();
 }
 
 void
 nsContentUtils::PlatformToDOMLineBreaks(nsString &aString)
 {
   if (!PlatformToDOMLineBreaks(aString, fallible)) {
     aString.AllocFailed(aString.Length());
   }
--- a/dom/base/nsFrameMessageManager.cpp
+++ b/dom/base/nsFrameMessageManager.cpp
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "base/basictypes.h"
 
 #include "nsFrameMessageManager.h"
 
 #include "ContentChild.h"
+#include "nsASCIIMask.h"
 #include "nsContentUtils.h"
 #include "nsDOMClassInfoID.h"
 #include "nsError.h"
 #include "nsIXPConnect.h"
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "nsJSUtils.h"
 #include "nsJSPrincipals.h"
@@ -586,17 +587,17 @@ AllowMessage(size_t aDataLength, const n
 {
   static const size_t kMinTelemetryMessageSize = 8192;
 
   if (aDataLength < kMinTelemetryMessageSize) {
     return true;
   }
 
   NS_ConvertUTF16toUTF8 messageName(aMessageName);
-  messageName.StripChars("0123456789");
+  messageName.StripTaggedASCII(ASCIIMask::Mask0to9());
 
   Telemetry::Accumulate(Telemetry::MESSAGE_MANAGER_MESSAGE_SIZE2, messageName,
                         aDataLength);
 
   // A message includes more than structured clone data, so subtract
   // 20KB to make it more likely that a message within this bound won't
   // result in an overly large IPC message.
   static const size_t kMaxMessageSize = IPC::Channel::kMaximumMessageSize - 20 * 1024;
@@ -672,17 +673,17 @@ nsFrameMessageManager::SendMessage(const
   }
 
   uint32_t latencyMs = round((TimeStamp::Now() - start).ToMilliseconds());
   if (latencyMs >= kMinTelemetrySyncMessageManagerLatencyMs) {
     NS_ConvertUTF16toUTF8 messageName(aMessageName);
     // NOTE: We need to strip digit characters from the message name in order to
     // avoid a large number of buckets due to generated names from addons (such
     // as "ublock:sb:{N}"). See bug 1348113 comment 10.
-    messageName.StripChars("0123456789");
+    messageName.StripTaggedASCII(ASCIIMask::Mask0to9());
     Telemetry::Accumulate(Telemetry::IPC_SYNC_MESSAGE_MANAGER_LATENCY_MS,
                           messageName, latencyMs);
   }
 
   if (!ok) {
     return NS_OK;
   }
 
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -5340,25 +5340,23 @@ HTMLInputElement::SanitizeValue(nsAStrin
   NS_ASSERTION(mDoneCreating, "The element creation should be finished!");
 
   switch (mType) {
     case NS_FORM_INPUT_TEXT:
     case NS_FORM_INPUT_SEARCH:
     case NS_FORM_INPUT_TEL:
     case NS_FORM_INPUT_PASSWORD:
       {
-        char16_t crlf[] = { char16_t('\r'), char16_t('\n'), 0 };
-        aValue.StripChars(crlf);
+        aValue.StripCRLF();
       }
       break;
     case NS_FORM_INPUT_EMAIL:
     case NS_FORM_INPUT_URL:
       {
-        char16_t crlf[] = { char16_t('\r'), char16_t('\n'), 0 };
-        aValue.StripChars(crlf);
+        aValue.StripCRLF();
 
         aValue = nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(aValue);
       }
       break;
     case NS_FORM_INPUT_NUMBER:
       {
         Decimal value;
         bool ok = ConvertStringToNumber(aValue, value);
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -528,17 +528,17 @@ TextEditRules::HandleNewLines(nsString& 
 
   switch(aNewlineHandling) {
     case nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
       // Strip trailing newlines first so we don't wind up with trailing spaces
       aString.Trim(CRLF, false, true);
       aString.ReplaceChar(CRLF, ' ');
       break;
     case nsIPlaintextEditor::eNewlinesStrip:
-      aString.StripChars(CRLF);
+      aString.StripCRLF();
       break;
     case nsIPlaintextEditor::eNewlinesPasteToFirst:
     default: {
       int32_t firstCRLF = aString.FindCharInSet(CRLF);
 
       // we get first *non-empty* line.
       int32_t offset = 0;
       while (firstCRLF == offset) {
--- a/netwerk/base/nsURLHelper.cpp
+++ b/netwerk/base/nsURLHelper.cpp
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/RangedPtr.h"
 
 #include <algorithm>
 #include <iterator>
 
+#include "nsASCIIMask.h"
 #include "nsURLHelper.h"
 #include "nsIFile.h"
 #include "nsIURLParser.h"
 #include "nsCOMPtr.h"
 #include "nsCRT.h"
 #include "nsNetCID.h"
 #include "mozilla/Preferences.h"
 #include "prnetdb.h"
@@ -517,17 +518,17 @@ net_ExtractURLScheme(const nsACString &i
         // Skip valid scheme characters or \r\n\t
     }
 
     if (!p.CheckChar(':')) {
         return NS_ERROR_MALFORMED_URI;
     }
 
     p.Claim(scheme);
-    scheme.StripChars("\r\n\t");
+    scheme.StripTaggedASCII(ASCIIMask::MaskCRLFTab());
     return NS_OK;
 }
 
 bool
 net_IsValidScheme(const char *scheme, uint32_t schemeLen)
 {
     // first char must be alpha
     if (!nsCRT::IsAsciiAlpha(*scheme))
@@ -586,46 +587,49 @@ net_IsAbsoluteURL(const nsACString& uri)
         return true;
     }
     return false;
 }
 
 void
 net_FilterURIString(const nsACString& input, nsACString& result)
 {
-    const char kCharsToStrip[] = "\r\n\t";
-
     result.Truncate();
 
     auto start = input.BeginReading();
     auto end = input.EndReading();
 
     // Trim off leading and trailing invalid chars.
     auto charFilter = [](char c) { return static_cast<uint8_t>(c) > 0x20; };
     auto newStart = std::find_if(start, end, charFilter);
     auto newEnd = std::find_if(
         std::reverse_iterator<decltype(end)>(end),
         std::reverse_iterator<decltype(newStart)>(newStart),
         charFilter).base();
 
     // Check if chars need to be stripped.
-    auto itr = std::find_first_of(
-        newStart, newEnd, std::begin(kCharsToStrip), std::end(kCharsToStrip));
-    const bool needsStrip = itr != newEnd;
+    bool needsStrip = false;
+    const ASCIIMaskArray& mask = ASCIIMask::MaskCRLFTab();
+    for (auto itr = start; itr != end; ++itr) {
+        if (ASCIIMask::IsMasked(mask, *itr)) {
+            needsStrip = true;
+            break;
+        }
+    }
 
     // Just use the passed in string rather than creating new copies if no
     // changes are necessary.
     if (newStart == start && newEnd == end && !needsStrip) {
         result = input;
         return;
     }
 
     result.Assign(Substring(newStart, newEnd));
     if (needsStrip) {
-        result.StripChars(kCharsToStrip);
+        result.StripTaggedASCII(mask);
     }
 }
 
 
 #if defined(XP_WIN)
 bool
 net_NormalizeFileURL(const nsACString &aURL, nsCString &aResultBuf)
 {
--- a/netwerk/mime/nsMIMEHeaderParamImpl.cpp
+++ b/netwerk/mime/nsMIMEHeaderParamImpl.cpp
@@ -513,17 +513,17 @@ nsMIMEHeaderParamImpl::DoParameterIntern
       if (caseAResult) {
         // we already have one caseA result, ignore subsequent ones
         goto increment_str;
       }
 
       // if the parameter spans across multiple lines we have to strip out the
       //     line continuation -- jht 4/29/98 
       nsAutoCString tempStr(valueStart, valueEnd - valueStart);
-      tempStr.StripChars("\r\n");
+      tempStr.StripCRLF();
       char *res = ToNewCString(tempStr);
       NS_ENSURE_TRUE(res, NS_ERROR_OUT_OF_MEMORY);
       
       if (isQuotedString)
         RemoveQuotedStringEscapes(res);
 
       caseAResult = res;
       // keep going, we may find a RFC 2231/5987 encoded alternative
@@ -756,17 +756,17 @@ internalDecodeRFC2047Header(const char* 
     aEatContinuations = false;
     aResult = aHeaderVal;
   }
 
   if (aEatContinuations) {
     nsAutoCString temp(aResult);
     temp.ReplaceSubstring("\n\t", " ");
     temp.ReplaceSubstring("\r\t", " ");
-    temp.StripChars("\r\n");
+    temp.StripCRLF();
     aResult = temp;
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMIMEHeaderParamImpl::DecodeRFC2047Header(const char* aHeaderVal, 
--- a/xpcom/string/nsTSubstring.cpp
+++ b/xpcom/string/nsTSubstring.cpp
@@ -1,14 +1,15 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "nsASCIIMask.h"
 #include "mozilla/CheckedInt.h"
 #include "mozilla/double-conversion.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Printf.h"
 
 using double_conversion::DoubleToStringConverter;
 
 const nsTSubstring_CharT::size_type nsTSubstring_CharT::kMaxCapacity =
@@ -876,17 +877,18 @@ nsTStringRepr_CharT::FindChar(char_type 
 }
 
 } // namespace detail
 } // namespace mozilla
 
 void
 nsTSubstring_CharT::StripChar(char_type aChar, int32_t aOffset)
 {
-  if (mLength == 0 || aOffset >= int32_t(mLength)) {
+  // Note that this implicitly guarantees mLength > 0
+  if (aOffset >= int32_t(mLength)) {
     return;
   }
 
   if (!EnsureMutable()) { // XXX do this lazily?
     AllocFailed(mLength);
   }
 
   // XXX(darin): this code should defer writing until necessary.
@@ -903,16 +905,17 @@ nsTSubstring_CharT::StripChar(char_type 
   }
   *to = char_type(0); // add the null
   mLength = to - mData;
 }
 
 void
 nsTSubstring_CharT::StripChars(const char_type* aChars, uint32_t aOffset)
 {
+  // Note that this implicitly guarantees mLength > 0
   if (aOffset >= uint32_t(mLength)) {
     return;
   }
 
   if (!EnsureMutable()) { // XXX do this lazily?
     AllocFailed(mLength);
   }
 
@@ -932,16 +935,55 @@ nsTSubstring_CharT::StripChars(const cha
       // Not stripped, copy this char.
       *to++ = theChar;
     }
   }
   *to = char_type(0); // add the null
   mLength = to - mData;
 }
 
+void
+nsTSubstring_CharT::StripTaggedASCII(const ASCIIMaskArray& aToStrip,
+                                     uint32_t aOffset)
+{
+  // Note that this implicitly guarantees mLength > 0
+  if (aOffset >= uint32_t(mLength)) {
+    return;
+  }
+
+  if (!EnsureMutable()) {
+    AllocFailed(mLength);
+  }
+
+  char_type* to   = mData + aOffset;
+  char_type* from = mData + aOffset;
+  char_type* end  = mData + mLength;
+
+  while (from < end) {
+    uint32_t theChar = (uint32_t)*from++;
+    // Replacing this with a call to ASCIIMask::IsMasked
+    // regresses performance somewhat, so leaving it inlined.
+    if (!mozilla::ASCIIMask::IsMasked(aToStrip, theChar)) {
+      // Not stripped, copy this char.
+      *to++ = (char_type)theChar;
+    }
+  }
+  *to = char_type(0); // add the null
+  mLength = to - mData;
+}
+
+void
+nsTSubstring_CharT::StripCRLF(uint32_t aOffset)
+{
+  // Expanding this call to copy the code from StripTaggedASCII
+  // instead of just calling it does somewhat help with performance
+  // but it is not worth it given the duplicated code.
+  StripTaggedASCII(mozilla::ASCIIMask::MaskCRLF(), aOffset);
+}
+
 struct MOZ_STACK_CLASS PrintfAppend_CharT : public mozilla::PrintfTarget
 {
   explicit PrintfAppend_CharT(nsTSubstring_CharT* aString)
     : mString(aString)
   {
   }
 
   bool append(const char* aStr, size_t aLen) override {
--- a/xpcom/string/nsTSubstring.h
+++ b/xpcom/string/nsTSubstring.h
@@ -1010,16 +1010,41 @@ public:
    *
    *  @param  aChars -- chars to be stripped
    *  @param  aOffset -- where in this string to start stripping chars
    */
 
   void StripChars(const char_type* aChars, uint32_t aOffset = 0);
 
   /**
+   * This method is used to remove all occurrences of some characters this
+   * from this string.  The characters removed have the corresponding
+   * entries in the bool array set to true; we retain all characters
+   * with code beyond 127.
+   * THE CALLER IS RESPONSIBLE for making sure the complete boolean
+   * array, 128 entries, is properly initialized.
+   *
+   * See also: ASCIIMask class.
+   *
+   *  @param  aToStrip -- Array where each entry is true if the
+   *          corresponding ASCII character is to be stripped.  All
+   *          characters beyond code 127 are retained.  Note that this
+   *          parameter is of ASCIIMaskArray type, but we expand the typedef
+   *          to avoid having to include nsASCIIMask.h in this include file
+   *          as it brings other includes.
+   *  @param  aOffset -- where in this string to start stripping chars
+   */
+  void StripTaggedASCII(const std::array<bool, 128>& aToStrip, uint32_t aOffset = 0);
+
+  /**
+   * A shortcut to strip \r and \n.
+   */
+  void StripCRLF(uint32_t aOffset = 0);
+
+  /**
    * If the string uses a shared buffer, this method
    * clears the pointer without releasing the buffer.
    */
   void ForgetSharedBuffer()
   {
     if (mFlags & nsSubstring::F_SHARED) {
       mData = char_traits::sEmptyBuffer;
       mLength = 0;
--- a/xpcom/tests/gtest/TestStrings.cpp
+++ b/xpcom/tests/gtest/TestStrings.cpp
@@ -1130,17 +1130,16 @@ TEST(Strings,ASCIIMask)
   EXPECT_TRUE(maskSome['Z'] && mozilla::ASCIIMask::IsMasked(maskSome, 'Z'));
   EXPECT_TRUE(maskSome['\b'] && mozilla::ASCIIMask::IsMasked(maskSome, '\b'));
   EXPECT_TRUE(maskSome['?'] && mozilla::ASCIIMask::IsMasked(maskSome, '?'));
   EXPECT_FALSE(maskSome['8'] || mozilla::ASCIIMask::IsMasked(maskSome, '8'));
   EXPECT_FALSE(maskSome['\0'] || mozilla::ASCIIMask::IsMasked(maskSome, '\0'));
   EXPECT_FALSE(mozilla::ASCIIMask::IsMasked(maskCRLF, 14324));
 }
 
-
 template <typename T> void
 CompressWhitespaceHelper()
 {
   T s;
   s.AssignLiteral("abcabcabc");
   s.CompressWhitespace(true, true);
   EXPECT_TRUE(s.EqualsLiteral("abcabcabc"));
 
@@ -1220,16 +1219,77 @@ TEST(Strings, CompressWhitespaceW)
 
   nsString str, result;
   str.AssignLiteral(u"\u263A    is\r\n   ;-)");
   result.AssignLiteral(u"\u263A is ;-)");
   str.CompressWhitespace(true, true);
   EXPECT_TRUE(str == result);
 }
 
+template <typename T> void
+StripCRLFHelper()
+{
+  T s;
+  s.AssignLiteral("abcabcabc");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("abcabcabc"));
+
+  s.AssignLiteral("   \n\rabcabcabc\r\n");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("   abcabcabc"));
+
+  s.AssignLiteral("   \n\rabc   abc   abc\r\n");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("   abc   abc   abc"));
+
+  s.AssignLiteral("   \n\rabc\r   abc\n   abc\r\n");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("   abc   abc   abc"));
+
+  s.AssignLiteral("   \n\rabc\r  \nabc\n   \rabc\r\n");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("   abc  abc   abc"));
+
+  s.AssignLiteral("   \n\rabc\r   abc\n   abc\r\n");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("   abc   abc   abc"));
+
+  s.AssignLiteral("  \r\n  ");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("    "));
+
+  s.AssignLiteral("  \r\n  \t");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("    \t"));
+
+  s.AssignLiteral("\n  \r\n  \t");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral("    \t"));
+
+  s.AssignLiteral("");
+  s.StripCRLF();
+  EXPECT_TRUE(s.EqualsLiteral(""));
+}
+
+TEST(Strings, StripCRLF)
+{
+  StripCRLFHelper<nsCString>();
+}
+
+TEST(Strings, StripCRLFW)
+{
+  StripCRLFHelper<nsString>();
+
+  nsString str, result;
+  str.AssignLiteral(u"\u263A    is\r\n   ;-)");
+  result.AssignLiteral(u"\u263A    is   ;-)");
+  str.StripCRLF();
+  EXPECT_TRUE(str == result);
+}
+
 #define TestExample1 "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,\n totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi\r architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur\n aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui\r\n\r dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
 
 #define TestExample2 "At vero eos et accusamus et iusto odio dignissimos ducimus\n\n qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt\r\r  \n mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda       est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
 
 #define TestExample3 " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ac tellus eget velit viverra viverra id sit amet neque. Sed id consectetur mi, vestibulum aliquet arcu. Curabitur sagittis accumsan convallis. Sed eu condimentum ipsum, a laoreet tortor. Orci varius natoque penatibus et magnis dis    \r\r\n\n parturient montes, nascetur ridiculus mus. Sed non tellus nec ante sodales placerat a nec risus. Cras vel bibendum sapien, nec ullamcorper felis. Pellentesque congue eget nisi sit amet vehicula. Morbi pulvinar turpis justo, in commodo dolor vulputate id. Curabitur in dui urna. Vestibulum placerat dui in sem congue, ut faucibus nibh rutrum. Duis mattis turpis facilisis ullamcorper tincidunt. Vestibulum pharetra tortor at enim sagittis, dapibus consectetur ex blandit. Curabitur ac fringilla quam. In ornare lectus ut ipsum mattis venenatis. Etiam in mollis lectus, sed luctus risus.\nCras dapibus\f\t  \n finibus justo sit amet dictum. Aliquam non elit diam. Fusce magna nulla, bibendum in massa a, commodo finibus lectus. Sed rutrum a augue id imperdiet. Aliquam sagittis sodales felis, a tristique ligula. Aliquam erat volutpat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Duis volutpat interdum lorem et congue. Phasellus porttitor posuere justo eget euismod. Nam a condimentum turpis, sit amet gravida lacus. Vestibulum dolor diam, lobortis ac metus et, convallis dapibus tellus. Ut nec metus in velit malesuada tincidunt et eget justo. Curabitur ut libero bibendum, porttitor diam vitae, aliquet justo. "
 
 #define TestExample4 " Donec feugiat volutpat massa. Cras ornare lacinia porta. Fusce in feugiat nunc. Praesent non felis varius diam feugiat ultrices ultricies a risus. Donec maximus nisi nisl, non consectetur nulla eleifend in. Nulla in massa interdum, eleifend orci a, vestibulum est. Mauris aliquet, massa et convallis mollis, felis augue vestibulum augue, in lobortis metus eros a quam. Nam              ac diam ornare, vestibulum elit sit amet, consectetur ante. Praesent massa mauris, pulvinar sit amet sapien vel, tempus gravida neque. Praesent id quam sit amet est maximus molestie eget at turpis. Nunc sit amet orci id arcu dapibus fermentum non eu erat.\f\tSuspendisse commodo nunc sem, eu congue eros condimentum vel. Nullam sit amet posuere arcu. Nulla facilisi. Mauris dapibus iaculis massa sed gravida. Nullam vitae urna at tortor feugiat auctor ut sit amet dolor. Proin rutrum at nunc et faucibus. Quisque suscipit id nibh a aliquet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam a dapibus erat, id imperdiet mauris. Nulla blandit libero non magna dapibus tristique. Integer hendrerit imperdiet lorem, quis facilisis lacus semper ut. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae Nullam dignissim elit in congue ultricies. Quisque erat odio, maximus mollis laoreet id, iaculis at turpis. "
 
@@ -1278,9 +1338,41 @@ MOZ_GTEST_BENCH(Strings, PerfCompressWhi
       test1.CompressWhitespace();
       test2.CompressWhitespace();
       test3.CompressWhitespace();
       test4.CompressWhitespace();
       test5.CompressWhitespace();
     }
 });
 
+MOZ_GTEST_BENCH(Strings, PerfStripCRLF, [] {
+    nsCString test1(TestExample1);
+    nsCString test2(TestExample2);
+    nsCString test3(TestExample3);
+    nsCString test4(TestExample4);
+    nsCString test5(TestExample5);
+    for (int i = 0; i < 20000; i++) {
+      test1.StripCRLF();
+      test2.StripCRLF();
+      test3.StripCRLF();
+      test4.StripCRLF();
+      test5.StripCRLF();
+    }
+});
+
+MOZ_GTEST_BENCH(Strings, PerfStripCharsCRLF, [] {
+    // This is the unoptimized (original) version of
+    // stripping \r\n using StripChars.
+    nsCString test1(TestExample1);
+    nsCString test2(TestExample2);
+    nsCString test3(TestExample3);
+    nsCString test4(TestExample4);
+    nsCString test5(TestExample5);
+    for (int i = 0; i < 20000; i++) {
+      test1.StripChars("\r\n");
+      test2.StripChars("\r\n");
+      test3.StripChars("\r\n");
+      test4.StripChars("\r\n");
+      test5.StripChars("\r\n");
+    }
+});
+
 } // namespace TestStrings
--- a/xpfe/appshell/nsXULWindow.cpp
+++ b/xpfe/appshell/nsXULWindow.cpp
@@ -910,17 +910,17 @@ NS_IMETHODIMP nsXULWindow::GetTitle(char
     return NS_ERROR_OUT_OF_MEMORY;
   return NS_OK;
 }
 
 NS_IMETHODIMP nsXULWindow::SetTitle(const char16_t* aTitle)
 {
   NS_ENSURE_STATE(mWindow);
   mTitle.Assign(aTitle);
-  mTitle.StripChars("\n\r");
+  mTitle.StripCRLF();
   NS_ENSURE_SUCCESS(mWindow->SetTitle(mTitle), NS_ERROR_FAILURE);
 
   // Tell the window mediator that a title has changed
   nsCOMPtr<nsIWindowMediator> windowMediator(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
   if (!windowMediator)
     return NS_OK;
 
   windowMediator->UpdateWindowTitle(static_cast<nsIXULWindow*>(this), aTitle);