OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. | 2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 28 matching lines...) Expand all Loading... | |
39 | 39 |
40 unsigned HTMLIdentifier::maxNameLength = 0; | 40 unsigned HTMLIdentifier::maxNameLength = 0; |
41 | 41 |
42 static IdentifierTable& identifierTable() | 42 static IdentifierTable& identifierTable() |
43 { | 43 { |
44 DEFINE_STATIC_LOCAL(IdentifierTable, table, ()); | 44 DEFINE_STATIC_LOCAL(IdentifierTable, table, ()); |
45 ASSERT(isMainThread() || !table.isEmpty()); | 45 ASSERT(isMainThread() || !table.isEmpty()); |
46 return table; | 46 return table; |
47 } | 47 } |
48 | 48 |
49 #ifndef NDEBUG | |
50 bool HTMLIdentifier::isKnown(const StringImpl* string) | |
51 { | |
52 const IdentifierTable& table = identifierTable(); | |
53 return table.contains(string->hash()); | |
54 } | |
55 #endif | |
56 | |
57 StringImpl* HTMLIdentifier::findIfKnown(const UChar* characters, unsigned length ) | 49 StringImpl* HTMLIdentifier::findIfKnown(const UChar* characters, unsigned length ) |
58 { | 50 { |
59 // We don't need to try hashing if we know the string is too long. | 51 // We don't need to try hashing if we know the string is too long. |
60 if (length > maxNameLength) | 52 if (length > maxNameLength) |
61 return 0; | 53 return 0; |
62 // computeHashAndMaskTop8Bits is the function StringImpl::hash() uses. | 54 // computeHashAndMaskTop8Bits is the function StringImpl::hash() uses. |
63 unsigned hash = StringHasher::computeHashAndMaskTop8Bits(characters, length) ; | 55 unsigned hash = StringHasher::computeHashAndMaskTop8Bits(characters, length) ; |
64 const IdentifierTable& table = identifierTable(); | 56 const IdentifierTable& table = identifierTable(); |
65 ASSERT(!table.isEmpty()); | 57 ASSERT(!table.isEmpty()); |
66 | 58 |
67 IdentifierTable::const_iterator it = table.find(hash); | 59 IdentifierTable::const_iterator it = table.find(hash); |
68 if (it == table.end()) | 60 if (it == table.end()) |
69 return 0; | 61 return 0; |
70 // It's possible to have hash collisions between arbitrary strings and | 62 // It's possible to have hash collisions between arbitrary strings and |
71 // known identifiers (e.g. "bvvfg" collides with "script"). | 63 // known identifiers (e.g. "bvvfg" collides with "script"). |
72 // However ASSERTs in addNames() guard against there ever being collisions | 64 // However ASSERTs in addNames() guard against there ever being collisions |
73 // between known identifiers. | 65 // between known identifiers. |
74 if (!equal(it->value, characters, length)) | 66 if (!equal(it->value, characters, length)) |
75 return 0; | 67 return 0; |
76 return it->value; | 68 return it->value; |
77 } | 69 } |
78 | 70 |
79 const String& HTMLIdentifier::asString() const | |
80 { | |
81 ASSERT(isMainThread()); | |
82 return m_string; | |
83 } | |
84 | |
85 const StringImpl* HTMLIdentifier::asStringImpl() const | |
86 { | |
87 return m_string.impl(); | |
88 } | |
89 | |
90 void HTMLIdentifier::addNames(const QualifiedName* const* names, unsigned namesC ount) | 71 void HTMLIdentifier::addNames(const QualifiedName* const* names, unsigned namesC ount) |
91 { | 72 { |
92 IdentifierTable& table = identifierTable(); | 73 IdentifierTable& table = identifierTable(); |
93 for (unsigned i = 0; i < namesCount; ++i) { | 74 for (unsigned i = 0; i < namesCount; ++i) { |
94 StringImpl* name = names[i]->localName().impl(); | 75 StringImpl* name = names[i]->localName().impl(); |
76 ASSERT(name->isStatic()); | |
95 unsigned hash = name->hash(); | 77 unsigned hash = name->hash(); |
96 IdentifierTable::AddResult addResult = table.add(hash, name); | 78 IdentifierTable::AddResult addResult = table.add(hash, name); |
97 maxNameLength = std::max(maxNameLength, name->length()); | 79 maxNameLength = std::max(maxNameLength, name->length()); |
98 // Ensure we're using the same hashing algorithm to get and set. | 80 // Ensure we're using the same hashing algorithm to get and set. |
99 ASSERT_UNUSED(addResult, !addResult.isNewEntry || HTMLIdentifier::findIf Known(String(name).charactersWithNullTermination().data(), name->length()) == na me); | 81 ASSERT_UNUSED(addResult, !addResult.isNewEntry || HTMLIdentifier::findIf Known(String(name).charactersWithNullTermination().data(), name->length()) == na me); |
100 // We expect some hash collisions, but only for identical strings. | 82 // We expect some hash collisions, but only for identical strings. |
101 // Since all of these names are AtomicStrings pointers should be equal. | 83 // Since all of these names are AtomicStrings pointers should be equal. |
102 // Note: If you hit this ASSERT, then we had a hash collision among | 84 // Note: If you hit this ASSERT, then we had a hash collision among |
103 // HTMLNames strings, and we need to re-design how we use this hash! | 85 // HTMLNames strings, and we need to re-design how we use this hash! |
104 ASSERT_UNUSED(addResult, !addResult.isNewEntry || name == addResult.iter ator->value); | 86 ASSERT_UNUSED(addResult, !addResult.isNewEntry || name == addResult.iter ator->value); |
105 } | 87 } |
106 } | 88 } |
107 | 89 |
108 void HTMLIdentifier::init() | 90 void HTMLIdentifier::init() |
109 { | 91 { |
110 ASSERT(isMainThread()); // Not technically necessary, but this is our curren t expected usage. | 92 ASSERT(isMainThread()); // Not technically necessary, but this is our curren t expected usage. |
111 static bool isInitialized = false; | 93 static bool isInitialized = false; |
112 if (isInitialized) | 94 if (isInitialized) |
113 return; | 95 return; |
114 isInitialized = true; | 96 isInitialized = true; |
115 | 97 |
116 // FIXME: We should atomize small whitespace (\n, \n\n, etc.) | 98 // FIXME: We should atomize small whitespace (\n, \n\n, etc.) |
117 addNames(getHTMLTags(), HTMLTagsCount); | 99 addNames(getHTMLTags(), HTMLTagsCount); |
118 addNames(getHTMLAttrs(), HTMLAttrsCount); | 100 addNames(getHTMLAttrs(), HTMLAttrsCount); |
abarth-chromium
2013/12/10 01:22:52
This CL is fine as a stepping stone, but we don't
| |
119 } | 101 } |
120 | 102 |
121 } | 103 } |
OLD | NEW |