|
1 /* |
|
2 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. |
|
3 * |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Library General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 * Library General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Library General Public License |
|
15 * along with this library; see the file COPYING.LIB. If not, write to |
|
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
17 * Boston, MA 02110-1301, USA. |
|
18 * |
|
19 */ |
|
20 |
|
21 #ifndef AtomicString_h |
|
22 #define AtomicString_h |
|
23 |
|
24 #include "AtomicStringImpl.h" |
|
25 #include "WTFString.h" |
|
26 |
|
27 // Define 'NO_IMPLICIT_ATOMICSTRING' before including this header, |
|
28 // to disallow (expensive) implicit String-->AtomicString conversions. |
|
29 #ifdef NO_IMPLICIT_ATOMICSTRING |
|
30 #define ATOMICSTRING_CONVERSION explicit |
|
31 #else |
|
32 #define ATOMICSTRING_CONVERSION |
|
33 #endif |
|
34 |
|
35 // FIXME: This is a temporary layering violation while we move string code to WTF. |
|
36 // Landing the file moves in one patch, will follow on with patches to change the namespaces. |
|
37 namespace WebCore { |
|
38 |
|
39 struct AtomicStringHash; |
|
40 |
|
41 class AtomicString { |
|
42 public: |
|
43 static void init(); |
|
44 |
|
45 AtomicString() { } |
|
46 AtomicString(const char* s) : m_string(add(s)) { } |
|
47 AtomicString(const UChar* s, unsigned length) : m_string(add(s, length)) { } |
|
48 AtomicString(const UChar* s, unsigned length, unsigned existingHash) : m_string(add(s, length, existingHash)) { } |
|
49 AtomicString(const UChar* s) : m_string(add(s)) { } |
|
50 ATOMICSTRING_CONVERSION AtomicString(StringImpl* imp) : m_string(add(imp)) { } |
|
51 AtomicString(AtomicStringImpl* imp) : m_string(imp) { } |
|
52 ATOMICSTRING_CONVERSION AtomicString(const String& s) : m_string(add(s.impl())) { } |
|
53 |
|
54 // Hash table deleted values, which are only constructed and never copied or destroyed. |
|
55 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { } |
|
56 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); } |
|
57 |
|
58 static AtomicStringImpl* find(const UChar* s, unsigned length, unsigned existingHash); |
|
59 |
|
60 operator const String&() const { return m_string; } |
|
61 const String& string() const { return m_string; }; |
|
62 |
|
63 AtomicStringImpl* impl() const { return static_cast<AtomicStringImpl *>(m_string.impl()); } |
|
64 |
|
65 const UChar* characters() const { return m_string.characters(); } |
|
66 unsigned length() const { return m_string.length(); } |
|
67 |
|
68 UChar operator[](unsigned int i) const { return m_string[i]; } |
|
69 |
|
70 bool contains(UChar c) const { return m_string.contains(c); } |
|
71 bool contains(const char* s, bool caseSensitive = true) const |
|
72 { return m_string.contains(s, caseSensitive); } |
|
73 bool contains(const String& s, bool caseSensitive = true) const |
|
74 { return m_string.contains(s, caseSensitive); } |
|
75 |
|
76 int find(UChar c, int start = 0) const { return m_string.find(c, start); } |
|
77 int find(const char* s, int start = 0, bool caseSentitive = true) const |
|
78 { return m_string.find(s, start, caseSentitive); } |
|
79 int find(const String& s, int start = 0, bool caseSentitive = true) const |
|
80 { return m_string.find(s, start, caseSentitive); } |
|
81 |
|
82 bool startsWith(const String& s, bool caseSensitive = true) const |
|
83 { return m_string.startsWith(s, caseSensitive); } |
|
84 bool endsWith(const String& s, bool caseSensitive = true) const |
|
85 { return m_string.endsWith(s, caseSensitive); } |
|
86 |
|
87 AtomicString lower() const; |
|
88 AtomicString upper() const { return AtomicString(impl()->upper()); } |
|
89 |
|
90 int toInt(bool* ok = 0) const { return m_string.toInt(ok); } |
|
91 double toDouble(bool* ok = 0) const { return m_string.toDouble(ok); } |
|
92 float toFloat(bool* ok = 0) const { return m_string.toFloat(ok); } |
|
93 bool percentage(int& p) const { return m_string.percentage(p); } |
|
94 |
|
95 bool isNull() const { return m_string.isNull(); } |
|
96 bool isEmpty() const { return m_string.isEmpty(); } |
|
97 |
|
98 static void remove(StringImpl*); |
|
99 |
|
100 #if PLATFORM(CF) |
|
101 AtomicString(CFStringRef s) : m_string(add(String(s).impl())) { } |
|
102 CFStringRef createCFString() const { return m_string.createCFString(); } |
|
103 #endif |
|
104 #ifdef __OBJC__ |
|
105 AtomicString(NSString* s) : m_string(add(String(s).impl())) { } |
|
106 operator NSString*() const { return m_string; } |
|
107 #endif |
|
108 #if PLATFORM(QT) |
|
109 AtomicString(const QString& s) : m_string(add(String(s).impl())) { } |
|
110 operator QString() const { return m_string; } |
|
111 #endif |
|
112 |
|
113 |
|
114 static const AtomicString& nullAtom2(); |
|
115 static const AtomicString& emptyAtom2(); |
|
116 static const AtomicString& textAtom2(); |
|
117 static const AtomicString& commentAtom2(); |
|
118 static const AtomicString& starAtom2(); |
|
119 static const AtomicString& xmlAtom2(); |
|
120 static const AtomicString& xmlnsAtom2(); |
|
121 |
|
122 |
|
123 private: |
|
124 String m_string; |
|
125 |
|
126 static PassRefPtr<StringImpl> add(const char*); |
|
127 static PassRefPtr<StringImpl> add(const UChar*, unsigned length); |
|
128 static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned existingHash); |
|
129 static PassRefPtr<StringImpl> add(const UChar*); |
|
130 ALWAYS_INLINE PassRefPtr<StringImpl> add(StringImpl* r) |
|
131 { |
|
132 if (!r || r->isAtomic()) |
|
133 return r; |
|
134 return addSlowCase(r); |
|
135 } |
|
136 static PassRefPtr<StringImpl> addSlowCase(StringImpl*); |
|
137 }; |
|
138 |
|
139 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); } |
|
140 bool operator==(const AtomicString& a, const char* b); |
|
141 inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); } |
|
142 inline bool operator==(const char* a, const AtomicString& b) { return b == a; } |
|
143 inline bool operator==(const String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); } |
|
144 |
|
145 inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); } |
|
146 inline bool operator!=(const AtomicString& a, const char *b) { return !(a == b); } |
|
147 inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); } |
|
148 inline bool operator!=(const char* a, const AtomicString& b) { return !(b == a); } |
|
149 inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); } |
|
150 |
|
151 inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } |
|
152 inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), b); } |
|
153 inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); } |
|
154 inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); } |
|
155 inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } |
|
156 |
|
157 // Define external global variables for the commonly used atomic strings. |
|
158 // These are only usable from the main thread. |
|
159 #ifndef ATOMICSTRING_HIDE_GLOBALS |
|
160 extern const JS_EXPORTDATA AtomicString nullAtom1; |
|
161 extern const JS_EXPORTDATA AtomicString emptyAtom1; |
|
162 extern const JS_EXPORTDATA AtomicString textAtom1; |
|
163 extern const JS_EXPORTDATA AtomicString commentAtom1; |
|
164 extern const JS_EXPORTDATA AtomicString starAtom1; |
|
165 extern const JS_EXPORTDATA AtomicString xmlAtom1; |
|
166 extern const JS_EXPORTDATA AtomicString xmlnsAtom1; |
|
167 |
|
168 #define nullAtom AtomicString::nullAtom2() |
|
169 #define emptyAtom AtomicString::emptyAtom2() |
|
170 #define textAtom AtomicString::textAtom2() |
|
171 #define commentAtom AtomicString::commentAtom2() |
|
172 #define starAtom AtomicString::starAtom2() |
|
173 #define xmlAtom AtomicString::xmlAtom2() |
|
174 #define xmlnsAtom AtomicString::xmlnsAtom2() |
|
175 |
|
176 |
|
177 |
|
178 #endif |
|
179 |
|
180 } // namespace WebCore |
|
181 |
|
182 |
|
183 namespace WTF { |
|
184 |
|
185 // AtomicStringHash is the default hash for AtomicString |
|
186 template<typename T> struct DefaultHash; |
|
187 template<> struct DefaultHash<WebCore::AtomicString> { |
|
188 typedef WebCore::AtomicStringHash Hash; |
|
189 }; |
|
190 |
|
191 } // namespace WTF |
|
192 |
|
193 #endif // AtomicString_h |