partly import changes from tenfourfox:

- #565: implement nsASCIIMask from M1358297 (41b1fc937)
- #565: M1358297 parts 3 and 4 (3fd15a87a)
- #632: M1424915 M1354233 M1324114 M1343008 M1236277(just backbugs) M1328955 (d87db7e16)
- #632: M1362498 M1397686 M136178 M1320252 M1355875 (82cb3b59e)
- #632: M241788 M1271955 M1249352(p1) + additional local optimizations (438bdb726)
- #632: M1249352 part 2 (2c61821e4)
- #632: M1249352(remaining) M1358297(backbugs) M1369317(pp1,3,4) M1426996 (1eab6170b)
This commit is contained in:
2021-01-25 17:28:58 +08:00
parent 07d60363f2
commit 2bd3a3c9ef
32 changed files with 480 additions and 105 deletions
+49 -11
View File
@@ -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;
}
/**
@@ -672,13 +679,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;
}