HTMLIdentifier.cpp [plain text]
#include "config.h"
#if ENABLE(THREADED_HTML_PARSER)
#include "HTMLIdentifier.h"
#include "HTMLNames.h"
#include <wtf/HashMap.h>
#include <wtf/MainThread.h>
#include <wtf/text/StringHash.h>
namespace WebCore {
using namespace HTMLNames;
typedef std::pair<unsigned, StringImpl*> IdentifierEntry;
typedef HashMap<unsigned, IdentifierEntry, AlreadyHashed> IdentifierTable;
unsigned HTMLIdentifier::maxNameLength = 0;
static IdentifierTable& identifierTable()
{
DEFINE_STATIC_LOCAL(IdentifierTable, table, ());
ASSERT(isMainThread() || !table.isEmpty());
return table;
}
#ifndef NDEBUG
bool HTMLIdentifier::hasIndex(const StringImpl* string)
{
const IdentifierTable& table = identifierTable();
return table.contains(string->hash());
}
#endif
unsigned HTMLIdentifier::findIndex(const UChar* characters, unsigned length)
{
if (length > maxNameLength)
return invalidIndex;
unsigned hash = StringHasher::computeHashAndMaskTop8Bits(characters, length);
const IdentifierTable& table = identifierTable();
ASSERT(!table.isEmpty());
IdentifierTable::const_iterator it = table.find(hash);
if (it == table.end())
return invalidIndex;
if (!equal(it->value.second, characters, length))
return invalidIndex;
return it->value.first;
}
const unsigned kHTMLNamesIndexOffset = 0;
const unsigned kHTMLAttrsIndexOffset = 1000;
COMPILE_ASSERT(kHTMLAttrsIndexOffset > HTMLTagsCount, kHTMLAttrsIndexOffset_should_be_larger_than_HTMLTagsCount);
static const String& nameForIndex(unsigned index)
{
unsigned adjustedIndex;
QualifiedName** names;
if (index < kHTMLAttrsIndexOffset) {
ASSERT(index < kHTMLNamesIndexOffset + HTMLTagsCount);
adjustedIndex = index - kHTMLNamesIndexOffset;
names = getHTMLTags();
} else {
ASSERT(index < kHTMLAttrsIndexOffset + HTMLAttrsCount);
adjustedIndex = index - kHTMLAttrsIndexOffset;
names = getHTMLAttrs();
}
return names[adjustedIndex]->localName().string();
}
const String& HTMLIdentifier::asString() const
{
ASSERT(isMainThread());
if (m_index != invalidIndex)
return nameForIndex(m_index);
return m_string;
}
const StringImpl* HTMLIdentifier::asStringImpl() const
{
if (m_index != invalidIndex)
return nameForIndex(m_index).impl();
return m_string.impl();
}
void HTMLIdentifier::addNames(QualifiedName** names, unsigned namesCount, unsigned indexOffset)
{
IdentifierTable& table = identifierTable();
for (unsigned i = 0; i < namesCount; ++i) {
StringImpl* name = names[i]->localName().impl();
unsigned hash = name->hash();
unsigned index = i + indexOffset;
IdentifierEntry entry(index, name);
IdentifierTable::AddResult addResult = table.add(hash, entry);
maxNameLength = std::max(maxNameLength, name->length());
ASSERT_UNUSED(addResult, !addResult.isNewEntry || HTMLIdentifier::findIndex(name->characters(), name->length()) == index);
ASSERT_UNUSED(addResult, !addResult.isNewEntry || name == addResult.iterator->value.second);
}
}
void HTMLIdentifier::init()
{
ASSERT(isMainThread()); static bool isInitialized = false;
if (isInitialized)
return;
isInitialized = true;
addNames(getHTMLTags(), HTMLTagsCount, kHTMLNamesIndexOffset);
addNames(getHTMLAttrs(), HTMLAttrsCount, kHTMLAttrsIndexOffset);
}
}
#endif // ENABLE(THREADED_HTML_PARSER)