Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / text / AtomicString.h
blobe90a664e370ea9bf005d58da335beef49eae28dc
1 /*
2 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
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.
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.
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.
21 #ifndef AtomicString_h
22 #define AtomicString_h
24 #include "wtf/HashTableDeletedValueType.h"
25 #include "wtf/WTFExport.h"
26 #include "wtf/text/CString.h"
27 #include "wtf/text/WTFString.h"
29 namespace WTF {
31 struct AtomicStringHash;
33 class WTF_EXPORT AtomicString {
34 public:
35 static void init();
37 AtomicString() { }
38 AtomicString(const LChar* s) : m_string(add(s)) { }
39 AtomicString(const char* s) : m_string(add(s)) { }
40 AtomicString(const LChar* s, unsigned length) : m_string(add(s, length)) { }
41 AtomicString(const UChar* s, unsigned length) : m_string(add(s, length)) { }
42 AtomicString(const UChar* s, unsigned length, unsigned existingHash) : m_string(add(s, length, existingHash)) { }
43 AtomicString(const UChar* s) : m_string(add(s)) { }
45 template<size_t inlineCapacity>
46 explicit AtomicString(const Vector<UChar, inlineCapacity>& characters)
47 : m_string(add(characters.data(), characters.size()))
51 // Constructing an AtomicString from a String / StringImpl can be expensive if
52 // the StringImpl is not already atomic.
53 explicit AtomicString(StringImpl* impl) : m_string(add(impl)) { }
54 explicit AtomicString(const String& s) : m_string(add(s.impl())) { }
56 AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_string(add(baseString, start, length)) { }
58 enum ConstructFromLiteralTag { ConstructFromLiteral };
59 AtomicString(const char* characters, unsigned length, ConstructFromLiteralTag)
60 : m_string(addFromLiteralData(characters, length))
64 template<unsigned charactersCount>
65 ALWAYS_INLINE AtomicString(const char (&characters)[charactersCount], ConstructFromLiteralTag)
66 : m_string(addFromLiteralData(characters, charactersCount - 1))
68 static_assert(charactersCount > 1, "AtomicString FromLiteralData should not be empty");
69 static_assert((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), "AtomicString FromLiteralData cannot overflow");
72 // Hash table deleted values, which are only constructed and never copied or destroyed.
73 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
74 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); }
76 static StringImpl* find(const StringImpl*);
78 operator const String&() const { return m_string; }
79 const String& string() const { return m_string; }
81 StringImpl* impl() const { return m_string.impl(); }
83 bool is8Bit() const { return m_string.is8Bit(); }
84 const LChar* characters8() const { return m_string.characters8(); }
85 const UChar* characters16() const { return m_string.characters16(); }
86 unsigned length() const { return m_string.length(); }
88 UChar operator[](unsigned i) const { return m_string[i]; }
90 bool contains(UChar c) const { return m_string.contains(c); }
91 bool contains(const LChar* s, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
92 { return m_string.contains(s, caseSensitivity); }
93 bool contains(const String& s, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
94 { return m_string.contains(s, caseSensitivity); }
96 size_t find(UChar c, size_t start = 0) const { return m_string.find(c, start); }
97 size_t find(const LChar* s, size_t start = 0, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
98 { return m_string.find(s, start, caseSensitivity); }
99 size_t find(const String& s, size_t start = 0, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
100 { return m_string.find(s, start, caseSensitivity); }
102 bool startsWith(const String& s, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
103 { return m_string.startsWith(s, caseSensitivity); }
104 bool startsWith(UChar character) const
105 { return m_string.startsWith(character); }
106 template<unsigned matchLength>
107 bool startsWith(const char (&prefix)[matchLength], TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
108 { return m_string.startsWith<matchLength>(prefix, caseSensitivity); }
110 bool endsWith(const String& s, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
111 { return m_string.endsWith(s, caseSensitivity); }
112 bool endsWith(UChar character) const
113 { return m_string.endsWith(character); }
114 template<unsigned matchLength>
115 bool endsWith(const char (&prefix)[matchLength], TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
116 { return m_string.endsWith<matchLength>(prefix, caseSensitivity); }
118 AtomicString lower() const;
119 AtomicString upper() const { return AtomicString(impl()->upper()); }
121 int toInt(bool* ok = 0) const { return m_string.toInt(ok); }
122 double toDouble(bool* ok = 0) const { return m_string.toDouble(ok); }
123 float toFloat(bool* ok = 0) const { return m_string.toFloat(ok); }
125 static AtomicString number(int);
126 static AtomicString number(unsigned);
127 static AtomicString number(long);
128 static AtomicString number(unsigned long);
129 static AtomicString number(long long);
130 static AtomicString number(unsigned long long);
132 static AtomicString number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
134 bool isNull() const { return m_string.isNull(); }
135 bool isEmpty() const { return m_string.isEmpty(); }
137 static void remove(StringImpl*);
139 #if USE(CF)
140 AtomicString(CFStringRef s) : m_string(add(s)) { }
141 #endif
142 #ifdef __OBJC__
143 AtomicString(NSString* s) : m_string(add((CFStringRef)s)) { }
144 operator NSString*() const { return m_string; }
145 #endif
146 // AtomicString::fromUTF8 will return a null string if
147 // the input data contains invalid UTF-8 sequences.
148 static AtomicString fromUTF8(const char*, size_t);
149 static AtomicString fromUTF8(const char*);
151 CString ascii() const { return m_string.ascii(); }
152 CString latin1() const { return m_string.latin1(); }
153 CString utf8(UTF8ConversionMode mode = LenientUTF8Conversion) const { return m_string.utf8(mode); }
155 #ifndef NDEBUG
156 void show() const;
157 #endif
159 private:
160 String m_string;
162 static PassRefPtr<StringImpl> add(const LChar*);
163 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); }
164 static PassRefPtr<StringImpl> add(const LChar*, unsigned length);
165 static PassRefPtr<StringImpl> add(const UChar*, unsigned length);
166 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); }
167 static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned existingHash);
168 static PassRefPtr<StringImpl> add(const UChar*);
169 static PassRefPtr<StringImpl> add(StringImpl*, unsigned offset, unsigned length);
170 ALWAYS_INLINE static PassRefPtr<StringImpl> add(StringImpl* r)
172 if (!r || r->isAtomic())
173 return r;
174 return addSlowCase(r);
176 static PassRefPtr<StringImpl> addFromLiteralData(const char* characters, unsigned length);
177 static PassRefPtr<StringImpl> addSlowCase(StringImpl*);
178 #if USE(CF)
179 static PassRefPtr<StringImpl> add(CFStringRef);
180 #endif
182 static AtomicString fromUTF8Internal(const char*, const char*);
185 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); }
186 WTF_EXPORT bool operator==(const AtomicString&, const LChar*);
187 inline bool operator==(const AtomicString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
188 inline bool operator==(const AtomicString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); }
189 inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); }
190 inline bool operator==(const LChar* a, const AtomicString& b) { return b == a; }
191 inline bool operator==(const char* a, const AtomicString& b) { return b == a; }
192 inline bool operator==(const String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); }
193 inline bool operator==(const Vector<UChar>& a, const AtomicString& b) { return b == a; }
195 inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); }
196 inline bool operator!=(const AtomicString& a, const LChar* b) { return !(a == b); }
197 inline bool operator!=(const AtomicString& a, const char* b) { return !(a == b); }
198 inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); }
199 inline bool operator!=(const AtomicString& a, const Vector<UChar>& b) { return !(a == b); }
200 inline bool operator!=(const LChar* a, const AtomicString& b) { return !(b == a); }
201 inline bool operator!=(const char* a, const AtomicString& b) { return !(b == a); }
202 inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); }
203 inline bool operator!=(const Vector<UChar>& a, const AtomicString& b) { return !(a == b); }
205 inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); }
206 inline bool equalIgnoringCase(const AtomicString& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
207 inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
208 inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
209 inline bool equalIgnoringCase(const LChar* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); }
210 inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }
211 inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); }
213 // Define external global variables for the commonly used atomic strings.
214 // These are only usable from the main thread.
215 WTF_EXPORT extern const AtomicString& nullAtom;
216 WTF_EXPORT extern const AtomicString& emptyAtom;
217 WTF_EXPORT extern const AtomicString& starAtom;
218 WTF_EXPORT extern const AtomicString& xmlAtom;
219 WTF_EXPORT extern const AtomicString& xmlnsAtom;
220 WTF_EXPORT extern const AtomicString& xlinkAtom;
222 inline AtomicString AtomicString::fromUTF8(const char* characters, size_t length)
224 if (!characters)
225 return nullAtom;
226 if (!length)
227 return emptyAtom;
228 return fromUTF8Internal(characters, characters + length);
231 inline AtomicString AtomicString::fromUTF8(const char* characters)
233 if (!characters)
234 return nullAtom;
235 if (!*characters)
236 return emptyAtom;
237 return fromUTF8Internal(characters, 0);
240 // AtomicStringHash is the default hash for AtomicString
241 template<typename T> struct DefaultHash;
242 template<> struct DefaultHash<AtomicString> {
243 typedef AtomicStringHash Hash;
246 } // namespace WTF
248 WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(AtomicString);
250 using WTF::AtomicString;
251 using WTF::nullAtom;
252 using WTF::emptyAtom;
253 using WTF::starAtom;
254 using WTF::xmlAtom;
255 using WTF::xmlnsAtom;
256 using WTF::xlinkAtom;
258 #include "wtf/text/StringConcatenate.h"
259 #endif // AtomicString_h