]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/text/StringConcatenate.h
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
30 #include "AtomicString.h"
35 template<typename StringType
>
36 class StringTypeAdapter
{
40 class StringTypeAdapter
<char> {
42 StringTypeAdapter
<char>(char buffer
)
47 unsigned length() { return 1; }
48 void writeTo(UChar
* destination
) { *destination
= m_buffer
; }
51 unsigned char m_buffer
;
55 class StringTypeAdapter
<UChar
> {
57 StringTypeAdapter
<UChar
>(UChar buffer
)
62 unsigned length() { return 1; }
63 void writeTo(UChar
* destination
) { *destination
= m_buffer
; }
70 class StringTypeAdapter
<char*> {
72 StringTypeAdapter
<char*>(char* buffer
)
74 , m_length(strlen(buffer
))
78 unsigned length() { return m_length
; }
80 void writeTo(UChar
* destination
)
82 for (unsigned i
= 0; i
< m_length
; ++i
) {
83 unsigned char c
= m_buffer
[i
];
94 class StringTypeAdapter
<const UChar
*> {
96 StringTypeAdapter
<const UChar
*>(const UChar
* buffer
)
100 while (m_buffer
[len
] != UChar(0))
103 if (len
> std::numeric_limits
<unsigned>::max())
109 unsigned length() { return m_length
; }
111 void writeTo(UChar
* destination
)
113 memcpy(destination
, m_buffer
, static_cast<size_t>(m_length
) * sizeof(UChar
));
117 const UChar
* m_buffer
;
122 class StringTypeAdapter
<const char*> {
124 StringTypeAdapter
<const char*>(const char* buffer
)
126 , m_length(strlen(buffer
))
130 unsigned length() { return m_length
; }
132 void writeTo(UChar
* destination
)
134 for (unsigned i
= 0; i
< m_length
; ++i
) {
135 unsigned char c
= m_buffer
[i
];
141 const char* m_buffer
;
146 class StringTypeAdapter
<Vector
<char> > {
148 StringTypeAdapter
<Vector
<char> >(const Vector
<char>& buffer
)
153 size_t length() { return m_buffer
.size(); }
155 void writeTo(UChar
* destination
)
157 for (size_t i
= 0; i
< m_buffer
.size(); ++i
) {
158 unsigned char c
= m_buffer
[i
];
164 const Vector
<char>& m_buffer
;
168 class StringTypeAdapter
<String
> {
170 StringTypeAdapter
<String
>(const String
& string
)
175 unsigned length() { return m_buffer
.length(); }
177 void writeTo(UChar
* destination
)
179 const UChar
* data
= m_buffer
.characters();
180 unsigned length
= m_buffer
.length();
181 for (unsigned i
= 0; i
< length
; ++i
)
182 destination
[i
] = data
[i
];
186 const String
& m_buffer
;
190 class StringTypeAdapter
<AtomicString
> {
192 StringTypeAdapter
<AtomicString
>(const AtomicString
& string
)
193 : m_adapter(string
.string())
197 unsigned length() { return m_adapter
.length(); }
198 void writeTo(UChar
* destination
) { m_adapter
.writeTo(destination
); }
201 StringTypeAdapter
<String
> m_adapter
;
204 inline void sumWithOverflow(unsigned& total
, unsigned addend
, bool& overflow
)
206 unsigned oldTotal
= total
;
207 total
= oldTotal
+ addend
;
208 if (total
< oldTotal
)
212 template<typename StringType1
, typename StringType2
>
213 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
)
215 StringTypeAdapter
<StringType1
> adapter1(string1
);
216 StringTypeAdapter
<StringType2
> adapter2(string2
);
219 bool overflow
= false;
220 unsigned length
= adapter1
.length();
221 sumWithOverflow(length
, adapter2
.length(), overflow
);
224 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
228 UChar
* result
= buffer
;
229 adapter1
.writeTo(result
);
230 result
+= adapter1
.length();
231 adapter2
.writeTo(result
);
233 return resultImpl
.release();
236 template<typename StringType1
, typename StringType2
, typename StringType3
>
237 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
, StringType3 string3
)
239 StringTypeAdapter
<StringType1
> adapter1(string1
);
240 StringTypeAdapter
<StringType2
> adapter2(string2
);
241 StringTypeAdapter
<StringType3
> adapter3(string3
);
244 bool overflow
= false;
245 unsigned length
= adapter1
.length();
246 sumWithOverflow(length
, adapter2
.length(), overflow
);
247 sumWithOverflow(length
, adapter3
.length(), overflow
);
250 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
254 UChar
* result
= buffer
;
255 adapter1
.writeTo(result
);
256 result
+= adapter1
.length();
257 adapter2
.writeTo(result
);
258 result
+= adapter2
.length();
259 adapter3
.writeTo(result
);
261 return resultImpl
.release();
264 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
>
265 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
)
267 StringTypeAdapter
<StringType1
> adapter1(string1
);
268 StringTypeAdapter
<StringType2
> adapter2(string2
);
269 StringTypeAdapter
<StringType3
> adapter3(string3
);
270 StringTypeAdapter
<StringType4
> adapter4(string4
);
273 bool overflow
= false;
274 unsigned length
= adapter1
.length();
275 sumWithOverflow(length
, adapter2
.length(), overflow
);
276 sumWithOverflow(length
, adapter3
.length(), overflow
);
277 sumWithOverflow(length
, adapter4
.length(), overflow
);
280 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
284 UChar
* result
= buffer
;
285 adapter1
.writeTo(result
);
286 result
+= adapter1
.length();
287 adapter2
.writeTo(result
);
288 result
+= adapter2
.length();
289 adapter3
.writeTo(result
);
290 result
+= adapter3
.length();
291 adapter4
.writeTo(result
);
293 return resultImpl
.release();
296 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
>
297 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
)
299 StringTypeAdapter
<StringType1
> adapter1(string1
);
300 StringTypeAdapter
<StringType2
> adapter2(string2
);
301 StringTypeAdapter
<StringType3
> adapter3(string3
);
302 StringTypeAdapter
<StringType4
> adapter4(string4
);
303 StringTypeAdapter
<StringType5
> adapter5(string5
);
306 bool overflow
= false;
307 unsigned length
= adapter1
.length();
308 sumWithOverflow(length
, adapter2
.length(), overflow
);
309 sumWithOverflow(length
, adapter3
.length(), overflow
);
310 sumWithOverflow(length
, adapter4
.length(), overflow
);
311 sumWithOverflow(length
, adapter5
.length(), overflow
);
314 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
318 UChar
* result
= buffer
;
319 adapter1
.writeTo(result
);
320 result
+= adapter1
.length();
321 adapter2
.writeTo(result
);
322 result
+= adapter2
.length();
323 adapter3
.writeTo(result
);
324 result
+= adapter3
.length();
325 adapter4
.writeTo(result
);
326 result
+= adapter4
.length();
327 adapter5
.writeTo(result
);
329 return resultImpl
.release();
332 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
>
333 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
)
335 StringTypeAdapter
<StringType1
> adapter1(string1
);
336 StringTypeAdapter
<StringType2
> adapter2(string2
);
337 StringTypeAdapter
<StringType3
> adapter3(string3
);
338 StringTypeAdapter
<StringType4
> adapter4(string4
);
339 StringTypeAdapter
<StringType5
> adapter5(string5
);
340 StringTypeAdapter
<StringType6
> adapter6(string6
);
343 bool overflow
= false;
344 unsigned length
= adapter1
.length();
345 sumWithOverflow(length
, adapter2
.length(), overflow
);
346 sumWithOverflow(length
, adapter3
.length(), overflow
);
347 sumWithOverflow(length
, adapter4
.length(), overflow
);
348 sumWithOverflow(length
, adapter5
.length(), overflow
);
349 sumWithOverflow(length
, adapter6
.length(), overflow
);
352 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
356 UChar
* result
= buffer
;
357 adapter1
.writeTo(result
);
358 result
+= adapter1
.length();
359 adapter2
.writeTo(result
);
360 result
+= adapter2
.length();
361 adapter3
.writeTo(result
);
362 result
+= adapter3
.length();
363 adapter4
.writeTo(result
);
364 result
+= adapter4
.length();
365 adapter5
.writeTo(result
);
366 result
+= adapter5
.length();
367 adapter6
.writeTo(result
);
369 return resultImpl
.release();
372 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
, typename StringType7
>
373 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
, StringType7 string7
)
375 StringTypeAdapter
<StringType1
> adapter1(string1
);
376 StringTypeAdapter
<StringType2
> adapter2(string2
);
377 StringTypeAdapter
<StringType3
> adapter3(string3
);
378 StringTypeAdapter
<StringType4
> adapter4(string4
);
379 StringTypeAdapter
<StringType5
> adapter5(string5
);
380 StringTypeAdapter
<StringType6
> adapter6(string6
);
381 StringTypeAdapter
<StringType7
> adapter7(string7
);
384 bool overflow
= false;
385 unsigned length
= adapter1
.length();
386 sumWithOverflow(length
, adapter2
.length(), overflow
);
387 sumWithOverflow(length
, adapter3
.length(), overflow
);
388 sumWithOverflow(length
, adapter4
.length(), overflow
);
389 sumWithOverflow(length
, adapter5
.length(), overflow
);
390 sumWithOverflow(length
, adapter6
.length(), overflow
);
391 sumWithOverflow(length
, adapter7
.length(), overflow
);
394 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
398 UChar
* result
= buffer
;
399 adapter1
.writeTo(result
);
400 result
+= adapter1
.length();
401 adapter2
.writeTo(result
);
402 result
+= adapter2
.length();
403 adapter3
.writeTo(result
);
404 result
+= adapter3
.length();
405 adapter4
.writeTo(result
);
406 result
+= adapter4
.length();
407 adapter5
.writeTo(result
);
408 result
+= adapter5
.length();
409 adapter6
.writeTo(result
);
410 result
+= adapter6
.length();
411 adapter7
.writeTo(result
);
413 return resultImpl
.release();
416 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
, typename StringType7
, typename StringType8
>
417 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
, StringType7 string7
, StringType8 string8
)
419 StringTypeAdapter
<StringType1
> adapter1(string1
);
420 StringTypeAdapter
<StringType2
> adapter2(string2
);
421 StringTypeAdapter
<StringType3
> adapter3(string3
);
422 StringTypeAdapter
<StringType4
> adapter4(string4
);
423 StringTypeAdapter
<StringType5
> adapter5(string5
);
424 StringTypeAdapter
<StringType6
> adapter6(string6
);
425 StringTypeAdapter
<StringType7
> adapter7(string7
);
426 StringTypeAdapter
<StringType8
> adapter8(string8
);
429 bool overflow
= false;
430 unsigned length
= adapter1
.length();
431 sumWithOverflow(length
, adapter2
.length(), overflow
);
432 sumWithOverflow(length
, adapter3
.length(), overflow
);
433 sumWithOverflow(length
, adapter4
.length(), overflow
);
434 sumWithOverflow(length
, adapter5
.length(), overflow
);
435 sumWithOverflow(length
, adapter6
.length(), overflow
);
436 sumWithOverflow(length
, adapter7
.length(), overflow
);
437 sumWithOverflow(length
, adapter8
.length(), overflow
);
440 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
444 UChar
* result
= buffer
;
445 adapter1
.writeTo(result
);
446 result
+= adapter1
.length();
447 adapter2
.writeTo(result
);
448 result
+= adapter2
.length();
449 adapter3
.writeTo(result
);
450 result
+= adapter3
.length();
451 adapter4
.writeTo(result
);
452 result
+= adapter4
.length();
453 adapter5
.writeTo(result
);
454 result
+= adapter5
.length();
455 adapter6
.writeTo(result
);
456 result
+= adapter6
.length();
457 adapter7
.writeTo(result
);
458 result
+= adapter7
.length();
459 adapter8
.writeTo(result
);
461 return resultImpl
.release();
464 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
, typename StringType7
, typename StringType8
, typename StringType9
>
465 PassRefPtr
<StringImpl
> tryMakeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
, StringType7 string7
, StringType8 string8
, StringType9 string9
)
467 StringTypeAdapter
<StringType1
> adapter1(string1
);
468 StringTypeAdapter
<StringType2
> adapter2(string2
);
469 StringTypeAdapter
<StringType3
> adapter3(string3
);
470 StringTypeAdapter
<StringType4
> adapter4(string4
);
471 StringTypeAdapter
<StringType5
> adapter5(string5
);
472 StringTypeAdapter
<StringType6
> adapter6(string6
);
473 StringTypeAdapter
<StringType7
> adapter7(string7
);
474 StringTypeAdapter
<StringType8
> adapter8(string8
);
475 StringTypeAdapter
<StringType9
> adapter9(string9
);
478 bool overflow
= false;
479 unsigned length
= adapter1
.length();
480 sumWithOverflow(length
, adapter2
.length(), overflow
);
481 sumWithOverflow(length
, adapter3
.length(), overflow
);
482 sumWithOverflow(length
, adapter4
.length(), overflow
);
483 sumWithOverflow(length
, adapter5
.length(), overflow
);
484 sumWithOverflow(length
, adapter6
.length(), overflow
);
485 sumWithOverflow(length
, adapter7
.length(), overflow
);
486 sumWithOverflow(length
, adapter8
.length(), overflow
);
487 sumWithOverflow(length
, adapter9
.length(), overflow
);
490 RefPtr
<StringImpl
> resultImpl
= StringImpl::tryCreateUninitialized(length
, buffer
);
494 UChar
* result
= buffer
;
495 adapter1
.writeTo(result
);
496 result
+= adapter1
.length();
497 adapter2
.writeTo(result
);
498 result
+= adapter2
.length();
499 adapter3
.writeTo(result
);
500 result
+= adapter3
.length();
501 adapter4
.writeTo(result
);
502 result
+= adapter4
.length();
503 adapter5
.writeTo(result
);
504 result
+= adapter5
.length();
505 adapter6
.writeTo(result
);
506 result
+= adapter6
.length();
507 adapter7
.writeTo(result
);
508 result
+= adapter7
.length();
509 adapter8
.writeTo(result
);
510 result
+= adapter8
.length();
511 adapter9
.writeTo(result
);
513 return resultImpl
.release();
518 template<typename StringType1
>
519 String
makeString(StringType1 string1
)
521 return String(string1
);
524 template<typename StringType1
, typename StringType2
>
525 String
makeString(StringType1 string1
, StringType2 string2
)
527 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
);
530 return resultImpl
.release();
533 template<typename StringType1
, typename StringType2
, typename StringType3
>
534 String
makeString(StringType1 string1
, StringType2 string2
, StringType3 string3
)
536 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
, string3
);
539 return resultImpl
.release();
542 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
>
543 String
makeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
)
545 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
, string3
, string4
);
548 return resultImpl
.release();
551 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
>
552 String
makeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
)
554 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
, string3
, string4
, string5
);
557 return resultImpl
.release();
560 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
>
561 String
makeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
)
563 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
, string3
, string4
, string5
, string6
);
566 return resultImpl
.release();
569 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
, typename StringType7
>
570 String
makeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
, StringType7 string7
)
572 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
, string3
, string4
, string5
, string6
, string7
);
575 return resultImpl
.release();
578 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
, typename StringType7
, typename StringType8
>
579 String
makeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
, StringType7 string7
, StringType8 string8
)
581 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
, string3
, string4
, string5
, string6
, string7
, string8
);
584 return resultImpl
.release();
587 template<typename StringType1
, typename StringType2
, typename StringType3
, typename StringType4
, typename StringType5
, typename StringType6
, typename StringType7
, typename StringType8
, typename StringType9
>
588 String
makeString(StringType1 string1
, StringType2 string2
, StringType3 string3
, StringType4 string4
, StringType5 string5
, StringType6 string6
, StringType7 string7
, StringType8 string8
, StringType9 string9
)
590 RefPtr
<StringImpl
> resultImpl
= tryMakeString(string1
, string2
, string3
, string4
, string5
, string6
, string7
, string8
, string9
);
593 return resultImpl
.release();
598 using WTF::makeString
;
600 #include "StringOperators.h"