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 JS_EXPORT_PRIVATE
UString(const UChar
* characters
, unsigned length
);
38 // Construct a string with UTF-16 data, from a null-terminated source.
39 JS_EXPORT_PRIVATE
UString(const UChar
*);
41 // Construct a string with latin1 data.
42 UString(const LChar
* characters
, unsigned length
);
43 JS_EXPORT_PRIVATE
UString(const char* characters
, unsigned length
);
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
);
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 JS_EXPORT_PRIVATE CString
ascii() const;
103 CString
latin1() const;
104 JS_EXPORT_PRIVATE 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 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);
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
; }
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
; }
130 size_t find(const LChar
* str
, unsigned start
= 0) const
131 { return m_impl
? m_impl
->find(str
, start
) : notFound
; }
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
; }
139 JS_EXPORT_PRIVATE UString
substringSharingImpl(unsigned pos
, unsigned len
= UINT_MAX
) const;
142 RefPtr
<StringImpl
> m_impl
;
146 inline const LChar
* UString::getCharacters
<LChar
>() const { return characters8(); }
149 inline const UChar
* UString::getCharacters
<UChar
>() const { return characters(); }
151 NEVER_INLINE
bool equalSlowCase(const UString
& s1
, const UString
& s2
);
153 ALWAYS_INLINE
bool operator==(const UString
& s1
, const UString
& s2
)
155 StringImpl
* rep1
= s1
.impl();
156 StringImpl
* rep2
= s2
.impl();
158 if (rep1
== rep2
) // If they're the same rep, they're equal.
165 size1
= rep1
->length();
168 size2
= rep2
->length();
170 if (size1
!= size2
) // If the lengths are not the same, we're done.
177 return (*rep1
)[0u] == (*rep2
)[0u];
179 return equalSlowCase(s1
, s2
);
183 inline bool operator!=(const UString
& s1
, const UString
& s2
)
185 return !JSC::operator==(s1
, s2
);
188 JS_EXPORT_PRIVATE
bool operator<(const UString
& s1
, const UString
& s2
);
189 JS_EXPORT_PRIVATE
bool operator>(const UString
& s1
, const UString
& s2
);
191 JS_EXPORT_PRIVATE
bool operator==(const UString
& s1
, const char* s2
);
193 inline bool operator!=(const UString
& s1
, const char* s2
)
195 return !JSC::operator==(s1
, s2
);
198 inline bool operator==(const char *s1
, const UString
& s2
)
200 return operator==(s2
, s1
);
203 inline bool operator!=(const char *s1
, const UString
& s2
)
205 return !JSC::operator==(s1
, s2
);
208 inline int codePointCompare(const UString
& s1
, const UString
& s2
)
210 return codePointCompare(s1
.impl(), s2
.impl());
214 static unsigned hash(StringImpl
* key
) { return key
->hash(); }
215 static bool equal(const StringImpl
* a
, const StringImpl
* b
)
222 unsigned aLength
= a
->length();
223 unsigned bLength
= b
->length();
224 if (aLength
!= bLength
)
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
++)
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());
242 unsigned halfLength
= aLength
>> 1;
243 for (unsigned i
= 0; i
!= halfLength
; ++i
)
244 if (*aChars
++ != *bChars
++)
247 if (aLength
& 1 && *reinterpret_cast<const uint16_t*>(aChars
) != *reinterpret_cast<const uint16_t*>(bChars
))
254 static unsigned hash(const RefPtr
<StringImpl
>& key
) { return key
->hash(); }
255 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
257 return equal(a
.get(), b
.get());
260 static unsigned hash(const UString
& key
) { return key
.impl()->hash(); }
261 static bool equal(const UString
& a
, const UString
& b
)
263 return equal(a
.impl(), b
.impl());
266 static const bool safeToCompareToEmptyOrDeleted
= false;
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
;
279 template <> struct VectorTraits
<JSC::UString
> : SimpleClassVectorTraits
{ };