Bug 1358297: Part 4. Optimize Strip/CompressWhitespace as a special case using ASCIIMask. r=froydnj

MozReview-Commit-ID: H43rS5WN5Ly

--HG--
extra : rebase_source : e9158bae137f9a6bb05f562bcb7716d2393a3e05
This commit is contained in:
Milan Sreckovic 2017-05-05 13:40:31 -04:00
Родитель 196fb1a3bc
Коммит a6ad922f92
2 изменённых файлов: 50 добавлений и 14 удалений

Просмотреть файл

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

Просмотреть файл

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTArray.h"
#include "nsASCIIMask.h"
#include "mozilla/CheckedInt.h"
/**
@ -401,10 +402,9 @@ 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);
mLength = nsBufferRoutines<CharT>::strip_chars(mData, mLength, aSet);
}
}
bool
@ -421,13 +421,20 @@ nsTString_CharT::StripChars( const char* aSet, const fallible_t& )
void
nsTString_CharT::StripWhitespace()
{
StripChars(kWhitespace);
if (!StripWhitespace(mozilla::fallible)) {
AllocFailed(mLength);
}
}
bool
nsTString_CharT::StripWhitespace(const fallible_t& aFallible)
nsTString_CharT::StripWhitespace( const fallible_t& )
{
return StripChars(kWhitespace, aFallible);
if (!EnsureMutable()) {
return false;
}
StripTaggedASCII(mozilla::ASCIIMask::MaskWhitespace());
return true;
}
/**
@ -666,19 +673,50 @@ nsTString_CharT::Trim( const char* aSet, bool aTrimLeading, bool aTrimTrailing,
/**
* nsTString::CompressWhitespace
* nsTString::CompressWhitespace.
*/
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;
}