diff --git a/docshell/base/nsDefaultURIFixup.cpp b/docshell/base/nsDefaultURIFixup.cpp index 77fecd5cfbf6f..30e6b9b81df06 100644 --- a/docshell/base/nsDefaultURIFixup.cpp +++ b/docshell/base/nsDefaultURIFixup.cpp @@ -167,7 +167,7 @@ nsDefaultURIFixup::GetFixupURIInfo(const nsACString& aStringURI, nsAutoCString uriString(aStringURI); // Eliminate embedded newlines, which single-line text fields now allow: - uriString.StripCRLF(); + uriString.StripChars("\r\n"); // Cleanup the empty spaces that might be on each end: uriString.Trim(" "); diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index bc3775afef57a..83ca1b57bbdb1 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -4780,7 +4780,7 @@ nsDocShell::LoadURIWithOptions(const char16_t* aURI, // Cleanup the empty spaces that might be on each end. uriString.Trim(" "); // Eliminate embedded newlines, which single-line text fields now allow: - uriString.StripCRLF(); + uriString.StripChars("\r\n"); NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE); rv = NS_NewURI(getter_AddRefs(uri), uriString); diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index be9a3e5414960..fe0d9a7411b6a 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -6684,7 +6684,9 @@ nsContentUtils::FlushLayoutForTree(nsPIDOMWindowOuter* aWindow) void nsContentUtils::RemoveNewlines(nsString &aString) { - aString.StripCRLF(); + // strip CR/LF and null + static const char badChars[] = {'\r', '\n', 0}; + aString.StripChars(badChars); } void diff --git a/dom/base/nsFrameMessageManager.cpp b/dom/base/nsFrameMessageManager.cpp index 8484e99c44a19..2707d380103aa 100644 --- a/dom/base/nsFrameMessageManager.cpp +++ b/dom/base/nsFrameMessageManager.cpp @@ -9,7 +9,6 @@ #include "nsFrameMessageManager.h" #include "ContentChild.h" -#include "nsASCIIMask.h" #include "nsContentUtils.h" #include "nsDOMClassInfoID.h" #include "nsError.h" @@ -592,7 +591,7 @@ AllowMessage(size_t aDataLength, const nsAString& aMessageName) } NS_ConvertUTF16toUTF8 messageName(aMessageName); - messageName.StripTaggedASCII(ASCIIMask::Mask0to9()); + messageName.StripChars("0123456789"); Telemetry::Accumulate(Telemetry::MESSAGE_MANAGER_MESSAGE_SIZE2, messageName, aDataLength); @@ -678,7 +677,7 @@ nsFrameMessageManager::SendMessage(const nsAString& 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.StripTaggedASCII(ASCIIMask::Mask0to9()); + messageName.StripChars("0123456789"); Telemetry::Accumulate(Telemetry::IPC_SYNC_MESSAGE_MANAGER_LATENCY_MS, messageName, latencyMs); } diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index a21d77f3a9c7d..7fb9ed1d98213 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -5345,13 +5345,15 @@ HTMLInputElement::SanitizeValue(nsAString& aValue) case NS_FORM_INPUT_TEL: case NS_FORM_INPUT_PASSWORD: { - aValue.StripCRLF(); + char16_t crlf[] = { char16_t('\r'), char16_t('\n'), 0 }; + aValue.StripChars(crlf); } break; case NS_FORM_INPUT_EMAIL: case NS_FORM_INPUT_URL: { - aValue.StripCRLF(); + char16_t crlf[] = { char16_t('\r'), char16_t('\n'), 0 }; + aValue.StripChars(crlf); aValue = nsContentUtils::TrimWhitespace(aValue); } diff --git a/editor/libeditor/TextEditRules.cpp b/editor/libeditor/TextEditRules.cpp index f7379c90d921e..7cf177e356877 100644 --- a/editor/libeditor/TextEditRules.cpp +++ b/editor/libeditor/TextEditRules.cpp @@ -533,7 +533,7 @@ TextEditRules::HandleNewLines(nsString& aString, aString.ReplaceChar(CRLF, ' '); break; case nsIPlaintextEditor::eNewlinesStrip: - aString.StripCRLF(); + aString.StripChars(CRLF); break; case nsIPlaintextEditor::eNewlinesPasteToFirst: default: { diff --git a/netwerk/base/nsURLHelper.cpp b/netwerk/base/nsURLHelper.cpp index 71ec18f47c056..a746bd7d0e664 100644 --- a/netwerk/base/nsURLHelper.cpp +++ b/netwerk/base/nsURLHelper.cpp @@ -9,7 +9,6 @@ #include #include -#include "nsASCIIMask.h" #include "nsURLHelper.h" #include "nsIFile.h" #include "nsIURLParser.h" @@ -523,7 +522,7 @@ net_ExtractURLScheme(const nsACString &inURI, } p.Claim(scheme); - scheme.StripTaggedASCII(ASCIIMask::MaskCRLFTab()); + scheme.StripChars("\r\n\t"); return NS_OK; } @@ -592,6 +591,8 @@ net_IsAbsoluteURL(const nsACString& uri) void net_FilterURIString(const nsACString& input, nsACString& result) { + const char kCharsToStrip[] = "\r\n\t"; + result.Truncate(); auto start = input.BeginReading(); @@ -606,14 +607,9 @@ net_FilterURIString(const nsACString& input, nsACString& result) charFilter).base(); // Check if chars need to be stripped. - bool needsStrip = false; - const ASCIIMaskArray& mask = ASCIIMask::MaskCRLFTab(); - for (auto itr = start; itr != end; ++itr) { - if (ASCIIMask::IsMasked(mask, *itr)) { - needsStrip = true; - break; - } - } + auto itr = std::find_first_of( + newStart, newEnd, std::begin(kCharsToStrip), std::end(kCharsToStrip)); + const bool needsStrip = itr != newEnd; // Just use the passed in string rather than creating new copies if no // changes are necessary. @@ -624,7 +620,7 @@ net_FilterURIString(const nsACString& input, nsACString& result) result.Assign(Substring(newStart, newEnd)); if (needsStrip) { - result.StripTaggedASCII(mask); + result.StripChars(kCharsToStrip); } } diff --git a/netwerk/mime/nsMIMEHeaderParamImpl.cpp b/netwerk/mime/nsMIMEHeaderParamImpl.cpp index a4d6b216e81d0..b799d2ed88db1 100644 --- a/netwerk/mime/nsMIMEHeaderParamImpl.cpp +++ b/netwerk/mime/nsMIMEHeaderParamImpl.cpp @@ -518,7 +518,7 @@ nsMIMEHeaderParamImpl::DoParameterInternal(const char *aHeaderValue, // 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.StripCRLF(); + tempStr.StripChars("\r\n"); char *res = ToNewCString(tempStr); NS_ENSURE_TRUE(res, NS_ERROR_OUT_OF_MEMORY); @@ -761,7 +761,7 @@ internalDecodeRFC2047Header(const char* aHeaderVal, const char* aDefaultCharset, nsAutoCString temp(aResult); temp.ReplaceSubstring("\n\t", " "); temp.ReplaceSubstring("\r\t", " "); - temp.StripCRLF(); + temp.StripChars("\r\n"); aResult = temp; } diff --git a/xpcom/string/moz.build b/xpcom/string/moz.build index 0cd7574dafa4d..273412178cd48 100644 --- a/xpcom/string/moz.build +++ b/xpcom/string/moz.build @@ -8,7 +8,6 @@ with Files('**'): BUG_COMPONENT = ('Core', 'String') EXPORTS += [ - 'nsASCIIMask.h', 'nsAString.h', 'nsCharTraits.h', 'nsDependentString.h', @@ -39,7 +38,6 @@ EXPORTS += [ ] UNIFIED_SOURCES += [ - 'nsASCIIMask.cpp', 'nsDependentString.cpp', 'nsDependentSubstring.cpp', 'nsPromiseFlatString.cpp', diff --git a/xpcom/string/nsASCIIMask.cpp b/xpcom/string/nsASCIIMask.cpp deleted file mode 100644 index 1c617a5b4a7d6..0000000000000 --- a/xpcom/string/nsASCIIMask.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * 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" - -namespace mozilla { - -constexpr bool TestWhitespace(char c) -{ - return c == '\f' || c == '\t' || c == '\r' || c == '\n' || c == ' '; -} -constexpr ASCIIMaskArray sWhitespaceMask = CreateASCIIMask(TestWhitespace); - -constexpr bool TestCRLF(char c) -{ - return c == '\r' || c == '\n'; -} -constexpr ASCIIMaskArray sCRLFMask = CreateASCIIMask(TestCRLF); - -constexpr bool TestCRLFTab(char c) -{ - return c == '\r' || c == '\n' || c == '\t'; -} -constexpr ASCIIMaskArray sCRLFTabMask = CreateASCIIMask(TestCRLFTab); - -constexpr bool TestZeroToNine(char c) -{ - return c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || - c == '5' || c == '6' || c == '7' || c == '8' || c == '9'; -} -constexpr ASCIIMaskArray sZeroToNineMask = CreateASCIIMask(TestZeroToNine); - -const ASCIIMaskArray& ASCIIMask::MaskWhitespace() -{ - return sWhitespaceMask; -} - -const ASCIIMaskArray& ASCIIMask::MaskCRLF() -{ - return sCRLFMask; -} - -const ASCIIMaskArray& ASCIIMask::MaskCRLFTab() -{ - return sCRLFTabMask; -} - -const ASCIIMaskArray& ASCIIMask::Mask0to9() -{ - return sZeroToNineMask; -} - -} // namespace mozilla diff --git a/xpcom/string/nsASCIIMask.h b/xpcom/string/nsASCIIMask.h deleted file mode 100644 index 7a3e76beff6be..0000000000000 --- a/xpcom/string/nsASCIIMask.h +++ /dev/null @@ -1,70 +0,0 @@ -/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * 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/. */ - -#ifndef nsASCIIMask_h_ -#define nsASCIIMask_h_ - -#include -#include "mozilla/IndexSequence.h" - -typedef std::array ASCIIMaskArray; - -namespace mozilla { - -// Boolean arrays, fixed size and filled in at compile time, meant to -// record something about each of the (standard) ASCII characters. -// No extended ASCII for now, there has been no use case. -// If you have loops that go through a string character by character -// and test for equality to a certain set of characters before deciding -// on a course of action, chances are building up one of these arrays -// and using it is going to be faster, especially if the set of -// characters is more than one long, and known at compile time. -class ASCIIMask -{ -public: - // Preset masks for some common character groups - // When testing, you must check if the index is < 128 or use IsMasked() - // - // if (someChar < 128 && MaskCRLF()[someChar]) this is \r or \n - - static const ASCIIMaskArray& MaskCRLF(); - static const ASCIIMaskArray& Mask0to9(); - static const ASCIIMaskArray& MaskCRLFTab(); - static const ASCIIMaskArray& MaskWhitespace(); - - static MOZ_ALWAYS_INLINE bool IsMasked(const ASCIIMaskArray& aMask, uint32_t aChar) - { - return aChar < 128 && aMask[aChar]; - } -}; - -// Outside of the preset ones, use these templates to create more masks. -// -// The example creation will look like this: -// -// constexpr bool TestABC(char c) { return c == 'A' || c == 'B' || c == 'C'; } -// constexpr std::array sABCMask = CreateASCIIMask(TestABC); -// ... -// if (someChar < 128 && sABCMask[someChar]) this is A or B or C - - -namespace details -{ -template -constexpr std::array CreateASCIIMask(F fun, mozilla::IndexSequence) -{ - return {{ fun(Indices)... }}; -} -} // namespace details - -template -constexpr std::array CreateASCIIMask(F fun) -{ - return details::CreateASCIIMask(fun, mozilla::MakeIndexSequence<128>::Type{}); -} - -} // namespace mozilla - -#endif // nsASCIIMask_h_ diff --git a/xpcom/string/nsStringObsolete.cpp b/xpcom/string/nsStringObsolete.cpp index 2cfe06be3d1ee..bd6daacab3b43 100644 --- a/xpcom/string/nsStringObsolete.cpp +++ b/xpcom/string/nsStringObsolete.cpp @@ -540,6 +540,8 @@ StripChars2(char16_t* aString,uint32_t aLength,const char* aSet) { /* ***** END RICKG BLOCK ***** */ +static const char* kWhitespace="\f\t\r\n "; + // This function is used to implement FindCharInSet and friends template #ifndef __SUNPRO_CC diff --git a/xpcom/string/nsTStringObsolete.cpp b/xpcom/string/nsTStringObsolete.cpp index c25ef05e2c167..edab84b02bba3 100644 --- a/xpcom/string/nsTStringObsolete.cpp +++ b/xpcom/string/nsTStringObsolete.cpp @@ -5,7 +5,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "nsTArray.h" -#include "nsASCIIMask.h" #include "mozilla/CheckedInt.h" /** @@ -402,9 +401,10 @@ nsTString_CharT::SetCharAt( char16_t aChar, uint32_t aIndex ) void nsTString_CharT::StripChars( const char* aSet ) { - if (!StripChars(aSet, mozilla::fallible)) { + if (!EnsureMutable()) AllocFailed(mLength); - } + + mLength = nsBufferRoutines::strip_chars(mData, mLength, aSet); } bool @@ -421,20 +421,13 @@ nsTString_CharT::StripChars( const char* aSet, const fallible_t& ) void nsTString_CharT::StripWhitespace() { - if (!StripWhitespace(mozilla::fallible)) { - AllocFailed(mLength); - } + StripChars(kWhitespace); } bool -nsTString_CharT::StripWhitespace( const fallible_t& ) +nsTString_CharT::StripWhitespace(const fallible_t& aFallible) { - if (!EnsureMutable()) { - return false; - } - - StripTaggedASCII(mozilla::ASCIIMask::MaskWhitespace()); - return true; + return StripChars(kWhitespace, aFallible); } /** @@ -673,50 +666,19 @@ nsTString_CharT::Trim( const char* aSet, bool aTrimLeading, bool aTrimTrailing, /** - * nsTString::CompressWhitespace. + * nsTString::CompressWhitespace */ void nsTString_CharT::CompressWhitespace( bool aTrimLeading, bool aTrimTrailing ) { - // Quick exit - if (mLength == 0) { - return; - } - - if (!EnsureMutable()) - AllocFailed(mLength); + const char* set = kWhitespace; - const ASCIIMaskArray& mask = mozilla::ASCIIMask::MaskWhitespace(); - - char_type* to = mData; - char_type* from = mData; - char_type* end = mData + mLength; - - // Compresses runs of whitespace down to a normal space ' ' and convert - // any whitespace to a normal space. This assumes that whitespace is - // all standard 7-bit ASCII. - bool skipWS = aTrimLeading; - while (from < end) { - uint32_t theChar = *from++; - if (mozilla::ASCIIMask::IsMasked(mask, theChar)) { - if (!skipWS) { - *to++ = ' '; - skipWS = true; - } - } else { - *to++ = theChar; - skipWS = false; - } - } - - // If we need to trim the trailing whitespace, back up one character. - if (aTrimTrailing && skipWS && to > mData) { - to--; - } + ReplaceChar(set, ' '); + Trim(set, aTrimLeading, aTrimTrailing); - *to = char_type(0); // add the null - mLength = to - mData; + // this one does some questionable fu... just copying the old code! + mLength = nsBufferRoutines::compress_chars(mData, mLength, set); } diff --git a/xpcom/string/nsTSubstring.cpp b/xpcom/string/nsTSubstring.cpp index 97cdd3a22a296..0cb3741e1861c 100644 --- a/xpcom/string/nsTSubstring.cpp +++ b/xpcom/string/nsTSubstring.cpp @@ -4,7 +4,6 @@ * 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" @@ -882,8 +881,7 @@ nsTStringRepr_CharT::FindChar(char_type aChar, index_type aOffset) const void nsTSubstring_CharT::StripChar(char_type aChar, int32_t aOffset) { - // Note that this implicitly guarantees mLength > 0 - if (aOffset >= int32_t(mLength)) { + if (mLength == 0 || aOffset >= int32_t(mLength)) { return; } @@ -910,7 +908,6 @@ nsTSubstring_CharT::StripChar(char_type aChar, int32_t aOffset) void nsTSubstring_CharT::StripChars(const char_type* aChars, uint32_t aOffset) { - // Note that this implicitly guarantees mLength > 0 if (aOffset >= uint32_t(mLength)) { return; } @@ -940,45 +937,6 @@ nsTSubstring_CharT::StripChars(const char_type* aChars, uint32_t aOffset) 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) diff --git a/xpcom/string/nsTSubstring.h b/xpcom/string/nsTSubstring.h index ca23060f797d3..8b07ef8d0244a 100644 --- a/xpcom/string/nsTSubstring.h +++ b/xpcom/string/nsTSubstring.h @@ -1014,31 +1014,6 @@ class nsTSubstring_CharT : public mozilla::detail::nsTStringRepr_CharT 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& 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. diff --git a/xpcom/tests/gtest/TestStrings.cpp b/xpcom/tests/gtest/TestStrings.cpp index 050d2dcc38956..8ebbb854d6a67 100644 --- a/xpcom/tests/gtest/TestStrings.cpp +++ b/xpcom/tests/gtest/TestStrings.cpp @@ -6,7 +6,6 @@ #include #include -#include "nsASCIIMask.h" #include "nsString.h" #include "nsStringBuffer.h" #include "nsReadableUtils.h" @@ -15,7 +14,6 @@ #include "mozilla/Unused.h" #include "nsTArray.h" #include "gtest/gtest.h" -#include "gtest/MozGTestBench.h" // For MOZ_GTEST_BENCH namespace TestStrings { @@ -278,22 +276,6 @@ TEST(Strings, trim) nsCString s(text); s.Trim(set); EXPECT_STREQ(s.get(), "a"); - - s.AssignLiteral("\t \t\t \t"); - s.Trim(set); - EXPECT_STREQ(s.get(), ""); - - s.AssignLiteral(" "); - s.Trim(set); - EXPECT_STREQ(s.get(), ""); - - s.AssignLiteral(" "); - s.Trim(set, false, true); - EXPECT_STREQ(s.get(), ""); - - s.AssignLiteral(" "); - s.Trim(set, true, false); - EXPECT_STREQ(s.get(), ""); } TEST(Strings, replace_substr) @@ -303,16 +285,16 @@ TEST(Strings, replace_substr) s.ReplaceSubstring("ppp", "www"); EXPECT_STREQ(s.get(), "abc-www-qqq-www-xyz"); - s.AssignLiteral("foobar"); + s.Assign("foobar"); s.ReplaceSubstring("foo", "bar"); s.ReplaceSubstring("bar", ""); EXPECT_STREQ(s.get(), ""); - s.AssignLiteral("foofoofoo"); + s.Assign("foofoofoo"); s.ReplaceSubstring("foo", "foo"); EXPECT_STREQ(s.get(), "foofoofoo"); - s.AssignLiteral("foofoofoo"); + s.Assign("foofoofoo"); s.ReplaceSubstring("of", "fo"); EXPECT_STREQ(s.get(), "fofoofooo"); } @@ -338,86 +320,77 @@ TEST(Strings, replace_substr_2) TEST(Strings, replace_substr_3) { nsCString s; - s.AssignLiteral("abcabcabc"); + s.Assign("abcabcabc"); s.ReplaceSubstring("ca", "X"); EXPECT_STREQ(s.get(), "abXbXbc"); - s.AssignLiteral("abcabcabc"); + s.Assign("abcabcabc"); s.ReplaceSubstring("ca", "XYZ"); EXPECT_STREQ(s.get(), "abXYZbXYZbc"); - s.AssignLiteral("abcabcabc"); + s.Assign("abcabcabc"); s.ReplaceSubstring("ca", "XY"); EXPECT_STREQ(s.get(), "abXYbXYbc"); - s.AssignLiteral("abcabcabc"); + s.Assign("abcabcabc"); s.ReplaceSubstring("ca", "XYZ!"); EXPECT_STREQ(s.get(), "abXYZ!bXYZ!bc"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("bcd", "X"); EXPECT_STREQ(s.get(), "aXaXaX"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("bcd", "XYZ!"); EXPECT_STREQ(s.get(), "aXYZ!aXYZ!aXYZ!"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("bcd", "XY"); EXPECT_STREQ(s.get(), "aXYaXYaXY"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("bcd", "XYZABC"); EXPECT_STREQ(s.get(), "aXYZABCaXYZABCaXYZABC"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("bcd", "XYZ"); EXPECT_STREQ(s.get(), "aXYZaXYZaXYZ"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("bcd", "XYZ!"); EXPECT_STREQ(s.get(), "aXYZ!aXYZ!aXYZ!"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("ab", "X"); EXPECT_STREQ(s.get(), "XcdXcdXcd"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("ab", "XYZABC"); EXPECT_STREQ(s.get(), "XYZABCcdXYZABCcdXYZABCcd"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("ab", "XY"); EXPECT_STREQ(s.get(), "XYcdXYcdXYcd"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("ab", "XYZ!"); EXPECT_STREQ(s.get(), "XYZ!cdXYZ!cdXYZ!cd"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("notfound", "X"); EXPECT_STREQ(s.get(), "abcdabcdabcd"); - s.AssignLiteral("abcdabcdabcd"); + s.Assign("abcdabcdabcd"); s.ReplaceSubstring("notfound", "longlongstring"); EXPECT_STREQ(s.get(), "abcdabcdabcd"); } TEST(Strings, strip_ws) { - const char* texts[] = {"", - " a $ ", - "Some\fother\t thing\r\n", - "And \f\t\r\n even\nmore\r \f"}; - const char* results[] = {"", - "a$", - "Someotherthing", - "Andevenmore"}; - for (size_t i=0; i void -CompressWhitespaceHelper() -{ - T s; - s.AssignLiteral("abcabcabc"); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("abcabcabc")); - - s.AssignLiteral(" \n\rabcabcabc\r\n"); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("abcabcabc")); - - s.AssignLiteral(" \n\rabc abc abc\r\n"); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("abc abc abc")); - - s.AssignLiteral(" \n\rabc\r abc\n abc\r\n"); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("abc abc abc")); - - s.AssignLiteral(" \n\rabc\r \nabc\n \rabc\r\n"); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("abc abc abc")); - - s.AssignLiteral(" \n\rabc\r abc\n abc\r\n"); - s.CompressWhitespace(false, true); - EXPECT_TRUE(s.EqualsLiteral(" abc abc abc")); - - s.AssignLiteral(" \n\rabc\r abc\n abc\r\n"); - s.CompressWhitespace(true, false); - EXPECT_TRUE(s.EqualsLiteral("abc abc abc ")); - - s.AssignLiteral(" \n\rabc\r abc\n abc\r\n"); - s.CompressWhitespace(false, false); - EXPECT_TRUE(s.EqualsLiteral(" abc abc abc ")); - - s.AssignLiteral(" \r\n "); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("")); - - s.AssignLiteral(" \r\n \t"); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("")); - - s.AssignLiteral("\n \r\n \t"); - s.CompressWhitespace(false, false); - EXPECT_TRUE(s.EqualsLiteral(" ")); - - s.AssignLiteral("\n \r\n \t"); - s.CompressWhitespace(false, true); - EXPECT_TRUE(s.EqualsLiteral("")); - - s.AssignLiteral("\n \r\n \t"); - s.CompressWhitespace(true, false); - EXPECT_TRUE(s.EqualsLiteral("")); - - s.AssignLiteral(""); - s.CompressWhitespace(false, false); - EXPECT_TRUE(s.EqualsLiteral("")); - - s.AssignLiteral(""); - s.CompressWhitespace(false, true); - EXPECT_TRUE(s.EqualsLiteral("")); - - s.AssignLiteral(""); - s.CompressWhitespace(true, false); - EXPECT_TRUE(s.EqualsLiteral("")); - - s.AssignLiteral(""); - s.CompressWhitespace(true, true); - EXPECT_TRUE(s.EqualsLiteral("")); -} - -TEST(Strings, CompressWhitespace) -{ - CompressWhitespaceHelper(); -} - -TEST(Strings, CompressWhitespaceW) -{ - CompressWhitespaceHelper(); - - nsString str, result; - str.AssignLiteral(u"\u263A is\r\n ;-)"); - result.AssignLiteral(u"\u263A is ;-)"); - str.CompressWhitespace(true, true); - EXPECT_TRUE(str == result); -} - -template 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(); -} - -TEST(Strings, StripCRLFW) -{ - StripCRLFHelper(); - - 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. " - -#define TestExample5 "Donec id risus urna. Nunc consequat lacinia urna id bibendum. Nulla faucibus faucibus enim. Cras ex risus, ultrices id semper vitae, luctus ut nulla. Sed vehicula tellus sed purus imperdiet efficitur. Suspendisse feugiat\n\n\n imperdiet odio, sed porta lorem feugiat nec. Curabitur laoreet massa venenatis\r\n risus ornare\r\n, vitae feugiat tortor accumsan. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas id scelerisque mauris, eget facilisis erat. Ut nec pulvinar risus, sed iaculis ante. Mauris tincidunt, risus et pretium elementum, leo nisi consectetur ligula, tincidunt suscipit erat velit eget libero. Sed ac est tempus, consequat dolor mattis, mattis mi. " - -// Note the five calls in the loop, so divide by 100k -MOZ_GTEST_BENCH(Strings, PerfStripWhitespace, [] { - nsCString test1(TestExample1); - nsCString test2(TestExample2); - nsCString test3(TestExample3); - nsCString test4(TestExample4); - nsCString test5(TestExample5); - for (int i = 0; i < 20000; i++) { - test1.StripWhitespace(); - test2.StripWhitespace(); - test3.StripWhitespace(); - test4.StripWhitespace(); - test5.StripWhitespace(); - } -}); - -MOZ_GTEST_BENCH(Strings, PerfStripCharsWhitespace, [] { - // This is the unoptimized (original) version of - // StripWhitespace 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("\f\t\r\n "); - test2.StripChars("\f\t\r\n "); - test3.StripChars("\f\t\r\n "); - test4.StripChars("\f\t\r\n "); - test5.StripChars("\f\t\r\n "); - } -}); - -MOZ_GTEST_BENCH(Strings, PerfCompressWhitespace, [] { - nsCString test1(TestExample1); - nsCString test2(TestExample2); - nsCString test3(TestExample3); - nsCString test4(TestExample4); - nsCString test5(TestExample5); - for (int i = 0; i < 20000; i++) { - 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 diff --git a/xpfe/appshell/nsXULWindow.cpp b/xpfe/appshell/nsXULWindow.cpp index 37988e7ecc987..2d21a44bab8d7 100644 --- a/xpfe/appshell/nsXULWindow.cpp +++ b/xpfe/appshell/nsXULWindow.cpp @@ -915,7 +915,7 @@ NS_IMETHODIMP nsXULWindow::SetTitle(const char16_t* aTitle) { NS_ENSURE_STATE(mWindow); mTitle.Assign(aTitle); - mTitle.StripCRLF(); + mTitle.StripChars("\n\r"); NS_ENSURE_SUCCESS(mWindow->SetTitle(mTitle), NS_ERROR_FAILURE); // Tell the window mediator that a title has changed