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 char* characters, unsigned length);
44 // Construct a string with latin1 data, from a null-terminated source.
45 UString(const char* characters);
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) { }
52 // Inline the destructor.
53 ALWAYS_INLINE ~UString() { }
55 void swap(UString& o) { m_impl.swap(o.m_impl); }
57 template<size_t inlineCapacity>
58 static UString adopt(Vector<UChar, inlineCapacity>& vector) { return StringImpl::adopt(vector); }
60 bool isNull() const { return !m_impl; }
61 bool isEmpty() const { return !m_impl || !m_impl->length(); }
63 StringImpl* impl() const { return m_impl.get(); }
65 unsigned length() const
69 return m_impl->length();
72 const UChar* characters() const
76 return m_impl->characters();
79 bool is8Bit() const { return false; }
81 CString ascii() const;
82 CString latin1() const;
83 CString utf8(bool strict = false) const;
85 UChar operator[](unsigned index) const
87 if (!m_impl || index >= m_impl->length())
89 return m_impl->characters()[index];
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);
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; }
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; }
112 UString substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
115 RefPtr<StringImpl> m_impl;
118 ALWAYS_INLINE bool operator==(const UString& s1, const UString& s2)
120 StringImpl* rep1 = s1.impl();
121 StringImpl* rep2 = s2.impl();
125 if (rep1 == rep2) // If they're the same rep, they're equal.
129 size1 = rep1->length();
132 size2 = rep2->length();
134 if (size1 != size2) // If the lengths are not the same, we're done.
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();
146 if (d1 == d2) // Check to see if the data pointers are the same.
149 // Do quick checks for sizes 1 and 2.
152 return d1[0] == d2[0];
154 return (d1[0] == d2[0]) & (d1[1] == d2[1]);
156 return memcmp(d1, d2, size1 * sizeof(UChar)) == 0;
161 inline bool operator!=(const UString& s1, const UString& s2)
163 return !JSC::operator==(s1, s2);
166 bool operator<(const UString& s1, const UString& s2);
167 bool operator>(const UString& s1, const UString& s2);
169 bool operator==(const UString& s1, const char* s2);
171 inline bool operator!=(const UString& s1, const char* s2)
173 return !JSC::operator==(s1, s2);
176 inline bool operator==(const char *s1, const UString& s2)
178 return operator==(s2, s1);
181 inline bool operator!=(const char *s1, const UString& s2)
183 return !JSC::operator==(s1, s2);
186 inline int codePointCompare(const UString& s1, const UString& s2)
188 return codePointCompare(s1.impl(), s2.impl());
192 static unsigned hash(StringImpl* key) { return key->hash(); }
193 static bool equal(const StringImpl* a, const StringImpl* b)
200 unsigned aLength = a->length();
201 unsigned bLength = b->length();
202 if (aLength != bLength)
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++)
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());
220 unsigned halfLength = aLength >> 1;
221 for (unsigned i = 0; i != halfLength; ++i)
222 if (*aChars++ != *bChars++)
225 if (aLength & 1 && *reinterpret_cast<const uint16_t*>(aChars) != *reinterpret_cast<const uint16_t*>(bChars))
232 static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); }
233 static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b)
235 return equal(a.get(), b.get());
238 static unsigned hash(const UString& key) { return key.impl()->hash(); }
239 static bool equal(const UString& a, const UString& b)
241 return equal(a.impl(), b.impl());
244 static const bool safeToCompareToEmptyOrDeleted = false;
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;
257 template <> struct VectorTraits<JSC::UString> : SimpleClassVectorTraits { };