1 /*
2 * Copyright (C) 2007 David Smith (catfish.man@gmail.com)
3 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
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"
22 #include "SpaceSplitString.h"
23
24 #include "HTMLParserIdioms.h"
25 #include <wtf/ASCIICType.h>
26 #include <wtf/text/StringBuilder.h>
27
28 using namespace WTF;
29
30 namespace WebCore {
31
hasNonASCIIOrUpper(const String & string)32 static bool hasNonASCIIOrUpper(const String& string)
33 {
34 const UChar* characters = string.characters();
35 unsigned length = string.length();
36 bool hasUpper = false;
37 UChar ored = 0;
38 for (unsigned i = 0; i < length; i++) {
39 UChar c = characters[i];
40 hasUpper |= isASCIIUpper(c);
41 ored |= c;
42 }
43 return hasUpper || (ored & ~0x7F);
44 }
45
createVector()46 void SpaceSplitStringData::createVector()
47 {
48 ASSERT(!m_createdVector);
49 ASSERT(m_vector.isEmpty());
50
51 if (m_shouldFoldCase && hasNonASCIIOrUpper(m_string))
52 m_string = m_string.foldCase();
53
54 const UChar* characters = m_string.characters();
55 unsigned length = m_string.length();
56 unsigned start = 0;
57 while (true) {
58 while (start < length && isHTMLSpace(characters[start]))
59 ++start;
60 if (start >= length)
61 break;
62 unsigned end = start + 1;
63 while (end < length && isNotHTMLSpace(characters[end]))
64 ++end;
65
66 m_vector.append(AtomicString(characters + start, end - start));
67
68 start = end + 1;
69 }
70
71 m_string = String();
72 m_createdVector = true;
73 }
74
containsAll(SpaceSplitStringData & other)75 bool SpaceSplitStringData::containsAll(SpaceSplitStringData& other)
76 {
77 ensureVector();
78 other.ensureVector();
79 size_t thisSize = m_vector.size();
80 size_t otherSize = other.m_vector.size();
81 for (size_t i = 0; i < otherSize; ++i) {
82 const AtomicString& name = other.m_vector[i];
83 size_t j;
84 for (j = 0; j < thisSize; ++j) {
85 if (m_vector[j] == name)
86 break;
87 }
88 if (j == thisSize)
89 return false;
90 }
91 return true;
92 }
93
add(const AtomicString & string)94 void SpaceSplitStringData::add(const AtomicString& string)
95 {
96 if (contains(string))
97 return;
98
99 m_vector.append(string);
100 }
101
remove(const AtomicString & string)102 void SpaceSplitStringData::remove(const AtomicString& string)
103 {
104 ensureVector();
105
106 size_t position = 0;
107 while (position < m_vector.size()) {
108 if (m_vector[position] == string)
109 m_vector.remove(position);
110 else
111 ++position;
112 }
113 }
114
add(const AtomicString & string)115 void SpaceSplitString::add(const AtomicString& string)
116 {
117 if (m_data)
118 m_data->add(string);
119 }
120
remove(const AtomicString & string)121 void SpaceSplitString::remove(const AtomicString& string)
122 {
123 if (m_data)
124 m_data->remove(string);
125 }
126
127 } // namespace WebCore
128