benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 1 | /* |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Apple Inc. All rights reserved. |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 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 | #include "config.h" |
| 27 | #include "JSStringJoiner.h" |
| 28 | |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 29 | #include "JSCInlines.h" |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 30 | |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 31 | namespace JSC { |
| 32 | |
commit-queue@webkit.org | 0595f48 | 2016-03-16 02:16:40 +0000 | [diff] [blame] | 33 | JSStringJoiner::~JSStringJoiner() |
| 34 | { |
| 35 | } |
| 36 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 37 | template<typename CharacterType> |
| 38 | static inline void appendStringToData(CharacterType*& data, StringView string) |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 39 | { |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 40 | string.getCharactersWithUpconvert(data); |
| 41 | data += string.length(); |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | template<typename CharacterType> |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 45 | static inline String joinStrings(const Vector<StringViewWithUnderlyingString>& strings, StringView separator, unsigned joinedLength) |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 46 | { |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 47 | ASSERT(joinedLength); |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 48 | |
| 49 | CharacterType* data; |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 50 | String result = StringImpl::tryCreateUninitialized(joinedLength, data); |
commit-queue@webkit.org | 0595f48 | 2016-03-16 02:16:40 +0000 | [diff] [blame] | 51 | if (UNLIKELY(result.isNull())) |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 52 | return result; |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 53 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 54 | appendStringToData(data, strings[0].view); |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 55 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 56 | 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.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 70 | } |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 71 | 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.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 78 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 79 | return result; |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 80 | } |
| 81 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 82 | inline unsigned JSStringJoiner::joinedLength(ExecState& state) const |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 83 | { |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 84 | unsigned numberOfStrings = m_strings.size(); |
| 85 | if (!numberOfStrings) |
| 86 | return 0; |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 87 | |
mark.lam@apple.com | 3103207 | 2014-03-19 17:53:08 +0000 | [diff] [blame] | 88 | Checked<unsigned, RecordOverflow> separatorLength = m_separator.length(); |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 89 | Checked<unsigned, RecordOverflow> totalSeparatorsLength = separatorLength * (numberOfStrings - 1); |
| 90 | Checked<unsigned, RecordOverflow> totalLength = totalSeparatorsLength + m_accumulatedStringsLength; |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 91 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 92 | unsigned result; |
| 93 | if (totalLength.safeGet(result) == CheckedState::DidOverflow) { |
| 94 | throwOutOfMemoryError(&state); |
| 95 | return 0; |
| 96 | } |
| 97 | return result; |
| 98 | } |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 99 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 100 | JSValue JSStringJoiner::join(ExecState& state) |
| 101 | { |
mark.lam@apple.com | 23e2544 | 2015-06-24 14:18:16 +0000 | [diff] [blame] | 102 | ASSERT(m_strings.size() <= m_strings.capacity()); |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 103 | |
| 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.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 114 | else |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 115 | result = joinStrings<UChar>(m_strings, m_separator, length); |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 116 | |
darin@apple.com | 6d8af84 | 2015-06-24 02:33:18 +0000 | [diff] [blame] | 117 | if (result.isNull()) |
| 118 | return throwOutOfMemoryError(&state); |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 119 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 120 | return jsString(&state, WTFMove(result)); |
benjamin@webkit.org | 7be398b | 2012-04-05 19:30:13 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } |