Files
palemoon27/xpcom/ds/Tokenizer.cpp
T
roytam1 ca069b707a import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1244228 - fix DrawTargetSkia::OptimizeSourceSurface to still create Skia surfaces for GPU canvases even if creating a GPU surface failed. r=jmuizelaar (fbc02e4dc2)
- Bug 1259621 - use AlphaBoxBlur in DrawTargetSkia::DrawSurfaceWithShadow. r=mchang (2c9df0d317)
- Bug 1257717 - throw InvalidStateError when CreatePattern fails to snapshot source. r=jrmuizel (95fc5c4101)
- Bug 1264246 - verify that SkCanvas::getClipBounds succeeds before using result. r=mchang (20de1e897d)
- Bug 1249033 - Enable Subpixel AA Text on Skia for Dwrite and GDI Fonts. r=jrmuizel (f7402a4c9d)
- Bug 1250787 - ensure that subpixel text is disabled for Skia's Cairo font host so that font rendering matches our font metrics. r=mchang (4d77670f3f)
- Bug 1260454. Disable LCD font smoothing if we have grayscale AA on OS X. r=lsalzman (44bf859df5)
- bug 1263685 - Check if paint has a shader before modifying local matrix in DrawTargetSkia::MaskSurface. r=mchang (7937265238)
- missing bit for bump version (e3b14b6854)
- Bug 1252650 - Add data parameter to NS_CreateServicesFromCategory; r=froydnj (3a3d392669)
- Bug 1252650 - char16_t*/char16ptr_t mismatch fixup. (66d4bb1d1b)
- Bug 1261382 - Add ReadUntil method to Tokenizer r=mayhemer (3c0b05a359)
- Bug 1242963 - nsID stringification helper for logging; r=froydnj (966c73478a)
- Bug 1262400 - remove outer pointers from nsCreateInstance* classes; r=erahm (16680909c0)
- Bug 1257331 - Use LPWSTR instead of LPVOID to prevent C4477 warning; r=froydnj (456b677e25)
- bits of Bug 868814 - Fold mozallo (3580f23f5a)
- Bug 1256027 - Cast a few things in varargs to the expected types for MSVC 2015 warning-compatibility. r=froydnj (6e3e5f1763)
- Bug 1257262 - Removed invalid nsAString:Assign variant. r=bsmedberg (1f4b303f7a)
- bug 761909 - crash reporter plumbing for gonk. r=bsmedberg (e27b276147)
- Bug 1223800 - Accept BHR reports from 50% of beta clients. Up from 1%. r=vladan (fb67094a56)
- Bug 1241507 - Enable BHR reporting on 100% of the Beta population. r=rvitillo (6b0abc7767)
- Bug 1234618. Fix misdetected BHR hangs. r=jchen (4adc7d57a1)
- bits of Bug 1128768: Part 2 - Refactor (f75559fe5b)
- Bug 1168167 - Mark LazyIdleThread::mIdleObserver with MOZ_UNSAFE_REF. r=froydnj (9750c9686d)
- Bug 1250829 - add customized assertions for completion promises to facilitate promise chaining. r=bobbyholley. (66fe29ec68)
- Bug 1191063 - Followup comments. r=me DONTBUILD (a627c0bb62)
- bits of Bug 1172769 (6ce96f8829)
- Bug 1241656 - Lazify AutoTaskDispatcher::mDirectTasks. r=bholley. (48fab6f3fb)
- Bug 1202667 - make TaskQueue dispatching slightly more efficient; r=mccr8 (0c9f19fae5)
- Bug 1202667 - make TaskQueue task running slightly more efficient; r=mccr8 (cee8821a9f)
- Bug 1207031 - Suppress -Wshadow warnings from google-breakpad headers in xpcom/threads. r=froydnj (e2426fcf8a)
- Bug 1222101 - Reorder some thread code (r=jld) a=kwierso (4b816f501e)
- Bug 1257862 - Allow running things from the delayed work queue during a nested event loop. r=khuey,dvander (066ce6ab76)
- Bug 1236789. Part 2 - fix assertions when |aFlags == NS_DISPATCH_TAIL|. r=bholley (d6500cceb0)
- Bug 1254218 - Rely on old-configure.in to detect dladdr(). r=njn (8beefa2792)
- Bug 1257786 - Proper init of LogModule for Nuwa r=khuey (5170b5250b)
- Bug 1236108: Add support for exception-context annotations for content processes to the crash reporter; r=bsmedberg (3441aa3773)
- Bug 1256541: Fix incorrect generation of path for child process .extra files when content sandboxing is disabled; r=bsmedberg (ae8ba562ca)
- bug 1264242 - Write memory info streams for Windows Minidumps of child processes. r=bsmedberg (245b797566)
- Bug 1257486 - Annotate global memory status in the crash reporter for child process; r=bsmedberg (9af844a7dd)
- Bug 1257209 - record uptime in crash metadata/pings, r=gfritzsche (bfe0ea8759)
- Bug 1241613 - Only pass MOZ_BUILDID to the one source that requires it in toolkit/xre. r=mshal (944abe034f)
- Bug 1255655 - Heap-allocate _progname. r=froydnj. (3cce05ccaa)
- Bug 828704: Avoid duplicate -foreground argument when launching Firefox on OSX. r=mstange (2e5c12bdda)
- bug 1166759 - force annotateCrashReport arguments to be UTF-8. r=bsmedberg (5f2c30676a)
- Bug 1261751 - Problems with OS X Sandboxed TempDir and Rules. r=bobowen r=gcp (9ad9272b92)
2024-07-17 22:13:12 +08:00

