OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // If the buffer has only one ref (by this StringBuilder), reallocate it, | 163 // If the buffer has only one ref (by this StringBuilder), reallocate it, |
164 // otherwise fall back to "allocate and copy" method. | 164 // otherwise fall back to "allocate and copy" method. |
165 m_string = String(); | 165 m_string = String(); |
166 | 166 |
167 ASSERT(m_is8Bit); | 167 ASSERT(m_is8Bit); |
168 ASSERT(m_buffer->is8Bit()); | 168 ASSERT(m_buffer->is8Bit()); |
169 | 169 |
170 if (m_buffer->hasOneRef()) { | 170 if (m_buffer->hasOneRef()) { |
171 m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength); | 171 m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength); |
172 m_bufferCharacters8 = const_cast<LChar*>(m_buffer->characters8()); | 172 m_bufferCharacters8 = const_cast<LChar*>(m_buffer->characters8()); |
173 } else | 173 } else { |
174 allocateBuffer(m_buffer->characters8(), requiredLength); | 174 allocateBuffer(m_buffer->characters8(), requiredLength); |
| 175 } |
175 } | 176 } |
176 | 177 |
177 template <> | 178 template <> |
178 void StringBuilder::reallocateBuffer<UChar>(unsigned requiredLength) | 179 void StringBuilder::reallocateBuffer<UChar>(unsigned requiredLength) |
179 { | 180 { |
180 // If the buffer has only one ref (by this StringBuilder), reallocate it, | 181 // If the buffer has only one ref (by this StringBuilder), reallocate it, |
181 // otherwise fall back to "allocate and copy" method. | 182 // otherwise fall back to "allocate and copy" method. |
182 m_string = String(); | 183 m_string = String(); |
183 | 184 |
184 if (m_buffer->is8Bit()) { | 185 if (m_buffer->is8Bit()) { |
185 allocateBufferUpConvert(m_buffer->characters8(), requiredLength); | 186 allocateBufferUpConvert(m_buffer->characters8(), requiredLength); |
186 } else if (m_buffer->hasOneRef()) { | 187 } else if (m_buffer->hasOneRef()) { |
187 m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength); | 188 m_buffer = StringImpl::reallocate(m_buffer.release(), requiredLength); |
188 m_bufferCharacters16 = const_cast<UChar*>(m_buffer->characters16()); | 189 m_bufferCharacters16 = const_cast<UChar*>(m_buffer->characters16()); |
189 } else | 190 } else { |
190 allocateBuffer(m_buffer->characters16(), requiredLength); | 191 allocateBuffer(m_buffer->characters16(), requiredLength); |
| 192 } |
191 } | 193 } |
192 | 194 |
193 void StringBuilder::reserveCapacity(unsigned newCapacity) | 195 void StringBuilder::reserveCapacity(unsigned newCapacity) |
194 { | 196 { |
195 if (m_buffer) { | 197 if (m_buffer) { |
196 // If there is already a buffer, then grow if necessary. | 198 // If there is already a buffer, then grow if necessary. |
197 if (newCapacity > m_buffer->length()) { | 199 if (newCapacity > m_buffer->length()) { |
198 if (m_buffer->is8Bit()) | 200 if (m_buffer->is8Bit()) |
199 reallocateBuffer<LChar>(newCapacity); | 201 reallocateBuffer<LChar>(newCapacity); |
200 else | 202 else |
201 reallocateBuffer<UChar>(newCapacity); | 203 reallocateBuffer<UChar>(newCapacity); |
202 } | 204 } |
203 } else { | 205 } else { |
204 // Grow the string, if necessary. | 206 // Grow the string, if necessary. |
205 if (newCapacity > m_length) { | 207 if (newCapacity > m_length) { |
206 if (!m_length) { | 208 if (!m_length) { |
207 LChar* nullPlaceholder = 0; | 209 LChar* nullPlaceholder = 0; |
208 allocateBuffer(nullPlaceholder, newCapacity); | 210 allocateBuffer(nullPlaceholder, newCapacity); |
209 } else if (m_string.is8Bit()) | 211 } else if (m_string.is8Bit()) { |
210 allocateBuffer(m_string.characters8(), newCapacity); | 212 allocateBuffer(m_string.characters8(), newCapacity); |
211 else | 213 } else { |
212 allocateBuffer(m_string.characters16(), newCapacity); | 214 allocateBuffer(m_string.characters16(), newCapacity); |
| 215 } |
213 } | 216 } |
214 } | 217 } |
215 } | 218 } |
216 | 219 |
217 // Make 'length' additional capacity be available in m_buffer, update m_string &
m_length, | 220 // Make 'length' additional capacity be available in m_buffer, update m_string &
m_length, |
218 // return a pointer to the newly allocated storage. | 221 // return a pointer to the newly allocated storage. |
219 template <typename CharType> | 222 template <typename CharType> |
220 ALWAYS_INLINE CharType* StringBuilder::appendUninitialized(unsigned length) | 223 ALWAYS_INLINE CharType* StringBuilder::appendUninitialized(unsigned length) |
221 { | 224 { |
222 ASSERT(length); | 225 ASSERT(length); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 ASSERT(m_buffer->length() >= m_length); | 286 ASSERT(m_buffer->length() >= m_length); |
284 | 287 |
285 allocateBufferUpConvert(m_buffer->characters8(), expandedCapacity(ca
pacity(), requiredLength)); | 288 allocateBufferUpConvert(m_buffer->characters8(), expandedCapacity(ca
pacity(), requiredLength)); |
286 } else { | 289 } else { |
287 ASSERT(m_string.length() == m_length); | 290 ASSERT(m_string.length() == m_length); |
288 allocateBufferUpConvert(m_string.isNull() ? 0 : m_string.characters8
(), expandedCapacity(capacity(), requiredLength)); | 291 allocateBufferUpConvert(m_string.isNull() ? 0 : m_string.characters8
(), expandedCapacity(capacity(), requiredLength)); |
289 } | 292 } |
290 | 293 |
291 memcpy(m_bufferCharacters16 + m_length, characters, static_cast<size_t>(
length) * sizeof(UChar)); | 294 memcpy(m_bufferCharacters16 + m_length, characters, static_cast<size_t>(
length) * sizeof(UChar)); |
292 m_length = requiredLength; | 295 m_length = requiredLength; |
293 } else | 296 } else { |
294 memcpy(appendUninitialized<UChar>(length), characters, static_cast<size_
t>(length) * sizeof(UChar)); | 297 memcpy(appendUninitialized<UChar>(length), characters, static_cast<size_
t>(length) * sizeof(UChar)); |
| 298 } |
295 } | 299 } |
296 | 300 |
297 void StringBuilder::append(const LChar* characters, unsigned length) | 301 void StringBuilder::append(const LChar* characters, unsigned length) |
298 { | 302 { |
299 if (!length) | 303 if (!length) |
300 return; | 304 return; |
301 ASSERT(characters); | 305 ASSERT(characters); |
302 | 306 |
303 if (m_is8Bit) { | 307 if (m_is8Bit) { |
304 LChar* dest = appendUninitialized<LChar>(length); | 308 LChar* dest = appendUninitialized<LChar>(length); |
305 if (length > 8) | 309 if (length > 8) { |
306 memcpy(dest, characters, static_cast<size_t>(length) * sizeof(LChar)
); | 310 memcpy(dest, characters, static_cast<size_t>(length) * sizeof(LChar)
); |
307 else { | 311 } else { |
308 const LChar* end = characters + length; | 312 const LChar* end = characters + length; |
309 while (characters < end) | 313 while (characters < end) |
310 *(dest++) = *(characters++); | 314 *(dest++) = *(characters++); |
311 } | 315 } |
312 } else { | 316 } else { |
313 UChar* dest = appendUninitialized<UChar>(length); | 317 UChar* dest = appendUninitialized<UChar>(length); |
314 const LChar* end = characters + length; | 318 const LChar* end = characters + length; |
315 while (characters < end) | 319 while (characters < end) |
316 *(dest++) = *(characters++); | 320 *(dest++) = *(characters++); |
317 } | 321 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 if (!canShrink()) | 391 if (!canShrink()) |
388 return; | 392 return; |
389 if (m_is8Bit) | 393 if (m_is8Bit) |
390 reallocateBuffer<LChar>(m_length); | 394 reallocateBuffer<LChar>(m_length); |
391 else | 395 else |
392 reallocateBuffer<UChar>(m_length); | 396 reallocateBuffer<UChar>(m_length); |
393 m_string = m_buffer.release(); | 397 m_string = m_buffer.release(); |
394 } | 398 } |
395 | 399 |
396 } // namespace WTF | 400 } // namespace WTF |
OLD | NEW |