1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
Copyright (C) 2004-2016 Apple Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
#include <wtf/Deque.h>
#include <wtf/text/StringView.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
// FIXME: This should not start with "k".
// FIXME: This is a shared tokenizer concept, not a SegmentedString concept, but this is the only common header for now.
constexpr LChar kEndOfFileMarker = 0;
class SegmentedString {
public:
SegmentedString() = default;
SegmentedString(String&&);
SegmentedString(const String&);
explicit SegmentedString(StringView);
SegmentedString(SegmentedString&&) = delete;
SegmentedString(const SegmentedString&) = delete;
SegmentedString& operator=(SegmentedString&&);
SegmentedString& operator=(const SegmentedString&) = default;
void clear();
void close();
void append(SegmentedString&&);
void append(const SegmentedString&);
void append(String&&);
void append(const String&);
void pushBack(String&&);
void setExcludeLineNumbers();
bool isEmpty() const { return !m_currentSubstring.length; }
unsigned length() const;
bool isClosed() const { return m_isClosed; }
void advance();
void advancePastNonNewline(); // Faster than calling advance when we know the current character is not a newline.
void advancePastNewline(); // Faster than calling advance when we know the current character is a newline.
enum AdvancePastResult { DidNotMatch, DidMatch, NotEnoughCharacters };
template<unsigned length> AdvancePastResult advancePast(const char (&literal)[length]) { return advancePast<length, false>(literal); }
template<unsigned length> AdvancePastResult advancePastLettersIgnoringASCIICase(const char (&literal)[length]) { return advancePast<length, true>(literal); }
unsigned numberOfCharactersConsumed() const;
String toString() const;
UChar currentCharacter() const { return m_currentCharacter; }
OrdinalNumber currentColumn() const;
OrdinalNumber currentLine() const;
// Sets value of line/column variables. Column is specified indirectly by a parameter columnAfterProlog
// which is a value of column that we should get after a prolog (first prologLength characters) has been consumed.
void setCurrentPosition(OrdinalNumber line, OrdinalNumber columnAfterProlog, int prologLength);
private:
struct Substring {
Substring() = default;
Substring(String&&);
explicit Substring(StringView);
UChar currentCharacter() const;
UChar currentCharacterPreIncrement();
unsigned numberOfCharactersConsumed() const;
void appendTo(StringBuilder&) const;
String underlyingString; // Optional, may be null.
unsigned originalLength { 0 };
unsigned length { 0 };
union {
const LChar* currentCharacter8;
const UChar* currentCharacter16;
};
bool is8Bit;
bool doNotExcludeLineNumbers { true };
};
enum FastPathFlags {
NoFastPath = 0,
Use8BitAdvanceAndUpdateLineNumbers = 1 << 0,
Use8BitAdvance = 1 << 1,
};
void appendSubstring(Substring&&);
void processPossibleNewline();
void startNewLine();
void advanceWithoutUpdatingLineNumber();
void advanceWithoutUpdatingLineNumber16();
void advanceAndUpdateLineNumber16();
void advancePastSingleCharacterSubstringWithoutUpdatingLineNumber();
void advancePastSingleCharacterSubstring();
void advanceEmpty();
void updateAdvanceFunctionPointers();
void updateAdvanceFunctionPointersForEmptyString();
void updateAdvanceFunctionPointersForSingleCharacterSubstring();
void decrementAndCheckLength();
template<typename CharacterType> static bool characterMismatch(CharacterType, char, bool lettersIgnoringASCIICase);
template<unsigned length, bool lettersIgnoringASCIICase> AdvancePastResult advancePast(const char (&literal)[length]);
AdvancePastResult advancePastSlowCase(const char* literal, bool lettersIgnoringASCIICase);
Substring m_currentSubstring;
Deque<Substring> m_otherSubstrings;
bool m_isClosed { false };
UChar m_currentCharacter { 0 };
unsigned m_numberOfCharactersConsumedPriorToCurrentSubstring { 0 };
unsigned m_numberOfCharactersConsumedPriorToCurrentLine { 0 };
int m_currentLine { 0 };
unsigned char m_fastPathFlags { NoFastPath };
void (SegmentedString::*m_advanceWithoutUpdatingLineNumberFunction)() { &SegmentedString::advanceEmpty };
void (SegmentedString::*m_advanceAndUpdateLineNumberFunction)() { &SegmentedString::advanceEmpty };
};
inline SegmentedString::Substring::Substring(StringView passedStringView)
: originalLength(passedStringView.length())
, length(passedStringView.length())
{
if (length) {
is8Bit = passedStringView.is8Bit();
if (is8Bit)
currentCharacter8 = passedStringView.characters8();
else
currentCharacter16 = passedStringView.characters16();
}
}
inline SegmentedString::Substring::Substring(String&& passedString)
: underlyingString(WTFMove(passedString))
, originalLength(underlyingString.length())
, length(underlyingString.length())
{
if (length) {
is8Bit = underlyingString.impl()->is8Bit();
if (is8Bit)
currentCharacter8 = underlyingString.impl()->characters8();
else
currentCharacter16 = underlyingString.impl()->characters16();
}
}
inline unsigned SegmentedString::Substring::numberOfCharactersConsumed() const
{
return originalLength - length;
}
ALWAYS_INLINE UChar SegmentedString::Substring::currentCharacter() const
{
ASSERT(length);
return is8Bit ? *currentCharacter8 : *currentCharacter16;
}
ALWAYS_INLINE UChar SegmentedString::Substring::currentCharacterPreIncrement()
{
ASSERT(length);
return is8Bit ? *++currentCharacter8 : *++currentCharacter16;
}
inline SegmentedString::SegmentedString(StringView stringView)
: m_currentSubstring(stringView)
{
if (m_currentSubstring.length) {
m_currentCharacter = m_currentSubstring.currentCharacter();
updateAdvanceFunctionPointers();
}
}
inline SegmentedString::SegmentedString(String&& string)
: m_currentSubstring(WTFMove(string))
{
if (m_currentSubstring.length) {
m_currentCharacter = m_currentSubstring.currentCharacter();
updateAdvanceFunctionPointers();
}
}
inline SegmentedString::SegmentedString(const String& string)
: SegmentedString(String { string })
{
}
ALWAYS_INLINE void SegmentedString::decrementAndCheckLength()
{
ASSERT(m_currentSubstring.length > 1);
if (UNLIKELY(--m_currentSubstring.length == 1))
updateAdvanceFunctionPointersForSingleCharacterSubstring();
}
ALWAYS_INLINE void SegmentedString::advanceWithoutUpdatingLineNumber()
{
if (LIKELY(m_fastPathFlags & Use8BitAdvance)) {
m_currentCharacter = *++m_currentSubstring.currentCharacter8;
decrementAndCheckLength();
return;
}
(this->*m_advanceWithoutUpdatingLineNumberFunction)();
}
inline void SegmentedString::startNewLine()
{
++m_currentLine;
m_numberOfCharactersConsumedPriorToCurrentLine = numberOfCharactersConsumed();
}
inline void SegmentedString::processPossibleNewline()
{
if (m_currentCharacter == '\n')
startNewLine();
}
inline void SegmentedString::advance()
{
if (LIKELY(m_fastPathFlags & Use8BitAdvance)) {
ASSERT(m_currentSubstring.length > 1);
bool lastCharacterWasNewline = m_currentCharacter == '\n';
m_currentCharacter = *++m_currentSubstring.currentCharacter8;
bool haveOneCharacterLeft = --m_currentSubstring.length == 1;
if (LIKELY(!(lastCharacterWasNewline | haveOneCharacterLeft)))
return;
if (lastCharacterWasNewline & !!(m_fastPathFlags & Use8BitAdvanceAndUpdateLineNumbers))
startNewLine();
if (haveOneCharacterLeft)
updateAdvanceFunctionPointersForSingleCharacterSubstring();
return;
}
(this->*m_advanceAndUpdateLineNumberFunction)();
}
ALWAYS_INLINE void SegmentedString::advancePastNonNewline()
{
ASSERT(m_currentCharacter != '\n');
advanceWithoutUpdatingLineNumber();
}
inline void SegmentedString::advancePastNewline()
{
ASSERT(m_currentCharacter == '\n');
if (m_currentSubstring.length > 1) {
m_currentCharacter = m_currentSubstring.currentCharacterPreIncrement();
decrementAndCheckLength();
if (m_currentSubstring.doNotExcludeLineNumbers)
startNewLine();
return;
}
(this->*m_advanceAndUpdateLineNumberFunction)();
}
inline unsigned SegmentedString::numberOfCharactersConsumed() const
{
return m_numberOfCharactersConsumedPriorToCurrentSubstring + m_currentSubstring.numberOfCharactersConsumed();
}
template<typename CharacterType> ALWAYS_INLINE bool SegmentedString::characterMismatch(CharacterType a, char b, bool lettersIgnoringASCIICase)
{
return lettersIgnoringASCIICase ? !isASCIIAlphaCaselessEqual(a, b) : a != b;
}
template<unsigned lengthIncludingTerminator, bool lettersIgnoringASCIICase> SegmentedString::AdvancePastResult SegmentedString::advancePast(const char (&literal)[lengthIncludingTerminator])
{
constexpr unsigned length = lengthIncludingTerminator - 1;
ASSERT(!literal[length]);
ASSERT(!strchr(literal, '\n'));
if (length + 1 < m_currentSubstring.length) {
if (m_currentSubstring.is8Bit) {
for (unsigned i = 0; i < length; ++i) {
if (characterMismatch(m_currentSubstring.currentCharacter8[i], literal[i], lettersIgnoringASCIICase))
return DidNotMatch;
}
m_currentSubstring.currentCharacter8 += length;
m_currentCharacter = *m_currentSubstring.currentCharacter8;
} else {
for (unsigned i = 0; i < length; ++i) {
if (characterMismatch(m_currentSubstring.currentCharacter16[i], literal[i], lettersIgnoringASCIICase))
return DidNotMatch;
}
m_currentSubstring.currentCharacter16 += length;
m_currentCharacter = *m_currentSubstring.currentCharacter16;
}
m_currentSubstring.length -= length;
return DidMatch;
}
return advancePastSlowCase(literal, lettersIgnoringASCIICase);
}
inline void SegmentedString::updateAdvanceFunctionPointers()
{
if (m_currentSubstring.length > 1) {
if (m_currentSubstring.is8Bit) {
m_fastPathFlags = Use8BitAdvance;
if (m_currentSubstring.doNotExcludeLineNumbers)
m_fastPathFlags |= Use8BitAdvanceAndUpdateLineNumbers;
return;
}
m_fastPathFlags = NoFastPath;
m_advanceWithoutUpdatingLineNumberFunction = &SegmentedString::advanceWithoutUpdatingLineNumber16;
if (m_currentSubstring.doNotExcludeLineNumbers)
m_advanceAndUpdateLineNumberFunction = &SegmentedString::advanceAndUpdateLineNumber16;
else
m_advanceAndUpdateLineNumberFunction = &SegmentedString::advanceWithoutUpdatingLineNumber16;
return;
}
if (!m_currentSubstring.length) {
updateAdvanceFunctionPointersForEmptyString();
return;
}
updateAdvanceFunctionPointersForSingleCharacterSubstring();
}
}
|