1/*
2 * Copyright (C) 2009-2018 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <wtf/CheckedArithmetic.h>
30#include <wtf/text/AtomicString.h>
31#include <wtf/text/IntegerToStringConversion.h>
32#include <wtf/text/StringView.h>
33#include <wtf/text/WTFString.h>
34
35namespace WTF {
36
37// StringBuilder currently uses a Checked<int32_t, ConditionalCrashOnOverflow> for m_length.
38// Ideally, we would want to make StringBuilder a template with an OverflowHandler parameter, and
39// m_length can be instantiated based on that OverflowHandler instead. However, currently, we're
40// not able to get clang to export explicitly instantiated template methods (which would be needed
41// if we templatize StringBuilder). As a workaround, we use the ConditionalCrashOnOverflow handler
42// instead to do a runtime check on whether it should crash on overflows or not.
43//
44// When clang is able to export explicitly instantiated template methods, we can templatize
45// StringBuilder and do away with ConditionalCrashOnOverflow.
46// See https://bugs.webkit.org/show_bug.cgi?id=191050.
47
48class StringBuilder {
49 // Disallow copying since it's expensive and we don't want code to do it by accident.
50 WTF_MAKE_NONCOPYABLE(StringBuilder);
51
52public:
53 enum class OverflowHandler {
54 CrashOnOverflow,
55 RecordOverflow
56 };
57
58 StringBuilder(OverflowHandler handler = OverflowHandler::CrashOnOverflow)
59 : m_bufferCharacters8(nullptr)
60 {
61 m_length.setShouldCrashOnOverflow(handler == OverflowHandler::CrashOnOverflow);
62 }
63 StringBuilder(StringBuilder&&) = default;
64 StringBuilder& operator=(StringBuilder&&) = default;
65
66 ALWAYS_INLINE void didOverflow() { m_length.overflowed(); }
67 ALWAYS_INLINE bool hasOverflowed() const { return m_length.hasOverflowed(); }
68 ALWAYS_INLINE bool crashesOnOverflow() const { return m_length.shouldCrashOnOverflow(); }
69
70 WTF_EXPORT_PRIVATE void append(const UChar*, unsigned);
71 WTF_EXPORT_PRIVATE void append(const LChar*, unsigned);
72
73 ALWAYS_INLINE void append(const char* characters, unsigned length) { append(reinterpret_cast<const LChar*>(characters), length); }
74
75 void append(const AtomicString& atomicString)
76 {
77 append(atomicString.string());
78 }
79
80 void append(const String& string)
81 {
82 if (hasOverflowed())
83 return;
84
85 if (!string.length())
86 return;
87
88 // If we're appending to an empty string, and there is not a buffer (reserveCapacity has not been called)
89 // then just retain the string.
90 if (!m_length && !m_buffer) {
91 m_string = string;
92 m_length = string.length();
93 m_is8Bit = m_string.is8Bit();
94 return;
95 }
96
97 if (string.is8Bit())
98 append(string.characters8(), string.length());
99 else
100 append(string.characters16(), string.length());
101 }
102
103 void append(const StringBuilder& other)
104 {
105 if (hasOverflowed())
106 return;
107 if (other.hasOverflowed())
108 return didOverflow();
109
110 if (!other.m_length)
111 return;
112
113 // If we're appending to an empty string, and there is not a buffer (reserveCapacity has not been called)
114 // then just retain the string.
115 if (!m_length && !m_buffer && !other.m_string.isNull()) {
116 m_string = other.m_string;
117 m_length = other.m_length;
118 return;
119 }
120
121 if (other.is8Bit())
122 append(other.characters8(), other.m_length.unsafeGet());
123 else
124 append(other.characters16(), other.m_length.unsafeGet());
125 }
126
127 void append(StringView stringView)
128 {
129 if (stringView.is8Bit())
130 append(stringView.characters8(), stringView.length());
131 else
132 append(stringView.characters16(), stringView.length());
133 }
134
135#if USE(CF)
136 WTF_EXPORT_PRIVATE void append(CFStringRef);
137#endif
138#if USE(CF) && defined(__OBJC__)
139 void append(NSString *string) { append((__bridge CFStringRef)string); }
140#endif
141
142 void append(const String& string, unsigned offset, unsigned length)
143 {
144 if (!string.length())
145 return;
146
147 if ((offset + length) > string.length())
148 return;
149
150 if (string.is8Bit())
151 append(string.characters8() + offset, length);
152 else
153 append(string.characters16() + offset, length);
154 }
155
156 void append(const char* characters)
157 {
158 if (characters)
159 append(characters, strlen(characters));
160 }
161
162 void append(UChar c)
163 {
164 if (hasOverflowed())
165 return;
166 unsigned length = m_length.unsafeGet<unsigned>();
167 if (m_buffer && length < m_buffer->length() && m_string.isNull()) {
168 if (!m_is8Bit) {
169 m_bufferCharacters16[length] = c;
170 m_length++;
171 return;
172 }
173
174 if (!(c & ~0xff)) {
175 m_bufferCharacters8[length] = static_cast<LChar>(c);
176 m_length++;
177 return;
178 }
179 }
180 append(&c, 1);
181 }
182
183 void append(LChar c)
184 {
185 if (hasOverflowed())
186 return;
187 unsigned length = m_length.unsafeGet<unsigned>();
188 if (m_buffer && length < m_buffer->length() && m_string.isNull()) {
189 if (m_is8Bit)
190 m_bufferCharacters8[length] = c;
191 else
192 m_bufferCharacters16[length] = c;
193 m_length++;
194 } else
195 append(&c, 1);
196 }
197
198 void append(char c)
199 {
200 append(static_cast<LChar>(c));
201 }
202
203 void append(UChar32 c)
204 {
205 if (U_IS_BMP(c)) {
206 append(static_cast<UChar>(c));
207 return;
208 }
209 append(U16_LEAD(c));
210 append(U16_TRAIL(c));
211 }
212
213 WTF_EXPORT_PRIVATE void appendQuotedJSONString(const String&);
214
215 template<unsigned characterCount>
216 ALWAYS_INLINE void appendLiteral(const char (&characters)[characterCount]) { append(characters, characterCount - 1); }
217
218 WTF_EXPORT_PRIVATE void appendNumber(int);
219 WTF_EXPORT_PRIVATE void appendNumber(unsigned);
220 WTF_EXPORT_PRIVATE void appendNumber(long);
221 WTF_EXPORT_PRIVATE void appendNumber(unsigned long);
222 WTF_EXPORT_PRIVATE void appendNumber(long long);
223 WTF_EXPORT_PRIVATE void appendNumber(unsigned long long);
224 // FIXME: Change appendNumber to be appendShortestFormNumber instead of appendFixedPrecisionNumber.
225 void appendNumber(float);
226 void appendNumber(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
227
228 WTF_EXPORT_PRIVATE void appendShortestFormNumber(float);
229 WTF_EXPORT_PRIVATE void appendShortestFormNumber(double);
230 WTF_EXPORT_PRIVATE void appendFixedPrecisionNumber(float, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
231 WTF_EXPORT_PRIVATE void appendFixedPrecisionNumber(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
232 WTF_EXPORT_PRIVATE void appendFixedWidthNumber(float, unsigned decimalPlaces);
233 WTF_EXPORT_PRIVATE void appendFixedWidthNumber(double, unsigned decimalPlaces);
234
235 // FIXME: Delete in favor of the name appendShortestFormNumber or just appendNumber.
236 void appendECMAScriptNumber(float);
237 void appendECMAScriptNumber(double);
238
239 String toString()
240 {
241 if (!m_string.isNull()) {
242 ASSERT(!m_buffer || m_isReified);
243 ASSERT(!hasOverflowed());
244 return m_string;
245 }
246
247 RELEASE_ASSERT(!hasOverflowed());
248 shrinkToFit();
249 reifyString();
250 return m_string;
251 }
252
253 const String& toStringPreserveCapacity() const
254 {
255 RELEASE_ASSERT(!hasOverflowed());
256 if (m_string.isNull())
257 reifyString();
258 return m_string;
259 }
260
261 AtomicString toAtomicString() const
262 {
263 RELEASE_ASSERT(!hasOverflowed());
264 if (!m_length)
265 return emptyAtom();
266
267 // If the buffer is sufficiently over-allocated, make a new AtomicString from a copy so its buffer is not so large.
268 if (canShrink()) {
269 if (is8Bit())
270 return AtomicString(characters8(), length());
271 return AtomicString(characters16(), length());
272 }
273
274 if (!m_string.isNull())
275 return AtomicString(m_string);
276
277 ASSERT(m_buffer);
278 return AtomicString(m_buffer.get(), 0, m_length.unsafeGet());
279 }
280
281 unsigned length() const
282 {
283 RELEASE_ASSERT(!hasOverflowed());
284 return m_length.unsafeGet();
285 }
286
287 bool isEmpty() const { return !m_length; }
288
289 WTF_EXPORT_PRIVATE void reserveCapacity(unsigned newCapacity);
290
291 unsigned capacity() const
292 {
293 RELEASE_ASSERT(!hasOverflowed());
294 return m_buffer ? m_buffer->length() : m_length.unsafeGet();
295 }
296
297 WTF_EXPORT_PRIVATE void resize(unsigned newSize);
298
299 WTF_EXPORT_PRIVATE bool canShrink() const;
300
301 WTF_EXPORT_PRIVATE void shrinkToFit();
302
303 UChar operator[](unsigned i) const
304 {
305 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!hasOverflowed() && i < m_length.unsafeGet<unsigned>());
306 if (m_is8Bit)
307 return characters8()[i];
308 return characters16()[i];
309 }
310
311 const LChar* characters8() const
312 {
313 ASSERT(m_is8Bit);
314 if (!m_length)
315 return 0;
316 if (!m_string.isNull())
317 return m_string.characters8();
318 ASSERT(m_buffer);
319 return m_buffer->characters8();
320 }
321
322 const UChar* characters16() const
323 {
324 ASSERT(!m_is8Bit);
325 if (!m_length)
326 return 0;
327 if (!m_string.isNull())
328 return m_string.characters16();
329 ASSERT(m_buffer);
330 return m_buffer->characters16();
331 }
332
333 bool is8Bit() const { return m_is8Bit; }
334
335 void clear()
336 {
337 m_length = 0;
338 m_string = String();
339 m_buffer = nullptr;
340 m_bufferCharacters8 = 0;
341 m_is8Bit = true;
342 }
343
344 void swap(StringBuilder& stringBuilder)
345 {
346 std::swap(m_length, stringBuilder.m_length);
347 m_string.swap(stringBuilder.m_string);
348 m_buffer.swap(stringBuilder.m_buffer);
349 std::swap(m_is8Bit, stringBuilder.m_is8Bit);
350 std::swap(m_bufferCharacters8, stringBuilder.m_bufferCharacters8);
351 ASSERT(!m_buffer || hasOverflowed() || m_buffer->length() >= m_length.unsafeGet<unsigned>());
352 }
353
354private:
355 void allocateBuffer(const LChar* currentCharacters, unsigned requiredLength);
356 void allocateBuffer(const UChar* currentCharacters, unsigned requiredLength);
357 void allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength);
358 template <typename CharType>
359 void reallocateBuffer(unsigned requiredLength);
360 template <typename CharType>
361 ALWAYS_INLINE CharType* appendUninitialized(unsigned length);
362 template <typename CharType>
363 CharType* appendUninitializedSlow(unsigned length);
364 template <typename CharType>
365 ALWAYS_INLINE CharType * getBufferCharacters();
366 WTF_EXPORT_PRIVATE void reifyString() const;
367
368 mutable String m_string;
369 RefPtr<StringImpl> m_buffer;
370 union {
371 LChar* m_bufferCharacters8;
372 UChar* m_bufferCharacters16;
373 };
374 static_assert(String::MaxLength == std::numeric_limits<int32_t>::max(), "");
375 Checked<int32_t, ConditionalCrashOnOverflow> m_length;
376 bool m_is8Bit { true };
377#if !ASSERT_DISABLED
378 mutable bool m_isReified { false };
379#endif
380};
381
382template <>
383ALWAYS_INLINE LChar* StringBuilder::getBufferCharacters<LChar>()
384{
385 ASSERT(m_is8Bit);
386 return m_bufferCharacters8;
387}
388
389template <>
390ALWAYS_INLINE UChar* StringBuilder::getBufferCharacters<UChar>()
391{
392 ASSERT(!m_is8Bit);
393 return m_bufferCharacters16;
394}
395
396inline void StringBuilder::appendNumber(float number)
397{
398 appendFixedPrecisionNumber(number);
399}
400
401inline void StringBuilder::appendNumber(double number, unsigned precision, TrailingZerosTruncatingPolicy policy)
402{
403 appendFixedPrecisionNumber(number, precision, policy);
404}
405
406inline void StringBuilder::appendECMAScriptNumber(float number)
407{
408 // FIXME: This preserves existing behavior but is not what we want.
409 // In the future, this should either be a compilation error or call appendShortestFormNumber without converting to double.
410 appendShortestFormNumber(static_cast<double>(number));
411}
412
413inline void StringBuilder::appendECMAScriptNumber(double number)
414{
415 appendShortestFormNumber(number);
416}
417
418template <typename CharType>
419bool equal(const StringBuilder& s, const CharType* buffer, unsigned length)
420{
421 if (s.length() != length)
422 return false;
423
424 if (s.is8Bit())
425 return equal(s.characters8(), buffer, length);
426
427 return equal(s.characters16(), buffer, length);
428}
429
430template <typename StringType>
431bool equal(const StringBuilder& a, const StringType& b)
432{
433 if (a.length() != b.length())
434 return false;
435
436 if (!a.length())
437 return true;
438
439 if (a.is8Bit()) {
440 if (b.is8Bit())
441 return equal(a.characters8(), b.characters8(), a.length());
442 return equal(a.characters8(), b.characters16(), a.length());
443 }
444
445 if (b.is8Bit())
446 return equal(a.characters16(), b.characters8(), a.length());
447 return equal(a.characters16(), b.characters16(), a.length());
448}
449
450inline bool operator==(const StringBuilder& a, const StringBuilder& b) { return equal(a, b); }
451inline bool operator!=(const StringBuilder& a, const StringBuilder& b) { return !equal(a, b); }
452inline bool operator==(const StringBuilder& a, const String& b) { return equal(a, b); }
453inline bool operator!=(const StringBuilder& a, const String& b) { return !equal(a, b); }
454inline bool operator==(const String& a, const StringBuilder& b) { return equal(b, a); }
455inline bool operator!=(const String& a, const StringBuilder& b) { return !equal(b, a); }
456
457template<> struct IntegerToStringConversionTrait<StringBuilder> {
458 using ReturnType = void;
459 using AdditionalArgumentType = StringBuilder;
460 static void flush(LChar* characters, unsigned length, StringBuilder* stringBuilder) { stringBuilder->append(characters, length); }
461};
462
463} // namespace WTF
464
465using WTF::StringBuilder;
466