Chromium Code Reviews| 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 11 matching lines...) Expand all Loading... | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "config.h" | 27 #include "config.h" |
| 28 #include "StringBuilder.h" | 28 #include "StringBuilder.h" |
| 29 | 29 |
| 30 #include "IntegerToStringConversion.h" | 30 #include "IntegerToStringConversion.h" |
| 31 #include "WTFString.h" | 31 #include "WTFString.h" |
| 32 #include "wtf/dtoa.h" | |
| 32 | 33 |
| 33 namespace WTF { | 34 namespace WTF { |
| 34 | 35 |
| 35 static unsigned expandedCapacity(unsigned capacity, unsigned requiredLength) | 36 static unsigned expandedCapacity(unsigned capacity, unsigned requiredLength) |
| 36 { | 37 { |
| 37 static const unsigned minimumCapacity = 16; | 38 static const unsigned minimumCapacity = 16; |
| 38 return std::max(requiredLength, std::max(minimumCapacity, capacity * 2)); | 39 return std::max(requiredLength, std::max(minimumCapacity, capacity * 2)); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void StringBuilder::reifyString() | 42 void StringBuilder::reifyString() |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 void StringBuilder::appendNumber(long long number) | 340 void StringBuilder::appendNumber(long long number) |
| 340 { | 341 { |
| 341 numberToStringSigned<StringBuilder>(number, this); | 342 numberToStringSigned<StringBuilder>(number, this); |
| 342 } | 343 } |
| 343 | 344 |
| 344 void StringBuilder::appendNumber(unsigned long long number) | 345 void StringBuilder::appendNumber(unsigned long long number) |
| 345 { | 346 { |
| 346 numberToStringUnsigned<StringBuilder>(number, this); | 347 numberToStringUnsigned<StringBuilder>(number, this); |
| 347 } | 348 } |
| 348 | 349 |
| 350 static void expandLCharToUCharInplace(UChar* buffer, size_t length) | |
| 351 { | |
| 352 const LChar* sourceEnd = reinterpret_cast<LChar*>(buffer) + length; | |
| 353 UChar* current = buffer + length; | |
| 354 while (current != buffer) | |
| 355 *--current = *--sourceEnd; | |
| 356 ASSERT(current == buffer); | |
|
abarth-chromium
2014/03/28 17:41:43
This ASSERT seems unnecessary. That's the only wa
fs
2014/03/31 07:41:56
Dropped.
| |
| 357 } | |
| 358 | |
| 359 void StringBuilder::appendNumber(double number, unsigned precision, TrailingZero sTruncatingPolicy trailingZerosTruncatingPolicy) | |
| 360 { | |
| 361 bool truncateTrailingZeros = trailingZerosTruncatingPolicy == TruncateTraili ngZeros; | |
| 362 size_t numberLength; | |
| 363 if (m_is8Bit) { | |
| 364 LChar* dest = appendUninitialized<LChar>(NumberToStringBufferLength); | |
| 365 const char* result = numberToFixedPrecisionString(number, precision, rei nterpret_cast<char*>(dest), truncateTrailingZeros); | |
| 366 numberLength = strlen(result); | |
| 367 } else { | |
| 368 UChar* dest = appendUninitialized<UChar>(NumberToStringBufferLength); | |
| 369 const char* result = numberToFixedPrecisionString(number, precision, rei nterpret_cast<char*>(dest), truncateTrailingZeros); | |
| 370 numberLength = strlen(result); | |
| 371 expandLCharToUCharInplace(dest, numberLength); | |
|
abarth-chromium
2014/03/28 17:41:43
Woah
| |
| 372 } | |
| 373 ASSERT(m_length >= NumberToStringBufferLength); | |
| 374 // Remove what appendUninitialized added. | |
| 375 m_length -= NumberToStringBufferLength; | |
| 376 ASSERT(numberLength <= NumberToStringBufferLength); | |
| 377 m_length += numberLength; | |
| 378 } | |
| 379 | |
| 349 bool StringBuilder::canShrink() const | 380 bool StringBuilder::canShrink() const |
| 350 { | 381 { |
| 351 // Only shrink the buffer if it's less than 80% full. Need to tune this heur istic! | 382 // Only shrink the buffer if it's less than 80% full. Need to tune this heur istic! |
| 352 return m_buffer && m_buffer->length() > (m_length + (m_length >> 2)); | 383 return m_buffer && m_buffer->length() > (m_length + (m_length >> 2)); |
| 353 } | 384 } |
| 354 | 385 |
| 355 void StringBuilder::shrinkToFit() | 386 void StringBuilder::shrinkToFit() |
| 356 { | 387 { |
| 357 if (!canShrink()) | 388 if (!canShrink()) |
| 358 return; | 389 return; |
| 359 if (m_is8Bit) | 390 if (m_is8Bit) |
| 360 reallocateBuffer<LChar>(m_length); | 391 reallocateBuffer<LChar>(m_length); |
| 361 else | 392 else |
| 362 reallocateBuffer<UChar>(m_length); | 393 reallocateBuffer<UChar>(m_length); |
| 363 m_string = m_buffer.release(); | 394 m_string = m_buffer.release(); |
| 364 } | 395 } |
| 365 | 396 |
| 366 } // namespace WTF | 397 } // namespace WTF |
| OLD | NEW |