2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "StringBuilder.h"
29 #include "WTFString.h"
33 static const unsigned minimumCapacity = 16;
35 void StringBuilder::reifyString()
37 // Check if the string already exists.
38 if (!m_string.isNull()) {
39 ASSERT(m_string.length() == m_length);
45 m_string = StringImpl::empty();
49 // Must be valid in the buffer, take a substring (unless string fills the buffer).
50 ASSERT(m_buffer && m_length <= m_buffer->length());
51 m_string = (m_length == m_buffer->length())
53 : StringImpl::create(m_buffer, 0, m_length);
56 void StringBuilder::resize(unsigned newSize)
58 // Check newSize < m_length, hence m_length > 0.
59 ASSERT(newSize <= m_length);
60 if (newSize == m_length)
64 // If there is a buffer, we only need to duplicate it if it has more than one ref.
66 if (!m_buffer->hasOneRef())
67 allocateBuffer(m_buffer->characters(), m_buffer->length());
73 // Since m_length && !m_buffer, the string must be valid in m_string, and m_string.length() > 0.
74 ASSERT(!m_string.isEmpty());
75 ASSERT(m_length == m_string.length());
76 ASSERT(newSize < m_string.length());
78 m_string = StringImpl::create(m_string.impl(), 0, newSize);
81 void StringBuilder::reserveCapacity(unsigned newCapacity)
84 // If there is already a buffer, then grow if necessary.
85 if (newCapacity > m_buffer->length())
86 allocateBuffer(m_buffer->characters(), newCapacity);
88 // Grow the string, if necessary.
89 if (newCapacity > m_length)
90 allocateBuffer(m_string.characters(), newCapacity);
94 // Allocate a new buffer, copying in currentCharacters (these may come from either m_string
95 // or m_buffer, neither will be reassigned until the copy has completed).
96 void StringBuilder::allocateBuffer(const UChar* currentCharacters, unsigned requiredLength)
98 // Copy the existing data into a new buffer, set result to point to the end of the existing data.
99 RefPtr<StringImpl> buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters);
100 memcpy(m_bufferCharacters, currentCharacters, static_cast<size_t>(m_length) * sizeof(UChar)); // This can't overflow.
102 // Update the builder state.
103 m_buffer = buffer.release();
107 // Make 'length' additional capacity be available in m_buffer, update m_string & m_length,
108 // return a pointer to the newly allocated storage.
109 UChar* StringBuilder::appendUninitialized(unsigned length)
113 // Calcuate the new size of the builder after appending.
114 unsigned requiredLength = length + m_length;
115 if (requiredLength < length)
119 // If the buffer is valid it must be at least as long as the current builder contents!
120 ASSERT(m_buffer->length() >= m_length);
122 // Check if the buffer already has sufficient capacity.
123 if (requiredLength <= m_buffer->length()) {
124 unsigned currentLength = m_length;
126 m_length = requiredLength;
127 return m_bufferCharacters + currentLength;
130 // We need to realloc the buffer.
131 allocateBuffer(m_buffer->characters(), std::max(requiredLength, std::max(minimumCapacity, m_buffer->length() * 2)));
133 ASSERT(m_string.length() == m_length);
134 allocateBuffer(m_string.characters(), std::max(requiredLength, std::max(minimumCapacity, m_length * 2)));
137 UChar* result = m_bufferCharacters + m_length;
138 m_length = requiredLength;
142 void StringBuilder::append(const UChar* characters, unsigned length)
148 memcpy(appendUninitialized(length), characters, static_cast<size_t>(length) * 2);
151 void StringBuilder::append(const char* characters, unsigned length)
157 UChar* dest = appendUninitialized(length);
158 const char* end = characters + length;
159 while (characters < end)
160 *(dest++) = *(const unsigned char*)(characters++);
163 void StringBuilder::shrinkToFit()
165 // If the buffer is at least 80% full, don't bother copying. Need to tune this heuristic!
166 if (m_buffer && m_buffer->length() > (m_length + (m_length >> 2))) {
168 m_string = StringImpl::createUninitialized(m_length, result);
169 memcpy(result, m_buffer->characters(), static_cast<size_t>(m_length) * 2); // This can't overflow.