initial import
[vuplus_webkit] / 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 char* characters, unsigned length);
43
44     // Construct a string with latin1 data, from a null-terminated source.
45     UString(const char* characters);
46
47     // Construct a string referencing an existing StringImpl.
48     UString(StringImpl* impl) : m_impl(impl) { }
49     UString(PassRefPtr<StringImpl> impl) : m_impl(impl) { }
50     UString(RefPtr<StringImpl> impl) : m_impl(impl) { }
51
52     // Inline the destructor.
53     ALWAYS_INLINE ~UString() { }
54
55     void swap(UString& o) { m_impl.swap(o.m_impl); }
56
57     template<size_t inlineCapacity>
58     static UString adopt(Vector<UChar, inlineCapacity>& vector) { return StringImpl::adopt(vector); }
59
60     bool isNull() const { return !m_impl; }
61     bool isEmpty() const { return !m_impl || !m_impl->length(); }
62
63     StringImpl* impl() const { return m_impl.get(); }
64
65     unsigned length() const
66     {
67         if (!m_impl)
68             return 0;
69         return m_impl->length();
70     }
71
72     const UChar* characters() const
73     {
74         if (!m_impl)
75             return 0;
76         return m_impl->characters();
77     }
78
79     bool is8Bit() const { return false; }
80
81     CString ascii() const;
82     CString latin1() const;
83     CString utf8(bool strict = false) const;
84
85     UChar operator[](unsigned index) const
86     {
87         if (!m_impl || index >= m_impl->length())
88             return 0;
89         return m_impl->characters()[index];
90     }
91
92     static UString number(int);
93     static UString number(unsigned);
94     static UString number(long);
95     static UString number(long long);
96     static UString number(double);
97
98     // Find a single character or string, also with match function & latin1 forms.
99     size_t find(UChar c, unsigned start = 0) const
100         { return m_impl ? m_impl->find(c, start) : notFound; }
101     size_t find(const UString& str, unsigned start = 0) const
102         { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
103     size_t find(const char* str, unsigned start = 0) const
104         { return m_impl ? m_impl->find(str, start) : notFound; }
105
106     // Find the last instance of a single character or string.
107     size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
108         { return m_impl ? m_impl->reverseFind(c, start) : notFound; }
109     size_t reverseFind(const UString& str, unsigned start = UINT_MAX) const
110         { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; }
111
112     UString substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
113
114 private:
115     RefPtr<StringImpl> m_impl;
116 };
117
118 ALWAYS_INLINE bool operator==(const UString& s1, const UString& s2)
119 {
120     StringImpl* rep1 = s1.impl();
121     StringImpl* rep2 = s2.impl();
122     unsigned size1 = 0;
123     unsigned size2 = 0;
124
125     if (rep1 == rep2) // If they're the same rep, they're equal.
126         return true;
127     
128     if (rep1)
129         size1 = rep1->length();
130         
131     if (rep2)
132         size2 = rep2->length();
133         
134     if (size1 != size2) // If the lengths are not the same, we're done.
135         return false;
136     
137     if (!size1)
138         return true;
139     
140     // At this point we know 
141     //   (a) that the strings are the same length and
142     //   (b) that they are greater than zero length.
143     const UChar* d1 = rep1->characters();
144     const UChar* d2 = rep2->characters();
145     
146     if (d1 == d2) // Check to see if the data pointers are the same.
147         return true;
148     
149     // Do quick checks for sizes 1 and 2.
150     switch (size1) {
151     case 1:
152         return d1[0] == d2[0];
153     case 2:
154         return (d1[0] == d2[0]) & (d1[1] == d2[1]);
155     default:
156         return memcmp(d1, d2, size1 * sizeof(UChar)) == 0;
157     }
158 }
159
160
161 inline bool operator!=(const UString& s1, const UString& s2)
162 {
163     return !JSC::operator==(s1, s2);
164 }
165
166 bool operator<(const UString& s1, const UString& s2);
167 bool operator>(const UString& s1, const UString& s2);
168
169 bool operator==(const UString& s1, const char* s2);
170
171 inline bool operator!=(const UString& s1, const char* s2)
172 {
173     return !JSC::operator==(s1, s2);
174 }
175
176 inline bool operator==(const char *s1, const UString& s2)
177 {
178     return operator==(s2, s1);
179 }
180
181 inline bool operator!=(const char *s1, const UString& s2)
182 {
183     return !JSC::operator==(s1, s2);
184 }
185
186 inline int codePointCompare(const UString& s1, const UString& s2)
187 {
188     return codePointCompare(s1.impl(), s2.impl());
189 }
190
191 struct UStringHash {
192     static unsigned hash(StringImpl* key) { return key->hash(); }
193     static bool equal(const StringImpl* a, const StringImpl* b)
194     {
195         if (a == b)
196             return true;
197         if (!a || !b)
198             return false;
199
200         unsigned aLength = a->length();
201         unsigned bLength = b->length();
202         if (aLength != bLength)
203             return false;
204
205         // FIXME: perhaps we should have a more abstract macro that indicates when
206         // going 4 bytes at a time is unsafe
207 #if CPU(ARM) || CPU(SH4) || CPU(MIPS) || CPU(SPARC)
208         const UChar* aChars = a->characters();
209         const UChar* bChars = b->characters();
210         for (unsigned i = 0; i != aLength; ++i) {
211             if (*aChars++ != *bChars++)
212                 return false;
213         }
214         return true;
215 #else
216         /* Do it 4-bytes-at-a-time on architectures where it's safe */
217         const uint32_t* aChars = reinterpret_cast<const uint32_t*>(a->characters());
218         const uint32_t* bChars = reinterpret_cast<const uint32_t*>(b->characters());
219
220         unsigned halfLength = aLength >> 1;
221         for (unsigned i = 0; i != halfLength; ++i)
222             if (*aChars++ != *bChars++)
223                 return false;
224
225         if (aLength & 1 && *reinterpret_cast<const uint16_t*>(aChars) != *reinterpret_cast<const uint16_t*>(bChars))
226             return false;
227
228         return true;
229 #endif
230     }
231
232     static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); }
233     static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b)
234     {
235         return equal(a.get(), b.get());
236     }
237
238     static unsigned hash(const UString& key) { return key.impl()->hash(); }
239     static bool equal(const UString& a, const UString& b)
240     {
241         return equal(a.impl(), b.impl());
242     }
243
244     static const bool safeToCompareToEmptyOrDeleted = false;
245 };
246
247 } // namespace JSC
248
249 namespace WTF {
250
251 // UStringHash is the default hash for UString
252 template<typename T> struct DefaultHash;
253 template<> struct DefaultHash<JSC::UString> {
254     typedef JSC::UStringHash Hash;
255 };
256
257 template <> struct VectorTraits<JSC::UString> : SimpleClassVectorTraits { };
258
259 } // namespace WTF
260
261 #endif
262