]>
Commit | Line | Data |
---|---|---|
6fe7ccc8 | 1 | /* |
93a37866 | 2 | * Copyright (C) 2012, 2013 Apple Inc. All rights reserved. |
6fe7ccc8 A |
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 | ||
29 | #include "ExceptionHelpers.h" | |
93a37866 | 30 | #include "JSScope.h" |
6fe7ccc8 | 31 | #include "JSString.h" |
93a37866 | 32 | #include "Operations.h" |
6fe7ccc8 A |
33 | #include <wtf/text/StringImpl.h> |
34 | ||
6fe7ccc8 A |
35 | namespace JSC { |
36 | ||
37 | // The destination is 16bits, at least one string is 16 bits. | |
93a37866 | 38 | static inline void appendStringToData(UChar*& data, const String& string) |
6fe7ccc8 A |
39 | { |
40 | if (string.isNull()) | |
41 | return; | |
42 | ||
43 | unsigned length = string.length(); | |
44 | const StringImpl* stringImpl = string.impl(); | |
45 | ||
46 | if (stringImpl->is8Bit()) { | |
47 | for (unsigned i = 0; i < length; ++i) { | |
48 | *data = stringImpl->characters8()[i]; | |
49 | ++data; | |
50 | } | |
51 | } else { | |
52 | for (unsigned i = 0; i < length; ++i) { | |
53 | *data = stringImpl->characters16()[i]; | |
54 | ++data; | |
55 | } | |
56 | } | |
57 | } | |
58 | ||
59 | // If the destination is 8bits, we know every string has to be 8bit. | |
93a37866 | 60 | static inline void appendStringToData(LChar*& data, const String& string) |
6fe7ccc8 A |
61 | { |
62 | if (string.isNull()) | |
63 | return; | |
64 | ASSERT(string.is8Bit()); | |
65 | ||
66 | unsigned length = string.length(); | |
67 | const StringImpl* stringImpl = string.impl(); | |
68 | ||
69 | for (unsigned i = 0; i < length; ++i) { | |
70 | *data = stringImpl->characters8()[i]; | |
71 | ++data; | |
72 | } | |
73 | } | |
74 | ||
75 | template<typename CharacterType> | |
93a37866 | 76 | static inline PassRefPtr<StringImpl> joinStrings(const Vector<String>& strings, const String& separator, unsigned outputLength) |
6fe7ccc8 A |
77 | { |
78 | ASSERT(outputLength); | |
79 | ||
80 | CharacterType* data; | |
81 | RefPtr<StringImpl> outputStringImpl = StringImpl::tryCreateUninitialized(outputLength, data); | |
82 | if (!outputStringImpl) | |
83 | return PassRefPtr<StringImpl>(); | |
84 | ||
93a37866 | 85 | const String firstString = strings.first(); |
6fe7ccc8 A |
86 | appendStringToData(data, firstString); |
87 | ||
88 | for (size_t i = 1; i < strings.size(); ++i) { | |
89 | appendStringToData(data, separator); | |
90 | appendStringToData(data, strings[i]); | |
91 | } | |
92 | ||
93 | ASSERT(data == (outputStringImpl->getCharacters<CharacterType>() + outputStringImpl->length())); | |
94 | return outputStringImpl.release(); | |
95 | } | |
96 | ||
93a37866 | 97 | JSValue JSStringJoiner::join(ExecState* exec) |
6fe7ccc8 A |
98 | { |
99 | if (!m_isValid) | |
100 | return throwOutOfMemoryError(exec); | |
101 | ||
102 | if (!m_strings.size()) | |
103 | return jsEmptyString(exec); | |
104 | ||
93a37866 | 105 | Checked<size_t, RecordOverflow> separatorLength = m_separator.length(); |
6fe7ccc8 A |
106 | // FIXME: add special cases of joinStrings() for (separatorLength == 0) and (separatorLength == 1). |
107 | ASSERT(m_strings.size() > 0); | |
93a37866 A |
108 | Checked<size_t, RecordOverflow> totalSeparactorsLength = separatorLength * (m_strings.size() - 1); |
109 | Checked<size_t, RecordOverflow> outputStringSize = totalSeparactorsLength + m_accumulatedStringsLength; | |
6fe7ccc8 | 110 | |
93a37866 A |
111 | size_t finalSize; |
112 | if (outputStringSize.safeGet(finalSize) == CheckedState::DidOverflow) | |
113 | return throwOutOfMemoryError(exec); | |
114 | ||
6fe7ccc8 A |
115 | if (!outputStringSize) |
116 | return jsEmptyString(exec); | |
117 | ||
118 | RefPtr<StringImpl> outputStringImpl; | |
119 | if (m_is8Bits) | |
93a37866 | 120 | outputStringImpl = joinStrings<LChar>(m_strings, m_separator, finalSize); |
6fe7ccc8 | 121 | else |
93a37866 | 122 | outputStringImpl = joinStrings<UChar>(m_strings, m_separator, finalSize); |
6fe7ccc8 A |
123 | |
124 | if (!outputStringImpl) | |
125 | return throwOutOfMemoryError(exec); | |
126 | ||
93a37866 | 127 | return JSString::create(exec->vm(), outputStringImpl.release()); |
6fe7ccc8 A |
128 | } |
129 | ||
130 | } |