]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/UString.h
7677161a36a5ce4942cd53a809f995957a4f7fc1
[apple/javascriptcore.git] / 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 JS_EXPORT_PRIVATE UString(const UChar* characters, unsigned length);
37
38 // Construct a string with UTF-16 data, from a null-terminated source.
39 JS_EXPORT_PRIVATE UString(const UChar*);
40
41 // Construct a string with latin1 data.
42 UString(const LChar* characters, unsigned length);
43 JS_EXPORT_PRIVATE 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 JS_EXPORT_PRIVATE 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 JS_EXPORT_PRIVATE CString ascii() const;
103 CString latin1() const;
104 JS_EXPORT_PRIVATE 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 JS_EXPORT_PRIVATE static UString number(int);
116 JS_EXPORT_PRIVATE static UString number(unsigned);
117 JS_EXPORT_PRIVATE static UString number(long);
118 static UString number(long long);
119 JS_EXPORT_PRIVATE 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
125 size_t find(const UString& str) const
126 { return m_impl ? m_impl->find(str.impl()) : notFound; }
127 size_t find(const UString& str, unsigned start) const
128 { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
129
130 size_t find(const LChar* str, unsigned start = 0) const
131 { return m_impl ? m_impl->find(str, start) : notFound; }
132
133 // Find the last instance of a single character or string.
134 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
135 { return m_impl ? m_impl->reverseFind(c, start) : notFound; }
136 size_t reverseFind(const UString& str, unsigned start = UINT_MAX) const
137 { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; }
138
139 JS_EXPORT_PRIVATE UString substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
140
141 private:
142 RefPtr<StringImpl> m_impl;
143 };
144
145 template<>
146 inline const LChar* UString::getCharacters<LChar>() const { return characters8(); }
147
148 template<>
149 inline const UChar* UString::getCharacters<UChar>() const { return characters(); }
150
151 NEVER_INLINE bool equalSlowCase(const UString& s1, const UString& s2);
152
153 ALWAYS_INLINE bool operator==(const UString& s1, const UString& s2)
154 {
155 StringImpl* rep1 = s1.impl();
156 StringImpl* rep2 = s2.impl();
157
158 if (rep1 == rep2) // If they're the same rep, they're equal.
159 return true;
160
161 unsigned size1 = 0;
162 unsigned size2 = 0;
163
164 if (rep1)
165 size1 = rep1->length();
166
167 if (rep2)
168 size2 = rep2->length();
169
170 if (size1 != size2) // If the lengths are not the same, we're done.
171 return false;
172
173 if (!size1)
174 return true;
175
176 if (size1 == 1)
177 return (*rep1)[0u] == (*rep2)[0u];
178
179 return equalSlowCase(s1, s2);
180 }
181
182
183 inline bool operator!=(const UString& s1, const UString& s2)
184 {
185 return !JSC::operator==(s1, s2);
186 }
187
188 JS_EXPORT_PRIVATE bool operator<(const UString& s1, const UString& s2);
189 JS_EXPORT_PRIVATE bool operator>(const UString& s1, const UString& s2);
190
191 JS_EXPORT_PRIVATE bool operator==(const UString& s1, const char* s2);
192
193 inline bool operator!=(const UString& s1, const char* s2)
194 {
195 return !JSC::operator==(s1, s2);
196 }
197
198 inline bool operator==(const char *s1, const UString& s2)
199 {
200 return operator==(s2, s1);
201 }
202
203 inline bool operator!=(const char *s1, const UString& s2)
204 {
205 return !JSC::operator==(s1, s2);
206 }
207
208 inline int codePointCompare(const UString& s1, const UString& s2)
209 {
210 return codePointCompare(s1.impl(), s2.impl());
211 }
212
213 struct UStringHash {
214 static unsigned hash(StringImpl* key) { return key->hash(); }
215 static bool equal(const StringImpl* a, const StringImpl* b)
216 {
217 if (a == b)
218 return true;
219 if (!a || !b)
220 return false;
221
222 unsigned aLength = a->length();
223 unsigned bLength = b->length();
224 if (aLength != bLength)
225 return false;
226
227 // FIXME: perhaps we should have a more abstract macro that indicates when
228 // going 4 bytes at a time is unsafe
229 #if CPU(ARM) || CPU(SH4) || CPU(MIPS) || CPU(SPARC)
230 const UChar* aChars = a->characters();
231 const UChar* bChars = b->characters();
232 for (unsigned i = 0; i != aLength; ++i) {
233 if (*aChars++ != *bChars++)
234 return false;
235 }
236 return true;
237 #else
238 /* Do it 4-bytes-at-a-time on architectures where it's safe */
239 const uint32_t* aChars = reinterpret_cast<const uint32_t*>(a->characters());
240 const uint32_t* bChars = reinterpret_cast<const uint32_t*>(b->characters());
241
242 unsigned halfLength = aLength >> 1;
243 for (unsigned i = 0; i != halfLength; ++i)
244 if (*aChars++ != *bChars++)
245 return false;
246
247 if (aLength & 1 && *reinterpret_cast<const uint16_t*>(aChars) != *reinterpret_cast<const uint16_t*>(bChars))
248 return false;
249
250 return true;
251 #endif
252 }
253
254 static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); }
255 static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b)
256 {
257 return equal(a.get(), b.get());
258 }
259
260 static unsigned hash(const UString& key) { return key.impl()->hash(); }
261 static bool equal(const UString& a, const UString& b)
262 {
263 return equal(a.impl(), b.impl());
264 }
265
266 static const bool safeToCompareToEmptyOrDeleted = false;
267 };
268
269 } // namespace JSC
270
271 namespace WTF {
272
273 // UStringHash is the default hash for UString
274 template<typename T> struct DefaultHash;
275 template<> struct DefaultHash<JSC::UString> {
276 typedef JSC::UStringHash Hash;
277 };
278
279 template <> struct VectorTraits<JSC::UString> : SimpleClassVectorTraits { };
280
281 } // namespace WTF
282
283 #endif
284