2 * Copyright (C) 2012-2015 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 JSStringJoiner_h
27 #define JSStringJoiner_h
29 #include "ExceptionHelpers.h"
30 #include "JSCJSValue.h"
34 class JSStringJoiner
{
36 JSStringJoiner(ExecState
&, LChar separator
, unsigned stringCount
);
37 JSStringJoiner(ExecState
&, StringView separator
, unsigned stringCount
);
39 void append(ExecState
&, JSValue
);
40 void appendEmptyString();
42 JSValue
join(ExecState
&);
45 void append(StringViewWithUnderlyingString
&&);
46 void append8Bit(const String
&);
47 void appendLiteral(const Identifier
&);
48 unsigned joinedLength(ExecState
&) const;
50 LChar m_singleCharacterSeparator
;
51 StringView m_separator
;
52 Vector
<StringViewWithUnderlyingString
> m_strings
;
53 Checked
<unsigned, RecordOverflow
> m_accumulatedStringsLength
;
54 bool m_isAll8Bit
{ true };
57 inline JSStringJoiner::JSStringJoiner(ExecState
& state
, StringView separator
, unsigned stringCount
)
58 : m_separator(separator
)
59 , m_isAll8Bit(m_separator
.is8Bit())
61 if (!m_strings
.tryReserveCapacity(stringCount
))
62 throwOutOfMemoryError(&state
);
65 inline JSStringJoiner::JSStringJoiner(ExecState
& state
, LChar separator
, unsigned stringCount
)
66 : m_singleCharacterSeparator(separator
)
67 , m_separator
{ &m_singleCharacterSeparator
, 1 }
69 if (!m_strings
.tryReserveCapacity(stringCount
))
70 throwOutOfMemoryError(&state
);
73 ALWAYS_INLINE
void JSStringJoiner::append(StringViewWithUnderlyingString
&& string
)
75 m_accumulatedStringsLength
+= string
.view
.length();
76 m_isAll8Bit
= m_isAll8Bit
&& string
.view
.is8Bit();
77 m_strings
.uncheckedAppend(WTF::move(string
));
80 ALWAYS_INLINE
void JSStringJoiner::append8Bit(const String
& string
)
82 ASSERT(string
.is8Bit());
83 m_accumulatedStringsLength
+= string
.length();
84 m_strings
.uncheckedAppend({ string
, string
});
87 ALWAYS_INLINE
void JSStringJoiner::appendLiteral(const Identifier
& literal
)
89 m_accumulatedStringsLength
+= literal
.length();
90 ASSERT(literal
.string().is8Bit());
91 m_strings
.uncheckedAppend({ literal
.string(), { } });
94 ALWAYS_INLINE
void JSStringJoiner::appendEmptyString()
96 m_strings
.uncheckedAppend({ { }, { } });
99 ALWAYS_INLINE
void JSStringJoiner::append(ExecState
& state
, JSValue value
)
101 // The following code differs from using the result of JSValue::toString in the following ways:
102 // 1) It's inlined more than JSValue::toString is.
103 // 2) It includes conversion to WTF::String in a way that avoids allocating copies of substrings.
104 // 3) It doesn't create a JSString for numbers, true, or false.
105 // 4) It turns undefined and null into the empty string instead of "undefined" and "null".
106 // 5) It uses optimized code paths for all the cases known to be 8-bit and for the empty string.
108 if (value
.isCell()) {
109 if (value
.asCell()->isString()) {
110 append(asString(value
)->viewWithUnderlyingString(state
));
113 append(value
.toString(&state
)->viewWithUnderlyingString(state
));
117 if (value
.isInt32()) {
118 append8Bit(state
.vm().numericStrings
.add(value
.asInt32()));
121 if (value
.isDouble()) {
122 append8Bit(state
.vm().numericStrings
.add(value
.asDouble()));
125 if (value
.isTrue()) {
126 append8Bit(state
.vm().propertyNames
->trueKeyword
.string());
129 if (value
.isFalse()) {
130 append8Bit(state
.vm().propertyNames
->falseKeyword
.string());
133 ASSERT(value
.isUndefinedOrNull());