blob: e8dc2e3fd280ca363af25bcdab1af41a9d169c76 [file] [log] [blame]
benjamin@webkit.org7be398b2012-04-05 19:30:13 +00001/*
darin@apple.com6d8af842015-06-24 02:33:18 +00002 * Copyright (C) 2012-2015 Apple Inc. All rights reserved.
benjamin@webkit.org7be398b2012-04-05 19:30:13 +00003 *
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#include "config.h"
27#include "JSStringJoiner.h"
28
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000029#include "JSCInlines.h"
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000030
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000031namespace JSC {
32
commit-queue@webkit.org0595f482016-03-16 02:16:40 +000033JSStringJoiner::~JSStringJoiner()
34{
35}
36
darin@apple.com6d8af842015-06-24 02:33:18 +000037template<typename CharacterType>
38static inline void appendStringToData(CharacterType*& data, StringView string)
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000039{
darin@apple.com6d8af842015-06-24 02:33:18 +000040 string.getCharactersWithUpconvert(data);
41 data += string.length();
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000042}
43
44template<typename CharacterType>
darin@apple.com6d8af842015-06-24 02:33:18 +000045static inline String joinStrings(const Vector<StringViewWithUnderlyingString>& strings, StringView separator, unsigned joinedLength)
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000046{
darin@apple.com6d8af842015-06-24 02:33:18 +000047 ASSERT(joinedLength);
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000048
49 CharacterType* data;
darin@apple.com6d8af842015-06-24 02:33:18 +000050 String result = StringImpl::tryCreateUninitialized(joinedLength, data);
commit-queue@webkit.org0595f482016-03-16 02:16:40 +000051 if (UNLIKELY(result.isNull()))
darin@apple.com6d8af842015-06-24 02:33:18 +000052 return result;
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000053
darin@apple.com6d8af842015-06-24 02:33:18 +000054 appendStringToData(data, strings[0].view);
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000055
darin@apple.com6d8af842015-06-24 02:33:18 +000056 unsigned size = strings.size();
57
58 switch (separator.length()) {
59 case 0:
60 for (unsigned i = 1; i < size; ++i)
61 appendStringToData(data, strings[i].view);
62 break;
63 case 1: {
64 CharacterType separatorCharacter = separator[0];
65 for (unsigned i = 1; i < size; ++i) {
66 *data++ = separatorCharacter;
67 appendStringToData(data, strings[i].view);
68 }
69 break;
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000070 }
darin@apple.com6d8af842015-06-24 02:33:18 +000071 default:
72 for (unsigned i = 1; i < size; ++i) {
73 appendStringToData(data, separator);
74 appendStringToData(data, strings[i].view);
75 }
76 }
77 ASSERT(data == result.characters<CharacterType>() + joinedLength);
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000078
darin@apple.com6d8af842015-06-24 02:33:18 +000079 return result;
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000080}
81
darin@apple.com6d8af842015-06-24 02:33:18 +000082inline unsigned JSStringJoiner::joinedLength(ExecState& state) const
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000083{
darin@apple.com6d8af842015-06-24 02:33:18 +000084 unsigned numberOfStrings = m_strings.size();
85 if (!numberOfStrings)
86 return 0;
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000087
mark.lam@apple.com31032072014-03-19 17:53:08 +000088 Checked<unsigned, RecordOverflow> separatorLength = m_separator.length();
darin@apple.com6d8af842015-06-24 02:33:18 +000089 Checked<unsigned, RecordOverflow> totalSeparatorsLength = separatorLength * (numberOfStrings - 1);
90 Checked<unsigned, RecordOverflow> totalLength = totalSeparatorsLength + m_accumulatedStringsLength;
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000091
darin@apple.com6d8af842015-06-24 02:33:18 +000092 unsigned result;
93 if (totalLength.safeGet(result) == CheckedState::DidOverflow) {
94 throwOutOfMemoryError(&state);
95 return 0;
96 }
97 return result;
98}
benjamin@webkit.org7be398b2012-04-05 19:30:13 +000099
darin@apple.com6d8af842015-06-24 02:33:18 +0000100JSValue JSStringJoiner::join(ExecState& state)
101{
mark.lam@apple.com23e25442015-06-24 14:18:16 +0000102 ASSERT(m_strings.size() <= m_strings.capacity());
darin@apple.com6d8af842015-06-24 02:33:18 +0000103
104 unsigned length = joinedLength(state);
105 if (state.hadException())
106 return jsUndefined();
107
108 if (!length)
109 return jsEmptyString(&state);
110
111 String result;
112 if (m_isAll8Bit)
113 result = joinStrings<LChar>(m_strings, m_separator, length);
benjamin@webkit.org7be398b2012-04-05 19:30:13 +0000114 else
darin@apple.com6d8af842015-06-24 02:33:18 +0000115 result = joinStrings<UChar>(m_strings, m_separator, length);
benjamin@webkit.org7be398b2012-04-05 19:30:13 +0000116
darin@apple.com6d8af842015-06-24 02:33:18 +0000117 if (result.isNull())
118 return throwOutOfMemoryError(&state);
benjamin@webkit.org7be398b2012-04-05 19:30:13 +0000119
aestes@apple.com13aae082016-01-02 08:03:08 +0000120 return jsString(&state, WTFMove(result));
benjamin@webkit.org7be398b2012-04-05 19:30:13 +0000121}
122
123}