]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSStringJoiner.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSStringJoiner.h
1 /*
2 * Copyright (C) 2012-2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #ifndef JSStringJoiner_h
27 #define JSStringJoiner_h
28
29 #include "ExceptionHelpers.h"
30 #include "JSCJSValue.h"
31
32 namespace JSC {
33
34 class JSStringJoiner {
35 public:
36 JSStringJoiner(ExecState&, LChar separator, unsigned stringCount);
37 JSStringJoiner(ExecState&, StringView separator, unsigned stringCount);
38
39 void append(ExecState&, JSValue);
40 void appendEmptyString();
41
42 JSValue join(ExecState&);
43
44 private:
45 void append(StringViewWithUnderlyingString&&);
46 void append8Bit(const String&);
47 void appendLiteral(const Identifier&);
48 unsigned joinedLength(ExecState&) const;
49
50 LChar m_singleCharacterSeparator;
51 StringView m_separator;
52 Vector<StringViewWithUnderlyingString> m_strings;
53 Checked<unsigned, RecordOverflow> m_accumulatedStringsLength;
54 bool m_isAll8Bit { true };
55 };
56
57 inline JSStringJoiner::JSStringJoiner(ExecState& state, StringView separator, unsigned stringCount)
58 : m_separator(separator)
59 , m_isAll8Bit(m_separator.is8Bit())
60 {
61 if (!m_strings.tryReserveCapacity(stringCount))
62 throwOutOfMemoryError(&state);
63 }
64
65 inline JSStringJoiner::JSStringJoiner(ExecState& state, LChar separator, unsigned stringCount)
66 : m_singleCharacterSeparator(separator)
67 , m_separator { &m_singleCharacterSeparator, 1 }
68 {
69 if (!m_strings.tryReserveCapacity(stringCount))
70 throwOutOfMemoryError(&state);
71 }
72
73 ALWAYS_INLINE void JSStringJoiner::append(StringViewWithUnderlyingString&& string)
74 {
75 m_accumulatedStringsLength += string.view.length();
76 m_isAll8Bit = m_isAll8Bit && string.view.is8Bit();
77 m_strings.uncheckedAppend(WTF::move(string));
78 }
79
80 ALWAYS_INLINE void JSStringJoiner::append8Bit(const String& string)
81 {
82 ASSERT(string.is8Bit());
83 m_accumulatedStringsLength += string.length();
84 m_strings.uncheckedAppend({ string, string });
85 }
86
87 ALWAYS_INLINE void JSStringJoiner::appendLiteral(const Identifier& literal)
88 {
89 m_accumulatedStringsLength += literal.length();
90 ASSERT(literal.string().is8Bit());
91 m_strings.uncheckedAppend({ literal.string(), { } });
92 }
93
94 ALWAYS_INLINE void JSStringJoiner::appendEmptyString()
95 {
96 m_strings.uncheckedAppend({ { }, { } });
97 }
98
99 ALWAYS_INLINE void JSStringJoiner::append(ExecState& state, JSValue value)
100 {
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.
107
108 if (value.isCell()) {
109 if (value.asCell()->isString()) {
110 append(asString(value)->viewWithUnderlyingString(state));
111 return;
112 }
113 append(value.toString(&state)->viewWithUnderlyingString(state));
114 return;
115 }
116
117 if (value.isInt32()) {
118 append8Bit(state.vm().numericStrings.add(value.asInt32()));
119 return;
120 }
121 if (value.isDouble()) {
122 append8Bit(state.vm().numericStrings.add(value.asDouble()));
123 return;
124 }
125 if (value.isTrue()) {
126 append8Bit(state.vm().propertyNames->trueKeyword.string());
127 return;
128 }
129 if (value.isFalse()) {
130 append8Bit(state.vm().propertyNames->falseKeyword.string());
131 return;
132 }
133 ASSERT(value.isUndefinedOrNull());
134 appendEmptyString();
135 }
136
137 }
138
139 #endif