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.
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.
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.
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.
26 #include <wtf/text/StringImpl.h>
32 // Construct a null string, distinguishable from an empty string.
35 // Construct a string with UTF-16 data.
36 UString(const UChar* characters, unsigned length);
38 // Construct a string with UTF-16 data, from a null-terminated source.
39 UString(const UChar*);
41 // Construct a string with latin1 data.
42 UString(const LChar* characters, unsigned length);
43 UString(const char* characters, unsigned length);
45 // Construct a string with latin1 data, from a null-terminated source.
46 UString(const LChar* characters);
47 UString(const char* characters);
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) { }
54 // Inline the destructor.
55 ALWAYS_INLINE ~UString() { }
57 void swap(UString& o) { m_impl.swap(o.m_impl); }
59 template<typename CharType, size_t inlineCapacity>
60 static UString adopt(Vector<CharType, inlineCapacity>& vector) { return StringImpl::adopt(vector); }
62 bool isNull() const { return !m_impl; }
63 bool isEmpty() const { return !m_impl || !m_impl->length(); }
65 StringImpl* impl() const { return m_impl.get(); }
67 unsigned length() const
71 return m_impl->length();
74 const UChar* characters() const
78 return m_impl->characters();
81 const LChar* characters8() const
85 ASSERT(m_impl->is8Bit());
86 return m_impl->characters8();
89 const UChar* characters16() const
93 ASSERT(!m_impl->is8Bit());
94 return m_impl->characters16();
97 template <typename CharType>
98 inline const CharType* getCharacters() const;
100 bool is8Bit() const { return m_impl->is8Bit(); }
102 CString ascii() const;
103 CString latin1() const;
104 CString utf8(bool strict = false) const;
106 UChar operator[](unsigned index) const
108 if (!m_impl || index >= m_impl->length())
111 return m_impl->characters8()[index];
112 return m_impl->characters16()[index];
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);
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; }
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; }
135 UString substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
138 RefPtr<StringImpl> m_impl;
142 inline const LChar* UString::getCharacters<LChar>() const
145 return characters8();
149 inline const UChar* UString::getCharacters<UChar>() const
152 return characters16();
155 NEVER_INLINE bool equalSlowCase(const UString& s1, const UString& s2);
157 ALWAYS_INLINE bool operator==(const UString& s1, const UString& s2)
159 StringImpl* rep1 = s1.impl();
160 StringImpl* rep2 = s2.impl();
162 if (rep1 == rep2) // If they're the same rep, they're equal.
169 size1 = rep1->length();
172 size2 = rep2->length();
174 if (size1 != size2) // If the lengths are not the same, we're done.
181 return (*rep1)[0u] == (*rep2)[0u];
183 return equalSlowCase(s1, s2);
187 inline bool operator!=(const UString& s1, const UString& s2)
189 return !JSC::operator==(s1, s2);
192 bool operator<(const UString& s1, const UString& s2);
193 bool operator>(const UString& s1, const UString& s2);
195 bool operator==(const UString& s1, const char* s2);
197 inline bool operator!=(const UString& s1, const char* s2)
199 return !JSC::operator==(s1, s2);
202 inline bool operator==(const char *s1, const UString& s2)
204 return operator==(s2, s1);
207 inline bool operator!=(const char *s1, const UString& s2)
209 return !JSC::operator==(s1, s2);
212 inline int codePointCompare(const UString& s1, const UString& s2)
214 return codePointCompare(s1.impl(), s2.impl());
218 static unsigned hash(StringImpl* key) { return key->hash(); }
219 static bool equal(const StringImpl* a, const StringImpl* b)
226 unsigned aLength = a->length();
227 unsigned bLength = b->length();
228 if (aLength != bLength)
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++)
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());
246 unsigned halfLength = aLength >> 1;
247 for (unsigned i = 0; i != halfLength; ++i)
248 if (*aChars++ != *bChars++)
251 if (aLength & 1 && *reinterpret_cast<const uint16_t*>(aChars) != *reinterpret_cast<const uint16_t*>(bChars))
258 static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); }
259 static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b)
261 return equal(a.get(), b.get());
264 static unsigned hash(const UString& key) { return key.impl()->hash(); }
265 static bool equal(const UString& a, const UString& b)
267 return equal(a.impl(), b.impl());
270 static const bool safeToCompareToEmptyOrDeleted = false;
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;
283 template <> struct VectorTraits<JSC::UString> : SimpleClassVectorTraits { };