#565: M1358297 parts 3 and 4

This commit is contained in:
Cameron Kaiser 2019-08-17 10:42:19 -07:00
parent 9ab5bb0ed4
commit 3fd15a87ad
12 changed files with 141 additions and 25 deletions

View File

@ -167,7 +167,7 @@ nsDefaultURIFixup::GetFixupURIInfo(const nsACString& aStringURI,
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(" ");

View File

@ -4616,7 +4616,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.StripChars("\r\n");
uriString.StripCRLF();
NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE);
rv = NS_NewURI(getter_AddRefs(uri), uriString);

View File

@ -6406,9 +6406,7 @@ nsContentUtils::FlushLayoutForTree(nsIDOMWindow* aWindow)
void nsContentUtils::RemoveNewlines(nsString &aString)
{
// strip CR/LF and null
static const char badChars[] = {'\r', '\n', 0};
aString.StripChars(badChars);
aString.StripCRLF();
}
void

View File

@ -4773,15 +4773,13 @@ HTMLInputElement::SanitizeValue(nsAString& aValue)
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);
}

View File

@ -527,7 +527,7 @@ nsTextEditRules::HandleNewLines(nsString &aString,
aString.ReplaceChar(CRLF, ' ');
break;
case nsIPlaintextEditor::eNewlinesStrip:
aString.StripChars(CRLF);
aString.StripCRLF();
break;
case nsIPlaintextEditor::eNewlinesPasteToFirst:
default:

View File

@ -521,7 +521,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.StripChars("\r\n");
tempStr.StripCRLF();
char *res = ToNewCString(tempStr);
NS_ENSURE_TRUE(res, NS_ERROR_OUT_OF_MEMORY);
@ -764,7 +764,7 @@ internalDecodeRFC2047Header(const char* aHeaderVal, const char* aDefaultCharset,
nsAutoCString temp(aResult);
temp.ReplaceSubstring("\n\t", " ");
temp.ReplaceSubstring("\r\t", " ");
temp.StripChars("\r\n");
temp.StripCRLF();
aResult = temp;
}

View File

@ -540,8 +540,6 @@ 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 <class CharT>
#ifndef __SUNPRO_CC

View File

@ -369,13 +369,13 @@ public:
* @param aSet -- characters to be cut from this
*/
void StripChars(const char* aSet);
bool StripChars(const char* aSet, const fallible_t&);
/**
* This method strips whitespace throughout the string.
*/
void StripWhitespace();
bool StripWhitespace(const fallible_t&);
/**
* swaps occurence of 1 string for another

View File

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTArray.h"
#include "nsASCIIMask.h"
/**
* nsTString::Find
@ -400,18 +401,40 @@ nsTString_CharT::SetCharAt( char16_t aChar, uint32_t aIndex )
void
nsTString_CharT::StripChars( const char* aSet )
{
if (!EnsureMutable())
if (!StripChars(aSet, mozilla::fallible)) {
AllocFailed(mLength);
}
}
bool
nsTString_CharT::StripChars( const char* aSet, const fallible_t& )
{
if (!EnsureMutable()) {
return false;
}
mLength = nsBufferRoutines<CharT>::strip_chars(mData, mLength, aSet);
return true;
}
void
nsTString_CharT::StripWhitespace()
{
StripChars(kWhitespace);
if (!StripWhitespace(mozilla::fallible)) {
AllocFailed(mLength);
}
}
bool
nsTString_CharT::StripWhitespace( const fallible_t& )
{
if (!EnsureMutable()) {
return false;
}
StripTaggedASCII(mozilla::ASCIIMask::MaskWhitespace());
return true;
}
/**
* nsTString::ReplaceChar,ReplaceSubstring
@ -651,13 +674,44 @@ nsTString_CharT::Trim( const char* aSet, bool aTrimLeading, bool aTrimTrailing,
void
nsTString_CharT::CompressWhitespace( bool aTrimLeading, bool aTrimTrailing )
{
const char* set = kWhitespace;
// Quick exit
if (mLength == 0) {
return;
}
ReplaceChar(set, ' ');
Trim(set, aTrimLeading, aTrimTrailing);
if (!EnsureMutable())
AllocFailed(mLength);
// this one does some questionable fu... just copying the old code!
mLength = nsBufferRoutines<char_type>::compress_chars(mData, mLength, set);
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--;
}
*to = char_type(0); // add the null
mLength = to - mData;
}

View File

@ -4,6 +4,7 @@
* 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"
@ -821,7 +822,8 @@ nsTSubstring_CharT::FindChar(char_type aChar, index_type aOffset) const
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;
}
@ -848,6 +850,7 @@ 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;
}
@ -877,6 +880,45 @@ 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);
}
int
nsTSubstring_CharT::AppendFunc(void* aArg, const char* aStr, uint32_t aLen)
{

View File

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// IWYU pragma: private, include "nsString.h"
#include <array>
#include "mozilla/Casting.h"
#include "mozilla/MemoryReporting.h"
@ -833,6 +834,31 @@ public:
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.

View File

@ -917,7 +917,7 @@ 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