weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 David Smith (catfish.man@gmail.com) |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008, 2011, 2012 Apple Inc. All rights reserved. |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public License |
| 16 | * along with this library; see the file COPYING.LIB. If not, write to |
| 17 | * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 | * Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
japhet@chromium.org | 8e9f23f | 2009-12-09 17:43:16 +0000 | [diff] [blame] | 22 | #include "SpaceSplitString.h" |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 23 | |
darin@apple.com | 7156d95 | 2010-10-04 22:41:47 +0000 | [diff] [blame] | 24 | #include "HTMLParserIdioms.h" |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 25 | #include <wtf/ASCIICType.h> |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 26 | #include <wtf/HashMap.h> |
| 27 | #include <wtf/text/AtomicStringHash.h> |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 28 | #include <wtf/text/StringBuilder.h> |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace WTF; |
| 31 | |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 32 | namespace WebCore { |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 33 | |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 34 | template <typename CharacterType> |
| 35 | static inline bool hasNonASCIIOrUpper(const CharacterType* characters, unsigned length) |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 36 | { |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 37 | bool hasUpper = false; |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 38 | CharacterType ored = 0; |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 39 | for (unsigned i = 0; i < length; i++) { |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 40 | CharacterType c = characters[i]; |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 41 | hasUpper |= isASCIIUpper(c); |
| 42 | ored |= c; |
| 43 | } |
| 44 | return hasUpper || (ored & ~0x7F); |
| 45 | } |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 46 | |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 47 | static inline bool hasNonASCIIOrUpper(const String& string) |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 48 | { |
kling@webkit.org | 068119f | 2011-12-29 03:54:44 +0000 | [diff] [blame] | 49 | unsigned length = string.length(); |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 50 | |
| 51 | if (string.is8Bit()) |
| 52 | return hasNonASCIIOrUpper(string.characters8(), length); |
| 53 | return hasNonASCIIOrUpper(string.characters16(), length); |
| 54 | } |
| 55 | |
benjamin@webkit.org | 6534a8f | 2013-07-22 22:23:14 +0000 | [diff] [blame] | 56 | template <typename CharacterType, typename TokenProcessor> |
| 57 | static inline void tokenizeSpaceSplitString(TokenProcessor& tokenProcessor, const CharacterType* characters, unsigned length) |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 58 | { |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 59 | unsigned start = 0; |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 60 | while (true) { |
darin@apple.com | 7156d95 | 2010-10-04 22:41:47 +0000 | [diff] [blame] | 61 | while (start < length && isHTMLSpace(characters[start])) |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 62 | ++start; |
| 63 | if (start >= length) |
| 64 | break; |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 65 | unsigned end = start + 1; |
darin@apple.com | 7156d95 | 2010-10-04 22:41:47 +0000 | [diff] [blame] | 66 | while (end < length && isNotHTMLSpace(characters[end])) |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 67 | ++end; |
| 68 | |
benjamin@webkit.org | 6534a8f | 2013-07-22 22:23:14 +0000 | [diff] [blame] | 69 | if (!tokenProcessor.processToken(characters + start, end - start)) |
| 70 | return; |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 71 | |
| 72 | start = end + 1; |
| 73 | } |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 74 | } |
| 75 | |
benjamin@webkit.org | 6534a8f | 2013-07-22 22:23:14 +0000 | [diff] [blame] | 76 | class AppendTokenToVectorTokenProcessor { |
| 77 | public: |
| 78 | AppendTokenToVectorTokenProcessor(Vector<AtomicString, 4>& vector) : m_vector(vector) { } |
| 79 | |
| 80 | template <typename CharacterType> |
| 81 | bool processToken(const CharacterType* characters, unsigned length) |
| 82 | { |
| 83 | m_vector.append(AtomicString(characters, length)); |
| 84 | return true; |
| 85 | } |
| 86 | private: |
| 87 | Vector<AtomicString, 4>& m_vector; |
| 88 | }; |
| 89 | |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 90 | void SpaceSplitStringData::createVector(const String& string) |
| 91 | { |
| 92 | unsigned length = string.length(); |
| 93 | |
benjamin@webkit.org | 6534a8f | 2013-07-22 22:23:14 +0000 | [diff] [blame] | 94 | AppendTokenToVectorTokenProcessor tokenProcessor(m_vector); |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 95 | if (string.is8Bit()) { |
benjamin@webkit.org | 6534a8f | 2013-07-22 22:23:14 +0000 | [diff] [blame] | 96 | tokenizeSpaceSplitString(tokenProcessor, string.characters8(), length); |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
benjamin@webkit.org | 6534a8f | 2013-07-22 22:23:14 +0000 | [diff] [blame] | 100 | tokenizeSpaceSplitString(tokenProcessor, string.characters16(), length); |
msaboff@apple.com | b2b6746 | 2012-09-15 22:48:31 +0000 | [diff] [blame] | 101 | } |
| 102 | |
japhet@chromium.org | 8e9f23f | 2009-12-09 17:43:16 +0000 | [diff] [blame] | 103 | bool SpaceSplitStringData::containsAll(SpaceSplitStringData& other) |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 104 | { |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 105 | if (this == &other) |
| 106 | return true; |
| 107 | |
darin@apple.com | e6e59e7 | 2008-03-29 17:51:50 +0000 | [diff] [blame] | 108 | size_t thisSize = m_vector.size(); |
| 109 | size_t otherSize = other.m_vector.size(); |
| 110 | for (size_t i = 0; i < otherSize; ++i) { |
| 111 | const AtomicString& name = other.m_vector[i]; |
| 112 | size_t j; |
| 113 | for (j = 0; j < thisSize; ++j) { |
| 114 | if (m_vector[j] == name) |
| 115 | break; |
| 116 | } |
| 117 | if (j == thisSize) |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 121 | } |
| 122 | |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 123 | void SpaceSplitStringData::add(const AtomicString& string) |
| 124 | { |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 125 | ASSERT(hasOneRef()); |
antti@apple.com | 2b48571 | 2012-10-30 20:25:52 +0000 | [diff] [blame] | 126 | ASSERT(!contains(string)); |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 127 | m_vector.append(string); |
| 128 | } |
| 129 | |
antti@apple.com | 2b48571 | 2012-10-30 20:25:52 +0000 | [diff] [blame] | 130 | void SpaceSplitStringData::remove(unsigned index) |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 131 | { |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 132 | ASSERT(hasOneRef()); |
antti@apple.com | 2b48571 | 2012-10-30 20:25:52 +0000 | [diff] [blame] | 133 | m_vector.remove(index); |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void SpaceSplitString::add(const AtomicString& string) |
| 137 | { |
antti@apple.com | 2b48571 | 2012-10-30 20:25:52 +0000 | [diff] [blame] | 138 | // FIXME: add() does not allow duplicates but createVector() does. |
| 139 | if (contains(string)) |
| 140 | return; |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 141 | ensureUnique(); |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 142 | if (m_data) |
| 143 | m_data->add(string); |
| 144 | } |
| 145 | |
antti@apple.com | 2b48571 | 2012-10-30 20:25:52 +0000 | [diff] [blame] | 146 | bool SpaceSplitString::remove(const AtomicString& string) |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 147 | { |
antti@apple.com | 2b48571 | 2012-10-30 20:25:52 +0000 | [diff] [blame] | 148 | if (!m_data) |
| 149 | return false; |
| 150 | unsigned i = 0; |
| 151 | bool changed = false; |
| 152 | while (i < m_data->size()) { |
| 153 | if ((*m_data)[i] == string) { |
| 154 | if (!changed) |
| 155 | ensureUnique(); |
| 156 | m_data->remove(i); |
| 157 | changed = true; |
| 158 | continue; |
| 159 | } |
| 160 | ++i; |
| 161 | } |
| 162 | return changed; |
commit-queue@webkit.org | 691d04f | 2010-10-29 09:32:14 +0000 | [diff] [blame] | 163 | } |
| 164 | |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 165 | typedef HashMap<AtomicString, SpaceSplitStringData*> SpaceSplitStringDataMap; |
| 166 | |
| 167 | static SpaceSplitStringDataMap& sharedDataMap() |
| 168 | { |
| 169 | DEFINE_STATIC_LOCAL(SpaceSplitStringDataMap, map, ()); |
| 170 | return map; |
| 171 | } |
| 172 | |
| 173 | void SpaceSplitString::set(const AtomicString& inputString, bool shouldFoldCase) |
| 174 | { |
| 175 | if (inputString.isNull()) { |
| 176 | clear(); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | String string(inputString.string()); |
| 181 | if (shouldFoldCase && hasNonASCIIOrUpper(string)) |
| 182 | string = string.foldCase(); |
| 183 | |
| 184 | m_data = SpaceSplitStringData::create(string); |
| 185 | } |
| 186 | |
benjamin@webkit.org | 6534a8f | 2013-07-22 22:23:14 +0000 | [diff] [blame] | 187 | class TokenIsEqualToCStringTokenProcessor { |
| 188 | public: |
| 189 | TokenIsEqualToCStringTokenProcessor(const char* referenceString, unsigned referenceStringLength) |
| 190 | : m_referenceString(referenceString) |
| 191 | , m_referenceStringLength(referenceStringLength) |
| 192 | , m_referenceStringWasFound(false) |
| 193 | { |
| 194 | } |
| 195 | |
| 196 | template <typename CharacterType> |
| 197 | bool processToken(const CharacterType* characters, unsigned length) |
| 198 | { |
| 199 | if (length == m_referenceStringLength && equal(characters, reinterpret_cast<const LChar*>(m_referenceString), length)) { |
| 200 | m_referenceStringWasFound = true; |
| 201 | return false; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | bool referenceStringWasFound() const { return m_referenceStringWasFound; } |
| 207 | |
| 208 | private: |
| 209 | const char* m_referenceString; |
| 210 | unsigned m_referenceStringLength; |
| 211 | bool m_referenceStringWasFound; |
| 212 | }; |
| 213 | |
| 214 | bool SpaceSplitString::spaceSplitStringContainsValue(const String& inputString, const char* value, unsigned valueLength, bool shouldFoldCase) |
| 215 | { |
| 216 | String string = inputString; |
| 217 | if (shouldFoldCase && hasNonASCIIOrUpper(string)) |
| 218 | string = string.foldCase(); |
| 219 | |
| 220 | TokenIsEqualToCStringTokenProcessor tokenProcessor(value, valueLength); |
| 221 | unsigned length = string.length(); |
| 222 | if (string.is8Bit()) |
| 223 | tokenizeSpaceSplitString(tokenProcessor, string.characters8(), length); |
| 224 | else |
| 225 | tokenizeSpaceSplitString(tokenProcessor, string.characters16(), length); |
| 226 | return tokenProcessor.referenceStringWasFound(); |
| 227 | } |
| 228 | |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 229 | SpaceSplitStringData::~SpaceSplitStringData() |
| 230 | { |
| 231 | if (!m_keyString.isNull()) |
| 232 | sharedDataMap().remove(m_keyString); |
| 233 | } |
| 234 | |
| 235 | PassRefPtr<SpaceSplitStringData> SpaceSplitStringData::create(const AtomicString& string) |
| 236 | { |
benjamin@webkit.org | ee55405 | 2012-10-07 23:12:07 +0000 | [diff] [blame] | 237 | SpaceSplitStringData*& data = sharedDataMap().add(string, 0).iterator->value; |
kling@webkit.org | c1f3c39 | 2012-01-17 20:57:23 +0000 | [diff] [blame] | 238 | if (!data) { |
| 239 | data = new SpaceSplitStringData(string); |
| 240 | return adoptRef(data); |
| 241 | } |
| 242 | return data; |
| 243 | } |
| 244 | |
| 245 | PassRefPtr<SpaceSplitStringData> SpaceSplitStringData::createUnique(const SpaceSplitStringData& other) |
| 246 | { |
| 247 | return adoptRef(new SpaceSplitStringData(other)); |
| 248 | } |
| 249 | |
| 250 | SpaceSplitStringData::SpaceSplitStringData(const AtomicString& string) |
| 251 | : m_keyString(string) |
| 252 | { |
| 253 | ASSERT(!string.isNull()); |
| 254 | createVector(string); |
| 255 | } |
| 256 | |
| 257 | SpaceSplitStringData::SpaceSplitStringData(const SpaceSplitStringData& other) |
| 258 | : RefCounted<SpaceSplitStringData>() |
| 259 | , m_vector(other.m_vector) |
| 260 | { |
| 261 | // Note that we don't copy m_keyString to indicate to the destructor that there's nothing |
| 262 | // to be removed from the sharedDataMap(). |
| 263 | } |
| 264 | |
weinig@apple.com | 1bed3cb | 2007-12-14 21:48:20 +0000 | [diff] [blame] | 265 | } // namespace WebCore |