#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

@@ -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;
}