2 * Copyright (C) 2007 David Smith (catfish.man@gmail.com)
3 * Copyright (C) 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
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.
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.
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.
22 #include "core/dom/SpaceSplitString.h"
24 #include "core/html/parser/HTMLParserIdioms.h"
25 #include "wtf/ASCIICType.h"
26 #include "wtf/HashMap.h"
27 #include "wtf/text/AtomicStringHash.h"
33 template <typename CharacterType>
34 static inline bool hasNonASCIIOrUpper(const CharacterType* characters, unsigned length)
36 bool hasUpper = false;
37 CharacterType ored = 0;
38 for (unsigned i = 0; i < length; i++) {
39 CharacterType c = characters[i];
40 hasUpper |= isASCIIUpper(c);
43 return hasUpper || (ored & ~0x7F);
46 static inline bool hasNonASCIIOrUpper(const String& string)
48 unsigned length = string.length();
51 return hasNonASCIIOrUpper(string.characters8(), length);
52 return hasNonASCIIOrUpper(string.characters16(), length);
55 template <typename CharacterType>
56 inline void SpaceSplitString::Data::createVector(const CharacterType* characters, unsigned length)
60 while (start < length && isHTMLSpace<CharacterType>(characters[start]))
64 unsigned end = start + 1;
65 while (end < length && isNotHTMLSpace<CharacterType>(characters[end]))
68 m_vector.append(AtomicString(characters + start, end - start));
74 void SpaceSplitString::Data::createVector(const String& string)
76 unsigned length = string.length();
78 if (string.is8Bit()) {
79 createVector(string.characters8(), length);
83 createVector(string.characters16(), length);
86 bool SpaceSplitString::Data::containsAll(Data& other)
91 size_t thisSize = m_vector.size();
92 size_t otherSize = other.m_vector.size();
93 for (size_t i = 0; i < otherSize; ++i) {
94 const AtomicString& name = other.m_vector[i];
96 for (j = 0; j < thisSize; ++j) {
97 if (m_vector[j] == name)
106 void SpaceSplitString::Data::add(const AtomicString& string)
109 ASSERT(!contains(string));
110 m_vector.append(string);
113 void SpaceSplitString::Data::remove(unsigned index)
116 m_vector.remove(index);
119 void SpaceSplitString::add(const AtomicString& string)
121 // FIXME: add() does not allow duplicates but createVector() does.
122 if (contains(string))
129 bool SpaceSplitString::remove(const AtomicString& string)
134 bool changed = false;
135 while (i < m_data->size()) {
136 if ((*m_data)[i] == string) {
148 SpaceSplitString::DataMap& SpaceSplitString::sharedDataMap()
150 DEFINE_STATIC_LOCAL(DataMap, map, ());
154 void SpaceSplitString::set(const AtomicString& inputString, bool shouldFoldCase)
156 if (inputString.isNull()) {
161 String string(inputString.string());
162 if (shouldFoldCase && hasNonASCIIOrUpper(string))
163 string = string.foldCase();
165 m_data = Data::create(AtomicString(string));
168 SpaceSplitString::Data::~Data()
170 if (!m_keyString.isNull())
171 sharedDataMap().remove(m_keyString);
174 PassRefPtr<SpaceSplitString::Data> SpaceSplitString::Data::create(const AtomicString& string)
176 Data*& data = sharedDataMap().add(string, 0).storedValue->value;
178 data = new Data(string);
179 return adoptRef(data);
184 PassRefPtr<SpaceSplitString::Data> SpaceSplitString::Data::createUnique(const Data& other)
186 return adoptRef(new SpaceSplitString::Data(other));
189 SpaceSplitString::Data::Data(const AtomicString& string)
190 : m_keyString(string)
192 ASSERT(!string.isNull());
193 createVector(string);
196 SpaceSplitString::Data::Data(const SpaceSplitString::Data& other)
198 , m_vector(other.m_vector)
200 // Note that we don't copy m_keyString to indicate to the destructor that there's nothing
201 // to be removed from the sharedDataMap().