Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / text / AtomicString.h
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 "wtf/HashTableDeletedValueType.h"
25 #include "wtf/WTFExport.h"
26 #include "wtf/text/CString.h"
27 #include "wtf/text/WTFString.h"
28
29 namespace WTF {
30
31 struct AtomicStringHash;
32
33 class WTF_EXPORT AtomicString {
34 public:
35     static void init();
36
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)) { }
44
45     template<size_t inlineCapacity>
46     explicit AtomicString(const Vector<UChar, inlineCapacity>& characters)
47         : m_string(add(characters.data(), characters.size()))
48     {
49     }
50
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())) { }
55
56     AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_string(add(baseString, start, length)) { }
57
58     enum ConstructFromLiteralTag { ConstructFromLiteral };
59     AtomicString(const char* characters, unsigned length, ConstructFromLiteralTag)
60         : m_string(addFromLiteralData(characters, length))
61     {
62     }
63
64     template<unsigned charactersCount>
65     ALWAYS_INLINE AtomicString(const char (&characters)[charactersCount], ConstructFromLiteralTag)
66         : m_string(addFromLiteralData(characters, charactersCount - 1))
67     {
68         COMPILE_ASSERT(charactersCount > 1, AtomicStringFromLiteralNotEmpty);
69         COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), AtomicStringFromLiteralCannotOverflow);
70     }
71
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(); }
75
76     static StringImpl* find(const StringImpl*);
77
78     operator const String&() const { return m_string; }
79     const String& string() const { return m_string; };
80
81     StringImpl* impl() const { return m_string.impl(); }
82
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(); }
87
88     UChar operator[](unsigned i) const { return m_string[i]; }
89
90     bool contains(UChar c) const { return m_string.contains(c); }
91     bool contains(const LChar* s, bool caseSensitive = true) const
92         { return m_string.contains(s, caseSensitive); }
93     bool contains(const String& s, bool caseSensitive = true) const
94         { return m_string.contains(s, caseSensitive); }
95
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, bool caseSentitive = true) const
98         { return m_string.find(s, start, caseSentitive); }
99     size_t find(const String& s, size_t start = 0, bool caseSentitive = true) const
100         { return m_string.find(s, start, caseSentitive); }
101
102     bool startsWith(const String& s, bool caseSensitive = true) const
103         { return m_string.startsWith(s, caseSensitive); }
104     bool startsWith(UChar character) const
105         { return m_string.startsWith(character); }
106     template<unsigned matchLength>
107     bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
108         { return m_string.startsWith<matchLength>(prefix, caseSensitive); }
109
110     bool endsWith(const String& s, bool caseSensitive = true) const
111         { return m_string.endsWith(s, caseSensitive); }
112     bool endsWith(UChar character) const
113         { return m_string.endsWith(character); }
114     template<unsigned matchLength>
115     bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
116         { return m_string.endsWith<matchLength>(prefix, caseSensitive); }
117
118     AtomicString lower() const;
119     AtomicString upper() const { return AtomicString(impl()->upper()); }
120
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); }
124
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);
131
132     static AtomicString number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
133
134     bool isNull() const { return m_string.isNull(); }
135     bool isEmpty() const { return m_string.isEmpty(); }
136
137     static void remove(StringImpl*);
138
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*);
150
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); }
154
155 #ifndef NDEBUG
156     void show() const;
157 #endif
158
159 private:
160     String m_string;
161
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)
171     {
172         if (!r || r->isAtomic())
173             return r;
174         return addSlowCase(r);
175     }
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
181
182     static AtomicString fromUTF8Internal(const char*, const char*);
183 };
184
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 String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); }
192 inline bool operator==(const Vector<UChar>& a, const AtomicString& b) { return b == a; }
193
194 inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); }
195 inline bool operator!=(const AtomicString& a, const LChar* b) { return !(a == b); }
196 inline bool operator!=(const AtomicString& a, const char* b) { return !(a == b); }
197 inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); }
198 inline bool operator!=(const AtomicString& a, const Vector<UChar>& b) { return !(a == b); }
199 inline bool operator!=(const LChar* a, const AtomicString& b) { return !(b == a); }
200 inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); }
201 inline bool operator!=(const Vector<UChar>& a, const AtomicString& b) { return !(a == b); }
202
203 inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); }
204 inline bool equalIgnoringCase(const AtomicString& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
205 inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
206 inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
207 inline bool equalIgnoringCase(const LChar* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); }
208 inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }
209 inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); }
210
211 // Define external global variables for the commonly used atomic strings.
212 // These are only usable from the main thread.
213 WTF_EXPORT extern const AtomicString& nullAtom;
214 WTF_EXPORT extern const AtomicString& emptyAtom;
215 WTF_EXPORT extern const AtomicString& starAtom;
216 WTF_EXPORT extern const AtomicString& xmlAtom;
217 WTF_EXPORT extern const AtomicString& xmlnsAtom;
218 WTF_EXPORT extern const AtomicString& xlinkAtom;
219
220 inline AtomicString AtomicString::fromUTF8(const char* characters, size_t length)
221 {
222     if (!characters)
223         return nullAtom;
224     if (!length)
225         return emptyAtom;
226     return fromUTF8Internal(characters, characters + length);
227 }
228
229 inline AtomicString AtomicString::fromUTF8(const char* characters)
230 {
231     if (!characters)
232         return nullAtom;
233     if (!*characters)
234         return emptyAtom;
235     return fromUTF8Internal(characters, 0);
236 }
237
238 // AtomicStringHash is the default hash for AtomicString
239 template<typename T> struct DefaultHash;
240 template<> struct DefaultHash<AtomicString> {
241     typedef AtomicStringHash Hash;
242 };
243
244 } // namespace WTF
245
246 WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(AtomicString);
247
248 using WTF::AtomicString;
249 using WTF::nullAtom;
250 using WTF::emptyAtom;
251 using WTF::starAtom;
252 using WTF::xmlAtom;
253 using WTF::xmlnsAtom;
254 using WTF::xlinkAtom;
255
256 #include "wtf/text/StringConcatenate.h"
257 #endif // AtomicString_h