]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSStringJoiner.cpp
2 * Copyright (C) 2012 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.
27 #include "JSStringJoiner.h"
29 #include "ExceptionHelpers.h"
31 #include "ScopeChain.h"
32 #include <wtf/text/StringImpl.h>
37 // The destination is 16bits, at least one string is 16 bits.
38 static inline void appendStringToData(UChar
*& data
, const UString
& string
)
43 unsigned length
= string
.length();
44 const StringImpl
* stringImpl
= string
.impl();
46 if (stringImpl
->is8Bit()) {
47 for (unsigned i
= 0; i
< length
; ++i
) {
48 *data
= stringImpl
->characters8()[i
];
52 for (unsigned i
= 0; i
< length
; ++i
) {
53 *data
= stringImpl
->characters16()[i
];
59 // If the destination is 8bits, we know every string has to be 8bit.
60 static inline void appendStringToData(LChar
*& data
, const UString
& string
)
64 ASSERT(string
.is8Bit());
66 unsigned length
= string
.length();
67 const StringImpl
* stringImpl
= string
.impl();
69 for (unsigned i
= 0; i
< length
; ++i
) {
70 *data
= stringImpl
->characters8()[i
];
75 template<typename CharacterType
>
76 static inline PassRefPtr
<StringImpl
> joinStrings(const Vector
<UString
>& strings
, const UString
& separator
, unsigned outputLength
)
81 RefPtr
<StringImpl
> outputStringImpl
= StringImpl::tryCreateUninitialized(outputLength
, data
);
82 if (!outputStringImpl
)
83 return PassRefPtr
<StringImpl
>();
85 const UString firstString
= strings
.first();
86 appendStringToData(data
, firstString
);
88 for (size_t i
= 1; i
< strings
.size(); ++i
) {
89 appendStringToData(data
, separator
);
90 appendStringToData(data
, strings
[i
]);
93 ASSERT(data
== (outputStringImpl
->getCharacters
<CharacterType
>() + outputStringImpl
->length()));
94 return outputStringImpl
.release();
97 JSValue
JSStringJoiner::build(ExecState
* exec
)
100 return throwOutOfMemoryError(exec
);
102 if (!m_strings
.size())
103 return jsEmptyString(exec
);
105 size_t separatorLength
= m_separator
.length();
106 // FIXME: add special cases of joinStrings() for (separatorLength == 0) and (separatorLength == 1).
107 ASSERT(m_strings
.size() > 0);
108 size_t totalSeparactorsLength
= separatorLength
* (m_strings
.size() - 1);
109 size_t outputStringSize
= totalSeparactorsLength
+ m_cumulatedStringsLength
;
111 if (!outputStringSize
)
112 return jsEmptyString(exec
);
114 RefPtr
<StringImpl
> outputStringImpl
;
116 outputStringImpl
= joinStrings
<LChar
>(m_strings
, m_separator
, outputStringSize
);
118 outputStringImpl
= joinStrings
<UChar
>(m_strings
, m_separator
, outputStringSize
);
120 if (!outputStringImpl
)
121 return throwOutOfMemoryError(exec
);
123 return JSString::create(exec
->globalData(), outputStringImpl
.release());