524 lines
9.8 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "Tokenizer.h"
#include "nsUnicharUtils.h"
namespace mozilla {
static const char sWhitespaces[] = " \t";
Tokenizer::Tokenizer(const nsACString& aSource,
const char* aWhitespaces,
const char* aAdditionalWordChars)
: mPastEof(false)
, mHasFailed(false)
, mWhitespaces(aWhitespaces ? aWhitespaces : sWhitespaces)
, mAdditionalWordChars(aAdditionalWordChars)
{
aSource.BeginReading(mCursor);
mRecord = mRollback = mCursor;
aSource.EndReading(mEnd);
}
Tokenizer::Tokenizer(const char* aSource,
const char* aWhitespaces,
const char* aAdditionalWordChars)
: Tokenizer(nsDependentCString(aSource), aWhitespaces, aAdditionalWordChars)
{
}
bool
Tokenizer::Next(Token& aToken)
{
if (!HasInput()) {
mHasFailed = true;
return false;
}
mRollback = mCursor;
mCursor = Parse(aToken);
aToken.AssignFragment(mRollback, mCursor);
mPastEof = aToken.Type() == TOKEN_EOF;
mHasFailed = false;
return true;
}
bool
Tokenizer::Check(const TokenType aTokenType, Token& aResult)
{
if (!HasInput()) {
mHasFailed = true;
return false;
}
nsACString::const_char_iterator next = Parse(aResult);
if (aTokenType != aResult.Type()) {
mHasFailed = true;
return false;
}
mRollback = mCursor;
mCursor = next;
aResult.AssignFragment(mRollback, mCursor);
mPastEof = aResult.Type() == TOKEN_EOF;
mHasFailed = false;
return true;
}
bool
Tokenizer::Check(const Token& aToken)
{
if (!HasInput()) {
mHasFailed = true;
return false;
}
Token parsed;
nsACString::const_char_iterator next = Parse(parsed);
if (!aToken.Equals(parsed)) {
mHasFailed = true;
return false;
}
mRollback = mCursor;
mCursor = next;
mPastEof = parsed.Type() == TOKEN_EOF;
mHasFailed = false;
return true;
}
bool
Tokenizer::HasFailed() const
{
return mHasFailed;
}
void
Tokenizer::SkipWhites(WhiteSkipping aIncludeNewLines)
{
if (!CheckWhite() && (aIncludeNewLines == DONT_INCLUDE_NEW_LINE || !CheckEOL())) {
return;
}
nsACString::const_char_iterator rollback = mRollback;
while (CheckWhite() || (aIncludeNewLines == INCLUDE_NEW_LINE && CheckEOL())) {
}
mHasFailed = false;
mRollback = rollback;
}
bool
Tokenizer::CheckChar(bool (*aClassifier)(const char aChar))
{
if (!aClassifier) {
MOZ_ASSERT(false);
return false;
}
if (!HasInput() || mCursor == mEnd) {
mHasFailed = true;
return false;
}
if (!aClassifier(*mCursor)) {
mHasFailed = true;
return false;
}
mRollback = mCursor;
++mCursor;
mHasFailed = false;
return true;
}
bool
Tokenizer::ReadChar(char* aValue)
{
MOZ_RELEASE_ASSERT(aValue);
Token t;
if (!Check(TOKEN_CHAR, t)) {
return false;
}
*aValue = t.AsChar();
return true;
}
bool
Tokenizer::ReadChar(bool (*aClassifier)(const char aChar), char* aValue)
{
MOZ_RELEASE_ASSERT(aValue);
if (!CheckChar(aClassifier)) {
return false;
}
*aValue = *mRollback;
return true;
}
bool
Tokenizer::ReadWord(nsACString& aValue)
{
Token t;
if (!Check(TOKEN_WORD, t)) {
return false;
}
aValue.Assign(t.AsString());
return true;
}
bool
Tokenizer::ReadWord(nsDependentCSubstring& aValue)
{
Token t;
if (!Check(TOKEN_WORD, t)) {
return false;
}
aValue.Rebind(t.AsString().BeginReading(), t.AsString().Length());
return true;
}
bool
Tokenizer::ReadUntil(Token const& aToken, nsACString& aResult, ClaimInclusion aInclude)
{
nsDependentCSubstring substring;
bool rv = ReadUntil(aToken, substring, aInclude);
aResult.Assign(substring);
return rv;
}
bool
Tokenizer::ReadUntil(Token const& aToken, nsDependentCSubstring& aResult, ClaimInclusion aInclude)
{
Record();
nsACString::const_char_iterator rollback = mCursor;
bool found = false;
Token t;
while (Next(t)) {
if (aToken.Equals(t)) {
found = true;
break;
}
}
Claim(aResult, aInclude);
mRollback = rollback;
return found;
}
void
Tokenizer::Rollback()
{
MOZ_ASSERT(mCursor > mRollback || mPastEof,
"Tokenizer::Rollback() cannot use twice or before any parsing");
mPastEof = false;
mHasFailed = false;
mCursor = mRollback;
}
void
Tokenizer::Record(ClaimInclusion aInclude)
{
mRecord = aInclude == INCLUDE_LAST
? mRollback
: mCursor;
}
void
Tokenizer::Claim(nsACString& aResult, ClaimInclusion aInclusion)
{
nsACString::const_char_iterator close = aInclusion == EXCLUDE_LAST
? mRollback
: mCursor;
aResult.Assign(Substring(mRecord, close));
}
void
Tokenizer::Claim(nsDependentCSubstring& aResult, ClaimInclusion aInclusion)
{
nsACString::const_char_iterator close = aInclusion == EXCLUDE_LAST
? mRollback
: mCursor;
aResult.Rebind(mRecord, close - mRecord);
}
// protected
bool
Tokenizer::HasInput() const
{
return !mPastEof;
}
nsACString::const_char_iterator
Tokenizer::Parse(Token& aToken) const
{
if (mCursor == mEnd) {
aToken = Token::EndOfFile();
return mEnd;
}
nsACString::const_char_iterator next = mCursor;
enum State {
PARSE_INTEGER,
PARSE_WORD,
PARSE_CRLF,
PARSE_LF,
PARSE_WS,
PARSE_CHAR,
} state;
if (IsWordFirst(*next)) {
state = PARSE_WORD;
} else if (IsNumber(*next)) {
state = PARSE_INTEGER;
} else if (strchr(mWhitespaces, *next)) { // not UTF-8 friendly?
state = PARSE_WS;
} else if (*next == '\r') {
state = PARSE_CRLF;
} else if (*next == '\n') {
state = PARSE_LF;
} else {
state = PARSE_CHAR;
}
mozilla::CheckedUint64 resultingNumber = 0;
while (next < mEnd) {
switch (state) {
case PARSE_INTEGER:
// Keep it simple for now
resultingNumber *= 10;
resultingNumber += static_cast<uint64_t>(*next - '0');
++next;
if (IsEnd(next) || !IsNumber(*next)) {
if (!resultingNumber.isValid()) {
aToken = Token::Error();
} else {
aToken = Token::Number(resultingNumber.value());
}
return next;
}
break;
case PARSE_WORD:
++next;
if (IsEnd(next) || !IsWord(*next)) {
aToken = Token::Word(Substring(mCursor, next));
return next;
}
break;
case PARSE_CRLF:
++next;
if (!IsEnd(next) && *next == '\n') { // LF is optional
++next;
}
aToken = Token::NewLine();
return next;
case PARSE_LF:
++next;
aToken = Token::NewLine();
return next;
case PARSE_WS:
++next;
aToken = Token::Whitespace();
return next;
case PARSE_CHAR:
++next;
aToken = Token::Char(*mCursor);
return next;
} // switch (state)
} // while (next < end)
return next;
}
bool
Tokenizer::IsEnd(const nsACString::const_char_iterator& caret) const
{
return caret == mEnd;
}
bool
Tokenizer::IsWordFirst(const char aInput) const
{
// TODO: make this fully work with unicode
return (ToLowerCase(static_cast<uint32_t>(aInput)) !=
ToUpperCase(static_cast<uint32_t>(aInput))) ||
'_' == aInput ||
(mAdditionalWordChars ? !!strchr(mAdditionalWordChars, aInput) : false);
}
bool
Tokenizer::IsWord(const char aInput) const
{
return IsWordFirst(aInput) || IsNumber(aInput);
}
bool
Tokenizer::IsNumber(const char aInput) const
{
// TODO: are there unicode numbers?
return aInput >= '0' && aInput <= '9';
}
// Tokenizer::Token
Tokenizer::Token::Token(const Token& aOther)
: mType(aOther.mType)
, mChar(aOther.mChar)
, mInteger(aOther.mInteger)
{
if (mType == TOKEN_WORD) {
mWord.Rebind(aOther.mWord.BeginReading(), aOther.mWord.Length());
}
}
Tokenizer::Token&
Tokenizer::Token::operator=(const Token& aOther)
{
mType = aOther.mType;
mChar = aOther.mChar;
mWord.Rebind(aOther.mWord.BeginReading(), aOther.mWord.Length());
mInteger = aOther.mInteger;
return *this;
}
void
Tokenizer::Token::AssignFragment(nsACString::const_char_iterator begin,
nsACString::const_char_iterator end)
{
mFragment.Rebind(begin, end - begin);
}
// static
Tokenizer::Token
Tokenizer::Token::Word(const nsACString& aValue)
{
Token t;
t.mType = TOKEN_WORD;
t.mWord.Rebind(aValue.BeginReading(), aValue.Length());
return t;
}
// static
Tokenizer::Token
Tokenizer::Token::Char(const char aValue)
{
Token t;
t.mType = TOKEN_CHAR;
t.mChar = aValue;
return t;
}
// static
Tokenizer::Token
Tokenizer::Token::Number(const uint64_t aValue)
{
Token t;
t.mType = TOKEN_INTEGER;
t.mInteger = aValue;
return t;
}
// static
Tokenizer::Token
Tokenizer::Token::Whitespace()
{
Token t;
t.mType = TOKEN_WS;
t.mChar = '\0';
return t;
}
// static
Tokenizer::Token
Tokenizer::Token::NewLine()
{
Token t;
t.mType = TOKEN_EOL;
return t;
}
// static
Tokenizer::Token
Tokenizer::Token::EndOfFile()
{
Token t;
t.mType = TOKEN_EOF;
return t;
}
// static
Tokenizer::Token
Tokenizer::Token::Error()
{
Token t;
t.mType = TOKEN_ERROR;
return t;
}
bool
Tokenizer::Token::Equals(const Token& aOther) const
{
if (mType != aOther.mType) {
return false;
}
switch (mType) {
case TOKEN_INTEGER:
return AsInteger() == aOther.AsInteger();
case TOKEN_WORD:
return AsString() == aOther.AsString();
case TOKEN_CHAR:
return AsChar() == aOther.AsChar();
default:
return true;
}
}
char
Tokenizer::Token::AsChar() const
{
MOZ_ASSERT(mType == TOKEN_CHAR || mType == TOKEN_WS);
return mChar;
}
nsDependentCSubstring
Tokenizer::Token::AsString() const
{
MOZ_ASSERT(mType == TOKEN_WORD);
return mWord;
}
uint64_t
Tokenizer::Token::AsInteger() const
{
MOZ_ASSERT(mType == TOKEN_INTEGER);
return mInteger;
}
} // mozilla