|
Line 0
a/Source/WebCore/html/parser/HTMLIdentifier.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2013 Google, Inc. All Rights Reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY |
| 14 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR |
| 17 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#if ENABLE(THREADED_HTML_PARSER) |
| 29 |
|
| 30 |
#include "HTMLIdentifier.h" |
| 31 |
|
| 32 |
#include "HTMLNames.h" |
| 33 |
#include <wtf/HashMap.h> |
| 34 |
#include <wtf/MainThread.h> |
| 35 |
#include <wtf/text/StringHash.h> |
| 36 |
|
| 37 |
namespace WebCore { |
| 38 |
|
| 39 |
using namespace HTMLNames; |
| 40 |
|
| 41 |
static const unsigned invalidIndex = -1; |
| 42 |
|
| 43 |
typedef std::pair<unsigned, StringImpl*> IdentifierEntry; |
| 44 |
typedef HashMap<unsigned, Vector<IdentifierEntry>, AlreadyHashed> IdentifierTable; |
| 45 |
|
| 46 |
static IdentifierTable& identifierTable() |
| 47 |
{ |
| 48 |
DEFINE_STATIC_LOCAL(IdentifierTable, table, ()); |
| 49 |
return table; |
| 50 |
} |
| 51 |
|
| 52 |
static unsigned findIndex(const UChar* characters, unsigned length) |
| 53 |
{ |
| 54 |
unsigned hash = StringHasher::computeHash(characters, length); |
| 55 |
const IdentifierTable& table = identifierTable(); |
| 56 |
|
| 57 |
IdentifierTable::const_iterator it = table.find(hash); |
| 58 |
if (it == table.end()) |
| 59 |
return invalidIndex; |
| 60 |
const Vector<IdentifierEntry>& entries = it->value; |
| 61 |
for (size_t i = 0; i < entries.size(); ++i) { |
| 62 |
if (equal(entries[i].second, characters, length)) |
| 63 |
return entries[i].first; |
| 64 |
} |
| 65 |
return invalidIndex; |
| 66 |
} |
| 67 |
|
| 68 |
HTMLIdentifier::HTMLIdentifier(const UChar* characters, unsigned length, bool isAll8BitData) |
| 69 |
: m_index(findIndex(characters, length)) |
| 70 |
{ |
| 71 |
if (m_index != invalidIndex) |
| 72 |
return; |
| 73 |
m_string = isAll8BitData ? String::make8BitFrom16BitSource(characters, length) : String(characters, length); |
| 74 |
} |
| 75 |
|
| 76 |
const String& HTMLIdentifier::asString() const |
| 77 |
{ |
| 78 |
if (m_index != invalidIndex) |
| 79 |
return getHTMLTags()[m_index]->localName(); |
| 80 |
return m_string; |
| 81 |
} |
| 82 |
|
| 83 |
void HTMLIdentifier::init() |
| 84 |
{ |
| 85 |
static bool isInitialized = false; |
| 86 |
if (isInitialized) |
| 87 |
return; |
| 88 |
isInitialized = true; |
| 89 |
|
| 90 |
WebCore::QualifiedName** tags = getHTMLTags(); |
| 91 |
IdentifierTable& table = identifierTable(); |
| 92 |
|
| 93 |
for (unsigned i = 0; i < HTMLTagsCount; ++i) { |
| 94 |
StringImpl* tagName = tags[i]->localName().impl(); |
| 95 |
unsigned hash = tagName->hash(); |
| 96 |
table.get(hash).append(IdentifierEntry(i, tagName)); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
#endif // ENABLE(THREADED_HTML_PARSER) |