c05ae5081017743f602ca07688df6a7d39f16667
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / UString.h
1 /*
2  * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4  * Copyright (C) 2009 Google Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #ifndef UString_h
24 #define UString_h
25
26 #include <wtf/text/StringImpl.h>
27
28 namespace JSC {
29
30 class UString {
31 public:
32     // Construct a null string, distinguishable from an empty string.
33     UString() { }
34
35     // Construct a string with UTF-16 data.
36     UString(const UChar* characters, unsigned length);
37
38     // Construct a string with UTF-16 data, from a null-terminated source.
39     UString(const UChar*);
40
41     // Construct a string with latin1 data.
42     UString(const LChar* characters, unsigned length);
43     UString(const char* characters, unsigned length);
44
45     // Construct a string with latin1 data, from a null-terminated source.
46     UString(const LChar* characters);
47     UString(const char* characters);
48
49     // Construct a string referencing an existing StringImpl.
50     UString(StringImpl* impl) : m_impl(impl) { }
51     UString(PassRefPtr<StringImpl> impl) : m_impl(impl) { }
52     UString(RefPtr<StringImpl> impl) : m_impl(impl) { }
53
54     // Inline the destructor.
55     ALWAYS_INLINE ~UString() { }
56
57     void swap(UString& o) { m_impl.swap(o.m_impl); }
58
59     template<typename CharType, size_t inlineCapacity>
60     static UString adopt(Vector<CharType, inlineCapacity>& vector) { return StringImpl::adopt(vector); }
61
62     bool isNull() const { return !m_impl; }
63     bool isEmpty() const { return !m_impl || !m_impl->length(); }
64
65     StringImpl* impl() const { return m_impl.get(); }
66
67     unsigned length() const
68     {
69         if (!m_impl)
70             return 0;
71         return m_impl->length();
72     }
73
74     const UChar* characters() const
75     {
76         if (!m_impl)
77             return 0;
78         return m_impl->characters();
79     }
80
81     const LChar* characters8() const
82     {
83         if (!m_impl)
84             return 0;
85         ASSERT(m_impl->is8Bit());
86         return m_impl->characters8();
87     }
88
89     const UChar* characters16() const
90     {
91         if (!m_impl)
92             return 0;
93         ASSERT(!m_impl->is8Bit());
94         return m_impl->characters16();
95     }
96
97     template <typename CharType>
98     inline const CharType* getCharacters() const;
99
100     bool is8Bit() const { return m_impl->is8Bit(); }
101
102     CString ascii() const;
103     CString latin1() const;
104     CString utf8(bool strict = false) const;
105
106     UChar operator[](unsigned index) const
107     {
108         if (!m_impl || index >= m_impl->length())
109             return 0;
110         if (is8Bit())
111             return m_impl->characters8()[index];
112         return m_impl->characters16()[index];
113     }
114
115     static UString number(int);
116     static UString number(unsigned);
117     static UString number(long);
118     static UString number(long long);
119     static UString number(double);
120
121     // Find a single character or string, also with match function & latin1 forms.
122     size_t find(UChar c, unsigned start = 0) const
123         { return m_impl ? m_impl->find(c, start) : notFound; }
124     size_t find(const UString& str, unsigned start = 0) const
125         { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
126     size_t find(const LChar* str, unsigned start = 0) const
127         { return m_impl ? m_impl->find(str, start) : notFound; }
128
129     // Find the last instance of a single character or string.
130     size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
131         { return m_impl ? m_impl->reverseFind(c, start) : notFound; }
132     size_t reverseFind(const UString& str, unsigned start = UINT_MAX) const
133         { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; }
134
135     UString substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
136
137 private:
138     RefPtr<StringImpl> m_impl;
139 };
140
141 template<>
142 inline const LChar* UString::getCharacters<LChar>() const
143 {
144     ASSERT(is8Bit());
145     return characters8();
146 }
147
148 template<>
149 inline const UChar* UString::getCharacters<UChar>() const
150 {
151     ASSERT(!is8Bit());
152     return characters16();
153 }
154
155 NEVER_INLINE bool equalSlowCase(const UString& s1, const UString& s2);
156
157 ALWAYS_INLINE bool operator==(const UString& s1, const UString& s2)
158 {
159     StringImpl* rep1 = s1.impl();
160     StringImpl* rep2 = s2.impl();
161
162     if (rep1 == rep2) // If they're the same rep, they're equal.
163         return true;
164
165     unsigned size1 = 0;
166     unsigned size2 = 0;
167
168     if (rep1)
169         size1 = rep1->length();
170
171     if (rep2)
172         size2 = rep2->length();
173
174     if (size1 != size2) // If the lengths are not the same, we're done.
175         return false;
176
177     if (!size1)
178         return true;
179
180     if (size1 == 1)
181         return (*rep1)[0u] == (*rep2)[0u];
182
183     return equalSlowCase(s1, s2);
184 }
185
186
187 inline bool operator!=(const UString& s1, const UString& s2)
188 {
189     return !JSC::operator==(s1, s2);
190 }
191
192 bool operator<(const UString& s1, const UString& s2);
193 bool operator>(const UString& s1, const UString& s2);
194
195 bool operator==(const UString& s1, const char* s2);
196
197 inline bool operator!=(const UString& s1, const char* s2)
198 {
199     return !JSC::operator==(s1, s2);
200 }
201
202 inline bool operator==(const char *s1, const UString& s2)
203 {
204     return operator==(s2, s1);
205 }
206
207 inline bool operator!=(const char *s1, const UString& s2)
208 {
209     return !JSC::operator==(s1, s2);
210 }
211
212 inline int codePointCompare(const UString& s1, const UString& s2)
213 {
214     return codePointCompare(s1.impl(), s2.impl());
215 }
216
217 struct UStringHash {
218     static unsigned hash(StringImpl* key) { return key->hash(); }
219     static bool equal(const StringImpl* a, const StringImpl* b)
220     {
221         if (a == b)
222             return true;
223         if (!a || !b)
224             return false;
225
226         unsigned aLength = a->length();
227         unsigned bLength = b->length();
228         if (aLength != bLength)
229             return false;
230
231         // FIXME: perhaps we should have a more abstract macro that indicates when
232         // going 4 bytes at a time is unsafe
233 #if CPU(ARM) || CPU(SH4) || CPU(MIPS) || CPU(SPARC)
234         const UChar* aChars = a->characters();
235         const UChar* bChars = b->characters();
236         for (unsigned i = 0; i != aLength; ++i) {
237             if (*aChars++ != *bChars++)
238                 return false;
239         }
240         return true;
241 #else
242         /* Do it 4-bytes-at-a-time on architectures where it's safe */
243         const uint32_t* aChars = reinterpret_cast<const uint32_t*>(a->characters());
244         const uint32_t* bChars = reinterpret_cast<const uint32_t*>(b->characters());
245
246         unsigned halfLength = aLength >> 1;
247         for (unsigned i = 0; i != halfLength; ++i)
248             if (*aChars++ != *bChars++)
249                 return false;
250
251         if (aLength & 1 && *reinterpret_cast<const uint16_t*>(aChars) != *reinterpret_cast<const uint16_t*>(bChars))
252             return false;
253
254         return true;
255 #endif
256     }
257
258     static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); }
259     static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b)
260     {
261         return equal(a.get(), b.get());
262     }
263
264     static unsigned hash(const UString& key) { return key.impl()->hash(); }
265     static bool equal(const UString& a, const UString& b)
266     {
267         return equal(a.impl(), b.impl());
268     }
269
270     static const bool safeToCompareToEmptyOrDeleted = false;
271 };
272
273 } // namespace JSC
274
275 namespace WTF {
276
277 // UStringHash is the default hash for UString
278 template<typename T> struct DefaultHash;
279 template<> struct DefaultHash<JSC::UString> {
280     typedef JSC::UStringHash Hash;
281 };
282
283 template <> struct VectorTraits<JSC::UString> : SimpleClassVectorTraits { };
284
285 } // namespace WTF
286
287 #endif
288