2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef StringConcatenate_h
27 #define StringConcatenate_h
32 #include "wtf/text/AtomicString.h"
35 // This macro is helpful for testing how many intermediate Strings are created while evaluating an
36 // expression containing operator+.
37 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING
38 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0)
43 template<typename StringType
>
44 class StringTypeAdapter
{
48 class StringTypeAdapter
<char> {
50 StringTypeAdapter
<char>(char buffer
)
55 unsigned length() { return 1; }
57 bool is8Bit() { return true; }
59 void writeTo(LChar
* destination
)
61 *destination
= m_buffer
;
64 void writeTo(UChar
* destination
) { *destination
= m_buffer
; }
67 unsigned char m_buffer
;
71 class StringTypeAdapter
<LChar
> {
73 StringTypeAdapter
<LChar
>(LChar buffer
)
78 unsigned length() { return 1; }
80 bool is8Bit() { return true; }
82 void writeTo(LChar
* destination
)
84 *destination
= m_buffer
;
87 void writeTo(UChar
* destination
) { *destination
= m_buffer
; }
94 class StringTypeAdapter
<UChar
> {
96 StringTypeAdapter
<UChar
>(UChar buffer
)
101 unsigned length() { return 1; }
103 bool is8Bit() { return m_buffer
<= 0xff; }
105 void writeTo(LChar
* destination
)
108 *destination
= static_cast<LChar
>(m_buffer
);
111 void writeTo(UChar
* destination
) { *destination
= m_buffer
; }
118 class WTF_EXPORT StringTypeAdapter
<char*> {
120 StringTypeAdapter
<char*>(char* buffer
)
122 , m_length(strlen(buffer
))
126 unsigned length() { return m_length
; }
128 bool is8Bit() { return true; }
130 void writeTo(LChar
* destination
);
132 void writeTo(UChar
* destination
);
135 const char* m_buffer
;
140 class WTF_EXPORT StringTypeAdapter
<LChar
*> {
142 StringTypeAdapter
<LChar
*>(LChar
* buffer
);
144 unsigned length() { return m_length
; }
146 bool is8Bit() { return true; }
148 void writeTo(LChar
* destination
);
150 void writeTo(UChar
* destination
);
153 const LChar
* m_buffer
;
158 class WTF_EXPORT StringTypeAdapter
<const UChar
*> {
160 StringTypeAdapter(const UChar
* buffer
);
162 unsigned length() { return m_length
; }
164 bool is8Bit() { return false; }
166 NO_RETURN_DUE_TO_CRASH
void writeTo(LChar
*)
168 RELEASE_ASSERT(false);
171 void writeTo(UChar
* destination
);
174 const UChar
* m_buffer
;
179 class WTF_EXPORT StringTypeAdapter
<const char*> {
181 StringTypeAdapter
<const char*>(const char* buffer
);
183 unsigned length() { return m_length
; }
185 bool is8Bit() { return true; }
187 void writeTo(LChar
* destination
);
189 void writeTo(UChar
* destination
);
192 const char* m_buffer
;
197 class WTF_EXPORT StringTypeAdapter
<const LChar
*> {
199 StringTypeAdapter
<const LChar
*>(const LChar
* buffer
);
201 unsigned length() { return m_length
; }
203 bool is8Bit() { return true; }
205 void writeTo(LChar
* destination
);
207 void writeTo(UChar
* destination
);
210 const LChar
* m_buffer
;
215 class WTF_EXPORT StringTypeAdapter
<Vector
<char>> {
217 StringTypeAdapter
<Vector
<char>>(const Vector
<char>& buffer
)
222 size_t length() { return m_buffer
.size(); }
224 bool is8Bit() { return true; }
226 void writeTo(LChar
* destination
);
228 void writeTo(UChar
* destination
);
231 const Vector
<char>& m_buffer
;
235 class StringTypeAdapter
<Vector
<LChar
>> {
237 StringTypeAdapter
<Vector
<LChar
>>(const Vector
<LChar
>& buffer
)
242 size_t length() { return m_buffer
.size(); }
244 bool is8Bit() { return true; }
246 void writeTo(LChar
* destination
);
248 void writeTo(UChar
* destination
);
251 const Vector
<LChar
>& m_buffer
;
255 class WTF_EXPORT StringTypeAdapter
<String
> {
257 StringTypeAdapter
<String
>(const String
& string
)
262 unsigned length() { return m_buffer
.length(); }
264 bool is8Bit() { return m_buffer
.isNull() || m_buffer
.is8Bit(); }
266 void writeTo(LChar
* destination
);
268 void writeTo(UChar
* destination
);
271 const String
& m_buffer
;
275 class StringTypeAdapter
<AtomicString
> {
277 StringTypeAdapter
<AtomicString
>(const AtomicString
& string
)
278 : m_adapter(string
.string())
282 unsigned length() { return m_adapter
.length(); }
284 bool is8Bit() { return m_adapter
.is8Bit(); }
286 void writeTo(LChar
* destination
) { m_adapter
.writeTo(destination
); }
287 void writeTo(UChar
* destination
) { m_adapter
.writeTo(destination
); }
290 StringTypeAdapter
<String
> m_adapter
;
293 inline void sumWithOverflow(unsigned& total
, unsigned addend
, bool& overflow
)
295 unsigned oldTotal
= total
;
296 total
= oldTotal
+ addend
;
297 if (total
< oldTotal
)
301 template<typename StringType1
, typename StringType2
>
302 PassRefPtr
<StringImpl
> makeString(StringType1 string1
, StringType2 string2
)
304 StringTypeAdapter
<StringType1
> adapter1(string1
);
305 StringTypeAdapter
<StringType2
> adapter2(string2
);
307 bool overflow
= false;
308 unsigned length
= adapter1
.length();
309 sumWithOverflow(length
, adapter2
.length(), overflow
);
313 if (adapter1
.is8Bit() && adapter2
.is8Bit()) {
315 RefPtr
<StringImpl
> resultImpl
= StringImpl::createUninitialized(length
, buffer
);
319 LChar
* result
= buffer
;
320 adapter1
.writeTo(result
);
321 result
+= adapter1
.length();
322 adapter2
.writeTo(result
);
324 return resultImpl
.release();
328 RefPtr
<StringImpl
> resultImpl
= StringImpl::createUninitialized(length
, buffer
);
332 UChar
* result
= buffer
;
333 adapter1
.writeTo(result
);
334 result
+= adapter1
.length();
335 adapter2
.writeTo(result
);
337 return resultImpl
.release();
342 #include "wtf/text/StringOperators.h"